Skip to content

Commit 79d70c5

Browse files
KatieLGscrambledpieJustCallMeRayJadePrideauxkate-ellen
committed
Dojo 21st Jan session Group 1 & 2
Co-authored-by: Michael Pearce <scrambledpie@users.noreply.github.com> Co-authored-by: Ray <JustCallMeRay@users.noreply.github.com> Co-authored-by: Jade Prideaux <JadePrideaux@users.noreply.github.com> Co-authored-by: kate-ellen <kate-ellen@users.noreply.github.com>
1 parent 621ced1 commit 79d70c5

6 files changed

Lines changed: 287 additions & 0 deletions

File tree

2026-01-21/Group-1/main.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#! python3
2+
"""
3+
Hello group 1!
4+
5+
Given a grid of mines generate the minesweeper grid
6+
7+
_XX_X_____
8+
_____X____
9+
__________
10+
____XXX___
11+
__________
12+
___X__X___
13+
_____X____
14+
__________
15+
__X_X_____
16+
_____X____
17+
18+
- for a cell:
19+
- find neighbours with an X
20+
- count
21+
- add number
22+
- output format:
23+
- numbers and Xs
24+
"""
25+
26+
RAW_GRID = """_XX_X_____
27+
_____X____
28+
__________
29+
____XXX___
30+
__________
31+
___X__X___
32+
_____X____
33+
__________
34+
__X_X_____
35+
_____X____"""
36+
GRID = RAW_GRID.split("\n")
37+
38+
DIRECTIONS = [
39+
(-1, 1), (0, 1), (1, 1),
40+
(-1, 0), (1, 0),
41+
(-1, -1), (0, -1), (1, -1),
42+
]
43+
44+
import rando
45+
46+
def random_grid() -> str:
47+
''''''
48+
width = random.randint(5, 10)
49+
height = random.randint(5, 10)
50+
51+
grid = ""
52+
count = 0
53+
54+
print(width)
55+
print(height)
56+
57+
for i in range(width):
58+
print(count)
59+
count =+ 1
60+
if (count - width) == 0:
61+
print("new line")
62+
grid += "\n"
63+
count = 0
64+
elif random.randint(0, 5) == 1:
65+
grid += "X"
66+
else:
67+
grid += "_"
68+
69+
return grid
70+
71+
def for_each_cell(input: str):
72+
...
73+
74+
def get_bounds(input:str) -> tuple[int, int]:
75+
all_lines = input.splitlines()
76+
assert len(all_lines) >= 2
77+
line_length = len(all_lines[0])
78+
assert line_length >= 2
79+
80+
assert all([ len(l) == line_length for l in all_lines ])
81+
82+
return line_length, len(all_lines)
83+
84+
def get_neighbour(data: list[str], i: int, j: int, dir: tuple[int, int]) -> int:
85+
"""
86+
Returns 1 if neighbor is a mine, 0 otherwise
87+
"""
88+
pos = (i + dir[0], j + dir[1])
89+
90+
if i < 0 or j < 0:
91+
return 0
92+
elif i > len(GRID) or j > len(GRID[0]):
93+
return 0
94+
95+
char = GRID[pos[0]][pos[1]]
96+
return 1 if char == "X" else 0
97+
98+
99+
def count_neighbours(data: list[str], i: int, j: int) -> int:
100+
return sum(map(lambda x: get_neighbour(i, j, x), DIRECTIONS))
101+
102+
new_grid = []
103+
if __name__ == "__main__":
104+
for i, row in enumerate(GRID):
105+
new_row = ""
106+
for j, col in enumerate(row):
107+
if col == "X":
108+
new_row.append("X")
109+
else: ghbours = count_neighbours(data, i, j)
110+
111+
112+
new_row.append()neighbours
113+
114+
115+
print(new_grid)

2026-01-21/Group-2/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# TODO
2+
- random mine generator: mine_locs = f(lenght, width)
3+
- cell counter : coubts_grid = count(mine_locs, length, width)
4+
- print to terminal: print(count_grid)
5+
6+
Example grid:
7+
_XX_X_____
8+
_____X____
9+
__________
10+
____XXX___
11+
__________
12+
___X__X___
13+
_____X____
14+
__________
15+
__X_X_____
16+
_____X____
17+
18+
Expected output:
19+
1XX2X2____
20+
12222X1___
21+
___12421__
22+
___1XXX1__
23+
___23432__
24+
___X22X1__
25+
___12X21__
26+
_11122____
27+
_1X2X2____
28+
_1122X1___

2026-01-21/Group-2/main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SAMPLE_GRID = """_XX_X_____
2+
_____X____
3+
__________
4+
____XXX___
5+
__________
6+
___X__X___
7+
_____X____
8+
__________
9+
__X_X_____
10+
_____X____"""
11+
12+
13+
print(__import__("os").listdir("."))
14+
15+

2026-01-21/Group-2/radnom_mines.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import random
2+
3+
4+
def make_mine_grid(
5+
height: int = 11,
6+
width: int = 12,
7+
ratio: float = 0.1,
8+
) -> list[list[int]]:
9+
"""
10+
Make a grid of booleans for bomb locations.
11+
"""
12+
grid = [[0 for _ in range(width)] for _ in range(height)]
13+
14+
for row_idx in range(height):
15+
for col_idx in range(width):
16+
if random.random() < ratio:
17+
grid[row_idx][col_idx] = 1
18+
19+
return grid
20+
21+
22+
def number_to_emoji(n: int) -> str:
23+
"""Make it angrier red as the number increases \033[38;2;146;255;12m"""
24+
intensity = [255, 150, 110, 80, 60, 40, 30, 0]
25+
26+
r, g, b = (255, intensity[n], intensity[n])
27+
return f"\033[38;2;{r};{g};{b};12m{n}"
28+
29+
30+
def do_the_thing(input: list[list[bool]]) -> None:
31+
for y, row in enumerate(input):
32+
for x, v in enumerate(row):
33+
if v:
34+
print("💣", end="")
35+
continue
36+
37+
adjacent_mines = 0
38+
for i in range(-1, 2):
39+
for j in range(-1, 2):
40+
if i == 0 and j == 0:
41+
continue
42+
yy = y + i
43+
xx = x + j
44+
if yy < 0 or xx < 0 or yy >= len(input) or xx >= len(row):
45+
continue
46+
if input[yy][xx]:
47+
adjacent_mines += 1
48+
print(f"{number_to_emoji(adjacent_mines)}", end=" ")
49+
print()
50+
51+
52+
if __name__ == "__main__":
53+
from pprint import pprint
54+
55+
grid = make_mine_grid()
56+
57+
pprint(grid)
58+
print("\n\n")
59+
60+
do_the_thing(grid)

2026-01-21/Group-2/results.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
3+
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
4+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
5+
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
6+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
7+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
8+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
9+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
10+
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0],
11+
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]
12+
1 1 0 0 0 0 0 0 0 1 1 1
13+
💣2 1 1 0 0 1 1 1 1 💣1
14+
1 2 💣2 1 0 1 💣1 1 1 1
15+
1 3 4 💣1 0 1 1 1 0 0 0
16+
1 💣💣2 1 1 2 3 2 1 0 0
17+
1 2 2 1 0 1 💣💣💣1 0 0
18+
0 0 0 0 0 1 2 3 2 1 0 0
19+
0 0 1 1 1 0 0 0 0 0 0 0
20+
0 0 1 💣2 2 1 1 0 1 1 1
21+
0 0 2 3 💣2 💣1 0 1 💣1
22+
0 0 1 💣2 2 1 1 0 1 1 1

2026-01-21/Group-2/test_main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
3+
TEST_CASES = {
4+
"""_XX_X_____
5+
_____X____
6+
__________
7+
____XXX___
8+
__________
9+
___X__X___
10+
_____X____
11+
__________
12+
__X_X_____
13+
_____X____""": """_XX_X_____
14+
_____X____
15+
__________
16+
____XXX___
17+
__________
18+
___X__X___
19+
_____X____
20+
__________
21+
__X_X_____
22+
_____X____""",
23+
"""_XX_X_____
24+
_____X____
25+
__________
26+
____XXX___
27+
__________
28+
___X__X___
29+
_____X____
30+
__________
31+
__X_X_____
32+
_____X____""": """_XX_X_____
33+
_____X____
34+
__________
35+
____XXX___
36+
__________
37+
___X__X___
38+
_____X____
39+
__________
40+
__X_X_____
41+
_____X____""",
42+
}
43+
44+
45+
@pytest.mark.parametrize("grid,expected", TEST_CASES.items())
46+
def test_main(grid: str, expected: str) -> None:
47+
assert grid == expected

0 commit comments

Comments
 (0)