What's new

C & C++ Pa help po c++

PHC_RYUJIN

Forum Veteran
Elite
Joined
May 1, 2022
Posts
1,504
Solutions
2
Reaction
1,107
Points
527
ok na
 
Last edited:
Mayroong ilang mali sa code na ito. Una, hindi maaring magdeclare ng global variables at maglagay ng function sa loob ng main function. Dapat ay ilagay ang mga ito sa labas ng main function. Pangalawa, mayroong isang typo sa code kung saan dapat ay "Draw" ang naka-lagay sa huling bahagi ng code, pero naiwan ito bilang isang hindi-deklaradong variable. Dapat na ilagay ang "Draw" sa loob ng quotation mark ("Draw").

Narito ang isang posibleng tama na code:

C++:
#include <iostream>
#include<string>
using namespace std;

char space[3][3]={{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
int row;
int column;
char token= 'X';
bool tie=false;

void functionOne();
void functionTwo(string, string);
bool functionThree(string, string);

int main()
{
    string n1;
    string n2;
    cout<<"Enter the name of Player 1 : \n";
    getline(cin, n1);
    cout<<"Enter the name of Player 2 : \n";
    getline (cin, n2);
    cout<<n1<< " Player 1 is the first player to move : \n";
    cout<<n2<< " Player 2 is the second player to move : \n";

    while(!functionThree(n1, n2))
    {
        functionOne();
        functionTwo(n1, n2);
    }
    if (token =='X' && tie == false)
    {
        cout<<n2<<" Wins!!"<<endl;
    }
    else if (token =='0' && tie == false)
    {
        cout<<n1<<" Wins!!"<<endl;
    }
    else
    {
        cout<<"Draw"<<endl;
    }
}

void functionOne()
{
    cout<< "   |   |   \n";
    cout<< "  "<<space[0][0]<<" | " <<space [0][1]<<" | "<<space [0][2]<<"   \n";
    cout<< "[B][I]|[/I][/B]|___\n";
    cout<< "   |   |   \n";
    cout<< "  "<<space[1][0]<<" | " <<space [1][1]<<" | "<<space [1][2]<<"   \n";
    cout<< "[B][I]|[/I][/B]|___\n";
    cout<< "   |   |   \n";
    cout<< "  "<<space[2][0]<<" | " <<space [2][1]<<" | "<<space [2][2]<<"   \n";
    cout<< "   |   |   \n";
}

void functionTwo(string n1, string n2){

    int digit;

    if (token=='X')
    {
        cout<<n1<<" Please enter: ";
        cin>>digit;
    }

    if (token=='0')
    {
        cout<<n2<<" Please enter: ";
        cin>>digit;
    }

    if (digit==1)
    {
        row=0;
        column=0;
    }

    if (digit==2)
    {
        row=0;
        column=1;
    }

    if (digit==3)
    {
        row=0;
        column=2;
    }

    if (digit==4)
    {
        row=1;
        column=0;
    }

    if (digit==5)
    {
        row=1;
        column=1;
    }

    if (digit==6)
    {
        row=1;
        column=2;
    }

    if (digit==7)
    {
        row=2;
        column=0;
    }

    if (digit==8)
    {
        row=2;
        column=1;
    }

    if (digit==9)
    {
        row=2;
        column=2;
    }

    else if (digit<1 || digit>9)
    {
        cout<<"Invalid input"<<endl;
        functionTwo(n1, n2); //tawagin ulit ang functionTwo kung may invalid input
    }

    if(token=='X' && space[row][column] !='X' && space[row][column] !='0')
    {
        space[row][column]='X';
        token='0';
    }
    else if(token=='0' && space[row][column] !='X' && space[row][column] !='0')
    {
        space[row][column]='0';
        token='X';
    }
    else
    {
        cout<<"The space is already occupied"<<endl;
        functionTwo(n1, n2); //tawagin ulit ang functionTwo kung occupied na ang space
    }
    functionOne();
}

bool functionThree(string n1, string n2)
{
    for(int i=0;i<3;i++)
    {
        if (space[i][0]==space[i][1] && space[i][0]==space[i][2] || space[0][i]==space[1][i] && space[0][i]==space[2][i])
        {
            return true;
        }
    }
    if(space[0][0]==space[1][1] && space[1][1]==space[2][2] || space[0][2]==space[1][1] && space[1][1]==space[2][0])
    {
        return true;
    }
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(space[i][j] != 'X' && space[i][j] != '0')
            {
                return false;
            }
        }
    }
    tie=true;
    return false;
}

Sa code na ito, nilipat natin ang function declarations sa labas ng main function at ginamit natin ang mga ito upang tawagin ang mga functions sa loob ng main. Nalagay din natin sa loob ng if statements ang return true para sa functionThree upang matiyak na hindi maaring mag-continue ang game kung may nanalo na o kung tie na. Ginamit din natin ang recursive function calls sa functionTwo kung may invalid input o kung occupied na ang space para magpatuloy ang game hanggang sa magamit ang valid na input.
 

Similar threads

Back
Top