コード例 #1
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        public void hit(int x, int y, Square attackSquare)
        {
            Random rand = new Random();
            int guess = rand.Next(1001) % 4;

            /// <summary>
            /// When up/down/left/right returns false, the guess shot was already attacked.
            ///  Guess a new shot
            /// </summary>
            switch (guess)
            {
                case 0:
                    if (!up(x, y, attackSquare)) hit(x, y, attackSquare);
                    break;
                case 1:
                    if (!down(x, y, attackSquare)) hit(x, y, attackSquare);
                    break;
                case 2:
                    if (!left(x, y, attackSquare)) hit(x, y, attackSquare);
                    break;
                case 3:
                    if (!right(x, y, attackSquare)) hit(x, y, attackSquare);
                    break;
            }
        }
コード例 #2
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
 public void TestShotDefaultConstructor()
 {
     Shot shot = new Shot();
     Square square = new Square();
     Assert.IsTrue(shot.getTarget().getXLoc() == square.getXLoc());
     Assert.IsTrue(shot.getTarget().getYLoc() == square.getYLoc());
     Assert.IsTrue(shot.getTarget().getSquareState() == square.getSquareState());
 }
コード例 #3
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        public void attackEnemy()
        {
            Square attackSquare = new Square();

            if (shipHit) hit(hitXLoc, hitYLoc, attackSquare);
            else if (shipSunk) miss(attackSquare);
            else miss(attackSquare);
        }
コード例 #4
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
 public void TestShotGettersAndSetters()
 {
     Shot shot = new Shot();
     Square square = new Square();
     shot.setTarget(square);
     Assert.IsTrue(shot.getTarget().getXLoc() == square.getXLoc());
     Assert.IsTrue(shot.getTarget().getYLoc() == square.getYLoc());
     Assert.IsTrue(shot.getTarget().getSquareState() == square.getSquareState());
 }
コード例 #5
0
ファイル: AIPlayer.cs プロジェクト: ninoaguilar/Battleship
 public Square makeRandomTarget()
 {
     Random r = new Random();
     Square target = new Square();
     bool valid = false;
     while (!valid)
     {
         target.setXLoc(r.Next() % 10);
         target.setYLoc(r.Next() % 10);
         if (!firedOn[target.getXLoc(), target.getYLoc()])
         {
             valid = true;
         }
     }
     return target;
 }
コード例 #6
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        public void miss(Square attackSquare)
        {
            int tempXLoc, tempYloc;
            Random rnd = new Random();
            tempXLoc = rnd.Next(10);

            /// <summary>
            /// AI will fire at a random square. Guessed squares will be every other square
            /// to eliminate guesses next to each other. Square locations will be;
            /// x = even, y = odd or vice-versa
            /// </summary>
            if (tempXLoc % 2 == 0) tempYloc = 1 + 2 * rnd.Next(5);
            else tempYloc = 2 * rnd.Next(5);

            attackSquare.setXLoc(tempXLoc);
            attackSquare.setYLoc(tempYloc);

            /// <summary>
            /// Check if attackSquare shot at an empty square. If shot hits a square that
            /// has already been attacked guess a different shot.
            /// </summary>
            if(!shoot(attackSquare))
            {
                miss(attackSquare);
            }
            /// <summary>
            /// From previous if statement, shoot function was called. attackSquare is
            /// either hit/miss. If hit save hit location and change bool values.
            /// </summary>
            else if(attackSquare.getSquareState() == State.hit)
            {
                shipHit = true;
                shipSunk = false;
                hitXLoc = tempXLoc;
                hitYLoc = tempYloc;
            }
            /// <summary>
            /// From previous if statments; attackSquare was a miss.
            /// </summary>
            else
            {
                //Do nothing; Accept Defeat
                return;
            }
        }
コード例 #7
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
        public void TestSquareGetterAndSetter()
        {
            Square square = new Square();
            Random r = new Random();
            int n = r.Next(-1, 100);
            int m = r.Next(-1, 100);

            square.setXLoc(n);
            square.setYLoc(m);
            square.setState(State.hit);
            Assert.IsTrue(square.getXLoc().Equals(n));
            Assert.IsTrue(square.getYLoc().Equals(m));
            Assert.IsTrue(square.getSquareState().Equals(State.hit));
        }
コード例 #8
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
 public void TestShotNonDefaultConstructor()
 {
     Square square = new Square();
     Shot shot = new Shot(square);
     Assert.IsTrue(shot.getTarget() == square);
 }
コード例 #9
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
 public void TestSquareDefaultConstructor()
 {
     Square square = new Square();
     Assert.IsTrue(square.getXLoc().Equals(0));
     Assert.IsTrue(square.getXLoc().Equals(0));
     Assert.IsTrue(square.getSquareState().Equals(State.empty));
 }
コード例 #10
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        // Check if ship being attack has been sunk
        private bool checkIfSunk(Square attackSquare, bool horizontal)
        {
            if (horizontal)
            {
                return checkHorizontal(hitXLoc, hitYLoc, attackSquare);
            }

            return checkVertical(hitXLoc, hitYLoc, attackSquare);
        }
コード例 #11
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        /// <summary>
        /// Check horizontal status of ship being attacked.
        /// A sunken ship will have miss on both ends of the ship 
        /// If the ship is at the edge the ship will have a miss on the opposite end
        /// </summary>
        private bool checkHorizontal(int x, int y, Square checkSquare)
        {
            int sizeOfLargestShip = 5;
            int tempX = x;

            for (int i = 0; i <= sizeOfLargestShip; i++)
            {
                tempX -= i;
                if (tempX < 0)
                {
                    tempX = 0;
                    break;
                }
                checkSquare.setXLoc(tempX);
                checkSquare.setYLoc(y);

                if (checkSquare.getSquareState() != State.hit)
                {
                    break;
                }
            }

            for (int i = 0; i <= sizeOfLargestShip; i++)
            {
                checkSquare.setYLoc(tempX + i);
                if (checkSquare.getSquareState() == State.miss)
                {
                    return true;
                }
                else if (checkSquare.getSquareState() != State.hit)
                {
                    return false;
                }
            }

            return false;
        }
コード例 #12
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
        public void TestSquareNotOccupiedFireHere()
        {
            Square square = new Square();

            Assert.IsFalse(square.fireHere());
        }
コード例 #13
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
        public void TestSquareOccupiedFireHere()
        {
            Square square = new Square();
            square.setState(State.miss);

            Assert.IsTrue(square.fireHere());
        }
コード例 #14
0
ファイル: Shot.cs プロジェクト: ninoaguilar/Battleship
 public void setTarget(Square square)
 {
     target = square;
 }
コード例 #15
0
ファイル: AIPlayer.cs プロジェクト: ninoaguilar/Battleship
 public Square makeSmartTarget()
 {
     Square target = new Square();
     target.setXLoc(-1);
     target.setYLoc(-1);
     bool needSelection = true;
     while (needSelection&&knownTargets.Count>0)
     {
         Random r = new Random();
         int t = r.Next() % knownTargets.Count();
         Square known = knownTargets[t];
         needSelection = false;
         if (known.getXLoc() > 0 && !firedOn[known.getXLoc() - 1, known.getYLoc()])
         {
             target.setXLoc(known.getXLoc() - 1);
             target.setYLoc(known.getYLoc());
         }
         else if (known.getXLoc() < 9 && !firedOn[known.getXLoc() + 1, known.getYLoc()])
         {
             target.setXLoc(known.getXLoc() + 1);
             target.setYLoc(known.getYLoc());
         }
         else if (known.getYLoc() > 0 && !firedOn[known.getXLoc(), known.getYLoc() - 1])
         {
             target.setYLoc(known.getYLoc() - 1);
             target.setXLoc(known.getXLoc());
         }
         else if (known.getYLoc() < 9 && !firedOn[known.getXLoc(), known.getYLoc() + 1])
         {
             target.setYLoc(known.getYLoc() + 1);
             target.setXLoc(known.getXLoc());
         }
         else
         {
             knownTargets.RemoveAt(t);
             needSelection = true;
         }
     }
     return target;
 }
コード例 #16
0
ファイル: Shot.cs プロジェクト: ninoaguilar/Battleship
        private Square target; //Square that is being attacked

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Default constructor, Unlikely to ever be used
        /// </summary>
        public Shot()
        {
            target = new Square();
        }
コード例 #17
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        /// <summary>
        /// Check vertical status of ship being attacked.
        /// A sunken ship will have miss on both ends of the ship 
        /// If the ship is at the edge the ship will have a miss on the opposite end
        /// </summary>
        private bool checkVertical(int x, int y, Square checkSquare)
        {
            int sizeOfLargestShip = 5;
            int tempY = y;

            //Looking for the end of the ship
            for(int i = 0; i <= sizeOfLargestShip; i++)
            {
                tempY -= i;
                if (tempY < 0)
                {
                    tempY = 0;
                    break;
                }
                checkSquare.setXLoc(x);
                checkSquare.setYLoc(tempY);

                //If the square being checked is not a hit, it must be the end of the ship
                if(checkSquare.getSquareState() != State.hit)
                {
                    break;
                }
            }

            //Starting from the end of the ship and check for a miss on both ends
            for(int i = 0; i <= sizeOfLargestShip; i++)
            {
                checkSquare.setYLoc(tempY + i);

                /// <summary>
                /// Starts from the end of ship
                /// If square status is a hit ignore
                /// If square status is a miss it is the end of the ship
                /// If square status is a not a hit or miss the ship has not been sunk
                /// </summary>
                if (checkSquare.getSquareState() == State.miss)
                {
                    return true;
                }
                else if (checkSquare.getSquareState() != State.hit)
                {
                    return false;
                }
            }

            return false;
        }
コード例 #18
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        private bool down(int x, int y, Square attackSquare)
        {
            horizontal = false;
            int tempY = y++; //Shift square guess down one

            /// <summary>
            /// Check to see if y value is within the gameboard.
            /// </summary>
            if (tempY >= 10)
            {
                //If true guess one up of the original location
                return up(x, y, attackSquare);
            }

            attackSquare.setXLoc(x);
            attackSquare.setYLoc(tempY);

            //If the square being attacked has already been hit, keep guessing down one
            if (attackSquare.getSquareState() == State.hit)
            {
                return down(x, tempY, attackSquare);
            }
            else if (shoot(attackSquare)) //Attacking a square not yet guessed
            {
                if (checkIfSunk(attackSquare, horizontal))
                {
                    shipSunk = true;
                    shipHit = false;
                }
                return true;
            }
            else
            {
                //shoot function shot at a square that has already been attacked
                return false;
            }
        }
コード例 #19
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        private bool up(int x, int y, Square attackSquare)
        {
            horizontal = false;
            int tempY = y--; //Shift square guess up one

            /// <summary>
            /// Check to see if Y value is within the gameboard.
            /// </summary>
            if (tempY < 0)
            {
                //If true, guess down of the original location
                return down(x, y, attackSquare);
            }

            attackSquare.setXLoc(x);
            attackSquare.setYLoc(tempY);

            if (attackSquare.getSquareState() == State.hit)
            {
                return up(x, tempY, attackSquare);
            }
            else if (shoot(attackSquare))
            {
                if(checkIfSunk(attackSquare, horizontal))
                {
                    shipSunk = true;
                    shipHit = false;
                }

                return true;
            }
            else
            {
                return false;
            }
        }
コード例 #20
0
ファイル: AIPlayer.cs プロジェクト: brandt19/Battleship
        private bool right(int x, int y, Square attackSquare)
        {
            horizontal = true;
            int tempX = x++; //Shift square guess over one to the right

            /// <summary>
            /// Check to see if X value is within the gameboard.
            /// </summary>
            if (tempX >= 10)
            {
                //If true guess  to the left of the original location
                return left(x, y, attackSquare);
            }

            attackSquare.setXLoc(tempX);
            attackSquare.setYLoc(y);

            if (attackSquare.getSquareState() == State.hit)
            {
                return right(tempX, y, attackSquare);
            }
            else if (shoot(attackSquare))
            {
                if (checkIfSunk(attackSquare, horizontal))
                {
                    shipSunk = true;
                    shipHit = false;
                }
                return true;
            }
            else return false;
        }
コード例 #21
0
ファイル: Shot.cs プロジェクト: ninoaguilar/Battleship
 public Shot(Square square)
 {
     target = square;
 }
コード例 #22
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
        public void TestSquareIsOccuppied()
        {
            Square square = new Square();
            square.setState(State.miss);

            Assert.IsTrue(square.isOccuppied());
        }
コード例 #23
0
ファイル: Form1.cs プロジェクト: ninoaguilar/Battleship
        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);

            }
        }
コード例 #24
0
ファイル: ObjectTests.cs プロジェクト: ninoaguilar/Battleship
        public void TestSquareNonDefaultConstructor()
        {
            Random r = new Random();
            int n = r.Next(-1, 100);
            int m = r.Next(-1, 100);

            Square square = new Square(n, m);
            Assert.IsTrue(square.getXLoc().Equals(n));
            Assert.IsTrue(square.getYLoc().Equals(m));
            Assert.IsTrue(square.getSquareState().Equals(State.empty));
        }