English 中文(简体)
C++ 中的 Fif 游戏
原标题:Fif Game in C++
  • 时间:2012-05-23 21:34:52
  •  标签:
  • c++

Fif 游戏: 9 个方格标有 1- 9 。 我正与计算机对着, 在 9 个方格中的每个方格上放一个 X 或 O 。 最先获得 15 和 至少 3 个方格的赢 。

我试图完成 Fif 游戏的 C++ 程序。 我找到了一些用于 tic-tac-toe 的在线代码 。 我借了一些代码, 稍微修改过一点( 但我还没有完成), 并将其输入到“ Fif” 游戏中。 它除了一些东西以外, 还能工作 。 索引关闭了 1 个( 这是次要的 ) 。 更重要的是, 它需要 3 个东西 。 1. 1 静态评估来判断计算机的移动 。 2. 移动生成器 3 。 移动生成器 3 。 Alpha- Beta Pruining 。 我完全使用了老师给我们的模板, 但是他确实有一个完整的阿尔法 - 贝塔 函数, 我认为这是程序基本有效以来我唯一缺少的东西。 我将阿尔法 - 贝塔 和 我所拥有的东西都放在后面。 基本上我需要帮助把一切都拼凑在一起 。

     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 << "

";
}
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 << "
This square is already taken, choose a different square. 
";
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!
";
cout << "3 of my numbers sums to 15, so you lose! "<<endl;
}
else if (winner == human)
{
cout << winner << " s won!
";
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;
}

}
问题回答

您无法对您的程序应用 prof s alpha beta 修剪, 因为您的程序缺乏静态评价 。

您需要一个函数, 给一个棋盘配置, 产生一个值, 即该配置对计算机的“ 良好 ” 值 。

您的 AI 在您的计算机移动 () 功能中, 您规定了一些明确的规则 :

1) If I can win in the next move, take it
2) If the human can win in one move, block it
3) Otherwise, take the next available "best" move

您必须将这些规则转换为数字值。 该函数应为更可取的棋盘( 计算机的棋盘) 返回更高的数值。 因此, 计算机赢得的棋盘将具有最高值。 一个他可以单步赢的棋盘将略微少一点。 一个人赢的棋盘将具有0等值。

一旦您有了魔力功能, 您的计算机需要创建所有可能的棋盘配置( 可能为创建一棵树的多圈), 并评估其“ 善良性 ” 。 甲型贝塔线将应用到已经确定不需要评估 b/ c 更好的选择的剪切分支的棋盘配置树上 。

如果想应用迷你最大和甲型贝塔的剪切法的话,看来你还有相当多的活儿要干。祝你好运!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签