Skip to content

Commit

Permalink
add Logo
Browse files Browse the repository at this point in the history
  • Loading branch information
houluy committed Mar 5, 2018
1 parent 19869f6 commit 086b443
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CocoaPods](https://img.shields.io/cocoapods/l/AFNetworking.svg?style=plastic)]()
[![PyPI](https://img.shields.io/pypi/status/Django.svg?style=plastic)]()
[![](https://img.shields.io/badge/version-1.2.0-ff69b4.svg?style=plastic)]()
[![](https://github.com/houluy/logo/blob/master/Logo.png)]()

A chessboard module for board games in Linux command-line

Expand Down
19 changes: 15 additions & 4 deletions chessboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MAX_CAP = MAX_NUM + 26
MAX_LOW = MAX_CAP + 26
DIR_NUM = 4
FULL_DIR_NUM = 8
sign = lambda a: (a > 10**(-10)) - (a < -10**(-10))

class PositionError(Exception):
Expand Down Expand Up @@ -79,6 +80,7 @@ def __init__(self, board_size=3, win=3, ch_off='O', ch_def='X', ch_blank=' ', us
self.check = {}
self.history = {}
self.angle = [_*math.pi/4 for _ in range(DIR_NUM)]
self.full_angle = [_*math.pi/4 for _ in range(FULL_DIR_NUM)]

def __str__(self):
return ''.join([''.join([str(x) for x in y]) for y in self.pos])
Expand Down Expand Up @@ -113,7 +115,7 @@ def within_range(self, pos):
return False

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

def get_all_pos(self, user):
pos_list = []
Expand Down Expand Up @@ -459,6 +461,8 @@ def __init__(self):
self.pos[4][4] = 1
self.pos[3][4] = 2
self.pos[4][3] = 2
self._user_pos_dict[1] = [(3, 3), (4, 4)]
self._user_pos_dict[2] = [(3, 4), (4, 3)]

def check_win(self):
count = self.count_chess()
Expand All @@ -470,18 +474,25 @@ def check_win(self):
def get_actions(self, player):
available_action = []
for chess in self._user_pos_dict[player]:
for angle in self.angle:
for angle in self.full_angle:
step = 1
oppo_chess_dict = {}
oppo_chess_count = 0
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):
print('Chess: {}, dir: {}, close: {}, player: {}'.format(chess, angle, close_pos, close_chess))
if close_chess == player or not self.within_range(close_pos):
print('1')
break
elif close_chess == self.another_player(player):
print('2')
step += 1
oppo_chess_count += 1
continue
else:
available_action.append(close_pos)
if oppo_chess_count > 0:
available_action.append(close_pos)
break
return available_action

Expand Down

0 comments on commit 086b443

Please sign in to comment.