-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
60 lines (56 loc) · 1.71 KB
/
main.rs
File metadata and controls
60 lines (56 loc) · 1.71 KB
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
use std::io::{self, BufRead};
pub mod game_engine;
use game_engine::{Coord, Move};
mod agents;
use agents::{Agent, RandomAgent};
fn main() {
let mut player = RandomAgent::new();
let mut line = String::new();
let stdin = io::stdin();
loop {
line.clear();
stdin.lock().read_line(&mut line).unwrap();
let trimmed = line.trim();
if line == "" {
break;
}
let parts: Vec<&str> = trimmed.split(" ").collect();
match parts[0] {
"init" => player.init(),
"move" => {
let location = player.make_first_move();
write_move(location)
}
"opponent" => {
let opponent_move = parse_move(parts[1]);
player.opponent_move(opponent_move);
let location = player.make_move();
write_move(location)
}
"timeout" => {
println!("Timeout")
}
"game" => {}
"match" => player.match_end(),
_ => println!("Bad input"),
}
}
}
fn write_move(location: Move) {
println!(
"send:{},{};{},{}",
location.large_board.x, location.large_board.y, location.sub_board.x, location.sub_board.y
)
}
fn parse_move(input: &str) -> Move {
let boards: Vec<&str> = input.split(";").collect();
let large_board: Vec<&str> = boards[0].split(",").collect();
let sub_board: Vec<&str> = boards[1].split(",").collect();
Move::new(
Coord::new(
large_board[0].parse().unwrap(),
large_board[1].parse().unwrap(),
),
Coord::new(sub_board[0].parse().unwrap(), sub_board[1].parse().unwrap()),
)
}