/// This method handles hits, determining which ships, if any, were hit and updating the instances accordingly. public int SquareHit(int posX, int posY, Human player, Rendering rendering) { int hitShipID; if (Map[posX, posY] != 0) //if the map square is a ship. { hitShipID = Map[posX, posY] - 1; if (Ships[hitShipID].ShipHit() == 0) { rendering.UpdateLog("Your shot hits!"); player.EnemyMap[posX, posY] = 2; } else { for (int count = 0; count < Ships[hitShipID].Length; count++) { player.EnemyMap[Ships[hitShipID].Coords[count, 0], Ships[hitShipID].Coords[count, 1]] = 3; } rendering.UpdateLog(Ships[hitShipID].Name + " destroyed!"); //player.EnemyMap[posX, posY] = 2; } return(0); } else { rendering.UpdateLog("Your shot misses!"); player.EnemyMap[posX, posY] = 1; return(-1); } }
static public void ArrangeCompleted(Rendering r) { r.UpdateLog("All ships placed!"); Console.BackgroundColor = ConsoleColor.Black; Console.SetCursorPosition(25, 5); Console.Write(" "); Console.SetCursorPosition(25, 4); Console.Write(" "); }
/// This method handles hits, determining which ships, if any, were hit and updating the instances accordingly. It will return a 0 if no ships were destroyed and a 1 if some were. public int SquareHit(int posX, int posY, Computer computer, Rendering rendering) { int hitShipID; if (Map[posX, posY] != 0 && Map[posX, posY] < 11) //if the map square is a ship. { hitShipID = Map[posX, posY] - 1; if (Ships[hitShipID].ShipHit() == 0) { computer.EnemyMap[posX, posY] = 2; Map[posX, posY] = 12; rendering.DrawGameScreens(this); rendering.UpdateLog("The enemy shot hits!"); } else { for (int count = 0; count < Ships[hitShipID].Length; count++) { computer.EnemyMap[Ships[hitShipID].Coords[count, 0], Ships[hitShipID].Coords[count, 1]] = 3; //Make it known to the computer that a ship has been destroyed. Map[Ships[hitShipID].Coords[count, 0], Ships[hitShipID].Coords[count, 1]] = 13; } //Map[posX, posY] = 13; rendering.DrawGameScreens(this); rendering.UpdateLog(Ships[hitShipID].Name + " destroyed!"); } return(0); } else { computer.EnemyMap[posX, posY] = 1; Map[posX, posY] = 11; rendering.DrawGameScreens(this); rendering.UpdateLog("The enemy shot misses!"); return(-1); } }
static private void PlaceShips(Human p, Rendering r) { int shipLength; int shipX, shipY; int userInput; bool shipPlaced; ShipOrientation so; for (int shipNumber = 0; shipNumber < 10; shipNumber++) { shipLength = p.Ships[shipNumber].Length; so = p.Ships[shipNumber].Orientation; shipX = 0; shipY = 0; shipPlaced = false; Console.BackgroundColor = ConsoleColor.Black; Console.SetCursorPosition(25, 4); Console.Write("(use arrows + space)"); Console.SetCursorPosition(25, 5); Console.Write("(r - rotate)"); r.UpdateLog("Place " + p.Ships[shipNumber].Name); while (!shipPlaced) { r.DrawGameScreens(p); r.DrawShipPlacement(shipX, shipY, shipLength, so); userInput = (int)Console.ReadKey(true).Key; switch (userInput) { case 82: //'r' - rotate if (so == ShipOrientation.Vertical) { if (shipX + shipLength > 10) { shipX = 10 - shipLength; } } else { if (shipY + shipLength > 10) { shipY = 10 - shipLength; } } p.Ships[shipNumber].Rotate(); so = p.Ships[shipNumber].Orientation; break; case 37: //left arrow if (shipX - 1 >= 0) { shipX--; } break; case 38: //up arrow if (shipY - 1 >= 0) { shipY--; } break; case 39: //right arrow if (so == ShipOrientation.Vertical) { if (shipX + 1 < 10) { shipX++; } } else { if (shipX + shipLength < 10) { shipX++; } } break; case 40: //down arrow if (so == ShipOrientation.Vertical) { if (shipY + shipLength < 10) { shipY++; } } else { if (shipY + 1 < 10) { shipY++; } } break; case 32: //Space bar if (ShipCollision(shipX, shipY, shipLength, so, p) == false) { for (int i = 0; i < shipLength; i++) { if (so == ShipOrientation.Vertical) { p.Map[shipX, shipY + i] = shipNumber + 1; //this will be a unique identifier in order to allow quick lookups of hit ships. } else { p.Map[shipX + i, shipY] = shipNumber + 1; //this will be a unique identifier in order to allow quick lookups of hit ships. } } p.Ships[shipNumber].PlaceShip(shipX, shipY, so); shipPlaced = true; } break; } } } r.UpdateLog("All ships placed!"); Console.BackgroundColor = ConsoleColor.Black; Console.SetCursorPosition(25, 5); Console.Write(" "); Console.SetCursorPosition(25, 4); Console.Write(" "); }
/// This method handles the player making a shot on the enemy ships. public int TakeShot(Computer computer, Rendering rendering) { int xSelection = 0; int ySelection = 0; bool shotFired = false; int userInput; int turn = -1; while (shotFired == false) { rendering.DrawGameScreens(this); rendering.UpdateLog("Select Target"); bool innerLoop = true; while (innerLoop) { userInput = (int)Console.ReadKey(true).Key; if (userInput < 75 && userInput > 64) //if the key pressed is a to j { xSelection = userInput - 65; //converts the keycode to an x co-ordinate; innerLoop = false; } } rendering.DrawTarget(this, xSelection); innerLoop = true; while (innerLoop) { userInput = (int)Console.ReadKey(true).Key; if (userInput < 58 && userInput > 47) //if the key pressed is 0 to 9 { ySelection = userInput - 48; innerLoop = false; } } rendering.DrawTarget(this, xSelection, ySelection); rendering.UpdateLog("Ready to Fire"); innerLoop = true; while (innerLoop) { userInput = (int)Console.ReadKey(true).Key; if (userInput == 32 || userInput == 13) //spacebar or enter { if (EnemyMap[xSelection, ySelection] != 0) { rendering.UpdateLog("Error: You've already fired at that square!"); } else { turn = computer.SquareHit(xSelection, ySelection, this, rendering); shotFired = true; } innerLoop = false; } else if (userInput == 8) //backspace { rendering.UpdateLog("Shot cancelled"); innerLoop = false; } } } return(turn); }