use rand::Rng; use std::cmp::Ordering; static goal:[i32; 8] = [ 0b111000000, 0b000111000, 0b000000111, 0b100100100, 0b010010010, 0b001001001, 0b100010001, 0b001010100]; fn check(player: i32) -> bool { for mask in goal { if player & mask == mask { return true } } return false } fn minmax(p1: i32, p2: i32, turn: bool) -> i32 { if check(p2) { if turn { return 1 } else { return -1 } } let board: i32 = p1 | p2; if board == 0b111111111{ return 0 } let mut w = Vec::new(); for i in 0..9 { if board & (1 << i) == 0 { w.push(i); } } let mut k = Vec::new(); if turn { for i in w { k.push(minmax(p2, p1 | (1 << i), !turn)) } return *k.iter().min().unwrap(); } else { for i in w { k.push(minmax(p2, p1 | (1 << i), !turn)) } return *k.iter().max().unwrap(); } } fn play(p1: i32, p2: i32, turn: bool) { if check(p2) { println!("{:09b}, {:09b}", p1, p2); return } let board: i32 = p1 | p2; if board == 0b111111111{ println!("{:09b}, {:09b}", p1, p2); return } let mut w = Vec::new(); for i in 0..9 { if board & (1 << i) == 0 { w.push(i); } } let mut r = Vec::new(); for i in w.clone() { r.push(minmax(p2, p1 | (1 << i), true)) } let n = r.iter() .enumerate() .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal)) .map(|(index, _)| index).unwrap(); let j = w[n]; play(p2, p1 | (1 << j), !turn); } fn main() { play(0, 0, true); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.32s
Running `target/debug/rust`
001110010, 110001101
う、なんか違うな…