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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![no_std]
#[rustfmt::skip]
mod consts;
mod test;
#[derive(Clone)]
pub struct Game(u64, u64);
impl Default for Game {
fn default() -> Self {
Self(0x0000_0008_1000_0000, 0x0000_0010_0800_0000)
}
}
impl core::fmt::Debug for Game {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for i in (0..64).rev() {
f.write_str(match (self.0 >> i & 1, self.1 >> i & 1) {
(0, 0) => " ",
(1, 0) => "O",
(0, 1) => "X",
(_, _) => unreachable!(),
})?;
if i % 8 == 0 {
f.write_str("\n")?;
}
}
Ok(())
}
}
impl Game {
#[must_use]
pub fn new(p: u64, n: u64) -> Option<Self> {
if p & n == 0 {
Some(Self(p, n))
} else {
None
}
}
#[must_use]
pub const fn get(&self) -> [u64; 2] {
[self.0, self.1]
}
#[must_use]
pub fn make_move(&self, place: usize) -> Option<Self> {
let diff = (0..4)
.map(|i| unsafe {
use bitintr::*;
use consts::*;
u64::from(*consts::RESULT.get_unchecked(
INDEX[place][i] as usize * 32
+ self.0.pext(MASK[place][i][0]) as usize * 64
+ self.1.pext(MASK[place][i][1]) as usize,
))
.pdep(MASK[place][i][1])
})
.fold(0, core::ops::BitOr::bitor);
if diff == 0 || ((self.0 | self.1) & 1 << place != 0) {
None
} else {
Some(Self(self.1 ^ diff, self.0 ^ diff ^ 1 << place))
}
}
#[must_use]
pub fn available_moves(&self) -> u64 {
[-9, -8, -7, -1, 1, 7, 8, 9]
.iter_mut()
.map(|i| self.gen(*i))
.fold(0, core::ops::BitOr::bitor)
& !self.0
& !self.1
}
#[must_use]
fn gen(&self, dir: isize) -> u64 {
fn shift(x: u64, y: isize) -> u64 {
if y > 0 {
x >> y
} else {
x << -y
}
}
let x = self.0;
let y = self.1
& match dir.rem_euclid(8) {
7 => consts::RIGHTY,
0 => !0,
1 => consts::LEFTY,
_ => unreachable!(),
};
let d = dir;
let x = x | y & shift(x, d);
let y = y & shift(y, d);
let d = d * 2;
let x = x | y & shift(x, d);
let y = y & shift(y, d);
let d = d * 2;
let x = x | y & shift(x, d);
shift(x ^ self.0, dir)
}
}