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
|
#include <iostream>
#include <iomanip>
using namespace std;
const int rows = 8;
const int columns = 5;
//This function creates a 8x5 board
char matrix[rows][columns] = { '.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.' };
//This function displays the board
void display()
{
int width = 3;
cout << setw(width) << "1" << setw(width) << "2" << setw(width) << "3" << setw(width) << "4" << setw(width) << "5" << setw(width) << '\n';
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << setw(3) << matrix[i][j] << setw(3);
}
cout << endl;
}
cout << setw(width) << "1" << setw(width) << "2" << setw(width) << "3" << setw(width) << "4" << setw(width) << "5" << setw(width) << '\n';
}
//This the main function that executes the player's selected column
void input(char player)
{
int a;
cout << "Enter the column" << endl;
cin >> a;
if (a > 0 && a < 6)
{
for (int i = 7; i >= 0; i++)
{
if (matrix[i][a - 1] == '.')
{
matrix[i][a - 1] = player;
break;
}
}
}
}
//This function changes the players between 'X' or 'O'
char togglePlayer(char player)
{
if (player == 'X')
{
return player = 'O';
}
else
{
return player = 'X';
}
}
int main()
{
char player = 'X';
while (true)
{
display();
input(player);
togglePlayer(player);
}
system("pause");
return 0;
}
|