mardi 24 décembre 2019

i am not getting player 1 to 2 in switch case when one player enters then it dosent changes to player 2 in output and ouputs player 2 again. in c++

Write a program that allows two players to play a game of tic - tac - toe. Use a two dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk(*). The program should run a loop that
• Displays the contents of the board array
• Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. • Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. • Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end.If a tie has occurred, the program should say so and end.Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board.A tie occurs when all of the locations on the board are full, but there is no winner.

#include <iostream>
using namespace std;
int main()
{
    char arr[3][3] = { { '1','2','3' },{ '4','5','6'},{ '7','8','9'} };
    char player;
    int number[3][3];

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << "|" << arr[i][j] << "|";
        }
        cout << endl;
    }
    cout << "Enter the player which wants to play\n only 1 or 2";
    cin >> player;
    for (int repeat = 0; repeat < 9; repeat++)
    {
        switch (player)
        {
        case '1':cout << "Player first turn\n";
            for (int i = 0; i < 1; i++)
            {
                for (int j = 0; j < 1; j++)
                {
                    cout << "Enter the number of row";
                    cin >> i;
                    cout << "Enter the number of cols";
                    cin >> j;
                    arr[i][j] = '*';
                    if (((arr[0][0] == '*') && (arr[0][1] == '*')&& (arr[0][2] == '*'))||
                        ((arr[1][0] == '*') && (arr[1][1] == '*') && (arr[1][2] == '*'))||
                        ((arr[2][0] == '*') && (arr[2][1] == '*') && (arr[2][2] == '*'))||
                        ((arr[0][2] == '*') && (arr[1][2] == '*') && (arr[2][2] == '*'))|| 
                        ((arr[0][1] == '*') && (arr[1][1] == '*') && (arr[2][1] == '*'))|| 
                        ((arr[0][0] == '*') && (arr[1][0] == '*') && (arr[2][0] == '*'))|| 
                        ((arr[0][0] == '*') && (arr[1][1] == '*') && (arr[2][2] == '*'))|| 
                        ((arr[0][2] == '*') && (arr[1][1] == '*') && (arr[2][0] == '*')))
                    {
                        cout << "player 1 wins";
                        break;
                    }
                }
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (((arr[0][0] == '*') && (arr[0][1] == '*') && (arr[0][2] == '*')) ||
                            ((arr[1][0] == '*') && (arr[1][1] == '*') && (arr[1][2] == '*')) ||
                            ((arr[2][0] == '*') && (arr[2][1] == '*') && (arr[2][2] == '*')) ||
                            ((arr[0][2] == '*') && (arr[1][2] == '*') && (arr[2][2] == '*')) ||
                            ((arr[0][1] == '*') && (arr[1][1] == '*') && (arr[2][1] == '*')) ||
                            ((arr[0][0] == '*') && (arr[1][0] == '*') && (arr[2][0] == '*')) ||
                            ((arr[0][0] == '*') && (arr[1][1] == '*') && (arr[2][2] == '*')) ||
                            ((arr[0][2] == '*') && (arr[1][1] == '*') && (arr[2][0] == '*')))
                        {
                            break;
                        }
                        cout << "|" << arr[i][j] << "|";

                    }
                    cout << endl;

                }

            }

        case '2':cout << "Player second turn\n";
            for (int i = 0; i < 1; i++)
            {
                for (int j = 0; j < 1; j++)
                {
                    cout << "Enter the number of row";
                    cin >> i;
                    cout << "Enter the number of cols";
                    cin >> j;
                    arr[i][j] = '*';
                    if (((arr[0][0] == '*') && (arr[0][1] == '*') && (arr[0][2] == '*')) ||
                        ((arr[1][0] == '*') && (arr[1][1] == '*') && (arr[1][2] == '*')) ||
                        ((arr[2][0] == '*') && (arr[2][1] == '*') && (arr[2][2] == '*')) ||
                        ((arr[0][2] == '*') && (arr[1][2] == '*') && (arr[2][2] == '*')) ||
                        ((arr[0][1] == '*') && (arr[1][1] == '*') && (arr[2][1] == '*')) ||
                        ((arr[0][0] == '*') && (arr[1][0] == '*') && (arr[2][0] == '*')) ||
                        ((arr[0][0] == '*') && (arr[1][1] == '*') && (arr[2][2] == '*')) ||
                        ((arr[0][2] == '*') && (arr[1][1] == '*') && (arr[2][0] == '*')))
                    {
                        cout << "player 2 wins";
                        break;
                    }
                }
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (((arr[0][0] == '*') && (arr[0][1] == '*') && (arr[0][2] == '*')) ||
                            ((arr[1][0] == '*') && (arr[1][1] == '*') && (arr[1][2] == '*')) ||
                            ((arr[2][0] == '*') && (arr[2][1] == '*') && (arr[2][2] == '*')) ||
                            ((arr[0][2] == '*') && (arr[1][2] == '*') && (arr[2][2] == '*')) ||
                            ((arr[0][1] == '*') && (arr[1][1] == '*') && (arr[2][1] == '*')) ||
                            ((arr[0][0] == '*') && (arr[1][0] == '*') && (arr[2][0] == '*')) ||
                            ((arr[0][0] == '*') && (arr[1][1] == '*') && (arr[2][2] == '*')) ||
                            ((arr[0][2] == '*') && (arr[1][1] == '*') && (arr[2][0] == '*')))
                        {
                            break;
                        }
                        cout << "|" << arr[i][j] << "|";
                    }
                    cout << endl;
                }
            }
        default:cout << "Enter correct number\n";
            cout << "only 1 or 2\n";
        }
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire