Esempio n. 1
0
        public void GetMove(Pile[] piles, out int pileIndex, out int sticksToRemove)
        {
            pileIndex = FindIndexOfMax(piles);
            sticksToRemove = piles[pileIndex].Count - Xor(piles[pileIndex].Count, NimSum(piles));

            if (sticksToRemove == 0)
                sticksToRemove = 1;
        }
Esempio n. 2
0
 private static void InitializePileSizes(Pile[] piles)
 {
     for (int i = 0; i < piles.Length; i++)
     {
         piles[i] = new Pile(GetIntInRange(0, int.MaxValue,
                                           String.Format("Please enter how many sticks you wish there to be in pile {0}.", i + 1)));
     }
     Console.WriteLine();
 }
Esempio n. 3
0
        private static bool CheckForWinner(Pile[] piles)
        {
            foreach(Pile pile in piles)
            {
                if (pile.Count > 0)
                    return false;
            }

            return true;
        }
Esempio n. 4
0
        private int NimSum(Pile[] piles)
        {
            int nimsum = 0;

            foreach (Pile pile in piles)
            {
                nimsum = Xor(nimsum, pile.Count);
            }

            return nimsum;
        }
Esempio n. 5
0
        private int FindIndexOfMax(Pile[] piles)
        {
            int max = -1;
            int maxIndex = 0;

            for (int i = 0; i < piles.Length; i++)
            {
                if (piles[i].Count > max)
                {
                    max = piles[i].Count;
                    maxIndex = i;
                }
            }

            return maxIndex;
        }
Esempio n. 6
0
        private static void PlayGame()
        {
            players currentPlayer = players.User;
            NimAi   computer = new NimAi();
            int     pileNumber = 0, sticksToRemove = 0;
            bool    winner = false;

            Pile[] piles = new Pile[GetIntInRange(0, 10, "How many piles do you wish to have?")];

            InitializePileSizes(piles);
            currentPlayer = GetFirstPlayer();

            while (!winner)
            {
                PrintPiles(piles);
                Console.WriteLine();
                if (currentPlayer == players.User)
                {
                    GetUserMove(piles, out pileNumber, out sticksToRemove);
                    piles[pileNumber].Count -= sticksToRemove;
                    AnnounceMove(currentPlayer, pileNumber, sticksToRemove);
                }
                else //player is computer
                {
                    computer.GetMove(piles, out pileNumber, out sticksToRemove);
                    piles[pileNumber].Count -= sticksToRemove;
                    AnnounceMove(currentPlayer, pileNumber, sticksToRemove);
                }

                winner = CheckForWinner(piles);

                if (winner)
                {
                    AnnounceWinner(currentPlayer);
                }
                else
                if (currentPlayer == players.User)
                {
                    currentPlayer = players.Computer;
                }
                else
                {
                    currentPlayer = players.User;
                }
            }
        }
Esempio n. 7
0
        public void NimAISticksToRemoveTest()
        {
            NimAi AI = new NimAi();
            Pile[] piles = new Pile[5];

            piles[0] = new Pile(25);
            piles[1] = new Pile(30);
            piles[2] = new Pile(4);
            piles[3] = new Pile(5);
            piles[4] = new Pile(50);

            int index, sticksToRemove;
            int expectedSticksToRemove = 44;
            AI.GetMove(piles, out index, out sticksToRemove);

            Assert.AreEqual(sticksToRemove, expectedSticksToRemove);
        }
Esempio n. 8
0
 public void PileSetCountNegativeTest()
 {
     Pile p = new Pile(-1);
 }
Esempio n. 9
0
 private static void PrintPiles(Pile[] piles)
 {
     Console.ForegroundColor = GAME_BOARD_COLOR;
     for(int i = 0; i < piles.Length; i++)
     {
         Console.WriteLine("Pile {0}: {1}\t {2,10}", i + 1, piles[i].Count, piles[i].ToString());
     }
     Console.ResetColor();
     Console.WriteLine();
 }
Esempio n. 10
0
        private static void PlayGame()
        {
            players currentPlayer = players.User;
            NimAi computer = new NimAi();
            int pileNumber = 0, sticksToRemove = 0;
            bool winner = false;
            Pile[] piles = new Pile[GetIntInRange(0, 10, "How many piles do you wish to have?")];

            InitializePileSizes(piles);
            currentPlayer = GetFirstPlayer();

            while(!winner)
            {
                PrintPiles(piles);
                Console.WriteLine();
                if (currentPlayer == players.User)
                {
                    GetUserMove(piles, out pileNumber, out sticksToRemove);
                    piles[pileNumber].Count -= sticksToRemove;
                    AnnounceMove(currentPlayer, pileNumber, sticksToRemove);
                }
                else //player is computer
                {
                    computer.GetMove(piles, out pileNumber, out sticksToRemove);
                    piles[pileNumber].Count -= sticksToRemove;
                    AnnounceMove(currentPlayer, pileNumber, sticksToRemove);
                }

                winner = CheckForWinner(piles);

                if (winner)
                    AnnounceWinner(currentPlayer);
                else
                    if (currentPlayer == players.User)
                        currentPlayer = players.Computer;
                    else
                        currentPlayer = players.User;
            }
        }
Esempio n. 11
0
 private static void InitializePileSizes(Pile[] piles)
 {
     for(int i = 0; i < piles.Length; i++)
     {
         piles[i] = new Pile(GetIntInRange(0, int.MaxValue,
             String.Format("Please enter how many sticks you wish there to be in pile {0}.", i + 1)));
     }
     Console.WriteLine();
 }
Esempio n. 12
0
        private static void GetUserMove( Pile[] piles, out int pileNumber, out int sticksToRemove)
        {
            bool validPile = false;

            do
            {
                pileNumber = GetIntInRange(1, piles.Length, "Please enter the pile you wish to remove sticks from.") - 1;

                if(piles[pileNumber].Count == 0)
                {
                    Console.ForegroundColor = ERROR_MESSAGE_COLOR;
                    Console.WriteLine("You may only select a pile that still has sticks in it!");
                }
                else
                {
                    validPile = true;
                }
            } while (!validPile);

            sticksToRemove = GetIntInRange(1, piles[pileNumber].Count,
                String.Format("Please enter how many sticks you wish to remove from pile {0}.", pileNumber + 1));

            Console.WriteLine();
        }
Esempio n. 13
0
        public void Add(int numOfSticks)
        {
            Pile newPile = new Pile(numOfSticks);

            pileList.Add(newPile);
        }