【问题标题】:Passing Array index from one Class to another C++将数组索引从一个类传递到另一个 C++
【发布时间】: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 testrun通信吗?
  • 避免使用using namespace std;,尤其是在标题中或#include之前。

标签: c++ arrays class header artificial-intelligence


【解决方案1】:

最常用的方法可能是控制反转。

下面的代码演示了Inversion of Control by Dependency Injection

所以井字棋可以通过指针作为类成员访问AI。

#include<iostream>

class TicTacToeAI{    
};

class TicTacToe{
    private:
        const TicTacToeAI* m_AI;
    public:
        TicTacToe( const TicTacToeAI* ai ) : m_AI( ai ){ // Inject the pointer to TicTacToe

        }
};

int main(){
    TicTacToeAI Test;
    TicTacToe run( &Test );
    run.Play();
}

【讨论】:

  • 这两种方式都可以吗?由于井字游戏 atm 拥有用户输入的所有控制权,因此输入到数组中。
  • 我没有完全阅读你的代码,但是只有在用户输入之后才需要人工智能(除非人工智能先行)。所以单向交互就足够了。只需将用户输入放入 AI 中,然后 AI 输出下一步即可。
  • AI的界面好像不是很清楚,不需要CreateArr这样的函数来操作AI。 AI 只需要两个公共函数: 1. 复位状态 2. 取用户输入和输出 AI 步。 X 和 O 无关紧要,它们只是一个符号。您应该处理TicTacToe 中的符号输出。
猜你喜欢
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多