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
use glucose::linear::vec::Point;

pub mod cache;
pub mod defaults;
#[cfg(feature = "discord")]
pub mod discord;
#[cfg(feature = "terminal")]
pub mod terminal;

#[derive(Debug, Copy, Clone)]
pub struct Move<const N: usize> {
    player_id: i32,
    piece_id: i32,
    from: Option<Point<i32, { N }>>,
    to: Point<i32, { N }>,
}

#[derive(Debug, Copy, Clone)]
pub struct RenderMove2D {
    player_id: i32,
    piece_id: i32,
    pos: Point<i32, 2>,
}

pub struct Player {
    id: i32,
}

impl Player {
    pub const fn new(id: i32) -> Self {
        Self { id }
    }
}

#[derive(Debug, Default, Copy, Clone)]
pub struct Piece<const N: usize> {
    player: i32,
    id: i32,
    sub_id: Option<i32>,
    ty: i32,
    pos: Point<i32, { N }>,
}

impl<const N: usize> Piece<{ N }> {
    pub fn new(player: i32, id: i32, ty: i32, pos: Point<i32, { N }>) -> Self {
        Self {
            player,
            id,
            sub_id: None,
            ty,
            pos,
        }
    }

    pub fn new_with_sub(
        player: i32,
        id: i32,
        sub_id: i32,
        ty: i32,
        pos: Point<i32, { N }>,
    ) -> Self {
        Self {
            player,
            id,
            sub_id: Some(sub_id),
            ty,
            pos,
        }
    }
}

pub struct Board<const DIMS: usize> {
    start: Point<i32, { DIMS }>,
    size: [i32; DIMS],
}

impl<const DIMS: usize> Board<{ DIMS }> {
    pub fn new(start: Point<i32, { DIMS }>, size: [i32; DIMS]) -> Self {
        Self { start, size }
    }
}

pub enum PlayerSwap {
    NextUp,
    NextDown,
    Same,
    Custom(i32),
}

pub trait Mode {
    const PLAYERS: usize;
    const STARTING_PLAYER: i32;
    const DIMENSIONS: usize;

    fn new() -> Self;

    fn create_player(&self) -> Vec<Player>;

    fn next_move(&mut self, input: String, player: i32) -> Result<Option<String>, String>;

    fn execute_move(&mut self, player: i32);

    fn board(&self) -> (Vec<RenderMove2D>, usize);

    fn next_player(&self) -> PlayerSwap;
}

pub trait Backend {
    fn new() -> Self;

    fn receive(&self) -> Result<Option<String>, String>;

    fn send(&mut self, msg: String) -> Result<Option<String>, String>;
}

pub struct Game<M: Mode, B: Backend> {
    mode: M,
    backend: B,
    players: Vec<Player>,
    current_player: (i32, usize),
}

impl<M: Mode, B: Backend> Game<M, B> {
    pub fn new() -> Self {
        let mode = M::new();
        let backend = B::new();

        let players = mode.create_player();

        Self {
            mode,
            backend,
            players,
            current_player: (M::STARTING_PLAYER, 0),
        }
    }

    pub fn next_move(&mut self) {
        let mut input = self.backend.receive();
        while input.is_err() {
            match self.backend.send(String::from("Wrong Input")) {
                Ok(_) => {}
                Err(err) => {
                    panic!(format!("{}", err))
                }
            };
            input = self.backend.receive();
        }
        match self
            .mode
            .next_move(input.unwrap().unwrap(), self.current_player.0)
        {
            Ok(_) => {}
            Err(err) => {
                match self
                    .backend
                    .send(String::from(format!("Error in Move: {}", err)))
                {
                    Ok(_) => {}
                    Err(err) => {
                        panic!(format!("{}", err))
                    }
                };
                self.next_move()
            }
        }
        self.mode.execute_move(self.current_player.0);
        self.swap_player(self.mode.next_player())
    }

    pub fn backend(&mut self) -> &mut B {
        &mut self.backend
    }

    fn swap_player(&mut self, swap: PlayerSwap) {
        match swap {
            PlayerSwap::NextUp => {
                let maybe_player = self.players.get(self.current_player.1 + 1);
                match maybe_player {
                    Some(player) => self.current_player = (player.id, self.current_player.1 + 1),
                    None => self.current_player = (self.players[0].id, 0),
                }
            }
            PlayerSwap::NextDown => {
                let maybe_player = self.players.get(self.current_player.1 - 1);
                match maybe_player {
                    Some(player) => self.current_player = (player.id, self.current_player.1 - 1),
                    None => self.current_player = (self.players[M::PLAYERS - 1].id, M::PLAYERS - 1),
                }
            }
            PlayerSwap::Same => {}
            PlayerSwap::Custom(next) => {
                self.current_player = self
                    .players
                    .iter()
                    .enumerate()
                    .find(|(_, player)| player.id == next)
                    .map(|(idx, player)| (player.id, idx))
                    .unwrap()
            }
        }
    }
}

#[test]
#[cfg(feature = "discord")]
fn test() {
    use crate::defaults::normal::Default8x8;
    use crate::discord::Discord;
    let mut game: Game<Default8x8, Discord> = Game::new();
    game.next_move()
}