fixed loose game spinning
This commit is contained in:
parent
0f92ed9b7e
commit
1ae2336180
1 changed files with 19 additions and 8 deletions
27
src/main.rs
27
src/main.rs
|
@ -13,6 +13,12 @@ struct Config {
|
|||
delay: Duration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Error {
|
||||
Disconnect,
|
||||
GameEnd,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let url = env::var("URL").unwrap_or_else(|_| "localhost:2048".to_string());
|
||||
|
@ -32,7 +38,7 @@ async fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
async fn game(mut write: OwnedWriteHalf, config: Config) -> Option<()> {
|
||||
async fn game(mut write: OwnedWriteHalf, config: Config) {
|
||||
'session: loop {
|
||||
let rules = GameRules::default();
|
||||
let mut game = Game::new(rules);
|
||||
|
@ -40,8 +46,10 @@ async fn game(mut write: OwnedWriteHalf, config: Config) -> Option<()> {
|
|||
|
||||
'_game: loop {
|
||||
let result = turn(&mut game, &mut controller, &mut write, config.delay).await;
|
||||
if result.is_none() {
|
||||
continue 'session;
|
||||
match result {
|
||||
Ok(()) => (),
|
||||
Err(Error::GameEnd) => continue 'session,
|
||||
Err(Error::Disconnect) => return,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,16 +60,19 @@ async fn turn(
|
|||
controller: &mut SimulatedController,
|
||||
write: &mut OwnedWriteHalf,
|
||||
delay: Duration,
|
||||
) -> Option<()> {
|
||||
) -> Result<(), Error> {
|
||||
let displayer = GridDisplayer::new(1234);
|
||||
let display = displayer.display(game.get_board());
|
||||
let clear = clear::All;
|
||||
let goto = cursor::Goto(1, 1);
|
||||
let frame = format!("{clear}{goto}{display}");
|
||||
write.write_all(frame.as_bytes()).await.ok()?;
|
||||
write
|
||||
.write_all(frame.as_bytes())
|
||||
.await
|
||||
.map_err(|_| Error::Disconnect)?;
|
||||
|
||||
let movement = controller.next_move(&*game).ok()?;
|
||||
game.turn(movement).ok()?;
|
||||
let movement = controller.next_move(&*game).map_err(|_| Error::GameEnd)?;
|
||||
game.turn(movement).map_err(|_| Error::GameEnd)?;
|
||||
sleep(delay).await;
|
||||
Some(())
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue