use crate::game::Game;
use std::io::Read;
mod game;
mod rules;
fn main() {
print_intro();
game_loop();
println!();
println!("Goodbye!");
}
fn print_intro() {
println!("Welcome to Pirates!");
println!();
println!("Sink the enemy's ship by shooting your cannons!");
println!();
print_controls();
}
fn print_controls() {
println!(" Controls:");
println!(" 1 - Fire cannonballs");
println!(" 2 - Fire grapeshots");
println!(" h - Display the controls");
println!(" s - Save the game state");
println!(" l - Load the savegame");
println!(" q - Quit");
}
fn print_separator() {
println!("--------------------------------------------------------------------------------");
}
fn game_loop() {
let mut game = Game::new();
turn_header(&game);
loop {
let input: Option<char> = std::io::stdin()
.bytes()
.next()
.and_then(|result| result.ok())
.map(|byte| byte as char);
if let Some(key) = input {
match key {
'1' => {
print_separator();
game.fire_cannonball();
if game.check_winner() {
break;
}
game.enemy_turn();
if game.check_winner() {
break;
}
turn_header(&game);
}
'2' => {
print_separator();
game.fire_grapeshot();
if game.check_winner() {
break;
}
game.enemy_turn();
if game.check_winner() {
break;
}
turn_header(&game);
}
'h' => print_controls(),
's' => game.save(),
'l' => {
game.load();
if game.check_winner() {
break;
}
turn_header(&game);
}
'q' => break,
_ => {}
}
}
}
}
fn turn_header(game: &Game) {
let (player_ship_hull, player_ship_crew) = game.player_stats();
let (enemy_ship_hull, enemy_ship_crew) = game.enemy_stats();
let stat_to_string = |stat| {
let i = (stat as usize + 5 - 1) / 5; std::iter::repeat("=")
.take(i)
.chain(std::iter::repeat(" ").take(20 - i))
.collect::<String>()
};
print_separator();
println!("--- PLAYER SHIP ENEMY SHIP ---");
println!(
"--- HULL [{}] HULL [{}] ---",
stat_to_string(player_ship_hull),
stat_to_string(enemy_ship_hull)
);
println!(
"--- CREW [{}] CREW [{}] ---",
stat_to_string(player_ship_crew),
stat_to_string(enemy_ship_crew)
);
print_separator();
}