อันนี้เป็น code เกี่ยวกับเกม XO
ทีนี้พยายามจะทำความเข้าใจกับเจ้าโค้ดพวกนี้แต่ยังมีจุดที่ยังสงสัยอยู่
พอจะช่วยหน่อยได้ไหมค่ะ T^T
[code] #include<iostream>
using namespace std;
int main()
{
int i,j,k,row,column;
char board[3][3];
char notPlay = ' ';
char aPlay = 'O';
char bPlay = 'X';
int currentPlayer = 1; // PlayerA = 1, PlayerB = 2
bool invalidPlay = true;
int gameStatus = 0; // Draw = 0, PlayerA win = 1, Player B win = 2
// clear board
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
board[j]=notPlay;
}
}
// play game
for(i=1;i<10;i++)
{
cout << "==================================================="< cout <<endl;
cout<<" Round = "<<i<<endl;
// display board
cout<<" C0 C1 C2 "<<endl;
for(j=0;j<3;j++)
{
cout<<" R"<<j;
for(k=0;k<3;k++)
{
cout<<"["<< board[j][k]<<"]";
}
cout << endl;
}
// check game status
if(gameStatus!=0)
{
break;
}
// read input
if(currentPlayer==1)
{
cout<<"Player A"<<endl;
}
else
{
cout<<"Player B"<<endl;
}
while(invalidPlay) // duplicate check
{
cout<<"Select row(0-2) and column(0-2) seperate by space: ";
cin>>row>>column;
if (board[row][column]==notPlay) // valid play
{
if(currentPlayer==1) // player A
{
board[row][column]=aPlay;
}
else
{
board[row][column]=bPlay;
}
invalidPlay = false;
}
}
// find the winner and set game status
for(j=0;j<3;j++)
{
// check row
if(board[j][0]==board[j][1]&&board[j][1]==board[j][2]&&board[j][0]!=notPlay)
{
gameStatus=currentPlayer;
}
// check column
if(board[0][j]==board[1][j]&&board[1][j]==board[2][j]&&board[0][j]!=notPlay)
{
gameStatus=currentPlayer;
}
}
// check cross line
if(board[0][0]==board[1][1]&&board[1][1]==board[2][2]&&board[0][0]!=notPlay)
{
gameStatus=currentPlayer;
}
if(board[0][2]==board[1][1]&&board[1][1]==board[2][0]&&board[0][2]!=notPlay)
{
gameStatus=currentPlayer;
}
invalidPlay=true; // reset variable for next input
// switch player for next turn
if(currentPlayer==1)
{
currentPlayer=2;
}
else
{
currentPlayer=1;
}
}
//---------------------------------------------------------
// display output
//---------------------------------------------------------
// display board
cout<<"==================================================="<<endl;
for(j=0;j<3;j++)
{
cout<<"*";
for(k=0;k<3;k++)
{
cout<<"t"<<board[j][k];
}
cout<<endl;
}
if(gameStatus==0)
{
cout<<"Draw"<<endl;
}
else if(gameStatus==1)
{
cout<<"Player A wins"<<endl;
}
else
{
cout<<"Player B wins"<<endl;
}
return 0;
}
[/code]
มีข้อสงสัยในโค้ดc++ ช่วยหน่อยได้ไหม
ทีนี้พยายามจะทำความเข้าใจกับเจ้าโค้ดพวกนี้แต่ยังมีจุดที่ยังสงสัยอยู่
พอจะช่วยหน่อยได้ไหมค่ะ T^T
[code] #include<iostream>
using namespace std;
int main()
{
int i,j,k,row,column;
char board[3][3];
char notPlay = ' ';
char aPlay = 'O';
char bPlay = 'X';
int currentPlayer = 1; // PlayerA = 1, PlayerB = 2
bool invalidPlay = true;
int gameStatus = 0; // Draw = 0, PlayerA win = 1, Player B win = 2
// clear board
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
board[j]=notPlay;
}
}
// play game
for(i=1;i<10;i++)
{
cout << "==================================================="< cout <<endl;
cout<<" Round = "<<i<<endl;
// display board
cout<<" C0 C1 C2 "<<endl;
for(j=0;j<3;j++)
{
cout<<" R"<<j;
for(k=0;k<3;k++)
{
cout<<"["<< board[j][k]<<"]";
}
cout << endl;
}
// check game status
if(gameStatus!=0)
{
break;
}
// read input
if(currentPlayer==1)
{
cout<<"Player A"<<endl;
}
else
{
cout<<"Player B"<<endl;
}
while(invalidPlay) // duplicate check
{
cout<<"Select row(0-2) and column(0-2) seperate by space: ";
cin>>row>>column;
if (board[row][column]==notPlay) // valid play
{
if(currentPlayer==1) // player A
{
board[row][column]=aPlay;
}
else
{
board[row][column]=bPlay;
}
invalidPlay = false;
}
}
// find the winner and set game status
for(j=0;j<3;j++)
{
// check row
if(board[j][0]==board[j][1]&&board[j][1]==board[j][2]&&board[j][0]!=notPlay)
{
gameStatus=currentPlayer;
}
// check column
if(board[0][j]==board[1][j]&&board[1][j]==board[2][j]&&board[0][j]!=notPlay)
{
gameStatus=currentPlayer;
}
}
// check cross line
if(board[0][0]==board[1][1]&&board[1][1]==board[2][2]&&board[0][0]!=notPlay)
{
gameStatus=currentPlayer;
}
if(board[0][2]==board[1][1]&&board[1][1]==board[2][0]&&board[0][2]!=notPlay)
{
gameStatus=currentPlayer;
}
invalidPlay=true; // reset variable for next input
// switch player for next turn
if(currentPlayer==1)
{
currentPlayer=2;
}
else
{
currentPlayer=1;
}
}
//---------------------------------------------------------
// display output
//---------------------------------------------------------
// display board
cout<<"==================================================="<<endl;
for(j=0;j<3;j++)
{
cout<<"*";
for(k=0;k<3;k++)
{
cout<<"t"<<board[j][k];
}
cout<<endl;
}
if(gameStatus==0)
{
cout<<"Draw"<<endl;
}
else if(gameStatus==1)
{
cout<<"Player A wins"<<endl;
}
else
{
cout<<"Player B wins"<<endl;
}
return 0;
}
[/code]