Hi there this is my first post here :)
So I got an assignment about TicTacToe project
here's a basic header and main file of my project
I think i should made a basic one before adding some "advanced" features
please help with the logic of the command undo
thank you in advance
<code>
//headerfile
//TicTacToe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe
{
private:
enum Status {WIN,DRAW,CONTINUE};
int board[3][3];
public:
TicTacToe();
void makeMove();
void printBoard();
bool validMove(int,int);
bool xoMove(int);
Status gameStatus();
};
#endif
</code>
//main file
//TicTacToe.cpp
#include <iostream>
using std::cin;
using std::cout;
#include <iomanip>
using std::setw;
#include "TicTacToe.h"
TicTacToe::TicTacToe()
{
for( int j = 0; j < 3; j++ )
for ( int k = 0; k< 3; k++ )
board[j][k]='';
}
bool TicTacToe::validMove(int r, int c)
{
return r>= 0 && r<3 && c>= 0 && c < 3 &&
board[r][c]=='';
}
TicTacToe::Status TicTacToe::gameStatus()
{
int a;
if(board[0][0]!='' && board[0][0] == board [1][1] &&
board[0][0]==board[2][2])
return WIN;
else if ( board[2][0]!='' && board [2][0] == board [1][1] && board [2][0] == board[0][2])
return WIN;
for(a=0;a<3;a++)
if(board[a][0] != '' && board[a][0] == board [a][1] && board [a][0] == board[a][2])
return WIN;
for(a=0;a<3;++a)
if(board[0][a] != '' && board[0][a] == board[1][a] && board [0][a] == board [2][a] )
return WIN;
for(int r=0;r<3;++r)
for(int c=0;c<3;++c)
if(board[r][c] =='')
return CONTINUE;
return DRAW;
}
void TicTacToe::printBoard()
{
cout<<" 0 1 2\n\n";
for(int r=0;r<3;++r)
{
cout<<r;
for(int c=0;c<3;++c)
{
cout<<setw(3)<<static_cast< char > (board[r][c]);
if (c!=2)
cout<<" |";
}
if(r!=2)
cout<<"\n__|__|__\n | | \n";
}
cout<<"\n\n";
}
void TicTacToe::makeMove()
{
printBoard();
while (true)
{
if(xoMove('X'))
break;
else if(xoMove('O'))
break;
}
}
bool TicTacToe::xoMove(int symbol)
{
int x;
int y;
do
{
cout<<"Player"<<static_cast< char >(symbol)<<"enter move:";
cin>>x>>y;
cout<<"\n";
}
while (!validMove(x,y));
board[x][y]= symbol;
printBoard();
Status xoStatus = gameStatus();
if (xoStatus == WIN)
{
cout<<"Player"<<static_cast<char>(symbol)<<"wins!\n";
return true;
}
else if (xoStatus == DRAW)
{
cout<<"Game is a draw\n";
return true;
}
else
return false;
}