我正在尝试制作一个Tic-Tac-Toe递归算法,该算法可以找到最佳的位置。现在,如果两个玩家都以最佳状态获胜,则应该返回10;如果在最佳游戏中失败,则返回-10;如果两个玩家都处于最佳状态,则返回0。我试图将递归每个级别的分数存储在数组tempMax或tempMin中,具体取决于计算机或人类是否在玩。但是,尽管它正确存储了每个递归级别的分数,但它会在调用它的递归函数中修改tempMax / Min数组。抱歉,如果我不能很好地解释这个问题。
我尝试了此代码的许多版本,但似乎都没有用。对于下面的代码,我清理了之前尝试过的大部分内容,但留在了打印语句中,并在实际的递归函数中进行了测试。
#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;
}
对于一个空的Tic-Tac-Toe棋盘,如果两个玩家都玩得最好,那应该是平局,因此程序应返回0。但是,它返回10。我也尝试过较小的情况,但仍然无法正常工作。
答案 0 :(得分:0)
数组按地址传递,因此当您将数组传递给函数时,它会获取其地址,但您可以对其进行复制然后传递给它 所以声明一个copy_array_function,它返回原始数组副本的新地址