Exemplo n.º 1
0
        public static void NewGame()
        {
            // Reset current game to initial state
            ActiveGame.Init();

            // Create players
            InitializePlayers();

            // User cancelled players creation and wants to exit
            if (ActiveGame.Players == null)
            {
                return;
            }

            // Create ships for users
            if (!InitializeShips())
            {
                // User cancelled ship creation
                return;
            }

            // Ask to start game
            if (!YesOrQuitMenu("Game menu", "Start game"))
            {
                return;
            }

            RunGame();
        }
Exemplo n.º 2
0
 static BaseConversion()
 {
     // Create entries according to max board size rule
     for (int i = 0; i < ActiveGame.GetRule(RuleType.BoardSize).MaxVal; i++)
     {
         ConversionDictionary.Add(i, ToBase26(i + 1));
     }
 }
Exemplo n.º 3
0
        private static void InitializePlayers()
        {
            var playerCount = ActiveGame.GetRuleVal(RuleType.PlayerCount);

            ActiveGame.Players = new List <Player>();

            for (var i = 0; i < playerCount; i++)
            {
                string name;

                while (true)
                {
                    name = NameMenu("Creating players", $"Input a name for player {i + 1}/{playerCount}");

                    // User chose to quit the menu
                    if (name == null)
                    {
                        return;
                    }

                    // Check input validity
                    if (!InputValidator.CheckValidPlayerName(name))
                    {
                        Console.WriteLine("Invalid name!");
                        Console.ReadKey(true);
                        continue;
                    }

                    break;
                }

                // Generate ships for the player based on current rules
                var ships = ShipLogic.GenGameShipList();
                ActiveGame.Players.Add(new Player(name, ships));
            }
        }
Exemplo n.º 4
0
        private static void GameLoop()
        {
            while (true)
            {
                // Check for any winners
                if (ActiveGame.TrySetWinner())
                {
                    break;
                }

                // Do the attacks
                foreach (var player in ActiveGame.Players)
                {
                    // Loop until first alive player is found
                    if (!PlayerLogic.IsAlive(player))
                    {
                        continue;
                    }

                    Console.Clear();
                    Console.WriteLine($"It is now {player.Name}'s turn");
                    Console.ReadKey(true);

                    // Find next player in list that is alive
                    var nextPlayer = GameLogic.FindNextPlayer(ActiveGame.Players, player);
                    var move       = Attack(player, nextPlayer);

                    // User wants to quit the game
                    if (move == null)
                    {
                        return;
                    }

                    ActiveGame.Moves.Add(move);

                    Console.WriteLine($"It was a {move.AttackResult}...");
                    Console.ReadKey(true);

                    // Check if target player is out of the game (all ships have been destroyed)
                    if (!PlayerLogic.IsAlive(nextPlayer))
                    {
                        Console.Clear();
                        Console.WriteLine($"{player.Name} knocked {nextPlayer.Name} out of the game!");
                        Console.ReadKey(true);
                    }
                }

                Console.Clear();
                Console.WriteLine($"End of round {ActiveGame.RoundCounter}");
                Console.ReadKey(true);

                ActiveGame.RoundCounter++;
            }

            if (ActiveGame.Winner != null)
            {
                Console.Clear();
                Console.WriteLine(
                    $"The winner of the game is {ActiveGame.Winner.Name} after {ActiveGame.RoundCounter} turns!");
                Console.ReadKey(true);
            }
        }