public AttackAction(BattleshipTeam attacker, BattleshipBoard board, int x, int y) { this.attacker = attacker; this.board = board; xCoord = x; yCoord = y; }
//Direction: 1 - Up, 3 - Down, 2 - Right, 4 - Left public void Hit(BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard, Game game) { int x = game.HitX; int y = game.HitY; if (x > 0 && x < secondPlayerBoard.MainBoard.GetLength(0) - 1) { if (y > 0 && y < secondPlayerBoard.MainBoard.GetLength(0) - 1) { ChangeCoordinates(ref y, ref x, dir); bool success = false; foreach (var cell in game.HitShip.CellsList) { if (x == cell.X && y == cell.Y) { cell.Destroy(); game.HitX = cell.X; game.HitY = cell.Y; success = true; break; } } ActionAfterHit(success, game, ref x, ref y, firstPlayerBoard, secondPlayerBoard); } } }
public Game() { board = new BattleshipBoard(); nobodyHasWonYet = true; p1Won = false; p2Won = false; }
public GameEngine() { board1 = new BattleshipBoard(); board2 = new BattleshipBoard(); player1 = new PlayerRandomAi(board1); player2 = new PlayerSearchingAi(board2); }
public bool IsOver(BattleshipBoard playerBoard) { int count = 0; for (int i = 0; i < playerBoard.ShipsList.Count; i++) { if (playerBoard.ShipsList[i].IsDestroyed()) count++; } return count == playerBoard.ShipsList.Count; }
static void Main(string[] args) { int boardsize; while (true) { Console.Clear(); try { Console.Write("Choose board size: "); boardsize = Convert.ToInt32(Console.ReadLine()); break; } catch (Exception) { Console.Clear(); Console.WriteLine("Incorrect input! \nPress any key to try again."); } Console.ReadKey(); } BattleshipBoard firstBoard = new BattleshipBoard(boardsize); BattleshipBoard secondBoard = new BattleshipBoard(boardsize); Random r = new Random(); firstBoard.AddShipToBoard(r); secondBoard.AddShipToBoard(r); Draw.DrawAllShips(firstBoard); Draw.DrawAllShips(secondBoard); ShowBoard(boardsize, firstBoard.MainBoard); ConsoleKeyInfo key; Game firstGame = new Game(new NoHitState()); Game secondGame = new Game(new NoHitState()); do { key = Console.ReadKey(); if (key.Key == ConsoleKey.RightArrow) { Console.Clear(); if (firstGame.IsOver(firstBoard) == true || secondGame.IsOver(secondBoard) == true) { GameOver(firstBoard, secondBoard, firstGame, secondGame); break; } firstGame.Hit(firstBoard, secondBoard); secondGame.Hit(secondBoard, firstBoard); Console.WriteLine("Main:"); ShowBoard(boardsize, firstBoard.MainBoard); Console.WriteLine("Hits:"); ShowBoard(boardsize, firstBoard.HitsBoard); } }while (key.Key != ConsoleKey.Escape); }
public static void DrawBoards(int boardsize, BattleshipBoard board) { board.MainBoard = new int[boardsize, boardsize]; board.HitsBoard = new int[boardsize, boardsize]; for (int i = 0; i < boardsize; i++) { board.MainBoard[i, i] = 0; board.HitsBoard[i, i] = 0; } }
public void ActionAfterHit(bool success, Game game, ref int x, ref int y, BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard) { if (success == true) { Draw.DrawHits(x, y, true, firstPlayerBoard, secondPlayerBoard); game.HitX = x; game.HitY = y; } else { Draw.DrawHits(x, y, false, firstPlayerBoard, secondPlayerBoard); } }
public static void DrawHits(int x, int y, bool success, BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard) { if (success) { firstPlayerBoard.HitsBoard[y, x] = 2; secondPlayerBoard.MainBoard[y, x] = 2; } else { firstPlayerBoard.HitsBoard[y, x] = 3; secondPlayerBoard.MainBoard[y, x] = 3; } }
public bool CheckSquare(int y, int x, BattleshipBoard board) { //--- Cell cellCenter = new Cell(x, y); Cell cellCenterUp = new Cell(x, y - 1); Cell cellCenterDown = new Cell(x, y + 1); Cell cellCenterLeft = new Cell(x - 1, y); Cell cellUpLeft = new Cell(x - 1, y - 1); Cell cellDownLeft = new Cell(x - 1, y + 1); Cell cellCenterRight = new Cell(x + 1, y); Cell cellUpRight = new Cell(x + 1, y - 1); Cell cellDownRight = new Cell(x + 1, y + 1); List <Cell> cells = new List <Cell>() { cellCenter, cellCenterDown, cellCenterLeft, cellCenterRight, cellCenterUp, cellDownLeft, cellUpLeft, cellUpRight, cellDownRight }; //--- bool success = false; bool check = true; if (board.ShipsList.Count != 0) { foreach (var ship in board.ShipsList) { foreach (var cell in ship.CellsList) { if (y > 0 && y < board.BoardSize - 1 && x > 0 && x < board.BoardSize - 1) { for (int i = 0; i < cells.Count; i++) { if (!(cell.X != cells[i].X || cell.Y != cells[i].Y)) { check = false; } } } } } success = check; } else { success = true; } return(success); }
public void ActionAfterDestroyCell(BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard, Game game, Ship enemyShip) { if (!enemyShip.IsDestroyed()) { game.State = new HitState(); } else { if (game.HitShip.IsDestroyed()) { Draw.DrawAfterKill(firstPlayerBoard, secondPlayerBoard, game.HitShip); } } }
public void TakeAShot(BattleshipBoard recievingEnd) { int row = 0; int col = 0; bool shotInvalid = true; while (shotInvalid) { GetCoordinates(out row, out col, recievingEnd.Equals(Player2)); // Check if coordinates are on board and not already used // if coordinates are valid, then board is automatically updated by CheckShot if (recievingEnd.CheckShot(row, col) != "bad_input") { shotInvalid = false; } } }
public void ActionAfterHit(bool success, Game game, ref int x, ref int y, BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard) { if (success == true) { if (game.HitShip.IsDestroyed()) { Draw.DrawAfterKill(firstPlayerBoard, secondPlayerBoard, game.HitShip); game.State = new NoHitState(); } } else { dir = ChangeDir(dir); x = game.HitX; y = game.HitY; } Draw.DrawHits(x, y, success, firstPlayerBoard, secondPlayerBoard); }
public static void GameOver(BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard, Game playerGame, Game enemyGame) { Console.Clear(); string player1Won = "Winner: Player1"; string player2Won = "Winner: Player2"; string finalString = "Game over! "; if (playerGame.IsOver(firstPlayerBoard)) { finalString += player2Won; } else if (enemyGame.IsOver(secondPlayerBoard)) { finalString += player1Won; } Console.WriteLine(finalString); Console.WriteLine("Player1:"); ShowBoard(firstPlayerBoard.BoardSize, firstPlayerBoard.MainBoard); Console.WriteLine("Player2:"); ShowBoard(secondPlayerBoard.BoardSize, secondPlayerBoard.MainBoard); }
public void PlaceShipsAuto(BattleshipBoard player) { // Something not quite right here... first ship might get placed twice int row = 0; int col = 0; int shipNum; char[] possibleDirections = { 'u', 'd', 'l', 'r' }; char direction; for (int i = 0; i < player.ShipHealth.Count; i++) { direction = possibleDirections[_randomizer.Next(0, 4)]; row = _randomizer.Next(0, player.SideLength); col = _randomizer.Next(0, player.SideLength); shipNum = i; if (!player.PlaceShipManual(row, col, shipNum, direction)) { i--; } } }
public static void DrawAllShips(BattleshipBoard board) { int x = 0; int y = 0; foreach (var ship in board.ShipsList) { foreach (var cell in ship.CellsList) { try { board.MainBoard[cell.Y, cell.X] = 1; x = cell.X; y = cell.Y; } catch { Console.WriteLine("" + cell.X + " " + cell.Y); } } } }
public void Hit(BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard, Game game) { Random rnd = new Random(); //int x = rnd.Next(0, firstPlayerBoard.BoardSize); //int y = rnd.Next(0, firstPlayerBoard.BoardSize); Cell randomCell = GenerateRandomCell(rnd, 0, firstPlayerBoard.BoardSize); int x = randomCell.X; int y = randomCell.Y; if (firstPlayerBoard.HitsBoard[y, x] != 2 && firstPlayerBoard.HitsBoard[y, x] != 3) { bool success = false; foreach (var enemyShip in secondPlayerBoard.ShipsList) { foreach (var cell in enemyShip.CellsList) { if (cell.X == x && cell.Y == y) { game.HitShip = enemyShip; cell.Destroy(); ActionAfterDestroyCell(firstPlayerBoard, secondPlayerBoard, game, enemyShip); success = true; break; } } } ActionAfterHit(success, game, ref x, ref y, firstPlayerBoard, secondPlayerBoard); } else { while (firstPlayerBoard.HitsBoard[y, x] == 2 || firstPlayerBoard.HitsBoard[y, x] == 3) { x = rnd.Next(0, firstPlayerBoard.BoardSize); y = rnd.Next(0, firstPlayerBoard.BoardSize); } } }
public static void Main(string[] args) { Console.Title = "Battleship!!!"; Console.WriteLine("Welcome to Battleship!! \r\n\r\n"); Console.WriteLine("What is your name? "); string name = System.Console.ReadLine(); Console.WriteLine(); BattleshipBoard b = new BattleshipBoard(); Player p = new Player(); p.Randomize(); while (p.getHitCount() < 17) { b.DisplayBoard(p.GetGrid()); p.AskCoordinates(); } Console.WriteLine("Congratulations!" + name + "! You Won \r\n"); Console.WriteLine("You missed: " + p.getMissCount() + "Times\r\n"); Console.WriteLine("Thanks for playing Battleship. Press enter to quit."); Console.ReadKey(true); }
public static void DrawAfterKill(BattleshipBoard firstPlayerBoard, BattleshipBoard secondPlayerBoard, Ship ship) { foreach (var cell in ship.CellsList) { if (ship.DeckCount != 1) { if (ship.Dir == Directions.UP || ship.Dir == Directions.DOWN) { DrawUpDownDirSurround(cell.X, cell.Y, firstPlayerBoard.HitsBoard); DrawUpDownDirSurround(cell.X, cell.Y, secondPlayerBoard.MainBoard); } else { DrawLeftRightDirSurround(cell.X, cell.Y, firstPlayerBoard.HitsBoard); DrawLeftRightDirSurround(cell.X, cell.Y, secondPlayerBoard.MainBoard); } } else { DrawSquare(cell.X, cell.Y, firstPlayerBoard.HitsBoard); DrawSquare(cell.X, cell.Y, secondPlayerBoard.MainBoard); } } }
public void PrepareBoard() { Board = new BattleshipBoard(this); }
public bool CheckCells(int y, int x, BattleshipBoard board, int shipDeckCount, Directions dir) { bool finalSuccess; bool check = false; bool success = true; switch (dir) { case Directions.UP: if (y - shipDeckCount >= 0) { for (int i = 0; i < shipDeckCount; i++) { check = CheckSquare(y, x, this); if (check == false) { success = false; } y--; } } else { success = false; } break; case Directions.RIGHT: if (x + shipDeckCount <= board.BoardSize - 1) { for (int i = 0; i < shipDeckCount; i++) { check = CheckSquare(y, x, this); if (check == false) { success = false; } x++; } } else { success = false; } break; case Directions.DOWN: if (y + shipDeckCount <= board.BoardSize - 1) { for (int i = 0; i < shipDeckCount; i++) { check = CheckSquare(y, x, this); if (check == false) { success = false; } y++; } } else { success = false; } break; case Directions.LEFT: if (x - shipDeckCount >= 0) { for (int i = 0; i < shipDeckCount; i++) { check = CheckSquare(y, x, this); if (check == false) { success = false; } x--; } } else { success = false; } break; } finalSuccess = success; return(finalSuccess); }
public void Hit(BattleshipBoard playerBoard, BattleshipBoard botBoard) { State.Hit(playerBoard, botBoard, this); }
public bool IsValid() { return(BattleshipBoard.IsOnBoard(xCoord, yCoord) && !board.IsLocationHit(xCoord, yCoord)); }
static void Main(string[] args) { Dictionary <int, int> difficulty = new Dictionary <int, int>(); difficulty.Add(1, 80); // 1. easy difficulty.Add(2, 60); // 2. medium difficulty.Add(3, 40); // 3. hard bool isPlaying = true; while (isPlaying) { Console.Clear(); Console.WriteLine("Single player Battleship!\r\n\r\n"); Console.WriteLine("Choose difficulty."); Console.WriteLine("1. Easy "); Console.WriteLine("2. Medium "); Console.WriteLine("3. Hard "); Console.WriteLine("Your choice: "); string input = Console.ReadLine(); bool wrongInput = true; int choice = 0; while (wrongInput) { if (int.TryParse(input, out int number)) { if (number < 1 || number > 3) { Console.WriteLine("Wrong input, try again: "); input = Console.ReadLine(); wrongInput = true; } else { choice = number; wrongInput = false; } } else { Console.WriteLine("Wrong input, try again: "); input = Console.ReadLine(); wrongInput = true; } } Console.WriteLine(); BattleshipBoard b = new BattleshipBoard(); Player p = new Player(); p.InitializeShips(); Console.Clear(); bool gameState = true; while (gameState) { Console.WriteLine("INFO: " + "Type 'EXIT' to give up\r\n"); Console.WriteLine("You cannot miss: " + difficulty[choice] + " times\r\n"); Console.WriteLine("You missed: " + p.getMissCount() + " times\r\n"); b.ShowBoard(p.GetGrid(), false); p.Shoot(); if (p.getMissCount() == difficulty[choice] || p.isGivingUp == true) { Console.Clear(); Console.WriteLine("You lose! See the ships below. \r\n"); b.ShowBoard(p.GetGrid(), true); gameState = false; } else if (p.getHitCount() == 17) { Console.Clear(); Console.WriteLine("Congratulations! You Won!\r\n"); gameState = false; } } Console.WriteLine("Play again? (y/n)"); string answer = Console.ReadLine(); bool notValid = true; while (notValid) { if (answer.ToLower().Equals("n")) { isPlaying = false; notValid = false; } else if (answer.ToLower().Equals("y")) { isPlaying = true; notValid = false; } else { Console.WriteLine("Wrong input. Enter y or n: "); answer = Console.ReadLine(); notValid = true; } } } }