Tic-Tac-Toe without a class
Write a modular program that allows two players to play a game of tic–tac-toe. Use a two-dimensional char array with 3 rows and 3 columns as the game board. Each element of the array should be initialized with an asterisk(*). The program should display the intial board configuration and then start a loop that does the following.
· Allow player 1 to select a location on the boardfor an X by entering a row and column number. Then redisplay the board with an X replacing * in the chosenlocation.
· If there is no winner yet and the board is not yet full, allow player 2 to select to location on the board for an 0 byentering a row a column number. Then redisplay the board with an O replacing the * in the chosen laction
The loop should continue until a player has won or a tie has occurred.
· Player 1 wins when there are three Xs in a row, a column, or a diagonal on the game board
· Player 2 wins when there are three Os in a row, ac olumn, or a diagonal on the game board.
· A tie occurs when all of the location on the board are full, but there is no winner.
************************************************************************************************
The "class-y" TicTacToe :)
The program should display an appropriate message indicating whowon, or reporting that a tie occurred.
The program needs to use a class TicTacToe. The main function needs to have a loop that drives the action of the game invoking methods on an instance the class TicTacToe.
The TicTacToe class should have a private data member for the game board (a two-dimensional array) and a private data member that keeps track of the current player - among any other private data members the class may require. Alter the state of the game board using public member functions.
I mostly need help with the TicTacToe class program. If someone could get back to me sooner that later, I would appreciate it.
Thanks in advance!
MY TICTACTOE CODE SO FAR (without a class):
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 3;
char a[3][3] = {
{'*','*','*'},
{'*','*','*'},
{'*','*','*'}
};
char b = '0';
void draw();
bool getinputP1();
bool getinputP2();
bool checkPlayerOne();
bool checkPlayerTwo();
void draw()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
cout << setw(5) << a[i][j];
}
cout << endl;
}
}
bool getinputP1()
{
cout << "Enter 1-9, respectively: ";
cin >> b;
switch(b)
{
case '1' : a[0][0] = 'X'; break;
case '2' : a[0][1] = 'X'; break;
case '3' : a[0][2] = 'X'; break;
case '4' : a[1][0] = 'X'; break;
case '5' : a[1][1] = 'X'; break;
case '6' : a[1][2] = 'X'; break;
case '7' : a[2][0] = 'X'; break;
case '8' : a[2][1] = 'X'; break;
case '9' : a[2][2] = 'X'; break;
default: cout << "You have to play by the rules!" << endl;
}
return 0;
}
bool getinputP2()
{
cout << "Enter 1-9, respectively: ";
cin >> b;
switch(b)
{
case '1' : a[0][0] = 'O'; break;
case '2' : a[0][1] = 'O'; break;
case '3' : a[0][2] = 'O'; break;
case '4' : a[1][0] = 'O'; break;
case '5' : a[1][1] = 'O'; break;
case '6' : a[1][2] = 'O'; break;
case '7' : a[2][0] = 'O'; break;
case '8' : a[2][1] = 'O'; break;
case '9' : a[2][2] = 'O'; break;
default: cout << "You have to play by the rules!" << endl;
}
return 0;
}
bool checkPlayerOne()
{
if(a[0][0] == 'X' && a[0][1] == 'X' && a[0][2] == 'X') return 1;
if(a[1][0] == 'X' && a[1][1] == 'X' && a[1][2] == 'X') return 1;
if(a[2][0] == 'X' && a[2][1] == 'X' && a[2][2] == 'X') return 1;
if(a[0][0] == 'X' && a[1][0] == 'X' && a[2][0] == 'X') return 1;
if(a[0][1] == 'X' && a[1][1] == 'X' && a[2][1] == 'X') return 1;
if(a[0][2] == 'X' && a[1][2] == 'X' && a[2][2] == 'X') return 1;
if(a[0][0] == 'X' && a[1][1] == 'X' && a[2][2] == 'X') return 1;
if(a[2][0] == 'X' && a[1][1] == 'X' && a[0][2] == 'X') return 1;
}
bool checkPlayerTwo()
{
if(a[0][0] == 'O' && a[0][1] == 'O' && a[0][2] == 'O') return 1;
if(a[1][0] == 'O' && a[1][1] == 'O' && a[1][2] == 'O') return 1;
if(a[2][0] == 'O' && a[2][1] == 'O' && a[2][2] == 'O') return 1;
if(a[0][0] == 'O' && a[1][0] == 'O' && a[2][0] == 'O') return 1;
if(a[0][1] == 'O' && a[1][1] == 'O' && a[2][1] == 'O') return 1;
if(a[0][2] == 'O' && a[1][2] == 'O' && a[2][2] == 'O') return 1;
if(a[0][0] == 'O' && a[1][1] == 'O' && a[2][2] == 'O') return 1;
if(a[2][0] == 'O' && a[1][1] == 'O' && a[0][2] == 'O') return 1;
}
int main()
{
cout << " -= Tic Tac Toe =-" << endl;
cout << " -----------------" << endl;
draw();
cout << "\nPlayer one" << endl;
getinputP1();
draw();
cout << "\nPlayer two" << endl;
getinputP2();
draw();
for (;;)
{
cout << "\nPlayer one" << endl;
getinputP1();
if(checkPlayerOne() == 1)
{
draw();
cout << "Player one wins!" << endl;
break;
}
draw();
cout << "\nPlayer two" << endl;
getinputP2();
if (checkPlayerTwo() == 1)
{
draw();
cout << "Player two wins!" << endl;
break;
}
else
{
draw();
cout << "Draw!" << endl;
break;
}
system("pause");
}
return 0;
}