使用类创建tic-tac-toe代码,但仅用于板显示和定义播放器 我刚刚开始学习C++,在我的问题上会有任何帮助。 我被要求为一个游戏创建一段代码 但它在两个人之间存在分歧。 我的角色是创建一个类,仅用于显示棋盘和选择玩家x或玩家o。 到目前为止,我试着创建这个C++代码,但是错误不断显示出来,有人知道如何纠正这个错误吗? p、 在游戏中,我把它归类为XsandOs #include <string> #include <iostream> using namespace std; class XsandOs // Name of the class { public: XsandOs(); void drawboard(); void printBoard(); void getMove(int move); void choosePlayer(char player); bool checkwinner(const char board[3][3], char symbol, int plays); private: const char board[3][3]; }; void XsandOs::drawboard // Develops the board { cout << "Let's play X's and O's\n" << "_________________________________\n\n"; //This prints a title declaring the name of the game //This action uses an array to create the board, the user will choose the number to select where they want the character to go. char board[3][3] = { { '1', '2', '3', }; //This creates the top row {'4', '5', '6', }; // This creates the middle row {'7', '8', '9', }; // This creates the bottom row }; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << board[i][j] << ""; } cout << endl; } } void XsandOs::choosePlayer(char player) { if (player == 'X') Player = 'O'; elseif(player == 'O') Player = 'X'; return(void); } int main() { draw(); while (1) { input(); draw(); choosePlayer(); } system("pause"); return 0; }
我已经解决了一些设计问题,并为您创建了一些方法。还有工作要做,你不能指望我们为你做。祝你好运使用类创建tic-tac-toe代码,但仅用于板显示和定义播放器 我刚刚开始学习C++,在我的问题上会有任何帮助。 我被要求为一个游戏创建一段代码 但它在两个人之间存在分歧。 我的角色是创建一个类,仅用于显示棋盘和选择玩家x或玩家o。 到目前为止,我试着创建这个C++代码,但是错误不断显示出来,有人知道如何纠正这个错误吗? p、 在游戏中,我把它归类为XsandOs #include <string> #include <iostream> using namespace std; class XsandOs // Name of the class { public: XsandOs(); void drawboard(); void printBoard(); void getMove(int move); void choosePlayer(char player); bool checkwinner(const char board[3][3], char symbol, int plays); private: const char board[3][3]; }; void XsandOs::drawboard // Develops the board { cout << "Let's play X's and O's\n" << "_________________________________\n\n"; //This prints a title declaring the name of the game //This action uses an array to create the board, the user will choose the number to select where they want the character to go. char board[3][3] = { { '1', '2', '3', }; //This creates the top row {'4', '5', '6', }; // This creates the middle row {'7', '8', '9', }; // This creates the bottom row }; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << board[i][j] << ""; } cout << endl; } } void XsandOs::choosePlayer(char player) { if (player == 'X') Player = 'O'; elseif(player == 'O') Player = 'X'; return(void); } int main() { draw(); while (1) { input(); draw(); choosePlayer(); } system("pause"); return 0; },c++,visual-c++,C++,Visual C++,我已经解决了一些设计问题,并为您创建了一些方法。还有工作要做,你不能指望我们为你做。祝你好运 #include <string> #include <iostream> using namespace std; class XsandOs // Name of the class { public: XsandOs(); void draw(); void input(); int getMove(); // 1-9 or zero
#include <string>
#include <iostream>
using namespace std;
class XsandOs // Name of the class
{
public:
XsandOs();
void draw();
void input();
int getMove(); // 1-9 or zero on error
void choosePlayer();
bool checkWinner(); // true means game over
private:
char board[3][3];
char player;
};
XsandOs::XsandOs()
{
// initialize the board
player = 'X'; // x goes first
for (int i = 0;i < 3;++i)
for (int j = 0;j < 3;++j)
board[i][j] = ' '; // blank means the spot is empty
}
void XsandOs::draw() // Develops the board
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
int XsandOs::getMove()
{
// ask for a move
// validate the move or get a new move
// TODO
}
void XsandOs::input()
{
// ask for a move
int move = 0;
while (!move) {
move = getMove();
}
// do the move and update the board
board[move-1] = player;
}
void XsandOs::choosePlayer()
{
if (player == 'X')
Player = 'O';
elseif(player == 'O')
Player = 'X';
}
bool XsandOs::checkWinner()
{
// TODO
// Check the board state and look for a winner.
return false;
}
int main()
{
XsandOs game;
bool gameOver = false;
game.draw();
while (!gameOver)
{
game.input();
game.draw();
game.checkWinner()
gameOver = game.choosePlayer();
}
system("pause");
return 0;
}
我以前见过这个,可以帮你节省很多时间。唯一的赢家是不玩。我看不出main和班级之间的联系。您是否在iostream的include之前添加了?void XsandOs::drawboard应该是void XsandOs::drawboard您在哪里看到这个:returnvoid?它的返回无效;或者干脆回去;需要从数组定义的中间删除分号。这里有一大堆完全错误的东西。建议再次点击文本,并对语法进行修改。