// Test the protocol components related to the 'move' message type.
            // Throws Exception if a 'move' message is generated which is
            // inconsistent with the specified move.
            private static void TestMoveMessage()
            {
                TzaarMessage.Move message = new TzaarMessage.Move(1, 2, 3, 4);

                // Check that the generated messages match the specified move.
                if (message.FromCol != 1)
                    throw new Exception();

                if (message.FromRow != 2)
                    throw new Exception();

                if (message.ToCol != 3)
                    throw new Exception();

                if (message.ToRow != 4)
                    throw new Exception();

                // Check that the generated string matches the format of the
                // 'move' message defined in the protocol for the specified
                // move.
                if (((string)message).CompareTo("Move{1,2,3,4}") != 0)
                    throw new Exception();

                message = new TzaarMessage.Move("Move{5,6,7,8}");

                // Check the functionality of another arbitrary move.
                if (message.FromCol != 5)
                    throw new Exception();

                if (message.FromRow != 6)
                    throw new Exception();

                if (message.ToCol != 7)
                    throw new Exception();

                if (message.ToRow != 8)
                    throw new Exception();
            }
Ejemplo n.º 2
0
        public void MakeMove(GamePlayer playerNumber, string msg)
        {
            // Parse it as a move command.
            TzaarMessage.Move move;
            try
            {
                move = new TzaarMessage.Move(msg);
            }
            catch (Exception)
            {
                // Junk message.
                IllegalMove();
                throw new Exception(playerNumber + " forfeits, because they sent a move command that could not be parsed.");
            }

            // Now execute the command we received.
            if (move.IsPass)
            {
                // Make the requested Pass.
                if (!Pass())
                {
                    // The attempt to Pass failed!
                    IllegalMove();
                    throw new Exception(playerNumber + " forfeits, because they attempted an illegal Pass.");
                }
            }
            else
            {
                // Make the requested move.
                try
                {
                    Move(move);
                }
                catch (Exception ex)
                {
                    // The move attempt failed!
                    IllegalMove();
                    throw new Exception(playerNumber + "Move Failed: " + move + "  Reason: " + ex.Message);
                }
            }

            CheckForGameOver();
        }