private bool left(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 < 0) { //If true guess to the right of the original location return(right(x, y, attackSquare)); } attackSquare.setXLoc(tempX); attackSquare.setYLoc(y); if (attackSquare.getSquareState() == State.hit) { return(left(tempX, y, attackSquare)); } else if (shoot(attackSquare)) { if (checkIfSunk(attackSquare, horizontal)) { shipSunk = true; shipHit = false; } return(true); } else { return(false); } }
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); }
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; } }
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; }
/// <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); }
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); }
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; } }
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); } }
/// <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); }
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); } }
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); } }
/// <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; }
/// <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; }
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; } }
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); } }
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; }
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; }
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)); }
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; } }