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
|
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <cstdlib>
#include <string>
using namespace std;
char checkWinner(char[][4]);
char checkTie(char[][4]);
const char PLAYER1='X';
const char PLAYER2='O';
int main()
{
//Game over variable
bool bGameOver=false;
//Variable to hold winner status
char winner;
winner='N';
//variable for play again
string again;
//variable for checkTie status
char tie;
//Constants for board size
const int ROWS=4;
const int COLS=4;
char board[ROWS][COLS] = {{' ', '1', '2', '3'},
{ '1', '*', '*', '*'},
{'2', '*', '*', '*'},
{'3', '*', '*', '*'}};
//counter variable
int i;
//display the board
for (int i = 0; i <ROWS; i++)
{
cout << board[i][0] << "\t" << board[i][1] << "\t" <<
board[i][2] << "\t" << board[i][3]<< endl;
}//end for
//Variables to hold selection
int rowx, colx;
do//Main game loop
{
//Get player 1 selection
cout << "Player X - enter row:"; cin >> rowx;
cout << "Player X - enter column:"; cin >> colx;
cout << "" << endl;
if (board[rowx][colx] != '*')
{
cout << "This space is taken...try again" << endl;
cout << "row:" << endl;
cin >> rowx;
cout << "column:";
cin >> colx;
}//end if
board[rowx][colx]=PLAYER1;
//counter variable
for (int i = 0; i <ROWS; i++)
{
cout << board[i][0] << "\t" << board[i][1] << "\t" <<
board[i][2] << "\t" << board[i][3]<< endl;
}//end for
//call checkwinner function
winner=checkWinner(board);
if (winner=='X')
cout << "Player 1 wins!" << endl;
//Get player 2 selection
cout << "Player O - enter row:"; cin >> rowx;
cout << "Player O - enter column:"; cin >> colx;
cout << "" << endl;
if (board[rowx][colx] != '*')
{
cout << "This space is taken...try again" << endl;
cout << "row:" << endl;
cin >> rowx;
cout << "column:";
cin >> colx;
}//end if
board[rowx][colx]=PLAYER2;
//counter variable
for (int i = 0; i <ROWS; i++)
{
cout << board[i][0] << "\t" << board[i][1] << "\t" <<
board[i][2] << "\t" << board[i][3]<< endl;
}//end for
//call checkwinner function
winner=checkWinner(board);
//call checkTie function
tie=checkTie(board);
if (winner=='O')
cout << "Player 2 wins!" << endl;
if(tie=='C')
{
cout << "It's a tie!" << endl;
winner='C';
}//end if
}while(winner != 'X' || winner != 'O' || winner != 'C');
if (winner=='X'|| winner=='O'|| winner=='C')
{
cout << "Game over!" << endl;
cin >> again;
}//end if
system ("PAUSE");
return 0;
} //end main
//Declare function checkWinner
char checkWinner(char arr[][4])
{
//variable to hold result
char result='B';
//variable to hold counter
int i=1;
//Check player 1 status
for(i=1; i<4; i++) /* check rows */
{
if(arr[i][1]=='X' && arr[i][2]== 'X' && arr[i][3]=='X')
{
result='X';
}
}
for(i=1; i<4; i++) /* check columns */
{
if(arr[1][i]=='X' && arr[2][i]== 'X' && arr[3][i]=='X')
{
result='X';
}
}
/* test diagonals */
if(arr[1][1]=='X' && arr[2][2]== 'X' && arr[3][3]=='X')
{
result='X';
}
if(arr[1][3]=='X' && arr[2][2]=='X' && arr[3][1]=='X')
{
result='X';
}
//Check player 2 status
for(i=1; i<4; i++) /* check rows */
{
if(arr[i][1]=='O' && arr[i][2]== 'O' && arr[i][3]=='O')
{
result='O';
}
}
for(i=1; i<4; i++) /* check columns */
{
if(arr[1][i]=='O' && arr[2][i]=='O' && arr[3][i]=='O')
{
result='O';
}
}
/* test diagonals */
if(arr[1][1]=='O' && arr[2][2]== 'O' && arr[3][3]=='O')
{
result='O';
}
if(arr[1][3]=='O' && arr[2][2]=='O' && arr[3][1]=='O')
{
result='O';
}
return result;
}//end function
//Define checkTie function
char checkTie(char arr[][4])
{
//constant for size
const int SIZE=4;
//variable to hold result
char result;
result;
//variable loop counter
int i=0;
for(i=0;i<=SIZE;i++)
{
if (arr[i][1]=='*' || arr[i][2]=='*' || arr[i][3]=='*'||
arr[1][i]=='*'|| arr[2][i]=='*'|| arr[3][i]=='*')
{
result='B';
}
else
{
result='C';
}
}//end if
return result;
}//end function
|