1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int checkwin();
char player1_mark;
char player2_mark;
//********BOARDSTATUS CLASS*********
//Holds information about board.
//
class Boardstatus
{
char A1;
char A2;
char A3;
char B1;
char B2;
char B3;
char C1;
char C2;
char C3;
public:
void setsquarestatus( int square, char status )
{
switch ( square )
{
case 1:
A1 = status;
break;
case 2:
A2 = status;
break;
case 3:
A3 = status;
break;
case 4:
B1 = status;
break;
case 5:
B2 = status;
break;
case 6:
B3 = status;
break;
case 7:
C1 = status;
break;
case 8:
C2 = status;
break;
case 9:
C3 = status;
break;
}
}
//this functions job is to return information. it does not modify the object in any way so its good
//practice to make functions like these const
char getsquarestatus( int square ) const
{
switch ( square )
{
case 1:
return A1;
break;
case 2:
return A2;
break;
case 3:
return A3;
break;
case 4:
return B1;
break;
case 5:
return B2;
break;
case 6:
return B3;
break;
case 7:
return C1;
break;
case 8:
return C2;
break;
case 9:
return C3;
break;
}
}
//End of public
};
//*********BOARDPRINT FUNCTION***********
//
//
//Function accepts a reference to the BSTAT object, a copy isn't necessary.
//and the object is made const because it shouldnt be modified in this function.
void boardprint(const Boardstatus& BSTAT)
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (" << player1_mark << ") - Player 2 (" << player2_mark << ")" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << BSTAT.getsquarestatus(1) << " | " << BSTAT.getsquarestatus(2) << " | " << BSTAT.getsquarestatus(3) << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << BSTAT.getsquarestatus(4) << " | " << BSTAT.getsquarestatus(5) << " | " << BSTAT.getsquarestatus(6) << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << BSTAT.getsquarestatus(7) << " | " << BSTAT.getsquarestatus(8) << " | " << BSTAT.getsquarestatus(9) << endl;
cout << " | | " << endl << endl;
}
int main()
{
//Start of initializing all squares as a blank
Boardstatus BSTAT;
BSTAT.setsquarestatus( 1, ' ' );
BSTAT.setsquarestatus( 2, ' ' );
BSTAT.setsquarestatus( 3, ' ' );
BSTAT.setsquarestatus( 4, ' ' );
BSTAT.setsquarestatus( 5, ' ' );
BSTAT.setsquarestatus( 6, ' ' );
BSTAT.setsquarestatus( 7, ' ' );
BSTAT.setsquarestatus( 8, ' ' );
BSTAT.setsquarestatus( 9, ' ' );
//End of square initialization
boardprint(BSTAT);//put object in function.
do
{
cout << "Player 1, choose X or O: ";
cin >> player1_mark;
cin.clear();
cin.sync();
if ( player1_mark == 'X' )
{
player2_mark = 'O';
}
else if (player1_mark == 'O' )
{
player2_mark = 'X';
}
else
{
cout << "Invalid character. Please try again." << endl;
}
} while ( player1_mark != 'X' && player1_mark != 'O');
boardprint(BSTAT);//put object in function
return 0;
}
|