Hi, I am a newby and am surprised I made it this far. My program is a TicTacToe program. I am having troubles with checking the validity of each move. I have tried multiple things, but could not figure it out. If someone could help point me in the right direction, that would be much appreciated.
#include <iostream>
using namespace std;
class ticTacToe
{
private:
char board[3][3];
char player;
int move;
public:
void initiate();
void postboard();
void input();
bool done();
bool checkmove();
void playagain();
};
int main()
{
ticTacToe game;
game.initiate();
do
{
game.postboard();
game.input();
//game.checkmove();
}
while(game.done() == false);
game.playagain();
}
void ticTacToe::initiate()
{
int n = 1;
for (int x=0; x<3; x++)
{
for (int y=0; y<3; y++)
{
board[x][y] = '0' + n++;
}
}
}
void ticTacToe::postboard()
{
system("cls");
for (int x=0; x<3; x++)
{
for (int y=0; y<3; y++)
{
cout << " " << board[x][y] << " ";
}
cout << endl;
}
}
void ticTacToe::input()
{
char move = '0';
if (player == ' ')
{
player = 'X';
}
else if (player == 'X')
{
player = 'O';
}
else
{
player = 'X';
}
cout << "Player " << player << " choose your move." << endl;
cin >> move;
bool checkout = false;
while (checkout == false)
{
if (move == '1')
{
board[0][0] = player;
}
else if (move == '2')
{
board[0][1]=player;
}
else if (move == '3')
{
board[0][2]=player;
}
else if (move == '4')
{
board[1][0] =player;
}
else if (move == '5')
{
board[1][1] = player;
}
else if (move == '6')
{ board[1][2] = player;
}
else if (move == '7')
{
board[2][0] = player;
}
else if (move == '8')
{
board[2][1] = player;
}
else if (move == '9')
{
board[2][2] = player;
}
else
{
cout << " uhh... wut";
}
checkout = checkmove();
}
}
bool ticTacToe::done()
{
int xwin=0;
int owin=0;
int cats=0;
bool end = false;
if (board[0][0] == board[0][1] && board[0][0] == board[0][2])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[1][0] == board[1][1] && board[1][0] == board[1][2])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[2][0] == board[2][1] && board[2][0] == board[2][2])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else if (board[2][0] == board[1][1] && board[2][0] == board[0][2])
{
cout <<"Player " << player << " won." << endl;
end = true;
}
else
end = false;
return end;
}
bool ticTacToe::checkmove()
{
}
void ticTacToe::playagain()
{
char again = ' ';
cout << "Play again?" << endl;
cout << "Y or N" << endl;
cin >> again;
again = toupper(again);
if (again == 'Y')
{
main();
}
else
{
cout << "Well alright....";
}
}