Skip to content

Instantly share code, notes, and snippets.

@VegaFromLyra
Created June 15, 2015 01:07
Show Gist options
  • Save VegaFromLyra/78c7e91eab9e7a88f8ae to your computer and use it in GitHub Desktop.
Save VegaFromLyra/78c7e91eab9e7a88f8ae to your computer and use it in GitHub Desktop.
Design Batteship
class Battleship {
Player currentPlayer;
Player opponetPlayer;
Player player1;
Player player2;
void init(Player p1, Player p2) {
player1Id = p1.Id;
player2Id = p2.Id;
}
public Battleship(Player p1, Player p2) {
init(p1, p2);
}
public void Run() {
bool done;
while (!done) {
switchPlayer();
var cell = currentPlayer.Fire();
var ship = opponentPlayer.RevealCell(cell.Row, cell.Column);
if (ship == Ship.None) {
currentPlayer.UpdateOpponentCell(cell.Row, cell.Column, Target.Miss);
} else {
currentPlayer.UpdateOpponentCell(cell.Row, cell.Column, Target.Hit);
}
done = currentPlayer.DoesOpponentBoardHave5Hits();
}
}
public void switchPlayer() {
if (currentPlayer == null) {
currentPlayer = player1;
opponetPlayer = player2;
} else if (currentPlayer.Id == player1.Id) {
currentPlayer = player2;
opponetPlayer = player1;
} else if (currentPlayer.Id == player2.Id) {
currentPlayer = player1;
opponetPlayer = player2;
} else {
throw new Exception("Invalid current player");
}
}
}
class Player {
Cell[,] ownBoard;
Cell[,] opponentBoard;
int hitsOnOpponentBoard;
Random randomGenerator;
readonly int ROWS;
readonly int COLUMNS;
public Ship RevealCell(int row, int col) {
return ownBoard[row, col];
}
public Player(int rows, int columns) {
init();
}
public bool DoesOpponentBoardHave5Hits() {
}
public Cell Fire() {
// Ask user for row and column input
return new Cell(row, column);
}
public void UpdateOpponentCell(int row, int column, Target value) {
opponentBoard[opponentCell.Row, opponentCell.Column] = value;
}
init() {
randomGenerator = new Random();
ROWS = rows;
COLUMNS = columns;
placeShipsRandomly();
}
placeShipsRandomly() {
}
}
class Cell {
public Cell(row, column, value) {
}
public int Row { get; set; }
public int Column { get; set; }
public Ship Value { get; set; }
}
enum Ship {
None,
AircraftCarrier,
Battleship,
Submarine,
Destroyer,
PatrolBoat
}
enum Target {
Hit,
Miss
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment