loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import unittest

class CreateBoard(unittest.TestCase):
  def test_create_board(self):
    expected_board = [[None, None, None], [None, None, None], [None, None, None]]
    self.assertEqual(expected_board, create_board(3))

  def test_format_board_for_cli(self):
    board = create_board(3)
    expected_print_string = "\n| 1 | 2 | 3 |\n-------------\n| 4 | 5 | 6 |\n-------------\n| 7 | 8 | 9 |\n"
    self.assertEqual(expected_print_string, format_board_for_cli(board))

  def test_mark_square(self):
    board = create_board(3)
    square = 1
    icon = "x"
    row_size = 3
    expected_marked_board = [['x', None, None], [None, None, None], [None, None, None]]
    self.assertEqual(expected_marked_board, mark_square(board, square, icon, row_size))

    

def create_board(row_size):
    return [[None] * row_size] * row_size
    #return [[None for _ in range(row_size)] for _ in range(row_size)]

def rows(board):
    return board

def get_row_at(board, index):
    return rows(board)[index]

def format_board_for_cli(board):
    board_array = rows(board)
    board_string = ""
    row_size = len(board_array)
    flattened_list = [item for sublist in board_array for item in sublist]
    index_replaced_list = [(idx if val == None else val) for idx, val in enumerate(flattened_list, start=1)]
    result = list(map(lambda x: "| " + str(x) + " ", index_replaced_list))
    for idx, val in enumerate(result, start=1):
        if idx % row_size == 0 and idx / row_size != row_size:
            board_string += val + "|\n-------------\n" 
        else:
            board_string += val
    return "\n" + board_string + "|\n"  

def square_to_rows_and_cols(square, row_size):
    return [ 
        (square - 1) // row_size,
        (square - 1) % row_size
    ]      

def mark_square(board, square, icon, row_size):
    coordinates = square_to_rows_and_cols(square, row_size)
    row_coordinate = coordinates[0]
    column_coordinate = coordinates[1]
    row_to_update = get_row_at(board, row_coordinate)
    row_to_update[column_coordinate] = icon
    return board

if __name__ == '__main__':
    unittest.main()