lib-battleship
A Rust library for battleship implementations.
How to use
You can set up the game by creating and configuring a PreGame
object like so:
extern crate lib_battleship;
use PreGame;
use *;
use *;
use ShootOk;
// when hard-coding the game's dimensions, use `unwrap()`.
// PreGame's constructor makes sure that the battlefield
// is no smaller than 2x2.
let mut pregame = new.unwrap;
// add ship types
// `PreGame` validates that a ship is no shorter
// than 1 in length, thus the call to `unwrap()`.
let sub = pregame.add_ship_type.unwrap;
let corvette = pregame.add_ship_type.unwrap;
Then each player has to place all their ships on the battlefield. Each player has one ship of every ship type.
// pregame also validates the placement of each ship.
pregame.place_ship.unwrap;
pregame.place_ship.unwrap;
pregame.place_ship.unwrap;
pregame.place_ship.unwrap;
Display a player's battlefield by iterating over its cells like so:
for y in 0..pregame.height
When all ships have been placed, start the game like so:
// pregame::start() checks that all ships have been
// placed and will complain if that's not the case.
let mut game = pregame.start.unwrap;
Use Game::get_cell and Game::get_opponent_cell to display the respective battlefields, similar to PreGame::get_cell above. Use Game::get_cell to display a battlefield to its owner and Game::get_opponent_cell to display their adversary's battlefield with unhit cells appearing empty.
From now on, players can take turns shooting at each other's ships. A player can keep shooting as long as they score hits.
/// game validates that it's the shooting player's
/// turn and that they don't shoot out of bounds.
match game.shoot.unwrap