Commit 91835824 authored by Éloïs's avatar Éloïs
Browse files

exo1

parents
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "getrandom"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "rust-exos"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[package]
name = "rust-exos"
version = "0.1.0"
authors = ["librelois <c@elo.tf>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.7.3"
use std::cmp::Ordering;
pub struct Ia {
// Put your fields here
}
impl Default for Ia {
fn default() -> Self {
// Init your IA here
todo!()
}
}
impl Ia {
pub fn think(&mut self, message: Message) -> u32 {
// Code your intelligence here
todo!()
}
}
pub enum Message {
NewGame { max: u32 },
Response(Ordering),
}
pub fn play(max: u32, ia: &mut Ia) -> u32 {
let mut message = Message::NewGame { max };
let mut score = 1;
let secret = (rand::random::<u32>() % max) + 1;
loop {
let ia_choice = ia.think(message);
message = match secret.cmp(&ia_choice) {
Ordering::Less => Message::Response(Ordering::Less),
Ordering::Equal => break score,
Ordering::Greater => Message::Response(Ordering::Greater),
};
score += 1;
if score > 1_000 {
panic!("infinite loop");
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let mut ia = Ia::default();
for _ in 0..50 {
assert!(play(100, &mut ia) < 8);
}
for _ in 0..500 {
assert!(play(1000, &mut ia) < 11);
}
for _ in 0..5 {
assert_eq!(play(1, &mut ia), 1);
}
}
}
pub mod exo1;
fn main() {
exo1::play(50, &mut exo1::Ia::default());
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment