Skip to content

Commit

Permalink
version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
houluy committed Feb 21, 2018
1 parent 47ac4f5 commit de2f6ac
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 66 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ Bug fix and Exception handles

## chessboard 1.0.14
Bug fix

## chessboard 1.1.0
Add color
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# chessboard
A chessboard module for board games in CLI
A chessboard module for board games in Linux command-line

Use
```
Expand Down
91 changes: 28 additions & 63 deletions chessboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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 Down Expand Up @@ -38,11 +40,13 @@ def __init__(self, board_size=3, win=3, ch_off='O', ch_def='X', ch_blank=' ', us
elif game_name == 'fourinarow':
self.board_size = 7
self.win = 4
elif game_name == 'Reversi':
self.board_size = 8
elif game_name == 'normal':
self.board_size = int(input('Board size: '))
self.win = int(input('Winning chess number: '))
else:
raise ValueError('Unsupported game, please use original Chessboard class!')
raise ValueError('Unsupported game, please refer to docs!')
elif pos:
self.win = win
if isinstance(pos, str):
Expand Down Expand Up @@ -71,6 +75,13 @@ def __init__(self, board_size=3, win=3, ch_off='O', ch_def='X', ch_blank=' ', us

self.count_round()
self.user_number = user_number
self.chess_number = [0 for x in range(self.user_number)]
if game_name == 'Reversi':
self.chess_number = [2, 2]
self.pos[3][3] = 1
self.pos[4][4] = 1
self.pos[3][4] = 2
self.pos[4][3] = 2
self.check = {}
self.history = {}
self.angle = [_*math.pi/4 for _ in range(DIR_NUM)]
Expand All @@ -91,7 +102,7 @@ def compute_coordinate(self, index):
return (i, j)

def count_round(self):
self._game_round = 1
self._game_round = 0
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 @@ -308,8 +319,17 @@ def count_chess(self):

class ChessboardExtension(Chessboard):
'''Provide extended methods for Class Chessboard'''
def __init__(self, board_size=3, win=3, ch_off='O', ch_def='X', ch_blank=' ', user_number=2, game_name=None):
super().__init__(board_size=board_size, win=win, ch_off=ch_off, ch_def=ch_def, ch_blank=ch_blank, user_number=user_number, game_name=game_name)
def __init__(self, board_size=3, win=3, ch_off='O', ch_def='X', ch_blank=' ', user_number=2, game_name=None, pos=None, nested=False):
super().__init__(
board_size=board_size,
win=win,
ch_off=ch_off,
ch_def=ch_def,
ch_blank=ch_blank,
user_number=user_number,
game_name=game_name,
pos=pos,
nested=nested)

def compare_board(self, dst, src=None):
'''Compare two chessboard'''
Expand All @@ -323,22 +343,21 @@ def compare_board(self, dst, src=None):
return False

def diff_state(self, obj, cur=None):
assert isinstance(obj, list)
if not cur:
cur = self.tostate()
assert len(obj) == len(cur)

diff_pos = []
for i, x in enumerate(cur):
if x != obj[i]:
if str(x) != str(obj[i]):
diff_pos.append(i)

return diff_pos

def coor_trans(self, two=None, one=None):
if two:
if two is not None:
return two[0]*self.board_size + two[1] - 1
elif one:
elif one is not None:
y = one%self.board_size
x = (one - y)//self.board_size + 1
return (x, y)
Expand Down Expand Up @@ -381,59 +400,5 @@ def tostate(self, pos=None):
pos = self.pos
return [y for x in pos for y in x]

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

board.print_pos()
while True:
try:
ipt = input('Input:')
except:
cprint('Input Error: try again.', color='g')
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')
board.print_pos()
sys.exit(0)
board.print_pos(coordinates=a)
#print(str(board))

if __name__ == '__main__':
#play_game()
board = ChessboardExtension()
d = board.diff_state(obj=[0,0,0,0,0,0,0,0,0])
print(d)
print(board.coor_trans(one=5))
#print(board.get_action())
play_game()
20 changes: 20 additions & 0 deletions chessboardCLI.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Metadata-Version: 1.2
Name: chessboardCLI
Version: 1.1.0
Summary: Chessboard generator in command line
Home-page: https://github.com/houluy/chessboard
Author: Houlu
Author-email: houlu8674@bupt.edu.cn
License: MIT
Description: UNKNOWN
Keywords: chessboard
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Requires-Python: >=3
9 changes: 9 additions & 0 deletions chessboardCLI.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
README.md
setup.py
chessboard/__init__.py
chessboardCLI.egg-info/PKG-INFO
chessboardCLI.egg-info/SOURCES.txt
chessboardCLI.egg-info/dependency_links.txt
chessboardCLI.egg-info/requires.txt
chessboardCLI.egg-info/top_level.txt
test/test.py
1 change: 1 addition & 0 deletions chessboardCLI.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions chessboardCLI.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
colorline>=1.0.3
1 change: 1 addition & 0 deletions chessboardCLI.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chessboard
Binary file added dist/chessboardCLI-1.1.0.tar.gz
Binary file not shown.
55 changes: 55 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import sys
from chessboard import Chessboard
from colorline import cprint

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

board.print_pos()
while True:
try:
ipt = input('Input:')
except:
cprint('Input Error: try again.', color='g')
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')
board.print_pos()
sys.exit(0)
board.print_pos(coordinates=a)
#print(str(board))

if __name__ == '__main__':
play_game()
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from codecs import open
from os import path

from chessboard import __version__

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

setup(
name='chessboardCLI',
version='1.0.23',
version=__version__,
description='Chessboard generator in command line',
url='https://github.com/houluy/chessboard',
author='Houlu',
Expand All @@ -26,7 +28,7 @@
keywords='chessboard',
packages=['chessboard',],
install_requires=[
'colorline',
'colorline>=1.0.3',
],
python_requires='>=3',
)

0 comments on commit de2f6ac

Please sign in to comment.