【发布时间】:2020-08-20 08:17:53
【问题描述】:
我只是想知道是否可以帮助我将数组指针从一个头文件传递到另一个头文件。
我有 TicTacToe 的头文件,其中包含游戏 TicTacToe 和另一个包含 My AI 及其方法的头文件。
我只是想知道我是否可以将 TicTacToe 的动作来回传递给 AI,以便 AI 可以做出明智的动作并将其返回到 TicTacToe 标头以进行游戏板的验证/更新(或者我将进行另一个验证在人工智能中)一旦我有了这个想法,我就会开始将方法从井字游戏中分离到自己的类中。
我已经包含了我的 Main、TicTacToe 和 AI 的代码 如果有任何批评,请让我有它????
主要
#include <iostream>
using namespace std;
#include "TicTacToe.h"
#include "AI.h"
int main()
{
TicTacToe run;
run.Play();
TicTacToeAI Test;
}
井字游戏.h
//Functional implementation for Tic Tac Toe game (incomplete)
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Game
{
};
class TicTacToe
{
private:
bool WIN =false;
bool DRAW = false;
char board[3][3];
int noOfMoves = 0;
char player = 'X';
char player2 =' ';
int row = 0;
int col =0;
public:
void PlayerFlick();
void getXOMove();
void Play();
bool addMove();
bool gameStatus();
bool isValidMove();
void displayBored()
{
for (int row = 0; row < 3; row++) {
cout << row + 1;
for (int col = 0; col < 3; col++) {
cout << setw(3) << board[row][col];
if (col != 2)
cout << " |";
}
cout << endl;
if (row != 2)
cout << " ____|____|____" << endl << " | | " << endl;
}
cout << endl;
}
void ResetBoard()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
board[i][j] = ' ';
}
};
// Methods
bool TicTacToe::gameStatus() // Works with cout << board[row][col]<< endl
{
bool CONTINUE = false;
for(int i = 0; i <= 2; i++)
{
if ((board[i][0] =='X' && board[i][1] == 'X' && board[i][2] == 'X') || (board[i][0] == 'O' && board[i][1]== 'O' && board[i][2] == 'O'))
{
cout << " CROSS" << endl; // 3,3 // 1,3
WIN = true;
return WIN;
}
}
for(int i= 0; i <=2; i++)
{
if((board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X') || (board[0][i] == 'O' && board[1][i]== 'O' && board[2][i] == 'O'))
{
cout << " DOWN" << endl; // 3,1
WIN = true;
return WIN;
}
}
if((board[0][0]== 'X' && board[1][1] == 'X' && board[2][2] == 'X') ||(board[0][0]== 'O' && board[1][1] == 'O'&& board[2][2] == 'O') )
{
cout << " RIGHT SIDE" << endl;
WIN = true;
return WIN;
}
if((board[0][2] == 'X'&& board[1][1] == 'X' && board[2][0] == 'X') || (board[0][2] == 'O' && board[1][1] == 'O'&& board[2][0] == 'O' ))
{
cout << " LEFT SIDE" << endl;
WIN = true;
return WIN;
}
if(noOfMoves == 9)
{
cout<< " DRAW"<< endl;
DRAW= true;
return WIN;
}
TicTacToe::PlayerFlick();
CONTINUE = false;
return CONTINUE;
}
void TicTacToe::getXOMove()// work with cout << board[row][col]<< endl
{
do {
cout << "Player " << player << " enter move: ";
cin >> row >> col;
cout << endl;
} while (!isValidMove());
row--;
col--;
}
bool TicTacToe::addMove() // does work with board[row][col]
{
bool gStatus = false;
noOfMoves++;
board[row][col] = player;
TicTacToe::displayBored();
gStatus = TicTacToe::gameStatus();
if (gStatus == true) {
cout << "Player " << player << " wins!" << endl;
return true;
} else if (noOfMoves >= 9) {
return true;
} else
return false;
}
bool TicTacToe::isValidMove()
{
if ((row <=3 && col <=3) && (board[row-1][col-1] != 'X' && board[row-1][col-1] != 'O' ) )
{
return true;
}
else
{
return false;
}
}
void TicTacToe::Play()
{
TicTacToe::ResetBoard();
TicTacToe::displayBored();
bool done = false;
while (!done)
{
TicTacToe::getXOMove();
done = TicTacToe::addMove();
}
}
void TicTacToe::PlayerFlick()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}
AI 标头
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class TicTacToeAI
{
private:
TicTacToe board;
int col =0;
int row =0;
int arr [3][3] = {0};
public:
void CreateArr(int *Arr, int arrLength);
void play();
void getXmove();
void getOMove();
void getXmove(char player, row&, col&);
void GetoMove(char playr , row&, col&);
};
void TicTacToeAI::CreateArr(int *Arr,int arrLength)
{
}
【问题讨论】:
-
这是文字墙..你能指定一个Minimal Reproducible Example 来展示你的想法吗?
-
或者通过你的
main,你想让TicTacToeAI test和run通信吗? -
避免使用
using namespace std;,尤其是在标题中或#include之前。
标签: c++ arrays class header artificial-intelligence