Exemplo n.º 1
0
 public void setPlayer(Player newPlayer)
 {
     player = newPlayer;
 }
Exemplo n.º 2
0
 void shotResolution(int sx, int sy, Player target)
 {
     bool hit = false;
     for(int shipnum = 0; shipnum < 5; shipnum++)
     {
         for(int ss = 0; ss < target.getShips()[shipnum].Length; ss++)
         {
             if(sx==target.getShips()[shipnum].Position[ss].getXLoc()&& sy == target.getShips()[shipnum].Position[ss].getYLoc())
             {
                 //hit
                 hit = true;
                 if (target == computer)
                 {
                     turnLabel.Text = "Hit on enemy!";
                     playerScore++;
                     playerScoreLabel.Text = "Your Score: " + playerScore + "/17";
                     enemyGridButtons[sy][sx].BackColor = Color.Red;
                 }
                 else
                 {
                     turnLabel.Text = "Enemy hit you!";
                     compScore++;
                     label42.Text = "Enemy Score: " + compScore + "/17";
                     playerGridButtons[sy][sx].BackColor = Color.Red;
                 }
                 target.getShips()[shipnum].Position[ss].setState(0);
                 int hitcount = 0;
                 for(int i = 0; i < target.getShips()[shipnum].Length; i++)
                 {
                     if (target.getShips()[shipnum].Position[i].getSquareState() == 0)
                     {
                         hitcount++;
                     }
                 }
                 if (hitcount == target.getShips()[shipnum].Length)
                 {
                     target.getShips()[shipnum].IsSunk = true;
                     if (target == computer)
                     {
                         turnLabel.Text = "Enemy ship sunk!";
                     }
                     else
                     {
                         turnLabel.Text = "Your ship has sunk!";
                     }
                 }
                 if (playerScore > 16)
                 {
                     GameOver(true);
                 }
                 else if(compScore > 16)
                 {
                     GameOver(false);
                 }
             }
         }
     }
     if (!hit)
     {
         if (target == computer)
         {
             turnLabel.Text = "You Missed!";
             enemyGridButtons[sy][sx].BackColor = Color.White;
         }
         else
         {
             turnLabel.Text = "Enemy Missed!";
             playerGridButtons[sy][sx].BackColor = Color.White;
         }
     }
 }
Exemplo n.º 3
0
 public GameController()
 {
     playerTurn = 1;
     gameOver = false;
     player = new Player();
 }
Exemplo n.º 4
0
        void shotResolution(int sx, int sy, Player target)
        {
            bool hit = false;
            for(int shipnum = 0; shipnum < 5; shipnum++)
            {
                for(int ss = 0; ss < target.getShips()[shipnum].Length; ss++)
                {
                    if(sx==target.getShips()[shipnum].Position[ss].getXLoc()&& sy == target.getShips()[shipnum].Position[ss].getYLoc())
                    {
                        //hit
                        hit = true;
                        if (target == computer)
                        {
                            turnLabel.Text = "Hit on enemy!";
                            playerScore++;
                            playerScoreLabel.Text = "Your Score: " + playerScore + "/17";
                            enemyGridButtons[sy][sx].BackColor = Color.Red;

                        }
                        else
                        {
                            turnLabel.Text = "Enemy hit you at "+sx+","+sy;
                            compScore++;
                            Square temp = new Square();
                            temp.setXLoc(sx);
                            temp.setYLoc(sy);
                            computer.knownTargets.Add(temp);
                            label42.Text = "Enemy Score: " + compScore + "/17";
                            playerGridButtons[sy][sx].BackColor = Color.Red;

                        }
                        target.getShips()[shipnum].Position[ss].setState(0);
                        int hitcount = 0;
                        for(int i = 0; i < target.getShips()[shipnum].Length; i++)
                        {
                            if (target.getShips()[shipnum].Position[i].getSquareState() == 0)
                            {
                                hitcount++;
                            }
                        }
                        if (hitcount == target.getShips()[shipnum].Length)
                        {
                            target.getShips()[shipnum].IsSunk = true;
                            if (target == computer)
                            {
                                turnLabel.Text = "Enemy ship sunk!";
                            }
                            else
                            {
                                turnLabel.Text = "Your ship has sunk!";
                            }
                        }
                    }
                }
            }
            if (!hit)
            {
                if (target == computer)
                {
                    turnLabel.Text = "You Missed!";
                    enemyGridButtons[sy][sx].BackColor = Color.White;
                }
                else
                {
                    turnLabel.Text = "Enemy Missed!";
                    playerGridButtons[sy][sx].BackColor = Color.White;
                }
            }
            if (playerScore > 16)
            {
                GameOver(true);
            }
            else if (compScore > 16)
            {
                GameOver(false);
            }
            if (target == computer && playerScore < 17)
            {
                controller.setPlayerTurn(2);
                myTimer = new System.Windows.Forms.Timer();
                myTimer.Tick += new EventHandler(aiSelection);
                myTimer.Interval = 1000;
                myTimer.Start();
            }
            else
            {
                controller.setPlayerTurn(1);

            }
        }