Skip to content

Commit

Permalink
Merge pull request #5 from ChaelCodes/ChaelCodes/issue1
Browse files Browse the repository at this point in the history
Cornelius has style
  • Loading branch information
ChaelCodes authored Sep 22, 2021
2 parents f366a4a + 90d1e19 commit e0c4fad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 65 deletions.
47 changes: 16 additions & 31 deletions src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn get_info() -> JsonValue {
"apiversion": "1",
"author": "ChaelCodes",
"color": "#F09383",
"head": "default",
"tail": "default",
"head": "bendr",
"tail": "round-bum",
});
}

Expand All @@ -37,25 +37,10 @@ pub fn get_move(game: &Game, _turn: &u32, board: &Board, me: &Battlesnake) -> &'
.into_iter()
.collect();

// Step 0: Don't let mer Battlesnake move back in on its own neck
// Step 0: Don't let your Battlesnake move back in on its own neck
let my_head = &me.head;
// let my_neck = &me.body[1];
// if my_neck.x < my_head.x {
// // my neck is left of my head
// possible_moves.insert("left", false);
// } else if my_neck.x > my_head.x {
// // my neck is right of my head
// possible_moves.insert("right", false);
// } else if my_neck.y < my_head.y {
// // my neck is below my head
// possible_moves.insert("down", false);
// } else if my_neck.y > my_head.y {
// // my neck is above my head
// possible_moves.insert("up", false);
// }

// TODO: Step 1 - Don't hit walls.
// Use board information to prevent mer Battlesnake from moving beyond the boundaries of the board.

// Use board information to prevent your Battlesnake from moving beyond the boundaries of the board.
let left = |head: &Coord| Coord {
x: head.x - 1,
y: head.y,
Expand All @@ -77,12 +62,12 @@ pub fn get_move(game: &Game, _turn: &u32, board: &Board, me: &Battlesnake) -> &'
possible_moves.insert("up", valid_move(&up(&my_head), &board, &me));
possible_moves.insert("down", valid_move(&down(&my_head), &board, &me));

// TODO: Step 2 - Don't hit merself.
// Use body information to prevent mer Battlesnake from colliding with itself.
// TODO: Step 2 - Don't hit yourself.
// Use body information to prevent your Battlesnake from colliding with itself.
// body = move_req.body

// TODO: Step 3 - Don't collide with others.
// Use snake vector to prevent mer Battlesnake from colliding with others.
// Use snake vector to prevent your Battlesnake from colliding with others.
// snakes = move_req.board.snakes

// TODO: Step 4 - Find food.
Expand All @@ -106,15 +91,15 @@ pub fn get_move(game: &Game, _turn: &u32, board: &Board, me: &Battlesnake) -> &'
fn valid_move(spot: &Coord, board: &Board, me: &Battlesnake) -> bool {
let board_width = board.width;
let board_height = board.height;
let my_neck = || { &me.body[1] };
let my_neck = || &me.body[1];

match spot {
Coord { y: 0, .. } => { println!("down"); return false; },
Coord { x: 0, .. } => { println!("left"); return false; },
Coord { y, .. } if y == &board_width => { println!("right"); return false; }, // Rust is weird
Coord { x, .. } if x == &board_height => { println!("up"); return false; },
Coord { x, y } if x == &my_neck().x && y == &my_neck().y => { println!("my neck"); return false; },
_ => { true }
Coord { y: 0, .. } => false,
Coord { x: 0, .. } => false,
Coord { y, .. } if y == &board_width => false, // Rust is weird
Coord { x, .. } if x == &board_height => false,
Coord { x, y } if x == &my_neck().x && y == &my_neck().y => false,
_ => true,
}
}

Expand Down Expand Up @@ -212,7 +197,7 @@ mod tests {
fn head_will_travel() {
let me = Battlesnake {
body: vec![Coord { x: 5, y: 9 }, Coord { x: 5, y: 8 }],
..Default::default()
..Default::default()
};
let board = Board {
width: 10,
Expand Down
69 changes: 35 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@ mod logic;
// Request types derived from https://docs.battlesnake.com/references/api#object-definitions
// For a full example of Game Board data, see https://docs.battlesnake.com/references/api/sample-move-request

#[derive(Deserialize, Serialize, Debug)]
pub struct Game {
id: String,
ruleset: HashMap<String, Value>,
timeout: u32,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Board {
height: u32,
width: u32,
food: Vec<Coord>,
snakes: Vec<Battlesnake>,
hazards: Vec<Coord>,
}

#[derive(Deserialize, Serialize, Debug, Default)]
pub struct Battlesnake {
id: String,
name: String,
health: u32,
body: Vec<Coord>,
head: Coord,
length: u32,
health: u32,
id: String,
latency: String,
length: u32,
name: String,

// Used in non-standard game modes
shout: Option<String>,
squad: Option<String>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Board {
food: Vec<Coord>,
hazards: Vec<Coord>,
height: u32,
snakes: Vec<Battlesnake>,
width: u32,
}

#[derive(Deserialize, Serialize, Debug, Default)]
pub struct Coord {
x: u32,
y: u32,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Game {
id: String,
ruleset: HashMap<String, Value>,
timeout: u32,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct GameState {
board: Board,
game: Game,
turn: u32,
board: Board,
you: Battlesnake,
}

Expand All @@ -69,8 +69,6 @@ fn handle_index() -> JsonValue {
logic::get_info()
}



#[post("/start", format = "json", data = "<start_req>")]
fn handle_start(start_req: Json<GameState>) -> Status {
logic::start(
Expand Down Expand Up @@ -105,22 +103,25 @@ fn handle_end(end_req: Json<GameState>) -> Status {
fn main() {
let address = "0.0.0.0";
let env_port = env::var("PORT").ok();
let env_port = env_port
.as_ref()
.map(String::as_str)
.unwrap_or("8080");
let env_port = env_port.as_ref().map(String::as_str).unwrap_or("8080");
let port = env_port.parse::<u16>().unwrap();

env_logger::init();

let config = Config::build(Environment::Development)
.address(address)
.port(port)
.finalize()
.unwrap();

info!("Starting Battlesnake Server at http://{}:{}...", address, port);
.address(address)
.port(port)
.finalize()
.unwrap();

info!(
"Starting Battlesnake Server at http://{}:{}...",
address, port
);
rocket::custom(config)
.mount("/", routes![handle_index, handle_start, handle_move, handle_end])
.mount(
"/",
routes![handle_index, handle_start, handle_move, handle_end],
)
.launch();
}

0 comments on commit e0c4fad

Please sign in to comment.