hello, i started programming again after a while and i made this just to pass time/relearn the basics and stuff
there is a weird issue that i can't figure out, when i input row 0 column 2 or row 1 column 2 it will fill the spot for both the inputted move and the row above/below it (if i input row 0 column 2, it will also output an X in row 1 column 0 and if i input row 1 column 2 it will also output an X in row 2 column 0)
another thing i was curious about was just general efficiency/neatness and any criticisms on any of that stuff would be greatly appreciated
thanks
#include <iostream>
using namespace std;
//functions
void PrintTurn();
void PrintBoard();
void GetMoveInput();
void CheckResult(int nRow, int nColumn);
void ClearScreen();
void AnnounceWinner();
void PlayAgain();
//variables
bool CurrentPlayerTurn=true; //true=x, false=o
char PlayerMove[2][2]; //first array: row, second: column
int MoveCount=0;
int main()
{
for(int i=0;i<=2;i++) //clear board
{
for(int j=0;j<=2;j++)
PlayerMove[i][j]='_';
}
ClearScreen();
return 0;
}
void PrintTurn()
{
cout<<"Current Player's Turn: ";
if(CurrentPlayerTurn)
cout<<"X";
else
cout<<"O";
cout<<endl;
}
void PrintBoard()
{
cout<<"RC0 1 2"<<endl;
for(int i=0;i<=2;i++)
{
cout<<i<<" ";
for(int j=0;j<=2;j++)
cout<<PlayerMove[i][j]<<" ";
cout<<endl;
}
}
void GetMoveInput()
{
int row, column;
cout<<"Row: ";
cin>>row;
if(row!=0 && row!=1 && row!=2)
{
ClearScreen();
return;
}
cout<<"Column: ";
cin>>column;
if(column!=0 && column!=1 && column!=2)
{
ClearScreen();
return;
}
CheckResult(row,column);
}
void ClearScreen()
{
system("cls");
PrintTurn();
PrintBoard();
cout<<endl<<endl;
GetMoveInput();
}
void CheckResult(int nRow, int nColumn)
{
if(PlayerMove[nRow][nColumn]!='_')
{
ClearScreen();
return;
}
char PlayerTurn;
MoveCount++; //for tie maybe
if(CurrentPlayerTurn)
{
PlayerMove[nRow][nColumn]='X';
PlayerTurn='X';
}
else {
PlayerMove[nRow][nColumn]='O';
PlayerTurn='O';
}
if(PlayerMove[0][0]==PlayerTurn && PlayerMove[0][1]==PlayerTurn && PlayerMove[0][2]==PlayerTurn) { //xxx row 1
AnnounceWinner();
return; }
else if(PlayerMove[1][0]==PlayerTurn && PlayerMove[1][1]==PlayerTurn && PlayerMove[1][2]==PlayerTurn) { //xxx row 2
AnnounceWinner();
return; }
else if(PlayerMove[2][0]==PlayerTurn && PlayerMove[2][1]==PlayerTurn && PlayerMove[2][2]==PlayerTurn) { //xxx row 3
AnnounceWinner();
return; }
else if(PlayerMove[0][0]==PlayerTurn && PlayerMove[1][0]==PlayerTurn && PlayerMove[2][0]==PlayerTurn) { //xxx column 1
AnnounceWinner();
return; }
else if(PlayerMove[0][1]==PlayerTurn && PlayerMove[1][1]==PlayerTurn && PlayerMove[2][1]==PlayerTurn) { //xxx column 2
AnnounceWinner();
return; }
else if(PlayerMove[0][2]==PlayerTurn && PlayerMove[1][2]==PlayerTurn && PlayerMove[2][2]==PlayerTurn) { //xxx column 3
AnnounceWinner();
return; }
else if(PlayerMove[0][0]==PlayerTurn && PlayerMove[1][1]==PlayerTurn && PlayerMove[2][2]==PlayerTurn) { //xxx diag left->right
AnnounceWinner();
return; }
else if(PlayerMove[0][2]==PlayerTurn && PlayerMove[1][1]==PlayerTurn && PlayerMove[2][0]==PlayerTurn) {//xxx diag right->left
AnnounceWinner();
return;
}
//check tie??
CurrentPlayerTurn=!CurrentPlayerTurn;
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
if(PlayerMove[i][j]!='X' && PlayerMove[i][j]!='O')
PlayerMove[i][j]='_';
}
}
ClearScreen();
}
void AnnounceWinner()
{
char Winner;
if(CurrentPlayerTurn)
Winner='X';
else
Winner='O';
cout<<endl<<Winner<<" has won the game!";
PlayAgain();
}
void PlayAgain()
{
char Answer;
cout<<endl<<"Play Again? (y/n) ";
cin>>Answer;
if(toupper(Answer)=='Y')
{
system("cls");
CurrentPlayerTurn=true;
MoveCount=0;
main();
}
}