aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 35c3e7aa9244ba1f90756794efe041f1a55f938d (plain)
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
const HEIGHT: u64 = 6;
const WIDTH:  u64 = 7;

type Bits = u64;

/**
 * bit order:
 *
 * .  .  .  .  .  .  .
 * 5 12 19 26 33 40 47
 * 4 11 18 25 32 39 46
 * 3 10 17 24 31 38 45
 * 2  9 16 23 30 37 44
 * 1  8 15 22 29 36 43
 * 0  7 14 21 28 35 42
 *
 * an extra row is added for the key represention
 *
 *
 *
 */
struct Position {
    /// stones of the current player
    player: u64,
    /// stones of the grid
    mask:   u64,
}

impl Position {
    fn new() -> Position {
        Position{ player: 0, mask: 0 }
    }

    fn from_position(col_moves: &[u64]) -> Position {
        let mut position = Position::new();
        for col_pos in col_moves {
            position.play(*col_pos);
        }
        position
    }

    fn play(&mut self, col_pos: u64) {
        self.player ^= self.mask;
        self.mask |= self.mask + (1 << (col_pos * (HEIGHT + 1)));
    }

    fn key(&self) -> u64 {
        self.player + self.mask
    }
}

use std::fmt;

impl fmt::Debug for Position {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "mask: {:064b}\n", self.mask)?;
        for i in 0..WIDTH {
            write!(f, "\t{:07b}\n", (self.mask & (0b1111111 << (i * WIDTH))) >> (i * WIDTH));
        }

        write!(f, "player: {:064b}\n", self.player)?;
        for i in 0..WIDTH {
            write!(f, "\t{:07b}\n", (self.player & (0b1111111 << (i * WIDTH))) >> (i * WIDTH));
        }

        write!(f, "key: {:064b}\n", self.key())?;
        for i in 0..WIDTH {
            write!(f, "\t{:07b}\n", (self.key() & (0b1111111 << (i * WIDTH))) >> (i * WIDTH));
        }
        Ok(())
    }
}

fn main() {
    let mut p = Position::new();
    // println!("{:?}", p);
    p.play(2);
    p.play(2);
    p.play(1);
    p.play(5);
    p.play(5);
    println!("{:?}", p);
    // p.play(4);
    // println!("------------------------------");
    // println!("{:?}", p);
}