Exemplo n.º 1
0
        private void NewGame()
        {
            if (rulebook == null) return;
            GameState state;
            this.game = new Game(rulebook, out state);
            gameState = state;
            game.SetSelectPieceFunction(SelectPiece);

            var cache = new ImageCache(game, gamename);
            SetCoordTransformation(game);

            gamePanel.Game = game;
            gamePanel.GetGameState = () => gameState;
            gamePanel.ImageCache = cache;

            offBoard.Game = game;
            offBoard.GetGameState = () => gameState;
            offBoard.ImageCache = cache;

            gamePanel.InvalidateVisual();

            offBoard.Refresh();

            currentPlayerLabel.Content = gameState.CurrentPlayer.ToString();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 1)
                {
                    PrintUsage();
                    return;
                }

                string fileName = System.IO.Path.Combine("Games", args[0] + ".game");
                if (!System.IO.File.Exists(fileName))
                {
                    Console.WriteLine("{0} does not exists!", fileName);
                    PrintUsage();
                    return;
                }

                GameState state;
                Game game = new Game(System.IO.File.ReadAllText(fileName), out state);

                game.SetSelectPieceFunction(PieceChooser);

                Console.WriteLine("Game loaded successfully");

                PrintHelp();

                while (!game.GameOver)
                {
                    Console.WriteLine();
                    Console.Write("> ");
                    string line = Console.ReadLine().Trim();
                    if (line.Length == 0)
                    {
                        Console.WriteLine("?");
                        continue;
                    }
                    string[] command = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    switch (command[0])
                    {
                        case "?":
                        case "h":
                            PrintHelp();
                            break;
                        case "b":
                            PrintBoard(game, state);
                            break;
                        case "l":
                            PrintMoves(game, state);
                            break;
                        case "m":
                            if (command.Length != 3) goto default;
                            GameState newState = PerformMove(game, state, command[1], command[2]);
                            if (newState != state)
                            {
                                state = newState;
                                Console.WriteLine("OK! Next player: {0}", state.CurrentPlayer);
                            }
                            break;
                        case "q":
                            Console.WriteLine("Bye!");
                            return;
                        default:
                            Console.WriteLine("Invalid command! Type ? for help.");
                            break;
                    }
                }

                if (game.PlayerCount == 1)
                {
                    if (game.Winners.Count() == 1)
                    {
                        Console.WriteLine("You won!");
                    }
                    else
                    {
                        Console.WriteLine("You lost!");
                    }
                }
                else
                {
                    if (game.Winners.Count() == 0)
                    {
                        Console.WriteLine("Tie!");
                    }
                    else if (game.Winners.Count() == 1)
                    {
                        Console.WriteLine("The winner is {0}", game.Winners.First());
                    }
                    else
                    {
                        Console.WriteLine("The winners are {0}", string.Join(", ", game.Winners));
                    }
                }
                Console.WriteLine("Press return to quit.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occured! Details follow:");
                Console.WriteLine(e);
            }
        }