// class method to switch sides of square (up <=> down) public void upsideDown() { var temp = this.Front; this.Front = this.Back; this.Back = temp; }
public static int GetOccupiedSquares(Square.Mark type) { switch (type) { case Mark.CARRIER: return(5); case Mark.BATTLESHIP: return(4); case Mark.CRUISER: return(3); case Mark.SUBMARINE: return(3); case Mark.DESTROYER: return(2); } return(-1); }
public void updateAfterHit(Square.Mark shipType) { if (shipType == Square.Mark.CARRIER) { shotsToCarrier--; } else if (shipType == Square.Mark.BATTLESHIP) { shotsToBattleship--; } else if (shipType == Square.Mark.CRUISER) { shotsToCruiser--; } else if (shipType == Square.Mark.SUBMARINE) { shotsToSubmarine--; } else if (shipType == Square.Mark.DESTROYER) { shotsToDestroyer--; } }
public bool verifyIfSunk(Square.Mark shipType) { if (shipType == Square.Mark.CARRIER) { return(shotsToCarrier == 0); } else if (shipType == Square.Mark.BATTLESHIP) { return(shotsToBattleship == 0); } else if (shipType == Square.Mark.CRUISER) { return(shotsToCruiser == 0); } else if (shipType == Square.Mark.SUBMARINE) { return(shotsToSubmarine == 0); } else if (shipType == Square.Mark.DESTROYER) { return(shotsToDestroyer == 0); } return(false); }
public bool DebugPutRandomlyShip(Square.Mark type) { bool horizontal = false; int starty, endy, startx, endx, initx, inity, initsize; if (random.Next(2) == 1) { horizontal = true; } ; int x = random.Next(10); int y = random.Next(10); initx = x; inity = y; int size = Square.GetOccupiedSquares(type); initsize = size; if (horizontal && initx + initsize > 9) { return(false); } if (!horizontal && inity + initsize > 9) { return(false); } if (horizontal) { //rysujemy na "wysokosci" x wzdluz y (po kazdym y) starty = endy = inity; startx = endx = initx; if (initx > 0) { startx = initx - 1; } if (initx + size < 9) { endx = initx + size + 1; } if (inity > 0) { starty = inity - 1; } if (inity < 9) { endy = inity + 1; } for (int cy = starty; cy < endy; cy++) { for (int cx = startx; cx < endx; cx++) { if (!Board[cx, cy].IsAvailable()) { return(false); } } } } else { //rysujemy na "wysokosci" y wzdluz x (po kazdym x) startx = endx = initx; starty = endy = inity; if (inity > 0) { starty = starty - 1; } if (inity + size < 9) { endy = inity + size + 1; } if (initx > 0) { startx = initx - 1; } if (initx < 9) { endx = initx + 1; } for (int cx = startx; cx < endx; cx++) { for (int cy = starty; cy < endy; cy++) { if (!Board[cx, cy].IsAvailable()) { return(false); } } } } if (horizontal) { for (int cx = initx; cx < initsize + initx; cx++) { Board[cx, inity].SetMark(type); } } else { for (int cy = inity; cy < initsize + inity; cy++) { Board[initx, cy].SetMark(type); } } return(true); }
/* * public bool IsVisible(){ * return this.Visible; * }*/ public Square() { this.Front = Mark.WATER; this.Back = Mark.NOT_SET; this.hasBeenShot = false; }
public void SetMark(Square.Mark value) { this.Back = value; }
public void SetFront(Square.Mark value) { this.Front = value; }
public void setVisible() { this.Front = this.Back; }
/* * public bool IsVisible(){ * return this.Visible; * }*/ public Square() { this.Front = Mark.WATER; this.Back = Mark.NOT_SET; }
static bool ShootToShip(Player[] players) { int j, x, y; bool allShipsSunk = false; Square currentShot; for (int i = 0; i < 2; i++) { if (i == 0) { j = 1; } else { j = 0; } // PRINT FRONT OF ENEMY'S OCEAN Console.Clear(); System.Console.WriteLine($"{players[i].Name} - {players[i].Type} is shooting"); players[j].MyOcean.DebugOceanFront(); // GET POSITION FROM SHOOTING PLAYER Console.WriteLine("Tell me where do you want to shoot"); position = Ocean.GetShipPosition(players[i].Type); x = position[1]; y = position[0]; // CHECK IF SHOOT IS HIT/MISS currentShot = players[j].MyOcean.Board[x, y]; if (currentShot.hasBeenShot == false) { currentShot.hasBeenShot = true; if (currentShot.GetMark() != Square.Mark.NOT_SET) { currentShot.SetFront(Square.Mark.HIT); System.Console.WriteLine("You got it! :D HIT"); Thread.Sleep(3000); Square.Mark shipType = currentShot.GetMark(); players[j].MyOcean.updateAfterHit(shipType); if (players[j].MyOcean.verifyAllShipsSunk()) { allShipsSunk = true; Console.Clear(); Thread.Sleep(1000); System.Console.WriteLine($"{players[i].Name} - YOU WON! CONGRATS!"); Thread.Sleep(5000); return(allShipsSunk); } else if (players[j].MyOcean.verifyIfSunk(shipType) == true) { System.Console.WriteLine($"You shot {shipType}"); Thread.Sleep(3000); for (int row = 0; row < 10; row++) { for (int col = 0; col < 10; col++) { if (players[j].MyOcean.Board[row, col].GetMark() == shipType) { players[j].MyOcean.Board[row, col].SetFront(Square.Mark.SUNK); } } } } } else { currentShot.SetFront(Square.Mark.MISSED); System.Console.WriteLine("Oh no... MISS but do not worry, you will have another chance"); Thread.Sleep(3000); } } } return(allShipsSunk); }
public bool DebugPutShip(Square.Mark type, bool isShipHorizontal, int[] position) { int positionX = position[1]; int positionY = position[0]; int initx = positionX; int inity = positionY; int size = Square.GetOccupiedSquares(type); var startX = positionX; if (startX > 0) { startX--; } var startY = positionY; if (startY > 0) { startY--; } var endX = positionX; var endY = positionY; if (!isShipHorizontal) { endY += size - 1; } else { endX += size - 1; } // if end point is not the last coordinate check one past it. if (endY < 9) { endY++; } if (endX < 9) { endX++; } if (!IsInsideBoard(9, startX, startY, endX, endY)) { return(false); } for (int cy = startY; cy <= endY; cy++) { for (int cx = startX; cx <= endX; cx++) { if (!Board[cx, cy].IsAvailable()) { return(false); } } } if (isShipHorizontal) { for (int cx = initx; cx < size + initx; cx++) { Board[cx, inity].SetMark(type); } } else { for (int cy = inity; cy < size + inity; cy++) { Board[initx, cy].SetMark(type); } } return(true); }