简体   繁体   English

如何在C ++中的井字游戏中显示赢家?

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

I wrote this code for tic-tac-toe game in C++. 我用C ++为井字游戏编写了这段代码。 I want to display if player 1 won or player 2 won or if the game is a draw. 我想显示玩家1赢了还是玩家2赢了,还是游戏是平局。 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 the tic-tac-toe board itself is a class. 我已经实现了井字游戏板本身是一类。

The following is the main.cpp 以下是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 以下是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 以下是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;
}

I've noticed you have a print_winner(int) function in your gameboard class 我注意到您的游戏板类中有一个print_winner(int)函数

 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 *) ;
 };

You could implement the method: 您可以实现该方法:

 void Game_board::print_winner( int = the player that's playing )
 { use the check_win() function
{ if( player.check_win() == true)
 cout << (number of player that's being passed in from above) << "Is the 
 winner!" 
 } 
else 
{
cout << (number of player that's being passed in from above) << "Lost" 
 }
}

Why not just add this? 为什么不只是添加它呢?

   if(check_win())
        std::cout << *player << " won!" << std::endl;
        break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM