s/grid/board/g

This commit is contained in:
Michael Vines 2018-09-27 22:04:05 -07:00
parent 4169e5c510
commit a5f2aa6777
1 changed files with 21 additions and 21 deletions

View File

@ -27,15 +27,15 @@ impl std::error::Error for Error {}
type Result<T> = std::result::Result<T, Error>; type Result<T> = std::result::Result<T, Error>;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
enum GridItem { enum BoardItem {
F, // Free F, // Free
X, X,
O, O,
} }
impl Default for GridItem { impl Default for BoardItem {
fn default() -> GridItem { fn default() -> BoardItem {
GridItem::F BoardItem::F
} }
} }
@ -58,8 +58,8 @@ impl Default for State {
struct Game { struct Game {
player_x: Pubkey, player_x: Pubkey,
player_o: Option<Pubkey>, player_o: Option<Pubkey>,
state: State, pub state: State,
grid: [GridItem; 9], board: [BoardItem; 9],
keep_alive: [i64; 2], keep_alive: [i64; 2],
} }
@ -89,13 +89,13 @@ impl Game {
} }
} }
fn same(x_or_o: GridItem, triple: &[GridItem]) -> bool { fn same(x_or_o: BoardItem, triple: &[BoardItem]) -> bool {
triple.iter().all(|&i| i == x_or_o) triple.iter().all(|&i| i == x_or_o)
} }
pub fn next_move(self: &mut Game, player: Pubkey, x: usize, y: usize) -> Result<()> { pub fn next_move(self: &mut Game, player: Pubkey, x: usize, y: usize) -> Result<()> {
let grid_index = y * 3 + x; let board_index = y * 3 + x;
if grid_index >= self.grid.len() || self.grid[grid_index] != GridItem::F { if board_index >= self.board.len() || self.board[board_index] != BoardItem::F {
Err(Error::InvalidMove)?; Err(Error::InvalidMove)?;
} }
@ -105,37 +105,37 @@ impl Game {
return Err(Error::PlayerNotFound); return Err(Error::PlayerNotFound);
} }
self.state = State::OMove; self.state = State::OMove;
(GridItem::X, State::XWon) (BoardItem::X, State::XWon)
} }
State::OMove => { State::OMove => {
if player != self.player_o.unwrap() { if player != self.player_o.unwrap() {
return Err(Error::PlayerNotFound); return Err(Error::PlayerNotFound);
} }
self.state = State::XMove; self.state = State::XMove;
(GridItem::O, State::OWon) (BoardItem::O, State::OWon)
} }
_ => { _ => {
return Err(Error::NotYourTurn); return Err(Error::NotYourTurn);
} }
}; };
self.grid[grid_index] = x_or_o; self.board[board_index] = x_or_o;
let winner = let winner =
// Check rows // Check rows
Game::same(x_or_o, &self.grid[0..3]) Game::same(x_or_o, &self.board[0..3])
|| Game::same(x_or_o, &self.grid[3..6]) || Game::same(x_or_o, &self.board[3..6])
|| Game::same(x_or_o, &self.grid[6..9]) || Game::same(x_or_o, &self.board[6..9])
// Check columns // Check columns
|| Game::same(x_or_o, &[self.grid[0], self.grid[3], self.grid[6]]) || Game::same(x_or_o, &[self.board[0], self.board[3], self.board[6]])
|| Game::same(x_or_o, &[self.grid[1], self.grid[4], self.grid[7]]) || Game::same(x_or_o, &[self.board[1], self.board[4], self.board[7]])
|| Game::same(x_or_o, &[self.grid[2], self.grid[5], self.grid[8]]) || Game::same(x_or_o, &[self.board[2], self.board[5], self.board[8]])
// Check both diagonals // Check both diagonals
|| Game::same(x_or_o, &[self.grid[0], self.grid[4], self.grid[8]]) || Game::same(x_or_o, &[self.board[0], self.board[4], self.board[8]])
|| Game::same(x_or_o, &[self.grid[2], self.grid[4], self.grid[6]]); || Game::same(x_or_o, &[self.board[2], self.board[4], self.board[6]]);
if winner { if winner {
self.state = won_state; self.state = won_state;
} else if self.grid.iter().all(|&p| p != GridItem::F) { } else if self.board.iter().all(|&p| p != BoardItem::F) {
self.state = State::Draw; self.state = State::Draw;
} }