summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 9ccefde2e3ec3d75488bfd987dce8959fd880bfe (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
extern crate pancurses;

use pancurses::Input::*;
use pancurses::*;

mod game;
use crate::game::*;

pub struct GameStateGUI {
    action_window: Window,
    pieces_window: Window,
}

impl GameStateGUI {
    pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> GameStateGUI {
        GameStateGUI {
            pieces_window: newwin(2, width, ypos, xpos),
            action_window: newwin(height, width, ypos + 3, xpos),
        }
    }

    pub fn update<L: Fn(String)>(&self, game: &Game<L>) {
        self.pieces_window.clear();
        self.pieces_window.addstr(format!(
            "W: {:2}  B: {:2}\nWC: {}  BC: {}",
            game.query_pieces(Player::White, Stone::Flat),
            game.query_pieces(Player::Black, Stone::Flat),
            game.query_pieces(Player::White, Stone::Capstone),
            game.query_pieces(Player::Black, Stone::Capstone),
        ));
        self.pieces_window.refresh();

        self.action_window.clear();
        self.action_window.addstr(game.query_action_lines());
        self.action_window.refresh();
    }
}

struct BoardGUI {
    board_window: Window,
    cell_windows: Vec<Window>,
    size: u8,
    cell_height: u8,
    cell_width: u8,
    hoff: i32,
    cur_cell: Position,
    has_colours: bool,
    white_colour: u8,
    black_colour: u8,
}

#[derive(PartialEq)]
enum BGAction {
    Quit,
    None,
}

impl BoardGUI {
    fn draw_grid(&self) {
        let s: i32 = self.size as i32;
        let h: i32 = self.cell_height as i32;
        let w: i32 = self.cell_width as i32;
        for y in 0..h * s + 1 {
            for x in 0..w * s + 1 {
                if y % h == 0 {
                    if x % w == 0 {
                        self.board_window.mvaddch(y, x + self.hoff, '+');
                    } else {
                        self.board_window.mvaddch(y, x + self.hoff, '-');
                    }
                } else if x % w == 0 {
                    self.board_window.mvaddch(y, x + self.hoff, '|');
                }
            }
        }
        for y in 0..s {
            self.board_window
                .mvaddstr(y * h + h / 2, 0, format!("{}.", s - y));
        }
        for x in 0..s {
            self.board_window.mvaddstr(
                s * h + 1,
                w * x + w / 2 + self.hoff,
                format!("{:x}.", x + 10),
            );
        }
        self.board_window.refresh();
    }

    fn pos_to_idx(&self, pos: &Position) -> usize {
        let y = pos.y as usize;
        let x = pos.x as usize;
        x + y * (self.size as usize)
    }

    fn mv_to_cell(&self, pos: &Position, line: i32) {
        if let Some(win) = self.cell_windows.get(self.pos_to_idx(pos)) {
            win.mv(line, 0);
            win.refresh();
        }
    }

    fn draw_stone(&self, win: &Window, top: bool, player: &Player, stone: &Stone) {
        if self.has_colours {
            match player {
                Player::Black => win.attrset(ColorPair(self.black_colour)),
                Player::White => win.attrset(ColorPair(self.white_colour)),
            };
            if top {
                win.addch(match stone {
                    Stone::Flat => '#',
                    Stone::Standing => '/',
                    Stone::Capstone => '*',
                });
            } else {
                win.addstr(format!("{}", player));
            }
        } else {
            win.addstr(format!("{}", player));
            if top {
                win.addstr(format!("{}", stone));
            }
        }
    }

    fn draw_stack(&self, pos: &Position, stack: &Stack) {
        if let Some(win) = self.cell_windows.get(self.pos_to_idx(pos)) {
            win.mv(0, 0);
            let l = stack.len() as i32;
            if l > 0 {
                for i in 0..l {
                    let piece = &stack[i as usize];
                    self.draw_stone(win, i == l - 1, &piece.player, &piece.stone);
                }
            }
            win.refresh();
        }
    }

    fn update<L: Fn(String)>(&self, game: &Game<L>) {
        // Is this really the `best' way?
        assert!(game.get_size() == self.size);
        let mut pos = Position { x: 0, y: 0 };
        for y in 0..self.size {
            pos.y = y;
            for x in 0..self.size {
                pos.x = x;
                if let Some(stack) = game.query_square(&pos) {
                    self.draw_stack(&pos, stack);
                }
            }
        }
    }

    fn handle_key(&mut self, key: Input) -> BGAction {
        match key {
            KeyDown => {
                if 0 < self.cur_cell.y {
                    self.cur_cell.y -= 1;
                    self.mv_to_cell(&self.cur_cell, 0);
                }
                BGAction::None
            }
            KeyUp => {
                if self.cur_cell.y + 1 < self.size {
                    self.cur_cell.y += 1;
                    self.mv_to_cell(&self.cur_cell, 0);
                }
                BGAction::None
            }
            KeyLeft => {
                if 0 < self.cur_cell.x {
                    self.cur_cell.x -= 1;
                    self.mv_to_cell(&self.cur_cell, 0);
                }
                BGAction::None
            }
            KeyRight => {
                if self.cur_cell.x + 1 < self.size {
                    self.cur_cell.x += 1;
                    self.mv_to_cell(&self.cur_cell, 0);
                }
                BGAction::None
            }
            Character('q') => BGAction::Quit,
            x => {
                self.board_window.addstr(format!("{:?}", x));
                self.board_window.refresh();
                BGAction::None
            }
        }
    }

    fn new(
        ypos: i32,
        xpos: i32,
        size: u8,
        cell_height: u8,
        cell_width: u8,
        hoff: u8,
        has_colours: bool,
        white_colour: u8,
        black_colour: u8,
    ) -> BoardGUI {
        let mut cell_windows: Vec<Window> = Vec::new();

        let hoff = hoff as i32;
        let s: i32 = size as i32;
        let h: i32 = cell_height as i32;
        let w: i32 = cell_width as i32;
        for i in 0..s * s {
            let y: i32 = i / s;
            let x: i32 = i % s;
            cell_windows.push(newwin(h - 1, w - 1, (s - y - 1) * h + 1, hoff + x * w + 1));
        }

        let bg = BoardGUI {
            board_window: newwin(h * s + 3, w * s + 3 + hoff, ypos, xpos),
            cell_windows: cell_windows,
            size: size,
            cell_height: cell_height,
            cell_width: cell_width,
            hoff: hoff,
            cur_cell: Position { x: 0, y: 0 },
            has_colours: has_colours,
            black_colour: black_colour,
            white_colour: white_colour,
        };

        bg.draw_grid();

        // for i in 0..s * s {
        //     let y: i32 = i / s;
        //     let x: i32 = i % s;
        //     let y = (s - y - 1) * h + 1;
        //     let x = hoff + x * w + 1;
        //     bg.cell_windows[i as usize].addstr(format!("{}:({},{})", i, y, x));
        //     bg.cell_windows[i as usize].refresh();
        // }

        bg.mv_to_cell(&bg.cur_cell, 0);
        bg.board_window.refresh();
        bg
    }
}

struct MoveEntry {
    win: Window,
}

impl MoveEntry {
    fn init(&self) {
        self.win.mvaddstr(0, 0, "Enter move: ");
        // Make shaded box for entry
        self.win.refresh();
    }

    // fn read_action(&self) -> &str {
    //     "a1"
    // }
}

struct MessageLog {
    win: Window,
    has_colours: bool,
    red_colour: u8,
}

impl MessageLog {
    fn init(&self) {}

    fn log_error(&self, str: String) {
        if self.has_colours {
            self.win.attrset(ColorPair(self.red_colour));
        }
        // self.win.clear();
        self.win.addstr(str);
        self.win.attrset(Attribute::Normal);
        self.win.refresh();
    }
}

fn main() {
    let screen = initscr();
    // screen.nodelay(true);
    screen.keypad(true);
    cbreak();
    noecho();
    curs_set(2);

    let has_colours = has_colors();
    let (black_colour, white_colour, red_colour): (u8, u8, u8) = (1, 2, 3);

    if has_colours {
        start_color();
        use_default_colors();
        init_pair(black_colour as i16, -1, COLOR_RED);
        init_pair(white_colour as i16, -1, COLOR_BLUE);
        init_pair(red_colour as i16, COLOR_RED, -1);
    }

    screen.refresh();

    let mut bg = BoardGUI::new(0, 0, 5, 4, 8, 3, has_colours, white_colour, black_colour);
    let gs = GameStateGUI::new(0, 5 * 8 + 3 + 3, 100, 20);

    let mew = newwin(1, 30, 25, 0);
    let mlw = newwin(3, 70, 26, 0);
    let me = MoveEntry { win: mew };
    let ml = MessageLog {
        win: mlw,
        has_colours: has_colours,
        red_colour: red_colour,
    };
    me.init();
    ml.init();

    let log = |s| MessageLog::log_error(&ml, s);
    let mut game = Game::new(5, "tslil", "taktician", log);

    let pos = Position { x: 0, y: 0 };
    let action = Action::Place(Player::White, pos, Stone::Flat);

    game.perform_action(action);

    bg.update(&game);
    gs.update(&game);

    // game_state.perform_action(&action);
    // game_state.is_legal_action(&Action::Move(Player::White, pos, Direction::Up, vec![0, 1]));
    // bg.draw_game_state(&game_state);

    loop {
        if let Some(inp) = screen.getch() {
            if bg.handle_key(inp) == BGAction::Quit {
                break;
            }
        }
    }
    endwin();
}