C++ C++;对象还是二维数组作为参数?
在我为学校创建的一个Tic-Tac-Toe游戏中,我有五个类:游戏(充当游戏的控制器)、玩家(基本类)、HumanPlayer和AIPlayer(均源自玩家)以及棋盘 电路板包含一个2D阵列和一些用于显示和处理阵列的功能。所有三个玩家类都有一个函数C++ C++;对象还是二维数组作为参数?,c++,arrays,C++,Arrays,在我为学校创建的一个Tic-Tac-Toe游戏中,我有五个类:游戏(充当游戏的控制器)、玩家(基本类)、HumanPlayer和AIPlayer(均源自玩家)以及棋盘 电路板包含一个2D阵列和一些用于显示和处理阵列的功能。所有三个玩家类都有一个函数makeAMove(Board&Board),它将在棋盘上选择一个位置并放置一个X或O 如您所见,makeAMove接受Board对象作为参数。然后,函数将决定是否移动并更改Board对象的Board数组。如果我理解正确,传入的实际对象将被更改,不需要
makeAMove(Board&Board)
,它将在棋盘上选择一个位置并放置一个X或O
如您所见,makeAMove
接受Board
对象作为参数。然后,函数将决定是否移动并更改Board对象的Board数组。如果我理解正确,传入的实际对象将被更改,不需要返回任何内容
我理解正确吗?使用2d数组作为参数是否更好
这是我的代码的浓缩版本
#include <iostream>
#include <memory> // auto_ptr
class Board
{
public:
Board()
{
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
board[row][column] = ' ';
}
}
void displayBoard()
{
std::cout << "\n-------------" << std::endl;
for (int row = 0; row < 3; row++)
{
std::cout << "| " ;
for (int column = 0; column < 3; column++)
std::cout << board[row][column] << " | ";
std::cout << "\n-------------" << std::endl;
}
}
/* My thought was to use getBoard to return the array rather than using the
object as a parameter
char getBoard()
{
return board[][3];
}
*/
char getCell(int row, int column)
{
return board[row][column];
}
void setCell(int row, int column, char player)
{
board[row][column] = player;
}
private:
char board[3][3];
};
class Player
{
public:
virtual void makeAMove(Board &board) = 0;
};
class AIPlayer : public Player
{
virtual void makeAMove(Board &board)
{
// do some work ont the board array
}
};
class HumanPlayer : public Player
{
virtual void makeAMove(Board &board)
{
// do some work on the board array
}
};
class Game
{
public:
Game(unsigned int players)
{
if (players == 1)
{
player1.reset (new HumanPlayer());
player2.reset (new AIPlayer());
}
else
{
player1.reset (new HumanPlayer());
player2.reset (new HumanPlayer());
}
}
void playGame()
{
player1->makeAMove(myBoard);
}
private:
std::auto_ptr<Player> player1; // pointer to a Player object (C++11 has more secure unique_ptr)
std::auto_ptr<Player> player2; // pointer to a Player object
Board myBoard;
};
int main()
{
Game myGame(1);
myGame.playGame();
return 0;
}
#包括
#包括//auto\u ptr
班级委员会
{
公众:
董事会()
{
对于(int行=0;行<3;行++)
{
for(int列=0;列<3;列++)
板[行][列]='';
}
}
无效显示板()
{
std::coutmakeAMove
属于board
。玩家对象应该决定移动什么,然后调用board::makeAMove
进行移动。这样,玩家对象就不关心board的内部表示形式。makeAMove
属于board
。玩家对象应该ld决定要做什么动作,然后调用board::makeAMove来做动作。这样,玩家对象就不关心棋盘的内部表示形式。makeAMove
属于board
。玩家对象应该决定要做什么动作,然后调用board::makeAMove
来做动作玩家对象不关心棋盘的内部表示形式。makeAMove
属于board
。玩家对象应该决定移动什么,然后调用board::makeAMove
进行移动。这样,玩家对象不关心棋盘的内部表示形式。 是的,因为您通过引用传递Board
对象,所以函数中对它的任何更改都将改变实际的Board
对象。我认为传递Board
对象是一个更好的主意,因为最好不要公开实际实现(在本例中是2D数组)因此,作为棋盘对象传递可以提供更多的抽象,这是很好的。是的,因为您通过引用传递棋盘
对象,在函数中对其进行任何更改都会改变实际的棋盘
对象。我认为传递棋盘
对象是一个更好的主意,因为最好不要暴露ac实际实现(本例中为2D数组)因此,作为棋盘对象传递可以提供更多的抽象,这是很好的。是的,因为您通过引用传递棋盘
对象,在函数中对其进行任何更改都会改变实际的棋盘
对象。我认为传递棋盘
对象是一个更好的主意,因为最好不要暴露ac实际实现(本例中为2D数组)因此,作为棋盘对象传递可以提供更多的抽象,这是很好的。是的,因为您通过引用传递棋盘
对象,在函数中对其进行任何更改都会改变实际的棋盘
对象。我认为传递棋盘
对象是一个更好的主意,因为最好不要暴露ac游戏的实际实现(在本例中是2D数组)给用户。因此,作为棋盘对象传递可以提供更多的抽象,这是很好的。通过引用传递对象,而不是数组。棋盘上的玩家所做的任何更改都将持续
棋盘
是棋盘
的私人成员。它只能通过值返回,不能通过引用返回。如果您通过值将2d数组传递给玩家,他们对棋盘所做的任何更改都不会持续
您的Player
类需要在makeAMove(Board&Board)
函数中访问Board.setCell(int,int)
,要做到这一点,它们需要Board对象
然而,你的玩家
职业需要阅读棋盘。为了进行移动,getCell(int,int)
函数可能就足够了,但嵌套循环所有单元格也可能会很乏味。getBoard
可能对可能的decideMove
函数有用。按原样通过引用传递对象,而不是数组。棋盘上的播放器所做的任何更改都将持续
棋盘
是棋盘
的私人成员。它只能通过值返回,不能通过引用返回。如果您通过值将2d数组传递给玩家,他们对棋盘所做的任何更改都不会持续
您的Player
类需要在makeAMove(Board&Board)
函数中访问Board.setCell(int,int)
,要做到这一点,它们需要Board对象
然而,你的玩家
职业需要阅读棋盘。为了进行移动,getCell(int,int)
函数可能就足够了,但嵌套循环所有单元格也可能会很乏味。getBoard
可能对可能的decideMove
函数有用。按原样通过引用传递对象,而不是数组。棋盘上的播放器所做的任何更改都将持续
board
是board
的私有成员。它只能通过值而不是引用返回。如果将2d数组传递给pla