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
|
#include <iostream>
#include "TicTacToe.h"
using namespace std;
int winner(char Array[][3], int idx);
void aTie();
void printBoard(char Array[][3], int idx);
int main()
{
char Array[3][3] = { {'-', '-', '-' },
{'-', '-', '-' },
{'-', '-', '-' } };
int idx, player = 1, player_row, player_col, gameOver;
int temp;
//board print
do
{
printBoard(Array, 3);
cout << "Player " << player << " Please enter a row and a column number." << endl;
cin >> player_row;
cin >> player_col;
if(player == 1)
Array[player_row][player_col] = 'X';
else
Array[player_row][player_col] = 'O';
player = (player%2 + 1);
temp = winner(Array,3);
} while((winner(Array, 3) == 0));
return 0;
}
//returns 1 if winner, 2 if board full, 0 if neither
int winner(char Array[][3], int idx)
{
for (int row = 0; row < 3; row++)
{
if (Array[row][0] == Array[row][1] && Array[row][1] == Array[row][2])
{
if (Array[row][0] == 'X')
{
cout << "That's impossible! Player 1 WON!" << endl;
return 1;
}
if (Array[row][0] == 'O')
{
cout << "This can't be real! Player 2 WON!" << endl;
return 1;
}
}
}
for (int col = 0; col < 3; col++)
{
if(Array[0][col] == Array[1][col] && Array[1][col] == Array[2][col])
{
if(Array[0][col] == 'X')
{
cout << "That's impossible! Player 1 WON!" << endl;
return 1;
}
if(Array[0][col] == 'O')
{
cout << "This can't be real! Player 2 WON!" << endl;
return 1;
}
}
}
if (Array[0][0] == Array[1][1] && Array[1][1] == Array[2][2])
{
if(Array[1][1] == 'X')
{
cout << "That's impossible! Player 1 WON!" << endl;
return 1;
}
if(Array[1][1] == 'O')
{
cout << "This can't be real! Player 2 WON!" << endl;
return 1;
}
}
if (Array[0][0] != '-' && Array[0][1] != '-' && Array[0][2] != '-' && Array[1][0] != '-' && Array[1][1] != '-' && Array[1][2] != '-' && Array[2][0] != '-' && Array[2][1] != '-' && Array[2][2] != '-')
{
aTie();
return 2;
}
else
return 0;
return 0;
}
//no winner/tie
void aTie()
{
char ans = ' ';
cout << "A forseen tie! Play again yes or no(y/n)?";
cin >> ans;
if (ans == 'y' || ans == 'Y')
main();
else
"Thanks for playing!";
}
void printBoard(char Array[][3], int idx)
{
for(int row = 0; row < 3 ; row++)
{
cout << endl;
for (int col = 0; col < 3 ; col++)
cout << Array[row][col] << "\t";
}
}
|