Exemple #1
0
 public void SetLog(string log)
 {
     Task.Factory.StartNew(() => {
         Log.Text += log;
         LogScrollView.ScrollToBottom();
     }, new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
 }
Exemple #2
0
 private void SetLogAsync(string log)
 {
     Task.Factory.StartNew(() => {
         Log.Text += log;
         LogScrollView.ScrollToBottom();
     }, new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()).Wait();
 }
        /// <summary>
        /// Fills the HitAreaStackPanel with rectangles representing Tiles in the Enemy's Playgrid
        /// This method will clear and rebuild the HitStackPanel if called multiple times
        /// </summary>
        public void FillEnemyGrid()
        {
            HitAreaStackPanel.Children.Clear();
            for (int i = 0; i < usedData.EnemyShipsGrid.Width; i++)
            {
                int        x  = i;
                StackPanel sp = new StackPanel();
                for (int j = 0; j < usedData.EnemyShipsGrid.Height; j++)
                {
                    int       y = j;
                    Rectangle r = new Rectangle()
                    {
                        Height      = HitAreaStackPanel.MinHeight / usedData.EnemyShipsGrid.Height,
                        Width       = HitAreaStackPanel.MinWidth / usedData.EnemyShipsGrid.Width,
                        DataContext = usedData.EnemyShipsGrid.Grid[i, j],
                        RadiusX     = 5,
                        RadiusY     = 5,
                    };
                    Binding b = new Binding("State");
                    b.Converter = t2bObf;
                    r.SetBinding(Rectangle.FillProperty, b);
                    //Fire on click logic here
                    r.MouseLeftButtonDown += async(sender, args) =>
                    {
                        if (isGameRunning)
                        {
                            TileState aimed = ((Tile)r.DataContext).State; //need to lambda because of this line refering to the rectangle
                            if (aimed != TileState.Hit && aimed != TileState.Missed)
                            {
                                TurnIdentifierLabel.Content    = "Enemy Turn";
                                TurnIdentifierLabel.Background = Brushes.IndianRed; //Make the turn label very obvious
                                HitAreaStackPanel.IsEnabled    = false;             //because of Delay(), the player could make another turn, so disable the stackpanel to prevent this from calling from another rectangle
                                bool youHit = usedData.Shoot(x, y, usedData.EnemyShipsGrid);
                                usedData.LogInfo = usedData.LogInfo + $"The Player hit point {ToBattleshipPoint(x, y)}";
                                usedData.LogInfo = usedData.LogInfo + (youHit ? "\nThe Player hits a ship!\n" : "\n");
                                Ship youMaybeHit = usedData.GetJustSank(usedData.EnemyShips);
                                if (youMaybeHit != null)
                                {
                                    usedData.LogInfo = usedData.LogInfo + $" !! The Player sank the enemy's {youMaybeHit.Name}\n";
                                    MessageBox.Show($"You just sank the enemy's {youMaybeHit.Name}!");
                                }
                                bool playerWin = GameEnd();

                                if (!playerWin)                           //AI moves if the game didnt end ie the player did not win
                                {
                                    await Delay(new Random().Next(1500)); //Random time to delay to make the AI seem like its 'thinking'

                                    Logical.Point p        = usedData.ActiveAI.ChoosePoint();
                                    bool          enemyHit = usedData.Shoot(p.X, p.Y, usedData.PlayerShipsGrid);
                                    usedData.LogInfo = usedData.LogInfo + $"The Enemy hit point {ToBattleshipPoint(p.X, p.Y)}";
                                    usedData.LogInfo = usedData.LogInfo + (enemyHit ? "\nThe Enemy hits a ship!\n" : "\n");
                                    Ship enemyMaybeHit = usedData.GetJustSank(usedData.PlayerShips);
                                    if (enemyMaybeHit != null)
                                    {
                                        usedData.LogInfo = usedData.LogInfo + $" !! The Enemy sank the player's {enemyMaybeHit.Name}\n";
                                        MessageBox.Show($"The enemy sank your {enemyMaybeHit.Name}!");
                                    }
                                    LogScrollView.ScrollToBottom();

                                    TurnIdentifierLabel.Content    = "Your turn";
                                    TurnIdentifierLabel.Background = Brushes.LawnGreen;
                                    HitAreaStackPanel.IsEnabled    = true;
                                    GameEnd();
                                }
                            }
                            else
                            {
                                MessageBox.Show("You already fired here");
                            }
                        }
                    };
                    r.MouseEnter += (sender, args) =>
                    {
                        hitAreaMouseX = x;
                        hitAreaMouseY = y;
                    };
                    sp.Children.Add(r);
                }
                HitAreaStackPanel.Children.Add(sp);
            }
        }