#
# Janvier 2024
+from random import randint
+
# Constants
-N = 10
+N = 9
tab_human = []
# init table
def init_table(tab):
- return
+ for i in range(N):
+ tab.append([])
+ for j in range(N):
+ tab[i].append(' ')
+
+# create line
+def create_line(prefix):
+ line = "+"
+ for i in range(N):
+ line = line + "-+"
+ return(prefix + line)
# display table
def display(main_tab, second_tab=[]):
-
+
+ # display header
+ lines = [" Human "]
+ lines.append(" ")
+ for j in range(N):
+ lines[1] = lines[1] + " " + str(j + 1)
+
# display all symbols in main tab
- print("foobar")
+ lines.append(create_line(" "))
+ for i in range(N):
+ line = str(i + 1) + "|"
+ for j in range(N):
+ line = line + main_tab[i][j] + "|"
+ lines.append(line)
+ lines.append(create_line(" "))
# if defined
if len(second_tab) != 0:
# only display 'O' and 'X'
- pass
-
- return
+
+ # display header
+ lines[0] = lines[0] + " Machine "
+ lines[1] = lines[1] + " "
+ for j in range(N):
+ lines[1] = lines[1] + " " + str(j + 1)
+
+ # display all symbols in main tab
+ lines[2] = lines[2] + create_line(" ")
+ for i in range(N):
+ line = " " + str(i + 1) + "|"
+ for j in range(N):
+ c = second_tab[i][j]
+ if c == 'S':
+ c = ' '
+ line = line + c + "|"
+ lines[2 * i + 3] = lines[2 * i + 3] + line
+ lines[2 * i + 4] = lines[2 * i + 4] + create_line(" ")
+
+ # display everything
+ for line in lines:
+ print(line)
# read coordonate
def read_coordonate(size, tab):
- return []
+ error = False
+ while True:
+ if error:
+ print("error!")
+ error = True
+ x = int(input("x-axis: "))
+ y = int(input("y-axis: "))
+ if (x <= 0) or (x > N) or (y <= 0) or (y > N):
+ continue
+ dir_x = dir_y = 0
+ if size > 1:
+ o = input("orientation (s/e): ")
+ if (o == 's') or (o == 'S'):
+ dir_x = 1
+ if x + size - 1 > N:
+ continue
+ elif (o == 'e') or (o == 'E'):
+ dir_y = 1
+ if y + size - 1 > N:
+ continue
+ else:
+ continue
+ pos = []
+ for k in range(size):
+ if tab[x + dir_x * k - 1][y + dir_y * k - 1] != ' ':
+ pos = []
+ break
+ pos.append({"x": x + dir_x * k - 1, "y": y + dir_y * k - 1})
+ if len(pos) > 0:
+ break
+
+ return pos
# generate coordonate
def generate_coordonate(size, tab):
- return []
+ error = False
+ while True:
+ if error:
+ print("error!")
+ error = True
+ x = randint(N)
+ y = randint(N)
+ if (x <= 0) or (x > N) or (y <= 0) or (y > N):
+ continue
+ dir_x = dir_y = 0
+ o = randint(2)
+ if o == 0:
+ dir_x = 1
+ if x + size - 1 > N:
+ continue
+ elif o == 1:
+ dir_x = 0
+ if y + size - 1 > N:
+ continue
+ pos = []
+ for k in range(size):
+ if tab[x + dir_x * k - 1][y + dir_y * k - 1] != ' ':
+ pos = []
+ break
+ pos.append({"x": x + dir_x * k - 1, "y": y + dir_y * k - 1})
+ if len(pos) > 0:
+ break
+ return pos
# put element into table
def put_in_table(element, pos, tab):
- return
+ if element == 'B':
+ if tab[pos.get("x")][pos.get("y")] == 'S':
+ tab[pos.get("x")][pos.get("y")] = 'X'
+ else:
+ tab[pos.get("x")][pos.get("y")] = 'O'
+ else:
+ tab[pos.get("x")][pos.get("y")] = 'S'
# check table status
def check_status(tab):
+ for i in range(N):
+ for j in range(N):
+ if tab[i][j] == 'S':
+ return False
return True
# main function
display(tab_human)
print("Where do you want to place a ship of", ship, "?")
pos = read_coordonate(ship, tab_human)
- put_in_table('S', pos, tab_human)
+ for p in pos:
+ print(p)
+ put_in_table('S', p, tab_human)
# init table for machine
init_table(tab_machine)
for ship in ships:
display(tab_machine)
pos = generate_coordonate(ship, tab_machine)
- put_in_table('S', pos, tab_machine)
+ for p in pos:
+ put_in_table('S', p, tab_machine)
# main loop
while True: