如何通过一个数组因为没有它改变原来的数组参数?

问题描述 投票:0回答:1

我试图做一个井字棋递归算法,找到发挥最佳位置。现在它应该返回10,如果当两个球员发挥最佳之能胜,-10若失,同时播放优化和0,如果这将是既发挥最佳领带。我试图存储tempMax或tempMin随计算机或人类是“玩”上的分数或者阵列中的递归的每一层。然而,虽然它正确地存储得分递归的每个层次,它修改在调用它的递归函数的tempMax /最小值阵列。很抱歉,如果我没有解释这个问题很好。

我试过这段代码有很多版本,但他们都不工作。对于低于我的代码,我清理了大部分的东西我以前试过,但留在打印报表和实际的递归函数测试的东西。

#include <iostream>
using namespace std;

string board[9];// = {"X", "O", "X", "O", "O", "_", "X", "_", "_"};
//uncomment out above line to enter a specific case 
int minBoard[9] = {999, 999, 999, 999, 999, 999, 999, 999, 999};
int maxBoard[9] = {-999, -999, -999, -999, -999, -999, -999, -999, -999};
string computer = "X", human = "O";

void reset(){
        for (int i = 0; i < 9; i++){
        minBoard[i] = 999;
        maxBoard[i] = -999;
    }
}

int maximum(int arr[]){
    int MAX = arr[0];
    for (int i = 1; i < 9; i++){
        MAX = max(arr[i], MAX);
    }return MAX;
}

int minimum(int arr[]){
    int MIN = arr[0];
    for (int i = 1; i < 9; i++){
        MIN = min(arr[i], MIN);
    }return MIN;
}

void printBoard(){
    for (int i = 0; i < 9; i++){
        if (i%3 != 0){
            cout << "|";
        }
        cout << board[i];
        if (i%3 == 2){
            cout << endl;
        }

    }cout << endl;
}

void updateSpots(){
    for (int i = 0; i < 9; i++){
        if (board[i] == "_"){
            availableSpots[i] = 1;
        }else{
            availableSpots[i] = 0;
        }
    }
}

bool tied(){
    for (int i = 0; i < 9; i++){
        if (board[i] == "_"){
            return false;
        }
    }return true;
} 

bool winning(string player){
    if ((board[0] == player && board[1] == player && board[2] == player) ||
    (board[3] == player && board[4] == player && board[5] == player) ||
    (board[6] == player && board[7] == player && board[8] == player) ||
    (board[0] == player && board[3] == player && board[6] == player) ||
    (board[1] == player && board[4] == player && board[7] == player) ||
    (board[2] == player && board[5] == player && board[8] == player) ||
    (board[0] == player && board[4] == player && board[8] == player) ||
    (board[2] == player && board[4] == player && board[6] == player)
){
    return true;
}else{
    return false;
}
}

int solve(string player, int tempMin[], int tempMax[]){
//  printBoard();
//  for (int i = 0; i < 9; i++){
//          cout << tempMax[i] << ", ";
//  }cout << " TEMPMAX" << endl;
//  for (int i = 0; i < 9; i++){
//          cout << tempMin[i] << ", ";
//  }cout << " TEMPMIN" << endl;
if (winning(human)){
    return -10;
}else if (winning(computer)){
    return 10;
}else if (tied()){
    return 0;
}else if (player == "X"){
    for (int i = 0; i < 9; i++){
        if (board[i] == "_"){
            board[i] = "X";
//              alpha = min(solve(human), alpha);
                reset();
                tempMax[i] = solve(human, minBoard, maxBoard);
//              cout << tempMax[i] << "   MAAAAAAAAAA " << i << endl;
                board[i] = "_";
            }
        }
//      for (int i = 0; i < 9; i++){
//          cout << tempMax[i] << ", ";
//      }cout << " TEMPMAX" << endl;
//      cout << maximum(tempMax) << " TEMPMAX" << endl;
        return maximum(tempMax);
//      int MAX = tempMax[0];
//      for (int i = 1; i < 9; i++){ 
//          MAX = max(tempMax[i], MAX);
//      }return MAX;
    }else if (player == "O"){
        for (int i = 0; i < 9; i++){  
            if (board[i] == "_"){
                board[i] = "O";
//              beta = max(solve(computer), beta);
                reset();
                tempMin[i] = solve(computer, minBoard, maxBoard);
//              cout << tempMin[i] << "   MIIIIIIIIII " << i << endl;
                board[i] = "_";
            }
        }
//      for (int i =  MPMIN" << endl;
//      for (int i = 0; i < 9; i++){
//          cout << tempMin[i] << ", ";
//      }cout << " TEMPMIN" << endl;
//      cout << minimum(tempMin) << " TEMPMIN" << endl;
        return minimum(tempMin);
//      int MIN = tempMin[0];
//      for (int i = 1; i < 9; i++){
//          MIN = min(tempMin[i], MIN);
//      }return MIN;
    }
    }

int main(){
    for (int i = 0; i < 9; i++){
        board[i] = "_";
    }//comment the for loop out for specific case testing
    cout << solve(computer, minBoard, maxBoard) << endl;
}

对于空井字棋板,如果双方球员发挥最佳的,它应该是平局所以程序应该返回0。但是,它返回10.我也曾尝试更小的情况下,但它仍然无法正常工作。

c++ recursion tic-tac-toe
1个回答
0
投票

阵列由转交地址,所以当你传递一个数组给一个函数需要它的地址,但你可以做它的一个副本,然后将它传递这样声明,返回原来的数组的副本的新地址的copy_array_function

© www.soinside.com 2019 - 2024. All rights reserved.