//Devin Scheu CS1A Chapter 7, P. 448, #15
//
/**************************************************************
*
* PLAY TIC-TAC-TOE GAME
* ____________________________________________________________
* This program allows two players to play tic-tac-toe, displaying
* the board, handling moves, and determining the winner or tie.
* ____________________________________________________________
* INPUT
* row : The row for the player's move
* col : The column for the player's move
*
* OUTPUT
* gameBoard : The current state of the tic-tac-toe board
* gameResult : The result of the game (win or tie)
*
**************************************************************/
#include <iostream>
using namespace std;
int main () {
//Variable Declarations
char board[3][3] = {{'*','*','*'}, {'*','*','*'}, {'*','*','*'}};
int turn = 1; // 1 for X, 2 for O
bool gameOver = false;
while (!gameOver) {
// Display board
cout << "\nCurrent Board:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
// Player turn
char player = (turn == 1) ? 'X' : 'O';
int row, col;
cout << "Player " << turn << " (" << player << "), enter row (1-3): ";
cin >> row;
cout << row << endl;
while (row < 1 || row > 3) {
cout << "Invalid row. Enter row (1-3): ";
cin >> row;
cout << row << endl;
}
cout << "Enter column (1-3): ";
cin >> col;
cout << col << endl;
while (col < 1 || col > 3) {
cout << "Invalid column. Enter column (1-3): ";
cin >> col;
cout << col << endl;
}
row--; col--; // Adjust to 0-based index
while (board[row][col] != '*') {
cout << "Spot taken. Enter row (1-3): ";
cin >> row;
cout << row << endl;
while (row < 1 || row > 3) {
cout << "Invalid row. Enter row (1-3): ";
cin >> row;
cout << row << endl;
}
cout << "Enter column (1-3): ";
cin >> col;
cout << col << endl;
while (col < 1 || col > 3) {
cout << "Invalid column. Enter column (1-3): ";
cin >> col;
cout << col << endl;
}
row--; col--;
}
board[row][col] = player;
// Check for win
bool win = false;
// Rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) win = true;
}
// Columns
for (int i = 0; i < 3; i++) {
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) win = true;
}
// Diagonals
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) win = true;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) win = true;
if (win) {
// Display final board
cout << "\nFinal Board:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
// Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
cout << "Player " << turn << " wins!" << endl;
gameOver = true;
return 0;
}
// Check for tie
bool tie = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '*') tie = false;
}
}
if (tie) {
// Display final board
cout << "\nFinal Board:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
// Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
cout << "It's a tie!" << endl;
gameOver = true;
return 0;
}
// Switch turn
turn = (turn == 1) ? 2 : 1;
}
} //end of main()