C++ Tic-Tac-Toe using classes
C++ Tic-Tac-Toe using classes
游戏显示矩阵,但不更新当前移动。我该怎么做?
在这里,我将显示到目前为止的代码。我为董事会定义了一个矩阵。这些功能将绘制棋盘,从玩家那里获得移动,并切换回合。
#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
以下是实现文件:我绘制棋盘的功能显示矩阵,但不更新移动。
#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;
}
}
}
这是主文件,我会继续循环游戏,而不是平局。我不知道为什么棋盘上没有显示这个动作。
#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()
总是绘制同一块板,因为它使用了本地定义的变量board
。要更正:删除本地定义并初始化构造函数中的类变量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;
}
}
相关文章:
- "using namespace std;"在C++的作用是什么?
- vector<vector<double>> to mxArray using memcpy
- Using C++ CryptAPI:CNG Create CSR
- STLPort using C++11
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- Using getchar_unlocked()
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 嵌套命名空间的"using"指令,但需要命名内部命名空间
- 为什么"using System;"不被视为不良做法?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- 'using namespace'实现细节的便捷方法(仅标头库)?
- C++中"typedef"、"using"、"namespace"和"using namespace"有什么区别?
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 如何区分C++中的"using declaration"和"using directive"?
- Threads with Classes and std::packaged_task
- C++ "using tcp=x"到"namespace tcp=x"有什么区别
- 如何为流输出运算符提供重载<<模板'using'类型别名?
- Using PrimitiveBatch::DrawIndexed with ID3D11DeviceContext::
- C++ Tic-Tac-Toe using classes
- DLL using JNI Java classes