コード例 #1
0
        /*
         * Play one Kalah game with the two given players, with firstPlayer
         * starting. This function returns TOP's score.
         */
        public static int playGame(Player pTop, Player pBot, Position firstPlayer)
        {
            b = new Board(firstPlayer);

            if (firstPlayer == Position.Top)
            {
                Console.WriteLine("Player " + pTop.getName() + " starts.");
            }
            else
            {
                Console.WriteLine("Player " + pBot.getName() + " starts.");
            }

            b.Display();

            while (!b.GameOver())
            {
                Console.WriteLine();
                if (b.WhoseMove() == Position.Top)
                {
                    move = pTop.chooseMove(b);
                    Console.WriteLine(pTop.getName() + " chooses move " + move);
                }
                else
                {
                    move = pBot.chooseMove(b);
                    Console.WriteLine(pBot.getName() + " chooses move " + move);
                }

                b.MakeMove(move, true);         // last parameter says to be chatty
                b.Display();

                if (b.GameOver())
                {
                    if (b.Winner() == Position.Top)
                    {
                        Console.WriteLine("Player " + pTop.getName() +
                                          " (TOP) wins " + b.ScoreTop() + " to " + b.ScoreBot());
                    }
                    else if (b.Winner() == Position.Bottom)
                    {
                        Console.WriteLine("Player " + pBot.getName() +
                                          " (BOTTOM) wins " + b.ScoreBot() + " to " + b.ScoreTop());
                    }
                    else
                    {
                        Console.WriteLine("A tie!");
                    }
                }
                else
                if (b.WhoseMove() == Position.Top)
                {
                    Console.WriteLine(pTop.getName() + " to move.");
                }
                else
                {
                    Console.WriteLine(pBot.getName() + " to move.");
                }
            }
            return(b.ScoreTop());
        }