SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Programming Projects
Question:10 | ISBN:9780321531346 | Edition: 7

Question

Write a program that will allow two users to play tic-tac-toe. The program

should ask for moves alternately from player X and player O. The program displays the game positions as follows:

1 2 3

4 5 6

7 8 9

The players enter their moves by entering the position number they wish

to mark. After each move, the program displays the changed board. A

sample board configuration is as follows:

X X O

4 5 6

O 8 9



TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
using namespace std;
// Function to display the tic-tac-toe board
void displayBoard(const char board[]) {
    cout << board[0] << " " << board[1] << " " << board[2] << endl;
    cout << board[3] << " " << board[4] << " " << board[5] << endl;
    cout << board[6] << " " << board[7] << " " << board[8] << endl;
}

// Function to check if a player has won
bool checkWin(const char board[], char player) {
    // Check rows
    for (int i = 0; i < 3; i++) {
        if (board[i] == player && board[i + 3] == player && board[i + 6] == player) {
            return true;
        }
    }
    
    // Check columns
    for (int i = 0; i < 3; i++) {
        if (board[i * 3] == player && board[i * 3 + 1] == player && board[i * 3 + 2] == player) {
            return true;
        }
    }
    
    // Check diagonals
    if ((board[0] == player && board[4] == player && board[8] == player) ||
        (board[2] == player && board[4] == player && board[6] == player)) {
        return true;
    }
    
    return false;
}

int main() {
    char board[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    char currentPlayer = 'X';
    int move;
    
    cout << "Let's play Tic-Tac-Toe!" << endl;
    
    while (true) {
        displayBoard(board);
        
        cout << "Player " << currentPlayer << ", enter your move: ";
        cin >> move;
        
        // Update the board with the player's move
        board[move - 1] = currentPlayer;
        
        // Check if the current player has won
        if (checkWin(board, currentPlayer)) {
            displayBoard(board);
            cout << "Player " << currentPlayer << " wins!" << endl;
            break;
        }
        
        // Switch players for the next turn
        currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
    }
    
    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Let's play Tic-Tac-Toe!
1 2 3
4 5 6
7 8 9
Player X, enter your move: 1
X 2 3
4 5 6
7 8 9
Player O, enter your move: 5
X 2 3
4 O 6
7 8 9
Player X, enter your move: 9
X 2 3
4 O 6
7 8 X
Player O, enter your move: 4
X 2 3
O O 6
7 8 X
Player X, enter your move: 3
X 2 X
O O 6
7 8 X
Player X wins!

 

0 0

Discussions

Post the discussion to improve the above solution.