lundi 18 mars 2019

How to display a winner in tic-tac-toe in C++?

I wrote this code for tic-tac-toe game in C++. I want to display if player 1 won or player 2 won or if the game is a draw. But I am not sure how it can be done using the following code. Any suggestions or changes to my code are highly appreciated! I have implemented it as tic-tac-toe game itself is a class.

The following is the main.cpp

#include "main.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    string player[2] ;
    char sym[2];
    Game_board  game1;

   // Player 1 details
    cout << "Player1, enter your name: " ;  getline(cin, player[0]);
    cout << player[0] <<  ", choose your symbol: " ;  cin >> sym[0];


    // Player 2 details
    cout << "\nPlayer2, enter your name: ";  cin.ignore(1000, '\n');
    getline(cin, player[1]);
    cout << player[1] <<  ", choose your symbol: " ; cin >> sym[1];

    while(sym[0] == sym[1]){
        cerr<< "\n****Error!!! Choose a different symbol than "<< player[0] << "\n" << endl;
        cout << player[1] <<  ", enter your symbol: " ; cin >> sym[1];
    }

    game1.play(player, sym);

    return 0;
}

The following is the class Game_board.h

#ifndef GAME_BOARD_H
#define GAME_BOARD_H
#include <string>
#include <utility>

class Game_board
{
    char places[10] = {'0', '1', '2', '3','4', '5', '6', '7', '8','9'};
    bool isValid(char, char*);

public:
    Game_board();
    void show();
    void update(char, char);
    //std::pair<bool, std::string> check_win(char *);
    bool check_win();
    void print_winner(int);
    void play(std::string *, char *) ;
};

#endif // GAME_BOARD_H

The following is the class Game_board.cpp

#include "main.h"

Game_board::Game_board()
{

}

void Game_board::play(string *player, char *sym){
    do
    {
        show(); char choice; int i = 0; 
        while(i <= 1)
        {
            cout << player[i] << ", choose your location: ";
            cin >> choice ;

            // Check if the choice entered is between 1 and 9
            while(choice<='0' || choice>'9' )
            {
                cerr << "\n**** Error!!! Enter a valid number between 1 and 9 ****\n\n";
                cout << player[i] << ", choose your location: ";
                cin >> choice ;
                if(choice=='1'||choice=='2'||choice=='3'||choice=='4'||choice=='5'||choice=='6'||choice=='7'||choice=='8'||choice=='9')
                    break;
            }

            // check if the choice entered is not already taken
            while(!isValid(choice, sym))
            {
                cerr << "\n**** Error!!! Choose a location that has not been chosen ****\n\n";
                cout << player[i] << ", choose your location: ";
                cin >> choice ;
                if(isValid(choice, sym))
                        break;
            }

            update(choice,sym[i]);
            if(check_win())
                break;
            else { ++i; }
        }
    }while(!check_win());

}


bool Game_board::isValid(char choice, char *sym)
{
    bool a ;
    if((places[int(choice)-48]== sym[0]) || (places[int(choice)-48]== sym[1]))
        a =false;
    else
        a = true;
    return a;
}


void Game_board::show(){
    system("cls");
    cout<<"\n\n\t*****************************\n";
    cout<<"\t*   Welcome to Tic-Tac-Toe  *\n";
    cout<<"\t*****************************\n\n";
    cout<<"\t---------|---------|---------\n";
    cout<<"\t    " <<places[1]<< "    |    " << places[2] << "    |    "  << places[3] << "    \n";
    cout<<"\t---------|---------|---------\n";
    cout<<"\t    " <<places[4]<< "    |    " << places[5] << "    |    "  << places[6] << "    \n";
    cout<<"\t---------|---------|---------\n";
    cout<<"\t    " <<places[7]<< "    |    " << places[8] << "    |    "  << places[9] << "    \n\n\n";
}

void Game_board::update(char i, char x)
{
    if (places[1] == '1' && i== '1')
    {
            places[int(i)-48]=x ;
            show();

    }
    else if(places[2] == '2' && i == '2')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[3] == '3' && i == '3')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[4] == '4' && i == '4')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[5] == '5' && i == '5')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[6] == '6' && i == '6')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[7] == '7' && i == '7')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[8] == '8' && i == '8')
    {
        places[int(i)-48]= x ;
        show();
    }
    else if(places[9] == '9' && i == '9')
    {
        places[int(i)-48]= x ;
        show();
    }

}

bool Game_board::check_win()
{   
    if(((places[1] == places[2]) && (places[2]== places[3])) ||
       ((places[1] == places[4]) && (places[4]== places[7])) ||
       ((places[2] == places[5]) && (places[5]== places[8])) ||
       ((places[4] == places[5]) && (places[5]== places[6])) ||
       ((places[3] == places[6]) && (places[6]== places[9])) ||
       ((places[7] == places[8]) && (places[8]== places[9])) ||
       ((places[1] == places[5]) && (places[5]== places[9])) ||
       ((places[3] == places[5]) && (places[5]== places[7]))){
        return true;
    }
    else
        return false;
}

Aucun commentaire:

Enregistrer un commentaire