コード例 #1
0
ファイル: ComputerPlayer.cs プロジェクト: JoshuaSalazar/Nim
        public bool makeMove(GameManager game)
        {
            int bestMove = -1;
            for (int numState = 0; numState < game.stateList.Count; numState++)
            {
                if (validMove(game, numState))
                {
                    if (numRowsToMove(game, numState) == 1)
                    {
                        if (isBestMove(game, numState, bestMove))
                        {
                            bestMove = numState;
                        }
                    }
                }
            }

            if (bestMove != -1 && game.stateList.Count > 0)
            {
                int topDiff = game.rows[0] - game.stateList[bestMove].TopRow;
                int midDiff = game.rows[1] - game.stateList[bestMove].MidRow;
                int botDiff = game.rows[2] - game.stateList[bestMove].BotRow;
                if (topDiff != 0)
                {
                    game.makeMove(0, topDiff);
                }
                else if (midDiff != 0)
                {
                    game.makeMove(1, midDiff);
                }
                else if (botDiff != 0)
                {
                    game.makeMove(2, botDiff);
                }
            }
            else
            {
                bool hasMoved = false;
                while (!hasMoved)
                {
                    int randRow = rand.Next(3);
                    int numToRemove = rand.Next(game.rows[randRow]+1);
                    hasMoved = game.makeMove(randRow, numToRemove);
                }
            }
            return true;
        }
コード例 #2
0
ファイル: ComputerPlayer.cs プロジェクト: JoshuaSalazar/Nim
 private int numRowsToMove(GameManager game, int numState)
 {
     int numrowsToMove = 0;
     if (game.stateList[numState].TopRow < game.rows[0])
     {
         numrowsToMove++;
     }
     if (game.stateList[numState].MidRow < game.rows[1])
     {
         numrowsToMove++;
     }
     if (game.stateList[numState].BotRow < game.rows[2])
     {
         numrowsToMove++;
     }
     return numrowsToMove;
 }
コード例 #3
0
ファイル: HumanPlayer.cs プロジェクト: JoshuaSalazar/Nim
 public bool makeMove(GameManager game)
 {
     bool moveMade = false;
     do
     {
         game.printBoard();
         writer("Choose number of row");
         int row = promptForInt("(0) for First Row, (1) for Second Row, (2) for Third Row");
         int num = promptForInt("How many pieces do you want to take away?");
         if (game.makeMove(row, num))
         {
             moveMade = true;
         }
         else
         {
             writer("Invalid Input, try again");
         }
     } while (!moveMade);
     return true;
 }
コード例 #4
0
ファイル: ComputerPlayer.cs プロジェクト: JoshuaSalazar/Nim
 private bool validMove(GameManager game, int numState)
 {
     return (game.stateList[numState].TopRow <= game.rows[0] &&
             game.stateList[numState].MidRow <= game.rows[1] &&
             game.stateList[numState].BotRow <= game.rows[2]);
 }
コード例 #5
0
ファイル: ComputerPlayer.cs プロジェクト: JoshuaSalazar/Nim
 private bool isBestMove(GameManager game, int numState, int bestMove)
 {
     return (bestMove == -1 || game.stateList[numState].getWeight() <= game.stateList[bestMove].getWeight());
 }
コード例 #6
0
ファイル: UserInput.cs プロジェクト: JoshuaSalazar/Nim
 public UserInput(writerDelegate newWriter, readerDelegate newReader)
 {
     writer = new writerDelegate(newWriter);
     readLine = new readerDelegate(newReader);
     game = new GameManager(writer);
 }