// Timothy Price CS1A Ch 7 Challenge 15
/*******************************************************************************
* Run game
*
* This program receives inputs to complete a game of tic-tac-toe.
*
* Inputs:
* Coordinates for placement of piece
*
* Outputs:
* Game board
* Selected placement
* Win or tie indicator
******************************************************************************/
#include <iostream>
using namespace std;
// Constants for board size
const int SIZE = 3;
// Function Prototypes
void initializeBoard(char board[SIZE][SIZE]);
void displayBoard(char board[SIZE][SIZE]);
bool checkWin(char board[SIZE][SIZE], char symbol);
bool isBoardFull(char board[SIZE][SIZE]);
int main() {
char board[SIZE][SIZE];
// Initialize board with placeholder asterisks
initializeBoard(board);
cout << "-------------------------------------" << endl;
cout << " TIC-TAC-TOE (X vs O) " << endl;
cout << "-------------------------------------" << endl;
cout << "Use row and column numbers (0, 1, or 2)." << endl;
bool gameRunning = true;
char currentPlayer = 'X'; // Player 1 starts with X
while (gameRunning) {
displayBoard(board);
int row, col;
// Get move from current player
cout << "Player ";
if (currentPlayer == 'X') {
cout << "1 (X)";
} else {
cout << "2 (O)";
}
cout << ", enter your move:" << endl;
while (true) {
cout << " Enter row (0-2): ";
cin >> row;
cout << row;
cout << " Enter column (0-2): ";
cin >> col;
cout << col;
// Validate input bounds and whether the spot is empty
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
if (board[row][col] == '*') {
board[row][col] = currentPlayer;
break;
} else {
cout << "Choose another position." << endl;
}
} else {
cout << "Invalid, enter numbers between 0 and 2." << endl;
}
}
// Check if current player won
if (checkWin(board, currentPlayer)) {
displayBoard(board);
if (currentPlayer == 'X') {
cout << "Player 1 (X) wins!" << endl;
} else {
cout << "Player 2 (O) wins!" << endl;
}
gameRunning = false;
}
// Check if the board is full (Tie)
else if (isBoardFull(board)) {
displayBoard(board);
cout << "It's a tie! The board is full." << endl;
gameRunning = false;
}
else {
// Switch turns
if (currentPlayer == 'X') {
currentPlayer = 'O';
} else {
currentPlayer = 'X';
}
}
}
cout << "-------------------------------------" << endl;
cout << " GAME OVER " << endl;
cout << "-------------------------------------" << endl;
return 0;
}
// Function to initialize the board with '*'
void initializeBoard(char board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
board[i][j] = '*';
}
}
}
// Function to display the game board grid
void displayBoard(char board[SIZE][SIZE]) {
cout << endl << " 0 1 2" << endl;
cout << " -------------";
for (int i = 0; i < SIZE; ++i) {
cout << endl << i << " | ";
for (int j = 0; j < SIZE; ++j) {
cout << board[i][j] << " | ";
}
cout << endl << " -------------";
}
cout << endl << endl;
}
// Function to check if a player has won
bool checkWin(char board[SIZE][SIZE], char symbol) {
// Check rows
for (int i = 0; i < SIZE; ++i) {
if (board[i][0] == symbol
&& board[i][1] == symbol
&& board[i][2] == symbol) {
return true;
}
}
// Check columns
for (int j = 0; j < SIZE; j++) {
if (board[0][j] == symbol
&& board[1][j] == symbol
&& board[2][j] == symbol) {
return true;
}
}
// Check diagonals
if (board[0][0] == symbol
&& board[1][1] == symbol
&& board[2][2] == symbol) {
return true;
}
if (board[0][2] == symbol
&& board[1][1] == symbol
&& board[2][0] == symbol) {
return true;
}
return false;
}
// Function to check if the board is completely full (used for ties)
bool isBoardFull(char board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (board[i][j] == '*') {
return false; // Found an empty spot, not full yet
}
}
}
return true; // No empty spots left
}