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
|
#include <iostream>
using namespace std;
void get_input (char []);
void showBoard (char [], int, int);
void winning (char [], int, int);
int main ()
{
int playerX(0), playerO(0);
char gameboard[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
cout << "This is a game of tic-tac-toe. You will choose your selection by"
<< " entering the corresponding number (1-9) on the gameboard.\n\n";
showBoard (gameboard, playerX, playerO);
cout << "\nPlayer X will select first.\n";
get_input (gameboard);
winning (gameboard, playerX, playerO);
return 0;
}
void get_input (char a[])
{
for (int i = 1; i <= 9; i++)
{
int playerX(0), playerO(0);
cout << "What is your choice player X? ";
cin >> playerX;
showBoard (a, playerX, playerO);
cout << "What is your choice player O? ";
cin >> playerO;
showBoard (a, playerX, playerO);
}
}
void showBoard (char a[], int playerX, int playerO)
{
cout << a[0] << " " << a[1] << " " << a[2] << endl;
cout << a[3] << " " << a[4] << " " << a[5] << endl;
cout << a[6] << " " << a[7] << " " << a[8] << endl;
if (playerX == 1)
a[0] = 'X';
else if (playerX == 2)
a[1] = 'X';
else if (playerX == 3)
a[2] = 'X';
else if (playerX == 4)
a[3] = 'X';
else if (playerX == 5)
a[4] = 'X';
else if (playerX == 6)
a[5] = 'X';
else if (playerX == 7)
a[6] = 'X';
else if (playerX == 8)
a[7] = 'X';
else if (playerX == 9)
a[8] = 'X';
else if (playerO == 1)
a[0] = 'O';
else if (playerO == 2)
a[1] = 'O';
else if (playerO == 3)
a[2] = 'O';
else if (playerO == 4)
a[3] = 'O';
else if (playerO == 5)
a[4] = 'O';
else if (playerO == 6)
a[5] = 'O';
else if (playerO == 7)
a[6] = 'O';
else if (playerO == 8)
a[7] = 'O';
else if (playerO == 9)
a[8] = 'O';
}
void winning (char a[], int playerX, int playerO)
{
// 16 winning conditions between two players
if (a[0]==a[1]==a[2]=='X')
cout << "You won player X!";
else if (a[3]==a[4]==a[5]=='X')
cout << "You won player X!";
else if (a[6]==a[7]==a[8]=='X')
cout << "You won player X!";
else if (a[0]==a[3]==a[6]=='X')
cout << "You won player X!";
else if (a[1]==a[4]==a[7]=='X')
cout << "You won player X!";
else if (a[2]==a[5]==a[8]=='X')
cout << "You won player X!";
else if (a[0]==a[4]==a[8]=='X')
cout << "You won player X!";
else if (a[2]==a[4]==a[6]=='X')
cout << "You won player X!";
else if (a[0]==a[1]==a[2]=='O')
cout << "You won player O!";
else if (a[3]==a[4]==a[5]=='O')
cout << "You won player O!";
else if (a[6]==a[7]==a[8]=='O')
cout << "You won player O!";
else if (a[0]==a[3]==a[6]=='O')
cout << "You won player O!";
else if (a[1]==a[4]==a[7]=='O')
cout << "You won player O!";
else if (a[2]==a[5]==a[8]=='O')
cout << "You won player O!";
else if (a[0]==a[4]==a[8]=='O')
cout << "You won player O!";
else if (a[2]==a[4]==a[6]=='O')
cout << "You won player O!";
else
cout << "It’s a draw. No one won.";
}
|