Example #1
0
        private static void StartGame()
        {
            Players CurrentPlayer = Players.User;
            NimAi   Computer = new NimAi();
            int     HeapNumber = 0, ObjectsToRemove = 0;
            bool    Winner = false;

            heap[] Heaps = new heap[GetIntInRange(0, 10, "How many heaps do you want this game to have?")];

            InitializeHeapsizes(Heaps);
            CurrentPlayer = GetFirstPlayer();

            while (!Winner)
            {
                PrintHeaps(Heaps);
                Console.WriteLine();
                if (CurrentPlayer == Players.User)
                {
                    GetPlayerMove(Heaps, out HeapNumber, out ObjectsToRemove);
                    Heaps[HeapNumber].Count -= ObjectsToRemove;
                    StateMove(CurrentPlayer, HeapNumber, ObjectsToRemove);
                }
                else // This means that the player is the computer.
                {
                    Computer.GetMove(Heaps, out HeapNumber, out ObjectsToRemove);
                    Heaps[HeapNumber].Count -= ObjectsToRemove;
                    StateMove(CurrentPlayer, HeapNumber, ObjectsToRemove);
                }

                Winner = CheckWhoIsWinner(Heaps);

                if (Winner)
                {
                    AnnounceWinner(CurrentPlayer);
                }
                else
                if (CurrentPlayer == Players.User)
                {
                    CurrentPlayer = Players.Computer;
                }
                else
                {
                    CurrentPlayer = Players.User;
                }
            }
        }
Example #2
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);
        }
Example #3
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;
                }
            }
        }
Example #4
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;
            }
        }