Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的Fif游戏+;_C++ - Fatal编程技术网

C++ C+中的Fif游戏+;

C++ C+中的Fif游戏+;,c++,C++,国际足联比赛:9个标有1-9的方块。我轮流对着电脑在9个方块上各放一个X或O。第一个得到15和至少3个方块的人获胜 我正在努力完成FIF游戏的C++程序。我在网上找到了一些关于tic-tac-toe的代码。我借用了其中的一些,对它进行了一些修改(但我还没有完成),并将其加入到“Fif”游戏中。除了一些事情外,它还能工作。索引被1关闭(这是次要的)。此外,更重要的是,它需要三件事。1.判断计算机动作的静态评估。2.移动发电机3。α-β-普鲁宁。我没有完全使用老师给我们的作业模板,但他有一个完整的a

国际足联比赛:9个标有1-9的方块。我轮流对着电脑在9个方块上各放一个X或O。第一个得到15和至少3个方块的人获胜

我正在努力完成FIF游戏的C++程序。我在网上找到了一些关于tic-tac-toe的代码。我借用了其中的一些,对它进行了一些修改(但我还没有完成),并将其加入到“Fif”游戏中。除了一些事情外,它还能工作。索引被1关闭(这是次要的)。此外,更重要的是,它需要三件事。1.判断计算机动作的静态评估。2.移动发电机3。α-β-普鲁宁。我没有完全使用老师给我们的作业模板,但他有一个完整的alpha-beta函数,我认为这是我唯一缺少的,因为程序基本上可以运行。我会发布alpha-beta,然后发布我所拥有的。基本上,我需要帮助把所有的东西组装起来

     float AlphaBeta(State S, Ply N, float Alpha, float Beta)
  //Recusively score state S using evaluation function Eval
  //and an N - Ply state space graph.
  {
    State Next;
    ListIndex I;
    float V, Value, BestScore;
    List L;                   //successors of S at this level

    if ((N == 0) || Terminal(S))
    {
      Value = Eval(S);
      T[S] = Value;    //record values only to confirm cut offs

    if (Value > 100)            //machine win
      return INT_MAX;
    else if (Value < -100)      //machine loss
      return  -INT_MAX;
    else if (Value == 0)        //draw
      return 0;
    else
      return Value;
    }
    else
    {
      if (MachineMove(N))         //program's move
    BestScore = Alpha;
      else
    BestScore = Beta;

      I = 1;
      while (I <= MaxNum)
      {
    Next = Child(S, I);
    V = AlphaBeta(Next, N - 1, Alpha, Beta);

    if (MachineMove(N))       //program's move
    {
      BestScore = Max(V, BestScore);
      Alpha = BestScore;
      if (Alpha >= Beta)
      {
        BestScore = Beta;
        I = MaxNum;           //prune remaining S successors
      }
    }
    else
    {
      BestScore = Min(V, BestScore);
      Beta = BestScore;
      if (Alpha >= Beta)
      {
        BestScore = Alpha;
        I = MaxNum;           //prune remaining S successors
      }
    }
    I = I + 1;
      }
      return BestScore;
    }
  }


//My Fif Program itself

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//global constants
const char X = 'X';
const char O = 'O'; 
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';
// function prototypes 

char askYesNo(string question);
int askNumber(string question, int high, int low = 1);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
int humanMove(const vector<char>& board, char human);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(vector<char> board, char computer);
void announceWinner(char winner, char computer, char human);
//main function
int main()
{

int move;
const int NUM_SQUARES = 9;
vector<char> board(NUM_SQUARES, EMPTY);
char human = humanPiece();
char computer = opponent(human);
char turn = X;
int ChoiceToPlayAgain;
string question;

cout << "Welcome to the 'Fif' Game!  You are competing aganst the computer."<<endl;  
cout << "Below is the board that you are using.  The object of the game is to" <<endl;
cout << "make your values to where you can get 3 of the values to sum to 15."<<endl;  
cout << "Valid input values are from 1-9" <<endl<<endl;

cout<<"123456789"<<endl;
displayBoard(board);
while (winner(board) == NO_ONE)
{
if (turn == human)
{
move = humanMove(board, human);
board[move] = human;
}
else
{
move = computerMove(board, computer);
board[move] = computer;
}
cout<<"123456789"<<endl;
displayBoard(board);
turn = opponent(turn);
}
announceWinner(winner(board), computer, human);
system("pause");
return 0;
}



char askYesNo(string question)
{
char response;
do
{
cout << question << "(C/Y): ";
cin >> response;
} while (response != 'y' && response != 'n');
return response;
}
int askNumber(string question, int high, int low)
{
int number;
do
{
cout << question << " (1-9): ";
cin >> number;
}while (number > high || number < low);
return number;
}
char humanPiece()

{
char go_first = askYesNo("Should I start or you?");
if (go_first == 'Y')
{
cout << "Make your move:";
return X;
}
else
{
cout << "I will start:";
return O;
}
}
char opponent(char piece)
{
if (piece == X)
return O;
else
return X;
}
void displayBoard(const vector<char>& board)
{
cout << board[0] << board[1] << board[2]<< board[3] << board[4] << board[5]<< board[6] << board[7] << board[8];

cout << "\n\n";
}
char winner(const vector<char>& board)
{
//all possible winning rows
const int WINNING_ROWS[8][3] = { 
{4,9,2},
{3,5,7},
{8,1,6},
{4,3,8},
{9,5,1},
{2,7,6},
{4,5,6},
{2,5,8}
 };
const int TOTAL_ROWS = 8;
//if any winning row has three value that are the same (and not EMPTY),
// then we have a winner
for(int row = 0; row < TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
{
return board[WINNING_ROWS[row][0]];
}
}
//since nobody has won, check for a tie (no empty squares left)
if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;
// since nobody has won it isn't a tie, the game ain't over 
return NO_ONE;
}
bool isLegal(int move, const vector<char>& board)
{
return (board[move] == EMPTY);
}
int humanMove(const vector<char>& board, char human)
{
int move = askNumber("Where will you move?", (board.size()-1));
while (!isLegal(move, board))
{

cout << "\nThis square is already taken, choose a different square. \n";
move = askNumber("Where will you move?", (board.size()-1));
}
return move;
}
int computerMove(vector<char> board, char computer)
{
cout << "I'll take number: ";
//if computer can win on next move, make that move
for(int move = 1; move < board.size(); ++move)
{
if (isLegal(move, board))
{
board[move] = computer;
if (winner(board) == computer)
{
cout << move << endl;
return move;
}
//done checking this move, undo it
board[move] = EMPTY;
}
}
//if human can win on next move, block that move
char human = opponent(computer);
for(int move = 1; move < board.size(); ++move)
{
if (isLegal( move, board))
{
board[move] = human;
if (winner(board) == human)
{ 
cout << move << endl;
return move;

}
// done checking this move, undo it
board[move] = EMPTY;
}
}
// the best moves to make, in order
const int BEST_MOVES[] = {5,4,2,8,6,9,3,7,1};
// since no one can win on next move, pick best open square
for(int i = 1; i <board.size(); ++i)
{
int move = BEST_MOVES[i];
if (isLegal(move, board))
{
cout << move << endl;
return move;
}
}
}
void announceWinner(char winner, char computer, char human)
{

if (winner == computer)
{
cout << winner << "'s won!\n";
cout << "3 of my numbers sums to 15, so you lose! "<<endl;
}
else if (winner == human)
{
cout << winner << "'s won!\n";
cout << "3 of your numbers sums to 15, so you win!" <<endl;

}
else
{
cout << "Nobody reached a sum of 15 after board is filled, so it's a tie." << endl;
}

}
float AlphaBeta(状态S、层N、float Alpha、float Beta)
//使用评估函数Eval对状态S进行递归评分
//和一个N层状态空间图。
{
然后陈述;
列表索引I;
浮动V,值,最佳分数;
列表L;//此级别的S的继承者
如果((N==0)| |终端)
{
数值=评估值;
T[S]=Value;//记录值仅用于确认截止值
如果(值>100)//机器获胜
返回INT_MAX;
否则如果(值<-100)//机器损失
返回-整数最大值;
else如果(值==0)//绘制
返回0;
其他的
返回值;
}
其他的
{
if(MachineMove(N))//程序的移动
最佳得分=α;
其他的
最佳得分=贝塔;
I=1;

而(I您不能将prof的alpha-beta修剪应用于您的程序,因为您的程序缺少静态评估

您需要一个函数,给定一个电路板配置,该函数生成一个值,该值表示该配置对计算机的“良好”程度

您的AI位于computerMove()函数中,您制定了一些明确的规则:

1) 如果我能在下一步中获胜,就接受它
2) 如果该人可以在一次移动中获胜,请阻止它
3) 否则,采取下一个可用的“最佳”行动

您必须将这些规则转换为数值。该函数应该为更理想的棋盘(计算机)返回更高的值。因此,计算机获胜的棋盘将具有最高的值。他可以在一次移动中获胜的棋盘将具有略低的值。人获胜的棋盘将具有0的值,以此类推

一旦你有了这个神奇的功能,你的计算机就需要创建所有可能的电路板配置(可能是多圈,这会创建一棵树)并评估它们的“优点”。alpha-beta修剪将应用于创建的线路板配置树,用于修剪不再需要评估的分支b/c已确定更好的选择

如果你想应用minimax修剪和alpha-beta修剪,看起来你还有很多工作要做。祝你好运