Skip to content

Commit 5d194c6

Browse files
committed
shuffling
1 parent 7021290 commit 5d194c6

6 files changed

Lines changed: 90 additions & 79 deletions

File tree

21_tictactoe/solution1.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def get_args():
1313
description='Tic-Tac-Toe',
1414
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
1515

16-
parser.add_argument('-s',
17-
'--state',
18-
help='Board state',
16+
parser.add_argument('-b',
17+
'--board',
18+
help='The state of the board',
1919
metavar='str',
2020
type=str,
2121
default='.' * 9)
@@ -41,10 +41,10 @@ def get_args():
4141
if any([args.player, args.cell]) and not all([args.player, args.cell]):
4242
parser.error('Must provide both --player and --cell')
4343

44-
if not re.search('^[.XO]{9}$', args.state):
45-
parser.error(f'--state "{args.state}" must be 9 characters of ., X, O')
44+
if not re.search('^[.XO]{9}$', args.board):
45+
parser.error(f'--board "{args.board}" must be 9 characters of ., X, O')
4646

47-
if args.player and args.cell and args.state[args.cell - 1] in 'XO':
47+
if args.player and args.cell and args.board[args.cell - 1] in 'XO':
4848
parser.error(f'--cell "{args.cell}" already taken')
4949

5050
return args
@@ -55,23 +55,23 @@ def main():
5555
"""Make a jazz noise here"""
5656

5757
args = get_args()
58-
state = list(args.state)
58+
board = list(args.board)
5959
player = args.player
6060
cell = args.cell
6161

6262
if player and cell:
63-
state[cell - 1] = player
63+
board[cell - 1] = player
6464

65-
print(format_board(state))
66-
winner = find_winner(state)
65+
print(format_board(board))
66+
winner = find_winner(board)
6767
print(f'{winner} has won!' if winner else 'No winner.')
6868

6969

7070
# --------------------------------------------------
71-
def format_board(state):
71+
def format_board(board):
7272
"""Format the board"""
7373

74-
cells = [str(i) if c == '.' else c for i, c in enumerate(state, 1)]
74+
cells = [str(i) if c == '.' else c for i, c in enumerate(board, start=1)]
7575
bar = '-------------'
7676
cells_tmpl = '| {} | {} | {} |'
7777
return '\n'.join([
@@ -83,15 +83,15 @@ def format_board(state):
8383

8484

8585
# --------------------------------------------------
86-
def find_winner(state):
86+
def find_winner(board):
8787
"""Return the winner"""
8888

8989
winning = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7],
9090
[2, 5, 8], [0, 4, 8], [2, 4, 6]]
9191

9292
for player in ['X', 'O']:
9393
for i, j, k in winning:
94-
combo = [state[i], state[j], state[k]]
94+
combo = [board[i], board[j], board[k]]
9595
if combo == [player, player, player]:
9696
return player
9797

21_tictactoe/solution2.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def get_args():
1313
description='Tic-Tac-Toe',
1414
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
1515

16-
parser.add_argument('-s',
17-
'--state',
18-
help='Board state',
16+
parser.add_argument('-b',
17+
'--board',
18+
help='The state of the board',
1919
metavar='str',
2020
type=str,
2121
default='.' * 9)
@@ -41,10 +41,10 @@ def get_args():
4141
if any([args.player, args.cell]) and not all([args.player, args.cell]):
4242
parser.error('Must provide both --player and --cell')
4343

44-
if not re.search('^[.XO]{9}$', args.state):
45-
parser.error(f'--state "{args.state}" must be 9 characters of ., X, O')
44+
if not re.search('^[.XO]{9}$', args.board):
45+
parser.error(f'--board "{args.board}" must be 9 characters of ., X, O')
4646

47-
if args.player and args.cell and args.state[args.cell - 1] in 'XO':
47+
if args.player and args.cell and args.board[args.cell - 1] in 'XO':
4848
parser.error(f'--cell "{args.cell}" already taken')
4949

5050
return args
@@ -55,24 +55,24 @@ def main():
5555
"""Make a jazz noise here"""
5656

5757
args = get_args()
58-
state = list(args.state)
58+
board = list(args.board)
5959
player = args.player
6060
cell = args.cell
6161

6262
if player and cell:
63-
state[cell - 1] = player
63+
board[cell - 1] = player
6464

65-
print(format_board(state))
66-
winner = find_winner(state)
65+
print(format_board(board))
66+
winner = find_winner(board)
6767
print(f'{winner} has won!' if winner else 'No winner.')
6868

6969

7070
# --------------------------------------------------
71-
def format_board(state):
71+
def format_board(board):
7272
"""Format the board"""
7373

7474
cells = []
75-
for i, char in enumerate(state, start=1):
75+
for i, char in enumerate(board, start=1):
7676
cells.append(str(i) if char == '.' else char)
7777

7878
bar = '-------------'
@@ -86,14 +86,14 @@ def format_board(state):
8686

8787

8888
# --------------------------------------------------
89-
def find_winner(state):
89+
def find_winner(board):
9090
"""Return the winner"""
9191

9292
winning = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7],
9393
[2, 5, 8], [0, 4, 8], [2, 4, 6]]
9494

9595
for combo in winning:
96-
group = list(map(lambda i: state[i], combo))
96+
group = list(map(lambda i: board[i], combo))
9797
for player in ['X', 'O']:
9898
if all(x == player for x in group):
9999
return player

21_tictactoe/test.py

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def test_no_input():
4848

4949

5050
# --------------------------------------------------
51-
def test_bad_state():
52-
"""dies on bad state"""
51+
def test_bad_board():
52+
"""dies on bad board"""
5353

54-
expected = '--state "{}" must be 9 characters of ., X, O'
54+
expected = '--board "{}" must be 9 characters of ., X, O'
5555

5656
for bad in ['ABC', '...XXX', 'XXXOOOXX']:
57-
rv, out = getstatusoutput(f'{prg} --state {bad}')
57+
rv, out = getstatusoutput(f'{prg} --board {bad}')
5858
print(out)
5959
assert rv != 0
6060
assert re.search(expected.format(bad), out)
@@ -104,10 +104,10 @@ def test_both_player_and_cell():
104104

105105

106106
# --------------------------------------------------
107-
def test_good_state():
107+
def test_good_board_01():
108108
"""makes board on good input"""
109109

110-
board1 = """
110+
board = """
111111
-------------
112112
| 1 | 2 | 3 |
113113
-------------
@@ -118,11 +118,17 @@ def test_good_state():
118118
No winner.
119119
""".strip()
120120

121-
rv1, out1 = getstatusoutput(f'{prg} -s .........')
122-
assert rv1 == 0
123-
assert out1.strip() == board1
121+
rv, out = getstatusoutput(f'{prg} -b .........')
122+
print(f'rv = "{rv}"')
123+
assert rv == 0
124+
assert out.strip() == board
125+
126+
127+
# --------------------------------------------------
128+
def test_good_board_02():
129+
"""makes board on good input"""
124130

125-
board2 = """
131+
board = """
126132
-------------
127133
| 1 | 2 | 3 |
128134
-------------
@@ -133,15 +139,15 @@ def test_good_state():
133139
No winner.
134140
""".strip()
135141

136-
rv2, out2 = getstatusoutput(f'{prg} -s ...OXX...')
137-
assert out2.strip() == board2
142+
rv, out = getstatusoutput(f'{prg} --board ...OXX...')
143+
assert out.strip() == board
138144

139145

140146
# --------------------------------------------------
141-
def test_mutate_state():
147+
def test_mutate_board_01():
142148
"""mutates board on good input"""
143149

144-
board1 = """
150+
board = """
145151
-------------
146152
| X | 2 | 3 |
147153
-------------
@@ -152,11 +158,16 @@ def test_mutate_state():
152158
No winner.
153159
""".strip()
154160

155-
rv1, out1 = getstatusoutput(f'{prg} -s ......... --player X -c 1')
156-
assert rv1 == 0
157-
assert out1.strip() == board1
161+
rv, out = getstatusoutput(f'{prg} -b ......... --player X -c 1')
162+
assert rv == 0
163+
assert out.strip() == board
164+
158165

159-
board2 = """
166+
# --------------------------------------------------
167+
def test_mutate_board_02():
168+
"""mutates board on good input"""
169+
170+
board = """
160171
-------------
161172
| X | X | O |
162173
-------------
@@ -167,53 +178,53 @@ def test_mutate_state():
167178
O has won!
168179
""".strip()
169180

170-
rv2, out2 = getstatusoutput(f'{prg} --state XXO...OOX --p O -c 5')
171-
assert rv2 == 0
172-
assert out2.strip() == board2
181+
rv, out = getstatusoutput(f'{prg} --board XXO...OOX --p O -c 5')
182+
assert rv == 0
183+
assert out.strip() == board
173184

174185

175186
# --------------------------------------------------
176-
def test_mutate_state_taken():
187+
def test_mutate_cell_taken():
177188
"""test for a cell already taken"""
178189

179-
rv1, out1 = getstatusoutput(f'{prg} -s XXO...OOX --player X --cell 9')
190+
rv1, out1 = getstatusoutput(f'{prg} -b XXO...OOX --player X --cell 9')
180191
assert rv1 != 0
181192
assert re.search('--cell "9" already taken', out1)
182193

183-
rv2, out2 = getstatusoutput(f'{prg} --state XXO...OOX --p O -c 1')
194+
rv2, out2 = getstatusoutput(f'{prg} --board XXO...OOX --p O -c 1')
184195
assert rv2 != 0
185196
assert re.search('--cell "1" already taken', out2)
186197

187198

188199
# --------------------------------------------------
189200
def test_winning():
190-
"""test winning states"""
201+
"""test winning boards"""
191202

192203
wins = [('PPP......'), ('...PPP...'), ('......PPP'), ('P..P..P..'),
193204
('.P..P..P.'), ('..P..P..P'), ('P...P...P'), ('..P.P.P..')]
194205

195206
for player in 'XO':
196207
other_player = 'O' if player == 'X' else 'X'
197208

198-
for state in wins:
199-
state = state.replace('P', player)
200-
dots = [i for i in range(len(state)) if state[i] == '.']
209+
for board in wins:
210+
board = board.replace('P', player)
211+
dots = [i for i in range(len(board)) if board[i] == '.']
201212
mut = random.sample(dots, k=2)
202-
test_state = ''.join([
203-
other_player if i in mut else state[i]
204-
for i in range(len(state))
213+
test_board = ''.join([
214+
other_player if i in mut else board[i]
215+
for i in range(len(board))
205216
])
206-
out = getoutput(f'{prg} -s {test_state}').splitlines()
217+
out = getoutput(f'{prg} -b {test_board}').splitlines()
207218
assert out[-1].strip() == f'{player} has won!'
208219

209220

210221
# --------------------------------------------------
211222
def test_losing():
212-
"""test losing states"""
223+
"""test losing boards"""
213224

214-
losing_state = list('XXOO.....')
225+
losing_board = list('XXOO.....')
215226
for i in range(10):
216-
random.shuffle(losing_state)
217-
print(f'{prg} {" ".join(losing_state)}')
218-
out = getoutput(f'{prg} -s {"".join(losing_state)}').splitlines()
227+
random.shuffle(losing_board)
228+
print(f'{prg} {" ".join(losing_board)}')
229+
out = getoutput(f'{prg} -b {"".join(losing_board)}').splitlines()
219230
assert out[-1].strip() == 'No winner.'

21_tictactoe/unit.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
# --------------------------------------------------
6-
def test_board_no_state():
6+
def test_board_no_board():
77
"""makes default board"""
88

99
board = """
@@ -20,7 +20,7 @@ def test_board_no_state():
2020

2121

2222
# --------------------------------------------------
23-
def test_board_with_state():
23+
def test_board_with_board():
2424
"""makes board"""
2525

2626
board = """
@@ -38,31 +38,31 @@ def test_board_with_state():
3838

3939
# --------------------------------------------------
4040
def test_winning():
41-
"""test winning states"""
41+
"""test winning boards"""
4242

4343
wins = [('PPP......'), ('...PPP...'), ('......PPP'), ('P..P..P..'),
4444
('.P..P..P.'), ('..P..P..P'), ('P...P...P'), ('..P.P.P..')]
4545

4646
for player in 'XO':
4747
other_player = 'O' if player == 'X' else 'X'
4848

49-
for state in wins:
50-
state = state.replace('P', player)
51-
dots = [i for i in range(len(state)) if state[i] == '.']
49+
for board in wins:
50+
board = board.replace('P', player)
51+
dots = [i for i in range(len(board)) if board[i] == '.']
5252
mut = random.sample(dots, k=2)
53-
test_state = ''.join([
54-
other_player if i in mut else state[i]
55-
for i in range(len(state))
53+
test_board = ''.join([
54+
other_player if i in mut else board[i]
55+
for i in range(len(board))
5656
])
57-
assert find_winner(test_state) == player
57+
assert find_winner(test_board) == player
5858

5959

6060
# --------------------------------------------------
6161
def test_losing():
62-
"""test losing states"""
62+
"""test losing boards"""
6363

64-
losing_state = list('XXOO.....')
64+
losing_board = list('XXOO.....')
6565

6666
for i in range(10):
67-
random.shuffle(losing_state)
68-
assert find_winner(''.join(losing_state)) == None
67+
random.shuffle(losing_board)
68+
assert find_winner(''.join(losing_board)) == None

0 commit comments

Comments
 (0)