简体   繁体   English

使用类的C ++井字游戏

[英]C++ Tic-Tac-Toe using classes

The game displays the matrix, but doesn't update the current move. 游戏显示矩阵,但不更新当前动作。 How can I do that? 我怎样才能做到这一点?

Here I will show the code I have until now. 在这里,我将显示到目前为止的代码。 I define a matrix for the board. 我为董事会定义一个矩阵。 The functions will draw the board, will get the move from the player, and will toggle the turns. 这些功能将绘制棋盘,从玩家那里获得走法,并切换回合。

#ifndef TICTACTOE_H
#define TICTACTOE_H

class TicTacToe
{
private:
    char board[3][3];

public:
    void DrawBoard();
    void PrintBoard();
    void GetMove(int move);
    void TogglePlayer(char player);
    bool DetermineDraw();
};

#endif

Here is the implementation file: My function to draw the board displays the matrix, but doesn't update the moves. 这是实现文件:我的画板功能显示矩阵,但不更新移动。

#include <iostream>
#include "TicTacToe.h"

using namespace std;


void TicTacToe::DrawBoard()
{
    system("cls");
    cout <<"\tWelcome to the Classes Tic Tac Toe! \n";
    char board[3][3] =
    { 
      {'1','2','3'},
      {'4','5','6'},
      {'7','8','9'},
    };

    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
}


void TicTacToe::GetMove(int move)
{
    char player = 'X';
    cout <<"\nEnter the number of the field you would like to move:\n";
    cin >> move;

    if( move == 1)
    {
        board[0][0] = player;
    }
    else if(move == 2)
    {
        board[0][1] = player;
    }
    else if(move == 3)
    {
        board[0][2] = player;
    }
    else if(move == 4)
    {
        board[1][0] = player;
    }
    else if(move == 5)
    {
        board[1][1] = player;
    }
    else if(move == 6)
    {
        board[1][2] = player;
    }
    else if(move == 7)
    {
        board[2][0] = player;
    }
    else if(move == 8)
    {
        board[2][1] = player;
    }
    else if(move == 9)
    {
        board[2][2] = player;
    }

}

void TicTacToe::TogglePlayer(char player)
{
    if (player == 'X')
        player = 'O';
    else if(player == 'O')
        player = 'X';
}

bool TicTacToe::DetermineDraw()
{
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            if(board[i][j] == 'X' && board[i][j] == 'O')
                return true;
            else 
                return false;
        }
    }
}

Here is the main file, I will keep looping through the game while it isn't a draw. 这是主要文件,我将不停地在游戏中循环播放。 I don't know why the move isn't shown on the board. 我不知道为什么此举没有在董事会上显示。

 #include <iostream>
#include "TicTacToe.h"

using namespace std;

int main()
{
    TicTacToe game;
    char player = 'X';
    while(game.DetermineDraw() == false)
    {
        game.DrawBoard();
        game.GetMove(player);
        game.TogglePlayer(player);
    }

    system("pause");
}

TicTacToe::DrawBoard() always draw the same board, because it uses locally defined variable board . TicTacToe::DrawBoard()始终绘制同一块板,因为它使用本地定义的变量board To correct: remove local definition and initialize class variable board in the constructor: 要更正:删除局部定义并在构造函数中初始化类变量board

TicTacToe::TicTacToe()
{
     for (int i = 0; i < 9; i++)
         board[i / 3][i % 3] = '1' + i;
}

void TicTacToe::DrawBoard()
{
    system("cls");
    cout <<"\tWelcome to the Classes Tic Tac Toe! \n";

    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
}

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

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