井字游戏与随机硬币翻转

时间:2018-11-05 04:06:36

标签: c++ tic-tac-toe

课堂上的作业问题:我必须制作一个井字游戏,该游戏使用掷硬币来确定谁先走。我已经生成了大部分代码,但是仍然有一些我无法弄清的东西/错误。

  1. 我知道如何掷硬币。但是,如何获得该程序来选择谁先行。
  2. 获胜方或游戏结束方不会获得X或O。
  3. 没有关于打领带游戏的消息。
  4. 当您只按Enter键而不输入任何值时,没有任何消息。

    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <time.h>
    
    
    using namespace std;
    
    void Flip_Coin();
    void Do_Exercise();
    void Display_Board();
    void Ask_Turn();
    char Check_Winner();
    void Computer_Player_Turn();
    
    char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
    int n = 0;
    
    int main()
    {
        srand(time(0));
    
        Do_Exercise();
    
        cin.get();
    
        return 0;
    }
    
    void Do_Exercise()
    {
        Flip_Coin();
        while (true)
        {
            n++;
            Display_Board();
            Ask_Turn();
            if (Check_Winner() == 'X')
            {
                cout << "Human Wins!" << endl;
                break;
            }
            else if (Check_Winner() == 'O')
            {
                cout << "Computer Wins!" << endl;
                break;
            }
            else if (Check_Winner() == 'T' && n == 9)
            {
    
                cout << "It's a draw." << endl;
                break;
            }
            Computer_Player_Turn();
            if (Check_Winner() == 'X')
            {
                cout << "Human Wins!" << endl;
                break;
            }
            else if (Check_Winner() == 'O')
            {
                cout << "Computer Wins!" << endl;
                break;
            }
            else if (Check_Winner() == 'T' && n == 9)
            {
    
                cout << "It's a draw." << endl;
                break;
            }
        }
    }
    
    void Flip_Coin()
    {
        int Flip;
    
        cout << "Welcome to Tic - Tac - toe.\n\n"
            << "Wait I am flipping a coin to see who goes first . . .\n\n";
    
        Flip = rand() % 2;
    
        if (Flip == 0)
        {
            cout << "Computer wins coin toss.\n\n";
        }
        else
        {
            cout << "Human wins coin toss.\n\n";
        }
        cout << "The board is laid out like this:\n\n";
    }
    
    void Display_Board()
    {
        for (int Row = 0; Row < 3; Row++)
        {
            for (int Column = 0; Column < 3; Column++)
            {
                cout << Board[Row][Column] << "\t";
            }
            cout << "\n\n";
        }
    }
    
    void Ask_Turn()
    {
        string input;
    
        while (true)
        {
            cout << "Enter the position to place your X: ";
            cin >> input;
            cout << endl;
    
            if (input != "")
            {
                char entered = input.c_str()[0]; // sets char character to act in place of string
    
                if (entered >= '1' && entered <= '9')
                {
                    cout << "The human places a X-token at position: " << entered << "\n\n";
                    cout << "Current board:\n\n";
                    int entered_number = entered - '0'; // changes char to int form
                    int index = entered_number - 1;
    
                    int row = index / 3;
                    int col = index % 3;
    
                    char grid_position = Board[row][col];
    
                    if (grid_position == 'X' || grid_position == 'O')
                    {
                        cout << "That position is already taken. ";
                    }
                    else {
                        Board[row][col] = 'X';
                        break;
                    }
                }
                else {
                    cout << "You must entered in the the range of 1-9.\n";
                }
            }
            else {
                cout << "You must enter something!"; // doesnt work
            }
        }
    }
    
    char Check_Winner()
    {
        //first player
        if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
            return 'X';
        if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
            return 'X';
        if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
            return 'X';
    
        if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
            return 'X';
        if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
            return 'X';
        if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
            return 'X';
    
        if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
            return 'X';
        if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
            return 'X';
    
        //second player
        if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
            return 'O';
        if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
            return 'O';
        if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
            return 'O';
    
        if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
            return 'O';
        if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
            return 'O';
        if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
            return 'O';
    
        if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
            return 'O';
        if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
            return 'O';
    
        return 'T';
    }
    
    void Computer_Player_Turn()
    {
        while (true)
        {
            int computer_choice = (rand() % 9) + 1;
    
            int row = (computer_choice - 1) / 3;
            int col = (computer_choice - 1) % 3;
    
            char grid_position = Board[row][col];
    
            if (grid_position == 'X' || grid_position == 'O')
            {
                continue;
            }
            else {
                cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
                Board[row][col] = 'O';
                break;
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

这是解决方案。

1)现在,您已经硬编码了先调用Ask_Turn,然后再调用Computer_Player_Turn。不必以这种方式调用它们,而是先从硬币翻转中调用它们,然后在完成后让两个函数相互调用。

2)致电Display_Board(),然后检查获胜者。

3)固定在这里。您每2圈仅将n加1。

4)在while循环中请求输入,只要输入为空,就进入循环

因此,进行所有这些更改,代码应类似于以下内容。

我已经在这里测试了代码https://onlinegdb.com/S1Qdcr63X

#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>


using namespace std;

void Flip_Coin();
void Do_Exercise();
void Display_Board();
void Ask_Turn();
char Check_Winner();
void Computer_Player_Turn();

char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

int n = 0;

void Computer_Player_Turn();
void Ask_Turn();

int main()
{
    srand(time(0));

    Do_Exercise();

    cin.get();

    return 0;
}

void Do_Exercise()
{
    Flip_Coin();
}

void Flip_Coin()
{
    int Flip;

    cout << "Welcome to Tic - Tac - toe.\n\n"
        << "Wait I am flipping a coin to see who goes first . . .\n\n";

    Flip = rand() % 2;

    if (Flip == 0)
    {
        cout << "Computer wins coin toss.\n\n";
        Computer_Player_Turn();
    }
    else
    {
        cout << "Human wins coin toss.\n\n";
        Ask_Turn();
    }
}

void Display_Board()
{
    for (int Row = 0; Row < 3; Row++)
    {
        for (int Column = 0; Column < 3; Column++)
        {
            cout << Board[Row][Column] << "\t";
        }
        cout << "\n\n";
    }
}


char Check_Winner()
{
    //first player
    if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
        return 'X';
    if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
        return 'X';
    if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
        return 'X';

    if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
        return 'X';
    if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
        return 'X';
    if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
        return 'X';

    if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
        return 'X';
    if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
        return 'X';

    //second player
    if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
        return 'O';
    if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
        return 'O';
    if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
        return 'O';

    if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
        return 'O';
    if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
        return 'O';
    if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
        return 'O';

    if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
        return 'O';
    if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
        return 'O';

    return 'T';
}

bool winner()
{
    if (Check_Winner() == 'X')
    {
        cout << "Human Wins!" << endl;
        return true;
    }
    else if (Check_Winner() == 'O')
    {
        cout << "Computer Wins!" << endl;
        return true;
    }
    else if (Check_Winner() == 'T' && n == 9)
    {

        cout << "It's a draw." << endl;
        return true;
    }
    return false;
}

void Ask_Turn()
{
    n++;

    string input = "";

    while (true)
    {
        do
        {
            cout << "Enter the position to place your X: ";
            cin >> input;
            cout << endl;
        }
        while(input == ""  || (input.c_str()[0] < '1' || input.c_str()[0] > '9'));


        char entered = input.c_str()[0]; // sets char character to act in place of string

        cout << "The human places a X-token at position: " << entered << "\n\n";
        cout << "Current board:\n\n";
        int entered_number = entered - '0'; // changes char to int form
        int index = entered_number - 1;

        int row = index / 3;
        int col = index % 3;

        char grid_position = Board[row][col];

        if (grid_position == 'X' || grid_position == 'O')
        {
            cout << "That position is already taken. ";
            n--;
            Ask_Turn();
        }
        else {
            Board[row][col] = 'X';
            break;
        }
    }


    Display_Board();

    if(winner())
        return;
    else
     Computer_Player_Turn();
}

void Computer_Player_Turn()
{
    n++;
    while (true)
    {
        int computer_choice = (rand() % 9) + 1;

        int row = (computer_choice - 1) / 3;
        int col = (computer_choice - 1) % 3;

        char grid_position = Board[row][col];

        if (grid_position == 'X' || grid_position == 'O')
        {
            continue;
        }
        else {
            cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
            Board[row][col] = 'O';
            break;
        }
    }

    Display_Board();

    if(winner())
        return;
    else
        Ask_Turn();
}