Fif Game in C++
<p>Fif Game: 9 squares labeled 1-9.  I'm taking turns against the computer to place an X or O on each of the 9 squares.  The first to get a sum of 15 with atleast 3 squares wins.</p>

<p>I'm trying to complete the C++ program of the Fif game.  I found some code online for tic-tac-toe.  I borrowed some of that, modified it a bit (but I'm not finished), and made it into the "Fif" game.  It works except for a few things.  The index is off by 1 (which is minor).  Also, more importantly, it needs 3 things.  1.Static Evaluation to judge the computer's moves.  2. Move Generator  3. Alpha-Beta Pruining.  I didn't fully use the template the teacher gave us for this assignment, but he did have a full alpha-beta function, which I think is the only thing I'm lacking since the program basically works.  I'll post the alpha-beta and then what I have.  Basically I need help putting everything together.  </p>

<pre><code>     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 &gt; 100)            //machine win
      return INT_MAX;
    else if (Value &lt; -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 &lt;= 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 &gt;= Beta)
      {
        BestScore = Beta;
        I = MaxNum;           //prune remaining S successors
      }
    }
    else
    {
      BestScore = Min(V, BestScore);
      Beta = BestScore;
      if (Alpha &gt;= Beta)
      {
        BestScore = Alpha;
        I = MaxNum;           //prune remaining S successors
      }
    }
    I = I + 1;
      }
      return BestScore;
    }
  }


//My Fif Program itself

#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;
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&lt;char&gt;&amp; board);
char winner(const vector&lt;char&gt;&amp; board);
int humanMove(const vector&lt;char&gt;&amp; board, char human);
bool isLegal(const vector&lt;char&gt;&amp; board, int move);
int humanMove(const vector&lt;char&gt;&amp; board, char human);
int computerMove(vector&lt;char&gt; board, char computer);
void announceWinner(char winner, char computer, char human);
//main function
int main()
{

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

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

cout&lt;&lt;"123456789"&lt;&lt;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&lt;&lt;"123456789"&lt;&lt;endl;
displayBoard(board);
turn = opponent(turn);
}
announceWinner(winner(board), computer, human);
system("pause");
return 0;
}



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

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

cout &lt;&lt; "\n\n";
}
char winner(const vector&lt;char&gt;&amp; 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 &lt; TOTAL_ROWS; ++row)
{
if ( (board[WINNING_ROWS[row][0]] != EMPTY) &amp;&amp;
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &amp;&amp;
(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&lt;char&gt;&amp; board)
{
return (board[move] == EMPTY);
}
int humanMove(const vector&lt;char&gt;&amp; board, char human)
{
int move = askNumber("Where will you move?", (board.size()-1));
while (!isLegal(move, board))
{

cout &lt;&lt; "\nThis square is already taken, choose a different square. \n";
move = askNumber("Where will you move?", (board.size()-1));
}
return move;
}
int computerMove(vector&lt;char&gt; board, char computer)
{
cout &lt;&lt; "I'll take number: ";
//if computer can win on next move, make that move
for(int move = 1; move &lt; board.size(); ++move)
{
if (isLegal(move, board))
{
board[move] = computer;
if (winner(board) == computer)
{
cout &lt;&lt; move &lt;&lt; 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 &lt; board.size(); ++move)
{
if (isLegal( move, board))
{
board[move] = human;
if (winner(board) == human)
{ 
cout &lt;&lt; move &lt;&lt; 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 &lt;board.size(); ++i)
{
int move = BEST_MOVES[i];
if (isLegal(move, board))
{
cout &lt;&lt; move &lt;&lt; endl;
return move;
}
}
}
void announceWinner(char winner, char computer, char human)
{

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

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

}
</code></pre>
