0

上周我做了一个作业,我们必须使用 3*3 棋盘编写井字游戏。现在,我正在尝试自己做同样的游戏,但玩家可以自定义棋盘大小。

我询问用户希望的行数和列数,我正确地创建了游戏板并使用我的 << 重载函数打印了这个游戏板。一切似乎都还好。但是当我尝试做一个动作(在棋盘上放置一个标记)时,用户传递的 x 和 y 坐标与我们在屏幕上看到的不匹配。

我不知道错误是来自我的 SetPosition() 还是 AtPosition()。这是我第一次处理这种数组,所以希望有人能帮助我。(对不起,我的英语不好)

那里有我的驱动程序文件和我的 GameBoard.h 和 .cpp 如果您需要更多信息,请询问我。

    #include "GameBoard.h"
    #include <iostream> /*cout, cin*/
    #include <stdlib.h> /* rand, srand */
    #include <time.h>   /* time        */

    using namespace std;

    // ************Prototypes*************
    int RandomInt(int low, int high); // generate a random number
    // ***********************************


    int main(void)
    {
        // *************Variables************
        int x_size;                 // the user decide of the size of the board
        int y_size;                 // the user decide of the size of the board
        int x_player;               // the user x-coordinate
        int y_player;               // the user y-coordinate
        int ID_player = 1;          // player ID
        int x_computer;             // the computer x-coordinate
        int y_computer;             // the computer y-coordinate 
        int ID_computer = 2;        // computer ID  
        int keepplaying = 0;        // use this for game loop until we do not have the check win function
        // **********************************
        srand(time(0));

        // ask to the user which size he wants to use
        cout << "Welcome to my Tic Tae Toe Game!" << endl;
        cout << "What size do you want?" << endl;
        cout << "Insert number of rows" << endl;
        cin >> x_size;
        cout << "Insert number of columns" << endl;
        cin >> y_size;

        cout << "This is the GameBoard we are using" << endl;

        // create the GameBoard    
        Board myBoard(x_size, y_size);
        // set all the array to 0
        myBoard.ResetBoard();
        // prints the 2D array
        cout << myBoard << endl;

        cout << "You are the player 1 and you play against the computer (number 2). Good Luck!" << endl;

        do
        {

            // ask to the user the coordinates in which he wants to place the mark
            do
            {
                cout << "Player 1 turn! " << endl;
                cout << "Insert x coordinate: ";
                cin >> x_player ;
                cout << "Insert y coordinate: ";
                cin >> y_player ;
            }
            while( (x_player < 0 || x_player > x_size) || (y_player < 0 || y_player > y_size) || myBoard.AtPosition(x_player, y_player) != 0 );
            // while the cell is not available or the coordinates are not valide
            myBoard.SetPosition(x_player, y_player , ID_player); // the mark has been placed

            // print the move
            cout << myBoard << endl; 
            cout << "End of turn!" << endl;

            // The computer place a mark: the x and y coordinates are randomly generated
            cout << "Computer Turn:" << endl;
            do
            {
                x_computer = RandomInt(0,x_size);
                y_computer = RandomInt(0,y_size);
            }while(myBoard.AtPosition(x_computer, y_computer) != 0);
            myBoard.SetPosition(x_computer, y_computer, ID_computer);
            cout << myBoard << endl; 

        }while(keepplaying != 1) ;         

        //return 0;
    }

    // This function create a random number
    int RandomInt(int low, int high)
    {
      int number = rand() % (high - low + 1) + low;
      return number;
    }

这是 main.cpp,它会询问用户的数据,然后调用在我的类 Board 中声明和定义的函数

    #include <iostream> /*cout, cin, ostream*/

    class Board
    {
        public:
       /* Board()
        { 
            x = 3;
            y = 3;
            //player_ID = 1;
            //computer_ID = 2;

        }*/
        Board(int x_, int y_); // non-default constructor



        void SetPosition(int x, int y, int player);     // Place a mark
        int AtPosition(int x, int y);                   // return the ID of the cell
        void ResetBoard(void);                        // set all the array to be 0

        friend std::ostream& operator<<(std::ostream &os, const Board &rhs); // overload the << operator

        private:    
        int x;                              // x-coordinates
        int y;                              // y-coordinates
        int *board;                    // contains the board in a 2D array (3*3 gameboard)


        int player_ID;                         // player ID 
        int computer_ID;                        // computer ID

    };

你有我班级的头文件

    #include "GameBoard.h"

    // non-default constructor
    Board::Board(int x_, int y_)
    {
        x = x_;
        y = y_;    
        board = new int[x*y];
    }
    // Goes through the board array and set all the ID to 0
    void Board::ResetBoard()
    {
        for(int i = 0; i < x*y; i++)
        {
            board[i] = 0;      
        }
    }

    // returns the ID of the passed cell
    int Board::AtPosition(int x, int y)
    {
       return board[x*y];
    }

    // put a mark in the cell and return true
    void Board::SetPosition(int x, int y, int player_ID)
    {
        board[x*y +x] = player_ID;
        //value = pd[row * 4 + column];
    }

    std::ostream& operator<<(std::ostream &os, const Board &rhs)
    {
      for (int i = 0; i < rhs.x*rhs.y; i++)
      {


        if(i % rhs.y == 0)
        {
          os << std::endl;
        }
        os << rhs.board[i]; 

      }   
        return os;
    }

我希望一切都清楚并得到很好的评论

4

2 回答 2

0

所以在你的Board课上你有:

// returns the ID of the passed cell
int Board::AtPosition(int x, int y)
{
   return board[x*y];
}

// put a mark in the cell and return true
void Board::SetPosition(int x, int y, int player_ID)
{
    board[x*y +x] = player_ID;
    //value = pd[row * 4 + column];
}

因此,您正在使用xy作为参数名称,这些名称隐藏了 x 和 y 类成员。你需要考虑你想要做什么。对于 3 x 3 阵列,您有:

0 1 2
3 4 5
6 7 8

在您的代码中,您似乎打算使用 x 作为列号和 y 作为行号(但不一致,所以我不能确定)。因此,对于 x=1 和 y=2,您的指数为 5。那么您如何计算呢?你有:

board[x*y +x] = player_ID;

你需要(类似):

board[this->x*y +x] = player_ID;

而且AtPosition你还需要:

return board[this->x*y +x];

但我建议重新命名你的变量以避免阴影。也许成员 vars 最好称为xsize/ysizexlen/ ylen

您还operator<<拥有:

if(i % rhs.y == 0)
{
    os << std::endl;
}
os << rhs.board[i]; 

我认为需要:

if(i % rhs.x == 0)
{
    os << std::endl;
}
os << rhs.board[i]; 

(因为我猜rhs.x是宽度,但如果不是,你有其他代码是错误的。)

于 2013-03-03T11:25:28.003 回答
0

首先,这个类的功能都搞砸了。如果要在内存中动态创建板,则可以直接访问它。当您调用 std::ostream 函数时,您只需创建一个名为 show 的函数。这里有一些代码可以帮助解释得更清楚一些。

包括“GameBoard.h”#include“Player.h”

   int main() {    GameBoard* board = new GameBoard;    board->show();    Player* player = new Player(board);//passing a pointer to a pointer this is called    //dynamic passing which allows a class to manipulate another with a class    int* row = new int;    int* col = new int;    row = 1 , col = 1; //the middle spot on 3x3 board    player->setPiece(row,col);//now the player class would control the rest    delete col; delete row; delete player;//delete all but the board    board->show();//the players piece would still be there this is what dynamic pass-    //ing can do    delete board;    return 0; }

如果操作正确,玩家将使用指向棋盘的指针在其自己的类中控制和操作棋盘。这称为数据抽象,并允许在类和 show 函数中使用 cout,不要引用 ostream。这是一种糟糕的编程习惯,可能会导致整个程序出现严重错误。相信我,我也学得很辛苦,哈哈。无论如何练习这些材料,你将成为编写几乎任何程序的天才。永远记住,这是 OOP(面向对象编程)而不是 PP(过程编程)。

于 2013-10-17T05:20:55.407 回答