Last active
December 28, 2015 09:49
-
-
Save Rican7/7481467 to your computer and use it in GitHub Desktop.
This is an Object Oriented rewrite of my already rewritten Dart battleship game. I wanted to see how some of the OOP magic features worked. Very nice.
Original, more functional approach: https://gist.github.com/Rican7/7480097
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:math"; | |
import "dart:io"; | |
/// Represent a mark on a board | |
class Mark extends Point<int> { | |
// Properties | |
bool _checked = false; | |
String _representation; | |
// Methods | |
/// Constructor | |
Mark(x, y, [_representation = "O"]) : super(x, y) { | |
this.representation = _representation; | |
} | |
// Setters | |
set representation(String value) => _representation = value.substring(0, 1).toUpperCase(); | |
set checked(bool value) { | |
_checked = value; | |
_representation = "X"; | |
} | |
// Getters | |
String get representation => _representation; | |
bool get checked => _checked; | |
/// String representation | |
String toString() { | |
return _representation; | |
} | |
} | |
/// Represents a game board | |
class Board { | |
// Properties | |
final int numOfRows, numOfCols; | |
List<List> board; | |
// Methods | |
/// Constructor | |
Board([this.numOfRows = 5, this.numOfCols = 5]) { | |
// Setup our board by filling it with [numOfRows] rows | |
this.board = new List<List>.generate(numOfRows, (rowIndex) { | |
// Create a list with a [numOfCols] of Marks | |
return new List<Mark>.generate(numOfCols, (colIndex) { | |
return new Mark(colIndex, rowIndex); | |
}); | |
}); | |
} | |
/// Get a mark on the board based on a Point | |
Mark getMarkByPoint(Point<int> point) { | |
return board[point.y][point.x]; | |
} | |
/// String representation | |
String toString() { | |
var string = "\n"; | |
for (var row in board) { | |
string += row.join(" ") + "\n"; | |
} | |
return string; | |
} | |
} | |
class Game { | |
// Properties | |
final int numOfTurns; | |
// Methods | |
/// Constructor | |
Game([this.numOfTurns = 4]); | |
/// Create random hidden ship for a given board | |
Point<int> createHiddenShip(Board board) { | |
// Generate a new randomizer | |
var randomizer = new Random(); | |
return new Point( | |
randomizer.nextInt(board.numOfCols), | |
randomizer.nextInt(board.numOfRows) | |
); | |
} | |
/// Print an introduction | |
void printIntroduction() { | |
print("Let's play Battleship!"); | |
} | |
/// Get the user's guess as a Point | |
Point<int> getGuessPoint() { | |
// Get the guess row | |
print("Guess row: "); | |
var guessRow = int.parse(stdin.readLineSync()); | |
// Get the guess col | |
print("Guess col: "); | |
var guessCol = int.parse(stdin.readLineSync()); | |
print(""); | |
// Create a point from their guess values | |
return new Point(guessCol, guessRow); | |
} | |
bool checkGuess(Board gameBoard, Point<int> hiddenShip, Point<int> guessPoint) { | |
if (hiddenShip == guessPoint) { | |
print("Congratulations! You sunk my battleship!"); | |
return true; | |
} else { | |
if ((guessPoint.x < 0 || guessPoint.x > gameBoard.numOfCols) || | |
(guessPoint.y < 0 || guessPoint.y > gameBoard.numOfRows)) { | |
print("Oops, that's not even in the ocean."); | |
} else if (gameBoard.getMarkByPoint(guessPoint).checked) { | |
print("You guessed that one already."); | |
} else { | |
print("You missed my battleship!"); | |
gameBoard.getMarkByPoint(guessPoint).checked = true; | |
} | |
} | |
return false; | |
} | |
/// Play the game | |
void play({Board gameBoard}) { | |
// Initialize | |
gameBoard = null != gameBoard ? gameBoard : new Board(); | |
// Hide the ship | |
var hiddenShip = createHiddenShip(gameBoard); | |
// Introduce them | |
printIntroduction(); | |
for (var turn = 0; turn < numOfTurns; turn++) { | |
print(gameBoard); | |
var guessPoint = getGuessPoint(); | |
// Check the guess' accuracy | |
var result = checkGuess(gameBoard, hiddenShip, guessPoint); | |
if (result) { | |
break; | |
} | |
print("Finished turn ${turn + 1}"); | |
} | |
print("Game Over"); | |
} | |
} | |
void main(args) { | |
new Game().play(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment