From 848923de06307936483011040d337d28ab24d163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Tue, 6 Feb 2018 23:46:15 +0100 Subject: [PATCH] code readability improvements --- board.py | 59 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/board.py b/board.py index 2dc1200..1adffd9 100644 --- a/board.py +++ b/board.py @@ -3,14 +3,18 @@ import numpy as np class Board: - # 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 - # while the other goes from 47-1 + # TODO: Remember to handle pushing other pieces to home + # TODO: Also remember that a player can't move backwards and the one + # player goes from 1-47 while the other goes from 47-1 - def __init__(self): 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): @@ -20,24 +24,29 @@ class Board: 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): - from_ = int(move[0]) - to = int(move[1]) - if from_ == 26: - from_ = 1 + from_idx = int(move[0]) + to_idx = int(move[1]) + if from_idx == 26: + from_idx = 1 roll -= 1 - elif from_ == 27: - from_ = 24 + elif from_idx == 27: + from_idx = 24 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 - elif (abs(from_ - to) != roll): + elif (abs(from_idx - to_idx) != roll): 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 def find_pieces_for_player(self,sym): @@ -65,10 +74,10 @@ class Board: legal_moves = [] for index in idxs_with_thing: - from_ = index - to = index+(roll*sym) - if self.check_move([from_,to], sym, roll): - legal_moves.append([from_,to]) + from_idx = index + to_idx = index+(roll*sym) + if self.check_move([from_idx,to_idx], sym, roll): + legal_moves.append([from_idx,to_idx]) return legal_moves @@ -95,15 +104,15 @@ class Board: else: 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.state[to] = 0 + self.state[to_idx] = 0 - self.state[to] += cur_sym + self.state[to_idx] += cur_sym