Skip to content

Commit

Permalink
version 1.2.0, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
houluy committed Feb 23, 2018
1 parent de2f6ac commit badda76
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 86 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ Bug fix

## chessboard 1.1.0
Add color

## chessboard 1.1.1 - 1.1.2
Bug fix

## chessboard 1.2.0
Stable version
114 changes: 64 additions & 50 deletions chessboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import copy
import math
import sys
import string
from itertools import combinations_with_replacement as comb
from colorline import cprint

__version__ = '1.1.0'

ASC_ONE = ord('1')
ASC_NINE = ord('9')
ASC_A = ord('A')
Expand All @@ -30,7 +29,8 @@ def __init__(self, board_size=3, win=3, ch_off='O', ch_def='X', ch_blank=' ', us
self.graph = []
self.board_size = board_size
self.win = win
if game_name:
self.game_name = game_name
if self.game_name:
if game_name == 'Gomoku':
self.board_size = 15
self.win = 5
Expand Down Expand Up @@ -95,14 +95,17 @@ def __repr__(self):
def str2state(self, pos_str):
return [int(x) for x in pos_str]

def get_column(self, column):
return [_[column] for _ in self.pos]

def compute_coordinate(self, index):
'''Compute two-dimension coordinate from one-dimension list'''
j = index%self.board_size
i = (index - j) // self.board_size
return (i, j)

def count_round(self):
self._game_round = 0
self._game_round = 1
for ind_i, val_i in enumerate(self.pos):
for ind_j, val_j in enumerate(val_i):
if val_j != 0:
Expand Down Expand Up @@ -185,44 +188,22 @@ def asc2pos(self, ch):
def get_player(self):
return 2 - self._game_round % 2

def set_pos(self, x, y, user=None, check=False):
def set_pos(self, pos, check=False):
'''Set a chess'''
if isinstance(x, str):
x, y = self.asc2pos(x), self.asc2pos(y)
else:
x -= 1
y -= 1
if not user:
user = self.get_player()
if x not in self.pos_range or y not in self.pos_range or user not in range(1, self.user_number + 1):
raise ValueError('Position or user value is out of range')
elif self.pos[x][y] != 0:
raise PositionError('There is a chess piece on that position')
else:
self.history[self._game_round] = copy.deepcopy(self.pos)
self.pos[x][y] = user
self._game_round += 1
if check:
winning = self.check_win_by_step(x, y, user)
if winning is True:
return winning
return (x, y)

def set_pos_on_board_special(self, board, x, y, user, user_number=2):
'''Set a chess based on a specific chessboard'''
if isinstance(x, str):
x, y = self.asc2pos(x), self.asc2pos(y)
else:
x -= 1
y -= 1
board_size = len(board[0])
if x not in range(board_size) or y not in range(board_size) or user not in range(1, user_number + 1):
raise ValueError('Position or user value is out of range')
elif board[x][y] != 0:
raise PositionError('There is a chess piece on that position')
else:
board[x][y] = user
return board
try:
self.validate_pos(pos)
except Exception as e:
raise e
x, y = pos
user = self.get_player()
self.history[self._game_round] = copy.deepcopy(self.pos)
self.pos[x][y] = user
self._game_round += 1
if check:
winning = self.check_win_by_step(x, y, user)
if winning is True:
return winning
return (x, y)

def _transform(self, val):
return self.character.get(val)
Expand All @@ -243,31 +224,64 @@ def check_win(self):
'''
pass

def handle_input(self, input_str, place=True, user=None, check=False):
'''Transfer user input to valid chess position'''
def validate_input(self, input_str, val_pos=True):
input_str = input_str.replace(' ', '')
pos_str = input_str.split(',')
pos_xy = []
for pos in pos_str:
if len(pos) != 1:
raise ValueError('Error position, form: x, y, one character at most')
pos_num = self.asc2pos(pos)
pos_xy.append(pos_num)

if self.game_name == 'fourinarow':
y = pos_xy[0]
x = 0
else:
if len(pos_xy) != 2:
raise ValueError('Error position, must have both x and y coordinates')
x, y = pos_xy
if val_pos:
try:
self.validate_pos((x, y))
except PositionError as e:
raise e
if self.game_name == 'fourinarow':
x = self.get_not_num(self.get_column(y)) - 1
if x not in self.pos_range:
raise PositionError('This column is full')
return (x, y)

def validate_pos(self, pos):
x, y = pos
for p in pos:
if p not in self.pos_range:
raise PositionError('Position value is out of board\'s range')
if self.pos[x][y] != 0:
raise PositionError('There is a chess piece on that position')

def handle_input(self, input_str, place=True, user=None, check=False):
'''Transfer user input to valid chess position'''
if not user:
user = self.get_player()
if pos_str[0] == 'u':
if input_str[0] == 'u':
try:
self.undo(int(pos_str[1]))
self.undo(int(input_str[1]))
except ValueError as e:
raise e
else:
return None
else:
pos = self.validate_input(input_str)
if place:
if (len(pos_str) != 2):
raise PositionError('Error number of coordinates or commands!')
x, y = pos_str
try:
result = self.set_pos(x, y, user, check)
result = self.set_pos(pos, check)
except (ValueError, PositionError) as e:
raise e
else:
return result
else:
return pos_str
return pos

def distance(self, piecex, piecey):
'''Return the distance of chess piece X and Y (Chebyshev Distance)'''
Expand Down
1 change: 1 addition & 0 deletions chessboard/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '1.2.0'
2 changes: 1 addition & 1 deletion chessboardCLI.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 1.2
Name: chessboardCLI
Version: 1.1.0
Version: 1.2.0
Summary: Chessboard generator in command line
Home-page: https://github.com/houluy/chessboard
Author: Houlu
Expand Down
1 change: 1 addition & 0 deletions chessboardCLI.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
README.md
setup.py
chessboard/__init__.py
chessboard/version.py
chessboardCLI.egg-info/PKG-INFO
chessboardCLI.egg-info/SOURCES.txt
chessboardCLI.egg-info/dependency_links.txt
Expand Down
Binary file added dist/chessboardCLI-1.2.0.tar.gz
Binary file not shown.
52 changes: 18 additions & 34 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
import sys
from chessboard import Chessboard
from chessboard import Chessboard, PositionError
from colorline import cprint

def play_game():
from functools import partial

eprint = partial(cprint, color='r', bcolor='c', mode='highlight')

def main():
while True:
game_name = input('Please input the game name: ')
try:
board = Chessboard(game_name=game_name)
except ValueError as e:
cprint(e)
eprint(e)
continue
else:
break

board.print_pos()
while True:
player_number = board.get_player()
cprint('Player {}\'s turn: '.format(player_number), color='y', bcolor='c', end='')
ipt = input('')
try:
ipt = input('Input:')
except:
cprint('Input Error: try again.', color='g')
pos = board.handle_input(ipt, check=True)
except Exception as e:
eprint(e)
board.print_pos()
continue
if game_name != 'fourinarow':
try:
a = board.handle_input(ipt, check=True)
except Exception as e:
cprint(e, color='g')
board.print_pos()
continue
else:
a = board.handle_input(ipt, place=False)
if a is None:
board.print_pos()
continue
column_num = int(a[0])
current_col = [_[column_num - 1] for _ in board.pos]
current_row = board.get_not_num(current_col)
if current_row == 0:
cprint('No place to put your chess!', color='g')
continue
else:
try:
a = board.set_pos(x=current_row, y=column_num, check=True)
except (PositionError, Exception) as e:
cprint(e, color='g')
continue
if a is True:
cprint('player {} wins'.format(board.get_player()), color='y', bcolor='b')
if pos is True:
cprint('player {} wins'.format(player_number), color='y', bcolor='b')
board.print_pos()
sys.exit(0)
board.print_pos(coordinates=a)
board.print_pos(coordinates=pos)
#print(str(board))

if __name__ == '__main__':
play_game()
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from codecs import open
from os import path

from chessboard import __version__
from chessboard.version import __version__

here = path.abspath(path.dirname(__file__))

Expand Down

0 comments on commit badda76

Please sign in to comment.