// Добавить запись в журнал.
 private void Log(string info)
 {
     try
     {
         Dispatcher.Invoke(delegate()
         {
             LogTextBlock.AppendText(info);
             if (_ShouldScrollToEnd)
             {
                 LogTextBlock.ScrollToEnd();
             }
         });
     }
     catch (TaskCanceledException)
     {
         return;
     }
 }
Beispiel #2
0
        /// <summary>
        /// Creates a new game
        /// </summary>
        /// <param name="loadFileToo">A bool telling whether or not to have the load file option. True (default) to show it, false to hide it</param>
        /// <param name="isStartUp">A bool telling if this is the startup call, which determines if closing this will shutdown the program. True if it is, false (default) if it is not</param>
        public async void NewGame(bool loadFileToo = true, bool isStartUp = false)
        {
            HitAreaStackPanel.IsEnabled    = true;
            TurnIdentifierLabel.Content    = "New game setup";
            TurnIdentifierLabel.Background = Brushes.LightYellow;
            PlayerShipAreaStackPanel.Children.Clear();
            HitAreaStackPanel.Children.Clear();
            LogTextBlock.Text = "";
            usedData          = new GameData();
            NewGameSettingsWindow ngsw = new NewGameSettingsWindow(loadFileToo);

            ngsw.AIChoiceComboBox.ItemsSource = new List <IAIModel>()
            {
                new EasyAI(usedData.PlayerShipsGrid),
                new MediumAI(usedData.PlayerShipsGrid),
                new HardAI(usedData.PlayerShipsGrid),
                new CheatingAI(usedData.PlayerShipsGrid),
            };
            bool?result = ngsw.ShowDialog();

            if ((result ?? false) == true)
            {
                if (ngsw.GameData != null)
                {
                    //implies that a file was loaded
                    usedData   = ngsw.GameData;
                    this.Title = "Battleship AI Level: " + usedData.ActiveAI.ToString();
                    TurnIdentifierLabel.Content    = "Your turn";
                    TurnIdentifierLabel.Background = Brushes.LawnGreen;
                    HitAreaStackPanel.IsEnabled    = true;
                }
                else
                {
                    usedData.ActiveAI = ngsw.ChoosenAI;
                    this.Title        = "Battleship AI Level: " + usedData.ActiveAI.ToString();
                    FillPlayerGrid();
                    HitAreaStackPanel.Children.Add(new TextBlock()
                    {
                        Text = "Left click to place ships into your area on the other, blue grid area\n" +
                               "Right click to rotate the ship\nThe game will automatically start when all ships are placed\n" +
                               "Placement collisions will be in red and ships cannot be placed if collisions are present" +
                               "\n\n\nClick in this area to shoot at the enemy when playing"
                    });
                    await PlayerPlacesShips(); //async because the ships should be placed before the enemy grid is filled up

                    FillEnemyGrid();
                    EnemyPlacesShips();
                }
                isGameRunning = true;
                usedData.ActiveAI.PlayerGrid = usedData.PlayerShipsGrid;
                LogStackPanel.DataContext    = usedData;
                Binding b = new Binding("LogInfo");
                LogTextBlock.SetBinding(TextBlock.TextProperty, b);
                FillGrids();
            }
            else
            {
                HitAreaStackPanel.Children.Add(new Label()
                {
                    Content = "Go to file and make a new game, or close the window"
                });
                PlayerShipAreaStackPanel.Children.Add(new Label()
                {
                    Content = "Go to file and make a new game or close the window"
                });
                TurnIdentifierLabel.Content    = "Make a new game";
                TurnIdentifierLabel.Background = Brushes.Orange;
                if (isStartUp)
                {
                    Close();
                }
            }
        }
Beispiel #3
0
 private void Clear_Click(object sender, RoutedEventArgs e)
 {
     LogTextBlock.Clear();
 }