From 782a6dce87d5da7242bbd9fa9dbcc1596bb02f11 Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Sat, 17 Feb 2024 12:55:36 +0100 Subject: [PATCH] cli start game --- client_cli/src/app.rs | 69 ++++++++++++++++++++++++---------- client_cli/src/main.rs | 2 +- doc/refs/inspirations.md | 80 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 22 deletions(-) diff --git a/client_cli/src/app.rs b/client_cli/src/app.rs index 1c5d500..103fb7c 100644 --- a/client_cli/src/app.rs +++ b/client_cli/src/app.rs @@ -1,10 +1,11 @@ +use store::GameState; + // Application. #[derive(Debug, Default)] pub struct App { // should the application exit? pub should_quit: bool, - // counter - pub counter: u8, + pub game: GameState, } impl App { @@ -13,8 +14,14 @@ impl App { Self::default() } + // Constructs a new instance of [`App`]. + pub fn start(&mut self) { + self.game = GameState::new(); + } + pub fn input(&mut self, input: &str) { println!("'{}'", input); + println!("'{}'", self.display()); if input == "quit" { self.quit(); } @@ -25,33 +32,55 @@ impl App { self.should_quit = true; } - pub fn increment_counter(&mut self) { - if let Some(res) = self.counter.checked_add(1) { - self.counter = res; - } - } + pub fn display(&mut self) -> String { + let mut board = " + 24 23 22 21 20 19 18 17 16 15 14 13 +-------------------------------------------------------------------" + .to_owned(); + board = board + + "------------------------------------------------------------------- + 1 2 3 4 5 6 7 8 9 10 11 12 "; - pub fn decrement_counter(&mut self) { - if let Some(res) = self.counter.checked_sub(1) { - self.counter = res; - } + // ligne 1 à 8 : positions 24 à 13 + // ligne 9 nombre exact + // ligne 10 --- + // lignes 11 à 18 : positions 1 à 12 + board + // self.game.to_string() } } #[cfg(test)] mod tests { use super::*; - #[test] - fn test_app_increment_counter() { - let mut app = App::default(); - app.increment_counter(); - assert_eq!(app.counter, 1); - } #[test] - fn test_app_decrement_counter() { + fn test_display() { + let expected = " + 24 23 22 21 20 19 18 17 16 15 14 13 +------------------------------------------------------------------- +| | | X | +| | | X | +| | | X | +| | | X | +| | | X | +| | | X | +| | | X | +| | | X | +| | | 15 | +|------------------------------ | | ------------------------------| +| | | 15 | +| | | O | +| | | O | +| | | O | +| | | O | +| | | O | +| | | O | +| | | O | +| | | O | +------------------------------------------------------------------- + 1 2 3 4 5 6 7 8 9 10 11 12 "; let mut app = App::default(); - app.decrement_counter(); - assert_eq!(app.counter, 0); + assert_eq!(app.display(), expected); } } diff --git a/client_cli/src/main.rs b/client_cli/src/main.rs index 3107db3..1ed2455 100644 --- a/client_cli/src/main.rs +++ b/client_cli/src/main.rs @@ -11,7 +11,7 @@ fn main() -> Result<()> { // Start the main loop. while !app.should_quit { - println!("what?>"); + println!("whot?>"); let mut input = String::new(); let _bytecount = io::stdin().read_line(&mut input)?; app.input(input.trim()); diff --git a/doc/refs/inspirations.md b/doc/refs/inspirations.md index 3306e71..97d567b 100644 --- a/doc/refs/inspirations.md +++ b/doc/refs/inspirations.md @@ -31,9 +31,87 @@ cf. https://blessed.rs/crates * go : https://bgammon.org/blog/20240101-hello-world/ - protocole de communication : https://code.rocket9labs.com/tslocum/bgammon/src/branch/main/PROTOCOL.md - +* ocaml : https://github.com/jacobhilton/backgammon?tab=readme-ov-file + cli example : https://www.jacobh.co.uk/backgammon/ * lib rust backgammon - https://github.com/carlostrub/backgammon - https://github.com/marktani/backgammon * network webtarot * front ? + + +## cli examples + +### GnuBackgammon + + (No game) new game + gnubg rolls 3, anthon rolls 1. + + GNU Backgammon Positions ID: 4HPwATDgc/ABMA + Match ID : MIEFAAAAAAAA + +12-11-10--9--8--7-------6--5--4--3--2--1-+ O: gnubg + | X O | | O X | 0 points + | X O | | O X | Rolled 31 + | X O | | O | + | X | | O | + | X | | O | + ^| |BAR| | (Cube: 1) + | O | | X | + | O | | X | + | O X | | X | + | O X | | X O | + | O X | | X O | 0 points + +13-14-15-16-17-18------19-20-21-22-23-24-+ X: anthon + + gnubg moves 8/5 6/5. + +### jacobh + +Move 11: player O rolls a 6-2. +Player O estimates that they have a 90.6111% chance of winning. + +Os borne off: none + 24 23 22 21 20 19 18 17 16 15 14 13 +------------------------------------------------------------------- +| v v v v v v | | v v v v v v | +| | | | +| X O O O | | O O O | +| X O O O | | O O | +| O | | | +| | X | | +| | | | +| | | | +| | | | +| | | | +|------------------------------| |------------------------------| +| | | | +| | | | +| | | | +| | | | +| X | | | +| X X | | X | +| X X X | | X O | +| X X X | | X O O | +| | | | +| ^ ^ ^ ^ ^ ^ | | ^ ^ ^ ^ ^ ^ | +------------------------------------------------------------------- + 1 2 3 4 5 6 7 8 9 10 11 12 +Xs borne off: none + +Move 12: player X rolls a 6-3. +Your move (? for help): bar/22 +Illegal move: it is possible to move more. +Your move (? for help): ? +Enter the start and end positions, separated by a forward slash (or any non-numeric character), of each counter you want to move. +Each position should be number from 1 to 24, "bar" or "off". +Unlike in standard notation, you should enter each counter movement individually. For example: + 24/18 18/13 + bar/3 13/10 13/10 8/5 + 2/off 1/off +You can also enter these commands: + p - show the previous move + n - show the next move + - toggle between showing the current and last moves + help - show this help text + quit - abandon game +