#include <cstdlib>
#include <iostream>
using namespace std;
class Game
{
private:
char board[3][3];
bool rowWin()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if((board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j]) || (board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2])) //condition for 3 in a row
{
if(board[i][j] == '+') cout << "Player 1 has won.";
else cout << "Player 2 has won.";
return true;
}
else return false;
}
}
}
bool diagWin()
{
if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
{
if(board[0][0] == '+' || board[0][2] == '+') cout << "Player 1 has won.";
else cout << "Player 2 has won.";
return true;
}
else return false;
}
public:
Game()
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
board[i][j] = '*';
}
void setSpot(int player, int i, int j)
{
if(player == 1) board[i][j] = '+';
else board[i][j] = '0';
}
void printBoard()
{
cout << "-------";
for(int i = 0; i < 3; i++)
{
cout << "\n|";
for(int j = 0; j < 3; j++)
{
cout << board[i][j] << "|";
}
}
cout << "\n-------";
}
bool hasWon()
{
return rowWin() && diagWin();
}
void doTurn()
{
int m, n;
for(int i = 0; i < 2; i++)
{
printBoard();
do
{
cout << "\n\nPlayer " << i+1 << " enter row and column of your move. (1-3): ";
cin >> m >> n;
setSpot(i, m-1, n-1);
} while((m > 0 || n < 4) && (n > 0 || n < 4));
}
}
};
int main()
{
Game tic;
cout << "Welcome to the Tic-Tac-Toe game!\n";
system("PAUSE");
do
{
system("CLS");
tic.doTurn();
system("PAUSE");
} while(tic.hasWon());
}
I'm getting some errors with this code. I'm pretty sure it has something to do with the input. I've also never used private member functions before, so that might have some issues as well. I'm not entirely sure.

New Topic/Question
Reply




MultiQuote





|