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
|
#include <iostream>
using namespace std;
void printBoard(char[3][3], int, int);
int getRow(void);
int getCol(void);
void playerOMove(char array[][3]);
void playerXMove(char array[][3]);
int main ()
{
// Named constants for array dimensions
const int ROWS = 3;
const int COLUMNS = 3;
// Initialize Board
char board[ROWS][COLUMNS] = {{'-','-','-'},
{'-','-','-'},
{'-','-','-'}};
printBoard(board, ROWS, COLUMNS);
playerXMove(board);
printBoard(board, ROWS, COLUMNS);
playerOMove(board);
printBoard(board, ROWS, COLUMNS);
system("pause");
return 0;
}
// Asks user to input what row for move
// Does check to see if valid input
int getRow(void)
{
int row;
do {
cout << "What row to make move?" << endl;
cin >> row;
if (row > 2 || row < 0) cout << "Incorrect row, please choose between 0, 1, or 2." << endl;
} while (row > 2 || row < 0);
return row;
}
// Asks user to input what column for move
// Does check to see if valid input
int getCol(void)
{
int col;
do {
cout << "What column to make move?" << endl;
cin >> col;
if (col > 2 || col < 0) cout << "Incorrect column, please choose between 0, 1, or 2." << endl;
} while (col > 2 || col < 0);
return col;
}
// Allows Player 'X' to move
// (does not check to see if space is already taken)
void playerXMove(char array[][3])
{
cout << "Pleyer X's turn" << endl;
int row = getRow();
int col = getCol();
array[row][col] = 'X';
}
// Allows Player 'O' to make a move
// does not check to see if space is already taken)
void playerOMove(char array[][3])
{
cout << "Pleyer O's turn" << endl;
int row = getRow();
int col = getCol();
array[row][col] = 'O';
}
// Function to print boad at whatever state it is in
void printBoard(char array[][3], int rows, int cols)
{
cout << "Heres the board:" << endl;
for (int i =0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
cout << array[i][j] << " ";
cout << endl;
}
}
|