Answer to Question #157610 in C++ for zain ul abdeen

Question #157610

 Write a program to play tic tac toe (zero kaataa). (Google tic tac toe if you dont know)


You need to create a 3 by 3 array to represent the game board.


One by one, each player would specify the row and column numbers and that player's symbol would be written to the corresponding array cell. Make sure a player doe not write to an already occupied cell.


At the end of each turn, the table would be drawn on the screen. At the end of each turn, your program should check if any player has won or not.


Game ends when either a player wins or the table is full.



1
Expert's answer
2021-01-23T10:19:10-0500

Here is the code:

// Example program
#include <iostream>
#include <string>

//symbols for game
const char Empty = '.', Player1 = 'X', Player2 = 'O';

//game field
char field[3][3] =
{
    { Empty, Empty, Empty },
    { Empty, Empty, Empty },
    { Empty, Empty, Empty }
};

//draw game field
void DrawField()
{
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            std::cout << field[x][y] << " ";
        }
        std::cout << std::endl;
    }
}

//place X or O
//Returns true if move is valid
//Returns false otherwise
bool MakeMove(int x, int y, char player)
{
    if (x < 1 || y < 1 || x > 3 || y > 3) return false;
    if (field[x - 1][y - 1] != Empty) return false;
    
    field[x - 1][y - 1] = player;
    return true;
}

//Check win for player
bool Win(int x, int y, char player)
{
    //4 possible ways to win:
    //horisontal line, vertical line and 2 diagonals
    bool horizontal = true, vertical = true,
        diag1 = true, diag2 = true;
    
    for (int i = 0; i < 3; i++)
    {
        if (field[i][y - 1] != player) horizontal = false;
        if (field[x - 1][i] != player) vertical = false;
        if (field[i][i] != player) diag1 = false;
        if (field[i][2 - i] != player) diag2 = false;
    }
    return horizontal || vertical || diag1 || diag2;
}

//Check if field is filled
bool Tie()
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            if (field[i][j] == Empty) return false;
    
    return true;
}

//toggle current player
void ChangePlayer(char & player)
{
    if (player == Player1) player = Player2;
    else  player = Player1;
}

int main()
{
    int x, y; //for reading coords
    char player = Player1; //current player
    
    //game loop
    while (true)
    {
        //show game state
        DrawField();
        std::cout << player << "'s turn" << std::endl << std::endl;
        
        //read coords
        std::cout << "Enter coordinates: ";
        std::cin >> x >> y;
        
        while (!MakeMove(x, y, player))
        {
            std::cout << "Invalid move" << std::endl;
            std::cout << "Enter coordinates: ";
            std::cin >> x >> y;
        }
        std::cout << std::endl;
        
        //check win
        if (Win(x, y, player))
        {
            DrawField();
            std::cout << player << " wins!" << std::endl;
            break;
        }
        
        //check tie
        if (Tie())
        {
            DrawField();
            std::cout << "Tie!" << std::endl;
            break;
        }
        
        //game is not over, preparation for next turn
        ChangePlayer(player);
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog