private void btnReset_Click(object sender, EventArgs e) { rtbLog.Text = introString; ResetGrid(PlayerGrid); //Re-enable the buttons //Destroyer btnDestroyer.Enabled = true; btnDestroyer.Text = "Destroyer"; btnDestroyer.Visible = true; //Cruiser btnCruiser.Enabled = true; btnCruiser.Text = "Cruiser"; btnCruiser.Visible = true; //Submarine btnSubmarine.Enabled = true; btnSubmarine.Text = "Submarine"; btnSubmarine.Visible = true; //Battleship btnBattleship.Enabled = true; btnBattleship.Text = "Battleship"; btnBattleship.Visible = true; //Carrier btnCarrier.Enabled = true; btnCarrier.Text = "Carrier"; btnCarrier.Visible = true; btnSubmit.Enabled = false; selectedShipType = ShipType.Default; shipIsHorizontal = false; shipPositions = new GridPosition[5, 2]; destroyer = null; cruiser = null; submarine = null; battleship = null; carrier = null; }
private bool PlaceShip(ShipType ship) { //Check that begin and end points of ship are different if (shipPositions[(int)ship, 0] == shipPositions[(int)ship, 1]) { return false; } //check that selected points are unique for (int r = 0; r < 5; r++) { for (int c = 0; c < 2; c++) { if ((int)ship != r && (shipPositions[(int)ship, 0] == shipPositions[r, c] || shipPositions[(int)ship, 1] == shipPositions[r, c])) { return false; } } } //Check to ensure that ship is in a straight line if (shipPositions[(int)ship, 0].x == shipPositions[(int)ship, 1].x) //Ship is vertical { shipIsHorizontal = false; } else if (shipPositions[(int)ship, 0].y == shipPositions[(int)ship, 1].y) //Ship is horizontal { shipIsHorizontal = true; } else return false; switch (ship) { case ShipType.Destroyer: if (shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].x - shipPositions[(int)ship, 1].x)) != 1) { return false; } } else if (!shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].y - shipPositions[(int)ship, 1].y)) != 1) { return false; } } destroyer = new Ship(ShipType.Destroyer, shipPositions[(int)ship, 0], shipPositions[(int)ship, 1], 2, shipIsHorizontal); if (CheckForShipCollisions(destroyer) == true) return false; break; case ShipType.Cruiser: if (shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].x - shipPositions[(int)ship, 1].x)) != 2) { return false; } } else if (!shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].y - shipPositions[(int)ship, 1].y)) != 2) { return false; } } cruiser = new Ship(ShipType.Cruiser, shipPositions[(int)ship, 0], shipPositions[(int)ship, 1], 3, shipIsHorizontal); if (CheckForShipCollisions(cruiser) == true) return false; break; case ShipType.Submarine: if (shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].x - shipPositions[(int)ship, 1].x)) != 2) { return false; } } else if (!shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].y - shipPositions[(int)ship, 1].y)) != 2) { return false; } } submarine = new Ship(ShipType.Submarine, shipPositions[(int)ship, 0], shipPositions[(int)ship, 1], 3, shipIsHorizontal); if (CheckForShipCollisions(submarine) == true) return false; break; case ShipType.Battleship: if (shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].x - shipPositions[(int)ship, 1].x)) != 3) { return false; } } else if (!shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].y - shipPositions[(int)ship, 1].y)) != 3) { return false; } } battleship = new Ship(ShipType.Battleship, shipPositions[(int)ship, 0], shipPositions[(int)ship, 1], 4, shipIsHorizontal); if (CheckForShipCollisions(battleship) == true) return false; break; case ShipType.Carrier: if (shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].x - shipPositions[(int)ship, 1].x)) != 4) { return false; } } else if (!shipIsHorizontal) { if (Math.Abs((shipPositions[(int)ship, 0].y - shipPositions[(int)ship, 1].y)) != 4) { return false; } } carrier = new Ship(ShipType.Carrier, shipPositions[(int)ship, 0], shipPositions[(int)ship, 1], 5, shipIsHorizontal); if (CheckForShipCollisions(carrier) == true) return false; break; } return true; }
private bool CheckForShipCollisions(Ship ship) { if (ship.type != ShipType.Destroyer && destroyer != null) { for (int o = 0; o < destroyer.occupiedSquares.Length; o++) { for (int s = 0; s < ship.occupiedSquares.Length; s++) { if (ship.occupiedSquares[s].x == destroyer.occupiedSquares[o].x && ship.occupiedSquares[s].y == destroyer.occupiedSquares[o].y) { return true; } } } } if (ship.type != ShipType.Cruiser && cruiser != null) { for (int o = 0; o < cruiser.occupiedSquares.Length; o++) { for (int s = 0; s < ship.occupiedSquares.Length; s++) { if (ship.occupiedSquares[s].x == cruiser.occupiedSquares[o].x && ship.occupiedSquares[s].y == cruiser.occupiedSquares[o].y) { return true; } } } } if (ship.type != ShipType.Submarine && submarine != null) { for (int o = 0; o < submarine.occupiedSquares.Length; o++) { for (int s = 0; s < ship.occupiedSquares.Length; s++) { if (ship.occupiedSquares[s].x == submarine.occupiedSquares[o].x && ship.occupiedSquares[s].y == submarine.occupiedSquares[o].y) { return true; } } } } if (ship.type != ShipType.Battleship && battleship != null) { for (int o = 0; o < battleship.occupiedSquares.Length; o++) { for (int s = 0; s < ship.occupiedSquares.Length; s++) { if (ship.occupiedSquares[s].x == battleship.occupiedSquares[o].x && ship.occupiedSquares[s].y == battleship.occupiedSquares[o].y) { return true; } } } } if (ship.type != ShipType.Carrier && carrier != null) { for (int o = 0; o < carrier.occupiedSquares.Length; o++) { for (int s = 0; s < ship.occupiedSquares.Length; s++) { if (ship.occupiedSquares[s].x == carrier.occupiedSquares[o].x && ship.occupiedSquares[s].y == carrier.occupiedSquares[o].y) { return true; } } } } return false; }
private void CalculateShipPositions(string ship, ShipType type, int length, int clientNo) { string[] points = new string[2]; points[0] = ship.Split('.')[0]; points[1] = ship.Split('.')[1]; bool isHorizontal = false; GridPosition startPos = new GridPosition(int.Parse(points[0].Split(',')[0]), int.Parse(points[0].Split(',')[1])); GridPosition endPos = new GridPosition(int.Parse(points[1].Split(',')[0]), int.Parse(points[1].Split(',')[1])); if (startPos.x == endPos.x)//Ship is vertical { isHorizontal = false; } else if (startPos.y == endPos.y)//Ship is horizontal { isHorizontal = true; } Ship tempShip = new Ship(type, startPos, endPos, length, isHorizontal); for (int i = 0; i < tempShip.occupiedSquares.Length; i++) { if (clientNo == 0) { client1TempPositions.Add(tempShip.occupiedSquares[i]); } else if (clientNo == 1) { client2TempPositions.Add(tempShip.occupiedSquares[i]); } } }