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
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <ctime>
using namespace std;
void playerOneWin(int board[]){
if((board[0] == 1 || board[2] == 1 || board[4] == 1 || board[6] == 1 || board [8] == 1) &&
(board[0] == 2 || board[2] == 2 || board[4] == 2 || board[6] == 2 || board [8] == 2) &&
(board[0] == 3 || board[2] == 3 || board[4] == 3 || board[6] == 3 || board [8] == 3))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 4 || board[2] == 4 || board[4] == 4 || board[6] == 4 || board [8] == 4) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 6 || board[2] == 6 || board[4] == 6 || board[6] == 6 || board [8] == 6))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 7 || board[2] == 7 || board[4] == 7 || board[6] == 7 || board [8] == 7) &&
(board[0] == 8 || board[2] == 8 || board[4] == 8 || board[6] == 8 || board [8] == 8) &&
(board[0] == 9 || board[2] == 9 || board[4] == 9 || board[6] == 9 || board [8] == 9))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 1 || board[2] == 1 || board[4] == 1 || board[6] == 1 || board [8] == 1) &&
(board[0] == 4 || board[2] == 4 || board[4] == 4 || board[6] == 4 || board [8] == 4) &&
(board[0] == 7 || board[2] == 7 || board[4] == 7 || board[6] == 7 || board [8] == 7))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 2 || board[2] == 2 || board[4] == 2 || board[6] == 2 || board [8] == 2) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 8 || board[2] == 8 || board[4] == 8 || board[6] == 8 || board [8] == 8))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 3 || board[2] == 3 || board[4] == 3 || board[6] == 3 || board [8] == 3) &&
(board[0] == 6 || board[2] == 6 || board[4] == 6 || board[6] == 6 || board [8] == 6) &&
(board[0] == 9 || board[2] == 9 || board[4] == 9 || board[6] == 9 || board [8] == 9))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 1 || board[2] == 1 || board[4] == 1 || board[6] == 1 || board [8] == 1) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 9 || board[2] == 9 || board[4] == 9 || board[6] == 9 || board [8] == 9))
{
cout << "Player 1 Wins!";
}
else if((board[0] == 3 || board[2] == 3 || board[4] == 3 || board[6] == 3 || board [8] == 3) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 7 || board[2] == 7 || board[4] == 7 || board[6] == 7 || board [8] == 7))
{
cout << "Player 1 Wins!";
}
}
void playerTwoWin(int board[])
{
if((board[1] == 1 || board[3] == 1 || board[5] == 1 || board[7] == 1) &&
(board[1] == 2 || board[3] == 2 || board[5] == 2 || board[7] == 2) &&
(board[1] == 3 || board[3] == 3 || board[5] == 3 || board[7] == 3))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 4 || board[3] == 4 || board[5] == 4 || board[7] == 4) &&
(board[1] == 5 || board[3] == 5 || board[5] == 5 || board[7] == 5) &&
(board[1] == 6 || board[3] == 6 || board[5] == 6 || board[7] == 6))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 7 || board[3] == 7 || board[5] == 7 || board[7] == 7) &&
(board[1] == 8 || board[3] == 8 || board[5] == 8 || board[7] == 8) &&
(board[1] == 9 || board[3] == 9 || board[5] == 9 || board[7] == 9))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 1 || board[3] == 1 || board[5] == 1 || board[7] == 1) &&
(board[1] == 4 || board[3] == 4 || board[5] == 4 || board[7] == 4) &&
(board[1] == 7 || board[3] == 7 || board[5] == 7 || board[7] == 7))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 2 || board[3] == 2 || board[5] == 2 || board[7] == 2) &&
(board[1] == 5 || board[3] == 5 || board[5] == 5 || board[7] == 5) &&
(board[1] == 8 || board[3] == 8 || board[5] == 8 || board[7] == 8))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 3 || board[3] == 3 || board[5] == 3 || board[7] == 3) &&
(board[1] == 6 || board[3] == 6 || board[5] == 6 || board[7] == 6) &&
(board[1] == 9 || board[3] == 9 || board[5] == 9 || board[7] == 9))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 1 || board[3] == 1 || board[5] == 1 || board[7] == 1) &&
(board[1] == 5 || board[3] == 5 || board[5] == 5 || board[7] == 5) &&
(board[1] == 9 || board[3] == 9 || board[5] == 9 || board[7] == 9))
{
cout << "Player 2 Wins!";
}
else if((board[1] == 3 || board[3] == 3 || board[5] == 3 || board[7] == 3) &&
(board[1] == 5 || board[3] == 5 || board[5] == 5 || board[7] == 5) &&
(board[1] == 7 || board[3] == 7 || board[5] == 7 || board[7] == 7))
{
cout << "Player 2 Wins!";
}
}
void printBoard(int board[], char numbers[], int j){
for(int x = 1; x < 10; x++)
{
int y = x - 1;
if(board[j] == x)
{
numbers[y] = 'O';
}
}
cout << " " << numbers[0] << " " << "|" << " " << numbers[1] << " " << "|" << " " << numbers[2] << " " << endl;
cout << "---|---|---" << endl;
cout << " " << numbers[3] << " " << "|" << " " << numbers[4] << " " << "|" << " " << numbers[5] << " " << endl;
cout << "---|---|---" << endl;
cout << " " << numbers[6] << " " << "|" << " " << numbers[7] << " " << "|" << " " << numbers[8] << " " << endl;
}
int main()
{
int j = 0;
int k = 1;
char numbers[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int board[9];
for(int i = 1; i < 10; i++)
{
if(i == 1 || i == 3 || i == 5 || i == 7 || i == 9)
{
cin >> board[j];
j = j + 2;
printBoard(board, numbers, j);
}
if(i == 2 || i == 4 || i == 6 || i == 8)
{
cin >> board[k];
k = k + 2;
}
}
playerOneWin(board);
playerTwoWin(board);
return 0;
}
| |