예제 #1
0
        public void AttackComputer_Repeat_Works()
        {
            GameController ctrl = SetupBoards();

            ctrl.AttackComputer(new Location(0, 0));
            var result = ctrl.AttackComputer(new Location(0, 0));

            Assert.IsTrue(result == AttackResult.repeat);
        }
예제 #2
0
        public void AttackComputer_Miss_Works()
        {
            GameController ctrl   = SetupBoards();
            var            result = ctrl.AttackComputer(new Location(1, 0));

            Assert.IsTrue(result == AttackResult.Miss);
        }
예제 #3
0
        public void AttackComputer_Miss_Recorded()
        {
            GameController ctrl   = SetupBoards();
            var            result = ctrl.AttackComputer(new Location(1, 0));

            Assert.IsTrue(ctrl.ComputerBoard.Cells[1, 0] == CellType.Miss);
        }
예제 #4
0
        // Note: The only buttons that can be clicked are those that are in the
        // enemy's grid
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if (btn == null)
            {
                return;
            }

            Location loc = (Location)btn.Tag;
            Image    img = new Image();

            img.Height = btn.Height;
            img.Width  = btn.Width;

            Image cimg = new Image();

            cimg.Height = btn.Height;
            cimg.Width  = btn.Width;

            SoundPlayer player = new SoundPlayer();


            if (gameRunning)
            {
                // Update model to reflect player's attack
                AttackResult playerAttackResult = ctrl.AttackComputer(loc);

                if (playerAttackResult == AttackResult.Miss)
                {
                    txtFeedback.Text     = "you missed";
                    img.Source           = new BitmapImage(new Uri("Assets/miss.jpg", UriKind.Relative));
                    player.SoundLocation = "Assets/miss.wav";
                }
                else if (playerAttackResult == AttackResult.Hit)
                {
                    txtFeedback.Text     = "Wooh! You hit a ship!!";
                    img.Source           = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
                    player.SoundLocation = "Assets/hit.wav";
                }
                else if (playerAttackResult == AttackResult.Sink)
                {
                    txtFeedback.Text     = "You managed to sink a ship, good job.";
                    img.Source           = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
                    player.SoundLocation = "Assets/sink.wav";
                }

                if (playerAttackResult != AttackResult.repeat) //ignore instead of throwing exception
                {
                    btn.Content = img;
                    player.Load();
                    player.Play();
                }

                if (ctrl.IsGameOver())
                {
                    player.SoundLocation = "Assets/win.wav";
                    player.Load();
                    player.Play();
                    gameRunning = false;
                    EndGame("You");
                }
            }


            if (gameRunning)
            {
                // Determine computer's response
                ComputerAttackResult computerAttackResult = ctrl.AttackPlayer();

                if (computerAttackResult.Result == AttackResult.Miss)
                {
                    cimg.Source = new BitmapImage(new Uri("Assets/miss.jpg", UriKind.Relative));
                }
                else if (computerAttackResult.Result == AttackResult.Hit)
                {
                    cimg.Source = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
                }
                else if (computerAttackResult.Result == AttackResult.Sink)
                {
                    cimg.Source = new BitmapImage(new Uri("Assets/hit.png", UriKind.Relative));
                }

                for (int i = 0; i < playerGridButtons.Count; i++)
                {
                    if ((playerGridButtons[i].Tag as Location).Column == computerAttackResult.Col && (playerGridButtons[i].Tag as Location).Row == computerAttackResult.Row)
                    {
                        playerGridButtons[i].Content = cimg;
                        break;
                    }
                }

                if (ctrl.IsGameOver())
                {
                    player.SoundLocation = "Assets/lose.wav";
                    player.Load();
                    player.Play();
                    gameRunning = false;
                    EndGame("The computer");
                }
            }
        }