Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c26ca1

Browse files
committed
adding READMEs, cleanup
1 parent f33e3d7 commit 7c26ca1

File tree

9 files changed

+212
-19
lines changed

9 files changed

+212
-19
lines changed

19_wod/manual3_map.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
with open('inputs/exercises.csv') as fh:
66
headers = fh.readline().rstrip().split(',')
7-
records = list(
8-
map(lambda line: dict(zip(headers,
9-
line.rstrip().split(','))), fh))
10-
pprint(records)
7+
mk_rec = lambda line: dict(zip(headers, line.rstrip().split(',')))
8+
records = map(mk_rec, fh)
9+
pprint(list(records))

19_wod/manual4_map2.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

21_tictactoe/README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Tic-Tac-Toe
2+
3+
Create a Python program called `tictactoe.py` that will play a single round of the game Tic-Tac-Toe.
4+
The program should accept the following parameters:
5+
6+
* `-b`|`--board`: The optional state of the board for the play. This will be a string of 9 characters representing the 9 cells of the 3x3 board. The string should be composed only of `X` and `O` to denote a player occupying that cell or `.` to show that the cell is open. The default is 9 '.' as all cells are open.
7+
* `-p`|`--player`: An optional player which must be either `X` or `O`.
8+
* `-c`|`--cell`: An optional cell which must be in the range 1-9 (inclusive).
9+
10+
Here is the usage the program should print for `-h` or `--help`:
11+
12+
```
13+
$ ./tictactoe.py -h
14+
usage: tictactoe.py [-h] [-b str] [-p str] [-c int]
15+
16+
Tic-Tac-Toe
17+
18+
optional arguments:
19+
-h, --help show this help message and exit
20+
-b str, --board str The state of the board (default: .........)
21+
-p str, --player str Player (default: None)
22+
-c int, --cell int Cell 1-9 (default: None)
23+
```
24+
25+
The program will print the state of the board plus any modifications to the state made by `--player` and `--cell` along with the final outcome of the game which can either be "No winner" or "{player} has won."
26+
27+
When run with no arguments, it should print a blank Tic-Tac-Toe board and "No winner":
28+
29+
```
30+
$ ./tictactoe.py
31+
-------------
32+
| 1 | 2 | 3 |
33+
-------------
34+
| 4 | 5 | 6 |
35+
-------------
36+
| 7 | 8 | 9 |
37+
-------------
38+
No winner.
39+
```
40+
41+
Given a valid `--player` trying to take an unoccupied `--cell`, the program should modify the state before printing the board and deciding the outcome:
42+
43+
```
44+
$ ./tictactoe.py -p X -c 1
45+
-------------
46+
| X | 2 | 3 |
47+
-------------
48+
| 4 | 5 | 6 |
49+
-------------
50+
| 7 | 8 | 9 |
51+
-------------
52+
No winner.
53+
```
54+
55+
The program should error out for a bad `--board`:
56+
57+
```
58+
$ ./tictactoe.py -b ABC......
59+
usage: tictactoe.py [-h] [-b str] [-p str] [-c int]
60+
tictactoe.py: error: --board "ABC......" must be 9 characters of ., X, O
61+
```
62+
63+
Or a bad `--cell`:
64+
65+
```
66+
$ ./tictactoe.py -p X -c 10
67+
usage: tictactoe.py [-h] [-b str] [-p str] [-c int]
68+
tictactoe.py: error: argument -c/--cell: invalid choice: 10 \
69+
(choose from 1, 2, 3, 4, 5, 6, 7, 8, 9)
70+
```
71+
72+
Or a bad `--player`:
73+
74+
```
75+
$ ./tictactoe.py -p A -c 1
76+
usage: tictactoe.py [-h] [-b str] [-p str] [-c int]
77+
tictactoe.py: error: argument -p/--player: invalid choice: 'A' \
78+
(choose from 'X', 'O')
79+
```
80+
81+
Or in the event a `--player` is trying to take an occupied `--cell`:
82+
83+
```
84+
$ ./tictactoe.py -b X........ -p O -c 1
85+
usage: tictactoe.py [-h] [-b str] [-p str] [-c int]
86+
tictactoe.py: error: --cell "1" already taken
87+
```
88+
89+
Or if only `--player` or `--cell` is provided:
90+
91+
```
92+
$ ./tictactoe.py --player X
93+
usage: tictactoe.py [-h] [-b board] [-p player] [-c cell]
94+
tictactoe.py: error: Must provide both --player and --cell
95+
```
96+
97+
The program should detect a winning state:
98+
99+
```
100+
$ ./tictactoe.py -b .XX....OO -p X -c 1
101+
-------------
102+
| X | X | X |
103+
-------------
104+
| 4 | 5 | 6 |
105+
-------------
106+
| 7 | O | O |
107+
-------------
108+
X has won!
109+
```
110+
111+
The program should pass all tests:
112+
113+
```
114+
$ make test
115+
pytest -xv test.py
116+
============================= test session starts ==============================
117+
...
118+
collected 15 items
119+
120+
test.py::test_exists PASSED [ 6%]
121+
test.py::test_usage PASSED [ 13%]
122+
test.py::test_no_input PASSED [ 20%]
123+
test.py::test_bad_board PASSED [ 26%]
124+
test.py::test_bad_player PASSED [ 33%]
125+
test.py::test_bad_cell_int PASSED [ 40%]
126+
test.py::test_bad_cell_str PASSED [ 46%]
127+
test.py::test_both_player_and_cell PASSED [ 53%]
128+
test.py::test_good_board_01 PASSED [ 60%]
129+
test.py::test_good_board_02 PASSED [ 66%]
130+
test.py::test_mutate_board_01 PASSED [ 73%]
131+
test.py::test_mutate_board_02 PASSED [ 80%]
132+
test.py::test_mutate_cell_taken PASSED [ 86%]
133+
test.py::test_winning PASSED [ 93%]
134+
test.py::test_losing PASSED [100%]
135+
136+
============================== 15 passed in 2.12s ==============================
137+
```

21_tictactoe/solution1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ def get_args():
1616
parser.add_argument('-b',
1717
'--board',
1818
help='The state of the board',
19-
metavar='str',
19+
metavar='board',
2020
type=str,
2121
default='.' * 9)
2222

2323
parser.add_argument('-p',
2424
'--player',
2525
help='Player',
2626
choices='XO',
27-
metavar='str',
27+
metavar='player',
2828
type=str,
2929
default=None)
3030

3131
parser.add_argument('-c',
3232
'--cell',
3333
help='Cell 1-9',
34-
metavar='int',
34+
metavar='cell',
3535
type=int,
3636
choices=range(1, 10),
3737
default=None)

21_tictactoe/solution2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ def get_args():
1616
parser.add_argument('-b',
1717
'--board',
1818
help='The state of the board',
19-
metavar='str',
19+
metavar='board',
2020
type=str,
2121
default='.' * 9)
2222

2323
parser.add_argument('-p',
2424
'--player',
2525
help='Player',
2626
choices='XO',
27-
metavar='str',
27+
metavar='player',
2828
type=str,
2929
default=None)
3030

3131
parser.add_argument('-c',
3232
'--cell',
3333
help='Cell 1-9',
34-
metavar='int',
34+
metavar='cell',
3535
type=int,
3636
choices=range(1, 10),
3737
default=None)

21_tictactoe/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def test_good_board_02():
140140
""".strip()
141141

142142
rv, out = getstatusoutput(f'{prg} --board ...OXX...')
143+
assert rv == 0
143144
assert out.strip() == board
144145

145146

22_itictactoe/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Interactive Tic-Tac-Toe
2+
3+
Write a Python program called `itictactoe.py` that will play an interactive game of Tic-Tac-Toe starting from a blank board and iterating between players `X` and `O` until the game is finished due to a draw or a win.
4+
When the game starts, a blank board with cells 1-9 should be shown along with a prompt for the current player (always starting with `X`) to select a cell:
5+
6+
```
7+
-------------
8+
| 1 | 2 | 3 |
9+
-------------
10+
| 4 | 5 | 6 |
11+
-------------
12+
| 7 | 8 | 9 |
13+
-------------
14+
Player X, what is your move? [q to quit]: 1
15+
```
16+
17+
If a player tries to select an occupied cell, the move is disallowed and the same player goes until a valid choice is made:
18+
19+
```
20+
-------------
21+
| X | 2 | 3 |
22+
-------------
23+
| 4 | 5 | 6 |
24+
-------------
25+
| 7 | 8 | 9 |
26+
-------------
27+
Player O, what is your move? [q to quit]: 1
28+
-------------
29+
| X | 2 | 3 |
30+
-------------
31+
| 4 | 5 | 6 |
32+
-------------
33+
| 7 | 8 | 9 |
34+
-------------
35+
Cell "1" already taken
36+
Player O, what is your move? [q to quit]:
37+
```
38+
39+
Play should stop when a player has won:
40+
41+
```
42+
-------------
43+
| X | O | 3 |
44+
-------------
45+
| X | O | 6 |
46+
-------------
47+
| 7 | 8 | 9 |
48+
-------------
49+
Player X, what is your move? [q to quit]: 7
50+
X has won!
51+
```
52+
53+
Or when the game is a draw:
54+
55+
```
56+
-------------
57+
| X | O | O |
58+
-------------
59+
| O | X | X |
60+
-------------
61+
| X | 8 | O |
62+
-------------
63+
Player X, what is your move? [q to quit]: 8
64+
All right, we'll call it a draw.
65+
```
File renamed without changes.

0 commit comments

Comments
 (0)