如何在C ++中展示tic-tac-toe的胜利者?

问题描述 投票:0回答:2

我在C ++中为tic-tac-toe游戏编写了这段代码。如果玩家1获胜或玩家2获胜或者游戏是平局,我想显示。但我不确定如何使用以下代码完成。我的代码的任何建议或更改都非常感谢!我已经实现了tic-tac-toe板本身就是一个类。

以下是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;
}

以下是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

以下是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;
}
c++ c++11 tic-tac-toe
2个回答
1
投票

我注意到你的游戏板类中有一个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 *) ;
 };

您可以实现该方法:

 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" 
 }
}

0
投票

为什么不加上这个呢?

   if(check_win())
        std::cout << *player << " won!" << std::endl;
        break;
© www.soinside.com 2019 - 2024. All rights reserved.