1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use crate::game; use crate::GameState; use std::io::{stdin, BufRead, BufReader, stdout, Write}; use std::io; fn prompt(s: &str) -> io::Result<()> { let stdout = stdout(); let mut stdout = stdout.lock(); stdout.write(s.as_bytes()); stdout.flush() } fn command_to_point(l: &str) -> (isize, isize) { let v: Vec<&str> = l.split(' ').collect(); if v.len() > 2 || v.len() < 1 { panic!("command error"); } let mut x: isize = 0; match v[0] { "a" | "A" => x = 1, "b" | "B" => x = 2, "c" | "C" => x = 3, "d" | "D" => x = 4, "e" | "E" => x = 5, "f" | "F" => x = 6, "g" | "G" => x = 7, "h" | "H" => x = 8, _ => x = 0, } let mut y: isize = 0; match v[1] { "1" => y = 1, "2" => y = 2, "3" => y = 3, "4" => y = 4, "5" => y = 5, "6" => y = 6, "7" => y = 7, "8" => y = 8, _ => y = 0, } if x == 0 || y == 0 { panic!("command error"); } (x, y) } pub fn prompt_game_start() { let stdin = stdin(); let stdin = stdin.lock(); let stdin = BufReader::new(stdin); let mut lines = stdin.lines(); let mut default_game = game::Game::default(); default_game.game_state = GameState::Running; loop { prompt("> ").unwrap(); if let Some(Ok(line)) = lines.next() { if line == "show" { println!("{:?}\n{}\n{}\n", default_game.now_turn, default_game.turns, default_game.print_now_board()); } else { let command_num = command_to_point(&line); default_game.proceed_one_turn(command_num.0, command_num.1); } } else { break; } } }