|
1 |
| -### components.py |
| 1 | +""" |
| 2 | +All basic functionality across both web and command-line interfaces are developed |
| 3 | +as functions dealing with initialisation and setup of the game as well as interacting |
| 4 | +with external files for ships and their placements on the player's board. |
| 5 | +
|
| 6 | +Imports random and json are from the Python Standard Library so no installs are needed |
| 7 | +and no constants were required for the basic functionality, so simplistic testing was |
| 8 | +used to ensure functionality and reliability. |
| 9 | +""" |
| 10 | + |
| 11 | +## components.py |
2 | 12 | # imports
|
3 | 13 | import random
|
4 | 14 | import json
|
5 | 15 |
|
6 | 16 | def print_board(board: list) -> None:
|
7 |
| - """to print game board with clarity and consistecy (in testing) |
8 |
| - |
9 |
| - keyword arguments: |
10 |
| - board -- 2d list of ship placements to print to console |
11 |
| - """ |
| 17 | + """to print game board with clarity and consistency (in testing) |
| 18 | +
|
| 19 | + keyword arguments: |
| 20 | +
|
| 21 | + board -- 2d list of ship placements to print to console |
| 22 | + """ |
12 | 23 |
|
13 |
| - for row in board: |
14 |
| - print(*row, sep="\t") |
| 24 | + for row in board: |
| 25 | + print(*row, sep="\t") |
15 | 26 |
|
16 | 27 |
|
17 | 28 | def initialise_board() -> list:
|
18 |
| - """returns 2d array of size*size containing None values |
19 |
| - """ |
| 29 | + """returns 2d array of size*size containing None values |
| 30 | + """ |
20 | 31 |
|
21 |
| - with open("config.json", "r") as f: |
22 |
| - size = json.load(f)["board_size"] |
| 32 | + with open("config.json", mode="r", encoding="utf-8") as config_file: |
| 33 | + size = json.load(config_file)["board_size"] |
23 | 34 |
|
24 |
| - board = [[None for i in range(size)] for j in range(size)] |
25 |
| - return board |
| 35 | + board = [[None for i in range(size)] for j in range(size)] |
| 36 | + return board |
26 | 37 |
|
27 | 38 | # test:
|
28 | 39 | # print_board(initialise_board())
|
29 | 40 |
|
30 | 41 |
|
31 | 42 | def create_battleships() -> dict:
|
32 |
| - """returns dict of ships:size from given file |
33 |
| - """ |
| 43 | + """returns dict of {ships:size} from given file |
| 44 | + """ |
34 | 45 |
|
35 |
| - with open("config.json", "r") as f: |
36 |
| - filename = json.load(f)["battleship_file"] |
| 46 | + with open("config.json", mode="r", encoding="utf-8") as config_file: |
| 47 | + filename = json.load(config_file)["battleship_file"] |
37 | 48 |
|
38 |
| - with open(filename ,"r") as f: |
39 |
| - ships = {} |
40 |
| - for line in f: |
41 |
| - line = line.split(":") |
42 |
| - ships[line[0]] = int(line[1]) |
| 49 | + with open(filename , mode="r", encoding="utf-8") as file_name: |
| 50 | + ships = {} |
| 51 | + for line in file_name: |
| 52 | + line = line.split(":") |
| 53 | + ships[line[0]] = int(line[1]) |
43 | 54 |
|
44 |
| - return(ships) |
| 55 | + return ships |
45 | 56 |
|
46 | 57 | # test:
|
47 | 58 | # print(create_battleships())
|
48 | 59 |
|
49 | 60 |
|
50 | 61 | def place_battleships(board: list, ships: dict, method: str="simple") -> list:
|
51 |
| - """returns board with placed ships based on given chosen method |
52 |
| - |
53 |
| - keyword arguments: |
54 |
| - board -- empty 2d list to add ship placements as given by function initialise_board, |
55 |
| - ships -- dictionary of ship names and sizes as given by function create_battleships, |
56 |
| - method -- algorithm for assigning board positions to ships, default='simple' |
57 |
| - """ |
58 |
| - |
59 |
| - if method=="simple": |
60 |
| - i = 0 |
61 |
| - for ship, size in ships.items(): |
62 |
| - for n in range(size): |
63 |
| - board[i][n] = ship |
64 |
| - i += 1 |
65 |
| - |
66 |
| - elif method=="random": |
67 |
| - for ship, size in ships.items(): |
68 |
| - placed = False |
69 |
| - while not placed: |
70 |
| - orien = random.choice("hv") |
71 |
| - if orien == "h": |
72 |
| - try: |
73 |
| - posx = random.randint(0, len(board)-1) |
74 |
| - posy = random.randint(0, len(board)-1) |
75 |
| - possible = True |
76 |
| - for n in range(size): |
77 |
| - if board[posy][posx+n]: |
78 |
| - possible = False |
79 |
| - except: |
80 |
| - pass |
81 |
| - else: |
82 |
| - if possible: |
83 |
| - for n in range(size): |
84 |
| - board[posy][posx+n] = ship |
85 |
| - placed = True |
86 |
| - |
87 |
| - else: |
88 |
| - try: |
89 |
| - posx = random.randint(0, len(board)-1) |
90 |
| - posy = random.randint(0, len(board)-1) |
91 |
| - possible = True |
92 |
| - for n in range(size): |
93 |
| - if board[posy+n][posx]: |
94 |
| - possible = False |
95 |
| - except: |
96 |
| - pass |
97 |
| - else: |
98 |
| - if possible: |
99 |
| - for n in range(size): |
100 |
| - board[posy+n][posx] = ship |
101 |
| - placed = True |
102 |
| - |
103 |
| - elif method=="custom": |
104 |
| - with open("config.json", "r") as f: |
105 |
| - filename = json.load(f)["ship_placement_file"] |
106 |
| - |
107 |
| - with open(filename, "r") as f: |
108 |
| - placements = json.load(f) |
109 |
| - |
110 |
| - for ship, size in ships.items(): |
111 |
| - orien = placements[ship][2] |
112 |
| - posx, posy = int(placements[ship][0]), int(placements[ship][1]) |
113 |
| - |
114 |
| - if orien == "h": |
115 |
| - for n in range(size): |
116 |
| - board[posy][posx+n] = ship |
117 |
| - else: |
118 |
| - for n in range(size): |
119 |
| - board[posy+n][posx] = ship |
120 |
| - |
121 |
| - |
122 |
| - return board |
| 62 | + """returns board with placed ships based on given chosen method |
| 63 | +
|
| 64 | + keyword arguments: |
| 65 | +
|
| 66 | + board -- empty 2d list to add ship placements as given by function initialise_board, |
| 67 | +
|
| 68 | + ships -- dictionary of ship names and sizes as given by function create_battleships, |
| 69 | +
|
| 70 | + method -- algorithm for assigning board positions to ships, default='simple' |
| 71 | + """ |
| 72 | + |
| 73 | + if method=="simple": |
| 74 | + i = 0 |
| 75 | + for ship, size in ships.items(): |
| 76 | + for size_n in range(size): |
| 77 | + board[i][size_n] = ship |
| 78 | + i += 1 |
| 79 | + |
| 80 | + elif method=="random": |
| 81 | + for ship, size in ships.items(): |
| 82 | + placed = False |
| 83 | + while not placed: |
| 84 | + orien = random.choice("hv") |
| 85 | + posx = random.randint(0, len(board)-1) |
| 86 | + posy = random.randint(0, len(board)-1) |
| 87 | + possible = True |
| 88 | + |
| 89 | + try: |
| 90 | + for size_n in range(size): |
| 91 | + if orien == "h": |
| 92 | + if board[posy][posx+size_n]: |
| 93 | + possible = False |
| 94 | + else: |
| 95 | + if board[posy+size_n][posx]: |
| 96 | + possible = False |
| 97 | + except IndexError: |
| 98 | + pass |
| 99 | + else: |
| 100 | + if possible: |
| 101 | + for size_n in range(size): |
| 102 | + if orien == "h": |
| 103 | + board[posy][posx+size_n] = ship |
| 104 | + else: |
| 105 | + board[posy+size_n][posx] = ship |
| 106 | + placed = True |
| 107 | + |
| 108 | + elif method=="custom": |
| 109 | + with open("config.json", mode="r", encoding="utf-8") as config_file: |
| 110 | + filename = json.load(config_file)["ship_placement_file"] |
| 111 | + |
| 112 | + with open(filename, mode="r", encoding="utf-8") as file_name: |
| 113 | + placements = json.load(file_name) |
| 114 | + |
| 115 | + for ship, size in ships.items(): |
| 116 | + orien = placements[ship][2] |
| 117 | + posx, posy = int(placements[ship][0]), int(placements[ship][1]) |
| 118 | + |
| 119 | + if orien == "h": |
| 120 | + for size_n in range(size): |
| 121 | + if orien == "h": |
| 122 | + board[posy][posx+size_n] = ship |
| 123 | + else: |
| 124 | + board[posy+size_n][posx] = ship |
| 125 | + |
| 126 | + |
| 127 | + return board |
123 | 128 |
|
124 | 129 | # test:
|
125 | 130 | # print_board(place_battleships(initialise_board(), create_battleships(), method="custom"))
|
0 commit comments