public void initBoard(char[,] charBoard, Game.PlayerColor currentColor) { myColor = currentColor; PlayerBoard currentPlayer = currentColor == Game.PlayerColor.Blue ? p1Board : p2Board; gameBoardCreator.initPlayerBoard(charBoard, currentPlayer); initColors.Add(currentColor); }
public void playMove(int target, Game.PlayerColor currentPlayerColor, char targetStatus, ShipType targetType, int[] startPos, bool horizontal) { PlayerBoard targetedPlayer = currentPlayerColor == Game.PlayerColor.Blue ? p2Board : p1Board; targetedPlayer.fireAtGrid(target, myColor != currentPlayerColor, targetStatus, targetType, startPos, horizontal); panels [0].setScore(p1Board.calculateHP()); panels [1].setScore(p2Board.calculateHP()); }
public void initPlayerBoard(char[,] inputBoard, PlayerBoard currentPlayer) { List <Ship> ships = new List <Ship> (); List <string> lines = new List <string> (); lines.AddRange(generateRows(inputBoard)); lines.AddRange(generateCols(inputBoard)); for (int i = 0; i < lines.Count; i++) { foreach (ShipFormat SF in shipFormats) { if (lines [i].Contains(SF.stringFormat)) { ships.Add(createShip(SF, i, lines[i].IndexOf(SF.stringFormat))); } } } currentPlayer.placeShips(ships); }
static void Main(string[] args) { // Create a Player Board IPlayerBoard playerBoard = new PlayerBoard(); // Get Parameters to Create a board Console.WriteLine("Enter no of rows"); int.TryParse(Console.ReadLine(), out var rows); Console.WriteLine("Enter no of Columns"); int.TryParse(Console.ReadLine(), out var columns); playerBoard.CreateBoard(rows, columns); // Get parameters to Add Battleship while (true) { Console.WriteLine("Create Battleship or q to quit"); if (Console.ReadLine().ToLower() == "q") { break; } Console.WriteLine("Enter Battleship Name"); string battleshipName = Console.ReadLine(); Console.WriteLine("Battle ship start Row"); int.TryParse(Console.ReadLine(), out var startRow); Console.WriteLine("Battleship Start Column"); int.TryParse(Console.ReadLine(), out var startColumn); Console.WriteLine("Battleship Length"); int.TryParse(Console.ReadLine(), out var length); Console.WriteLine("Battleship alignment 1 for horizantal, or any number for vertical"); int.TryParse(Console.ReadLine(), out var alignment); var align = (alignment == 1) ? Services.Enums.Alignment.Horizantal : Services.Enums.Alignment.Vertical; Coordinate coordinate = new Coordinate(startRow, startColumn, length, align); try { playerBoard.AddBattleShip(coordinate, battleshipName); } catch (IndexOutOfRangeException indexOutOfRangeException) { Console.WriteLine(indexOutOfRangeException.Message); } catch (ArgumentException argumentException) { Console.WriteLine(argumentException.Message); } catch (InvalidOperationException invalidOperationException) { Console.WriteLine(invalidOperationException.Message); } } while (!playerBoard.GameStatus()) { Console.WriteLine("Enter row to attack"); int.TryParse(Console.ReadLine(), out var row); Console.WriteLine("Enter Column to attack"); int.TryParse(Console.ReadLine(), out var column); var result = playerBoard.Attack(row, column); Console.WriteLine(result); } if (playerBoard.GameStatus() != false) { Console.WriteLine("Game Over!!!"); } Console.ReadLine(); }