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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
#include <iostream>
class Board
{
private:
static const int NO_ROWS{3};
static const int NO_COLS{3};
char symbol[3]{'*', 'O', 'X'};
const char TIE = 'T';
char board[NO_ROWS][NO_COLS];
int NO_MOVES{0};
int MAX_NO_MOVES;
const int MAX_MOVES = NO_COLS * NO_ROWS; //total cells on the board
public:
Board()
{
for(int row = 0; row < NO_ROWS; row++)
{
for(int col = 0; col < NO_COLS; col++)
{
board[row][col] = symbol[0];
}
}
};
~Board(){};
// displays board
void display()
{
for(int row = 0; row < NO_ROWS; row++)
{
for(int col = 0; col < NO_COLS; col++)
{
std::cout << board[row][col] << ' ';
}
std::cout << '\n';
}
}
// checks if the move is valid
bool move(int player, int row, int col)
{
if( isValidMove(row,col) )
{
board[row][col] = symbol[player];
NO_MOVES++;
display();
return true;
}
else
{
std::cout
<< row << '-' << col << " is not a valid move ... try again\n\n";
return false;
}
}
bool isValidMove(int row, int col)
{
if( board[row][col] == symbol[0])
return true;
else
return false;
}
// switches players after every move
void SwapPlayer(int player)
{
if (player == 1)
{
player = 2;
}
else if (player == 2)
{
player = 1;
}
}
// User's choice to drop piece
int dropChoice(int b, char player){
if(b >=0 && b<= 3)
{
if(board[0][b] == ' '){
int i;
for(i = 0;board[i][b] == ' ';i++)
if(i == 2
){board[i][b] = player;
return i;}
i--;
board[i][b] =player;
return i;
}
else{
return -1;
}
}
else{
return -1;
}
}
};
using namespace std;
// assing names and symbol to user
struct playerInfo
{
char playerName[10];
char playerID;
};
//char board[3][3] or char board[NO_ROWS][NO_COLS];?
int PlayerDrop( char board[3][3], playerInfo activePlayer );
void CheckBelow ( char board[3][3], playerInfo activePlayer, int dropChoice );
void DisplayBoard ( char board[3][3] );
int checkThree ( char board[3][3], playerInfo activePlayer );
int FullBoard( char board[3][3] );
void PlayerWin ( playerInfo activePlayer );
int restart ( char board[3][3]);
int main()
{
int dropChoice, win, full, again;
char board[3][3];
cout << "Let's Play Connect 3. Below is the initial game board.\n"
<<"Select column number 1, 2, 3; along the top; to indicate your move.\n"
<<"Enter 0 to quit.\n"
<< endl;
// display #'s on game board
cout<<"1 2 3\n";
cout <<"-----\n";
// displays board
Board playingBoard;
playingBoard.display();
cout<<endl;
playerInfo playerOne, playerTwo;
cout << "Player One please enter your name: ";
cin >> playerOne.playerName;
playerOne.playerID = 'X';
cout << "Player Two please enter your name: ";
cin >> playerTwo.playerName;
playerTwo.playerID = 'O';
cout << "The two player tokens are X and O. " << playerOne.playerName
<< " begins."<< endl;
// receive user input and print board with user input
// display #'s on game board
cout<<"1 2 3\n";
cout <<"-----\n";
playingBoard.display();
do
{
dropChoice = PlayerDrop( board, playerOne );
CheckBelow( board, playerOne, dropChoice );
DisplayBoard( board );
win = checkThree( board, playerOne );
if ( win == 1 )
{
PlayerWin(playerOne);
again = restart(board);
if (again == 2)
{
break;
}
}
dropChoice = PlayerDrop( board, playerTwo );
CheckBelow( board, playerTwo, dropChoice );
DisplayBoard( board );
win = checkThree( board, playerTwo );
if ( win == 1 )
{
PlayerWin(playerTwo);
again = restart(board);
if (again == 2)
{
break;
}
}
full = FullBoard( board );
if ( full == 7 )
{
cout << "The board is full, it is a draw!" << endl;
again = restart(board);
}
}while ( again != 2 );
return 0;
}
//=====================
// check win patterns
int checkThree (char board[3][3], playerInfo activePlayer)
{
char XO;
int win;
XO = activePlayer.playerID;
win = 0;
for( int i = 3; i >= 1; --i )
{
for( int j = 3; j >= 1; --j )
{
// decending diagonal
if( board[i][j] == XO &&
board[i-1][j-1] == XO &&
board[i+1][j+1] == XO )
{
win = 1;
}
// ascending diagonal
if( board[i][j] == XO &&
board[i+1][j-1] == XO &&
board[i-1][j+1] == XO )
{
win = 1;
}
// top horizontal
if( board[i-1][j-1] == XO &&
board[i-1][j] == XO &&
board[i-1][j+1] == XO)
{
win = 1;
}
// middle horizontal
if( board[i][j] == XO &&
board[i][j-1] == XO &&
board[i][j+1] == XO )
{
win = 1;
}
// bottom horizontal
if( board[i+1][j-1] == XO &&
board[i+1][j] == XO &&
board[i+1][j+1] == XO)
{
win = 1;
}
//left vertical
if ( board[i-1][j-1] == XO &&
board[i][j-1] == XO &&
board[i+1][j-1] == XO )
{
win = 1;
}
//middle vertical
if ( board[i-1][j] == XO &&
board[i][j] == XO &&
board[i+1][j] == XO )
{
win = 1;
}
// right vertival
if ( board[i-1][j+1] == XO &&
board[i][j+1] == XO &&
board[i+1][j+1] == XO )
{
win = 1;
}
}
}
return win;
}
int fullBoard(char board[3][3])
{
int full;
full = 0;
for ( int i = 1; i <= 3; ++i )
{
if ( board[1][i] != '*' )
++full;
}
return full;
}
int restart (char board[3][3])
{
int restart;
cout << "Would you like to play again? Yes(1) No(2): ";
cin >> restart;
if ( restart == 1 )
{
for(int i = 1; i <= 3; i++)
{
for(int j = 1; j <= 3; j++)
{
board[i][j] = '*';
}
}
}
else
// need counted to calculate amount of games played and how many were won/who won the most
cout << "Goodbye!\n"
<< "Player 1 won: 1 times. 100.00% Player 2 won: 0 times. 0.00%"
<<"This ends the Connect game.\n"
<< "Goodbye!\n";
return restart;
}
// player wins
void PlayerWin ( playerInfo activePlayer )
{
cout << endl << activePlayer.playerName << " connected three, You Win!" << endl;
}
|