将数组索引从一个 Class 传递到另一个 C++

Passing Array index from one Class to another C++

我只是想知道是否可以在将数组指针从一个 header 文件传递​​到另一个文件时获得一些帮助。

我有 Header 个 TicTacToe 文件,其中包含游戏 TicTacToe 和另一个 Header 文件,其中将包含我的 AI 及其方法。

我只是想知道我是否可以来回传递从 TicTacToe 到 AI 的移动,以便 ai 可以做出明智的移动并且 return 它回到 TicTacToe header for Validation/Updating 用于 gameBoard(或者我将在 AI 中进行另一个验证)一旦我有了想法,我将开始将方法从 TicTacToe 分离到它们自己的 class.

我已经包含了我的 Main、TicTacToe 和 AI 的代码 如果有任何批评,请告诉我

主要

#include <iostream>
using namespace std;
#include "TicTacToe.h"
#include "AI.h"
int main()
{
TicTacToe run;
run.Play();
TicTacToeAI Test;
}

TicTacToe.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 header

     #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)
     {

     }

最常见的方式可能是控制反转。

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

因此 TicTacToe 可以作为 class 成员通过指针访问 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();
}