code readability improvements

This commit is contained in:
Christoffer Müller Madsen 2018-02-06 23:46:15 +01:00
parent 3c0625ef47
commit 848923de06

View File

@ -3,14 +3,18 @@ import numpy as np
class Board: class Board:
# Remember to handle pushing other pieces to home # TODO: Remember to handle pushing other pieces to home
# Also remember that a player can't move backwards and the one player goes from 1-47 # TODO: Also remember that a player can't move backwards and the one
# while the other goes from 47-1 # player goes from 1-47 while the other goes from 47-1
def __init__(self): def __init__(self):
self.cup = Cup() self.cup = Cup()
self.state = [0,2,0,0,0,0,-5,0,-3,0,0,0,5,-5,0,0,0,3,0,5,0,0,0,0,-2,0, 0, 0] self.state = [ 0,
2, 0, 0, 0, 0, -5,
0, -3, 0, 0, 0, 5,
-5, 0, 0, 0, 3, 0,
5, 0, 0, 0, 0, -2,
0, 0, 0]
def get_state(self): def get_state(self):
@ -20,24 +24,29 @@ class Board:
return -1 if cur == 1 else 1 return -1 if cur == 1 else 1
# Remember to handle edge case when we're on the last moves and you may go from position 22 -> 24 on a 6, if you have no pieces behind 22. Simply check if any are behind if you're circle or if any are higher if you are X, then it can be allowed. # Remember to handle edge case when we're on the last moves and you may go
# from position 22 -> 24 on a 6, if you have no pieces behind 22. Simply
# check if any are behind if you're circle or if any are higher if you are
# X, then it can be allowed.
# Also, the check_move will also fail when you're attempting to leave a jail. A fix of this is of course to check if the from_ = jail and if so, allow some extra stuff! # Also, the check_move will also fail when you're attempting to leave a
# jail. A fix of this is of course to check if the from_idx = jail and if so,
# allow some extra stuff!
def check_move(self, move, sym, roll): def check_move(self, move, sym, roll):
from_ = int(move[0]) from_idx = int(move[0])
to = int(move[1]) to_idx = int(move[1])
if from_ == 26: if from_idx == 26:
from_ = 1 from_idx = 1
roll -= 1 roll -= 1
elif from_ == 27: elif from_idx == 27:
from_ = 24 from_idx = 24
roll -= 1 roll -= 1
if (from_ < 1 or from_ > 24) or (to < 1 or to > 24): if (from_idx < 1 or from_idx > 24) or (to_idx < 1 or to_idx > 24):
return False return False
elif (abs(from_ - to) != roll): elif (abs(from_idx - to_idx) != roll):
return False return False
elif ((self.state[to] * sym >= 0 or self.state[to] == 0) or (self.state[to] * self.switch(sym) == 1)) and self.state[from_] * sym >= 1: elif ((self.state[to_idx] * sym >= 0 or self.state[to_idx] == 0) or (self.state[to_idx] * self.switch(sym) == 1)) and self.state[from_idx] * sym >= 1:
return True return True
def find_pieces_for_player(self,sym): def find_pieces_for_player(self,sym):
@ -65,10 +74,10 @@ class Board:
legal_moves = [] legal_moves = []
for index in idxs_with_thing: for index in idxs_with_thing:
from_ = index from_idx = index
to = index+(roll*sym) to_idx = index+(roll*sym)
if self.check_move([from_,to], sym, roll): if self.check_move([from_idx,to_idx], sym, roll):
legal_moves.append([from_,to]) legal_moves.append([from_idx,to_idx])
return legal_moves return legal_moves
@ -95,15 +104,15 @@ class Board:
else: else:
self.state[27] += sym self.state[27] += sym
def move_thing(self, cur_sym, from_, to): def move_thing(self, cur_sym, from_idx, to_idx):
self.state[from_] -= cur_sym self.state[from_idx] -= cur_sym
if self.state[to] * cur_sym <= -1: if self.state[to_idx] * cur_sym <= -1:
self.move_to_jail(self.switch(cur_sym)) self.move_to_jail(self.switch(cur_sym))
self.state[to] = 0 self.state[to_idx] = 0
self.state[to] += cur_sym self.state[to_idx] += cur_sym