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
|
#include <iostream>
#include <vector>
using namespace std;
struct playerInfo
{
char playerName[81];
char playerID;
};
void PlayerWin(playerInfo activePlayer);
void draw_board(const vector<vector<char> >& vec)
{
cout << " 0 1 2 \n";
cout << " +---+---+---+\n";
for (int i = 0; i < vec.size(); i++)
{
cout << i << " ";
for (int j = 0; j < vec[i].size(); j++)
{
std::cout << "| " << vec[i][j] << " ";
}
cout << "|";
cout << '\n';
cout << " +---+---+---+\n";
}
}
void enter(int row, int col, char ch, std::vector< std::vector<char> >& vec)
{
vec[row][col] = ch;
}
bool check(const vector<vector<char> >& vec)
{
//to check diagonals
if ((vec[0][0] == 'X' && vec[1][1] == 'X' && vec[2][2] == 'X')
|| (vec[0][2] == 'X' && vec[1][1] == 'X' && vec[2][0] == 'X'))
{
std::cout << "Player X won this game\n";
return true;
}
else if ((vec[0][0] == 'O' && vec[1][1] == 'O' && vec[2][2] == 'O')
|| (vec[0][2] == 'O' && vec[1][1] == 'O' && vec[2][0] == 'O'))
{
std::cout << "Player O won this game\n";
return true;
}
//to check horizonatal and vertical
for (int i = 0; i < vec.size(); i++)
{
if ((vec[i][0] == 'X' && vec[i][1] == 'X' && vec[i][2] == 'X')
|| (vec[0][i] == 'X' && vec[1][i] == 'X' && vec[2][i] == 'X'))
{
std::cout << "Player X won this game\n";
return true;
}
else if ((vec[i][0] == 'O' && vec[i][1] == 'O' && vec[i][2] == 'O')
|| (vec[0][i] == 'O' && vec[1][i] == 'O' && vec[2][i] == 'O'))
{
std::cout << "Player O won this game\n";
return true;
}
}
}
void start(vector<vector<char>>& vec)
{
int row, col, res = 0;
char ch;
std::vector<int> index = { 0, 1, 2 };
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
vec[i][j] = ' ';
}
}
for (int i = 0; i < 9;)
{
if (i == 0 || i % 2 == 0)
{
ch = 'X';
cout << "Chance to enter X\n";
}
else
{
ch = 'O';
cout << "Chance to enter O\n";
}
cout << "Enter row number ";
cin >> row;
cout << "Enter column number ";
cin >> col;
if (vec[row][col] == ' ' || vec[row][col] == ' ')
{
enter(row, col, ch, vec);
draw_board(vec);
if (i >= 2)
{
res = check(vec);
if (res == 1)
{
break;
}
}
i++;
}
else
{
cerr << "This position already contains a character\n"; // I found this on a website we used for the connect 4 game, and it came in hany
}
}
if (res == 0)
{
std::cout << "This game draws\n";
}
}
void PlayerWin(playerInfo activePlayer)
{
cout << endl << activePlayer.playerName << " Tic Tac Toe, You Win!" << endl;
}
int main()
{
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';
vector< std::vector<char> > board(3, std::vector<char>(3));
draw_board(board);
start(board);
void PlayerWin(playerInfo activePlayer);
}
|