Skip to content

Commit

Permalink
add some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
houluy committed Mar 4, 2018
1 parent 65d0bb0 commit 19869f6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions chessboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def pos_dict(self):
def user_pos_dict(self):
return self._user_pos_dict

def get_chess(self, pos):
return self.pos[pos[0]][pos[1]]

def within_range(self, pos):
if 0 < pos[0] < self.board_size and 0 < pos[1] < self.board_size:
return True
else:
return False

def get_close_chess(self, current, angle, step=1):
return (int(current + step*math.cos(angle)), int(current + step*math.sin(angle)))

def get_all_pos(self, user):
pos_list = []
for x, i in enumerate(self.pos):
Expand Down Expand Up @@ -200,6 +212,9 @@ def asc2pos(self, ch):

def get_player(self):
return 2 - self._game_round % 2

def another_player(self, player):
return 2 - (1 + player) % 2

def set_pos(self, pos, check=False):
'''Set a chess'''
Expand Down Expand Up @@ -452,8 +467,23 @@ def check_win(self):
else:
return False

def get_actions(self, user):
pass
def get_actions(self, player):
available_action = []
for chess in self._user_pos_dict[player]:
for angle in self.angle:
step = 1
while True:
close_pos = self.get_close_chess(chess, angle, step)
close_chess = self.get_chess(close_pos)
if close_chess == player or self.within_range(close_pos):
break
elif close_chess == self.another_player(player):
step += 1
continue
else:
available_action.append(close_pos)
break
return available_action

if __name__ == '__main__':
play_game()

0 comments on commit 19869f6

Please sign in to comment.