Compare commits

..

No commits in common. "multiplayer" and "main" have entirely different histories.

3 changed files with 21 additions and 104 deletions

View File

@ -1,79 +0,0 @@
def make_board(size: int, filler = 0) -> list:
return [[filler for _ in range(size)] for _ in range(size)]
class DrawerHelper:
@staticmethod
def draw_columnsnrows(board) -> None:
max_num_len = len(str(len(board) - 1))
# Currently inserts the columns and rows into the board
for i in range(len(board)):
for j in range(len(board[i])):
if i == 0 and j == 0:
board[i][j] = " " * max_num_len
elif i == 0:
board[i][j] = chr(j + 64)
elif j == 0:
board[i][j] = str(i).rjust(max_num_len)
else:
board[i][j] = " "
def draw_ships(board, ships) -> None:
for i in range(len(ships)):
for j in range(len(ships[i])):
if ships[i][j] == 1:
board[i + 1][j + 1] = "B"
def draw_shots(board, hit_ships) -> None:
for i in range(len(hit_ships)):
for j in range(len(hit_ships[i])):
if hit_ships[i][j] == 1:
board[i + 1][j + 1] = "X"
if hit_ships[i][j] == -1:
board[i + 1][j + 1] = "O"
def draw_board(board) -> None:
for i in range(len(board)):
for j in range(len(board[i])):
print(board[i][j], end=" ")
print()
class Game:
def __init__(self, board_size: int, enemy_amount: int = 5) -> None:
self.my_ships = make_board(board_size)
# Two different boards, one to show your ships and one to show the places you have shot
self.hit_ships = make_board(board_size) # -1 = shot but not hit, 0 = not hit, 1 = hit
self.total_enemy_amount = enemy_amount
self.my_drawn_board = make_board(board_size + 1, " ")
self.enemy_drawn_board = make_board(board_size + 1, " ")
@property
def enemy_amount(self) -> int:
# Get the enemy amount based on the amount of ships that are not hit in the hit ships var
return self.total_enemy_amount - sum([row.count(1) for row in self.hit_ships])
def draw(self) -> None:
DrawerHelper.draw_ships(self.my_drawn_board, self.my_ships)
DrawerHelper.draw_shots(self.my_drawn_board, self.hit_ships)
DrawerHelper.draw_board(self.my_drawn_board)
def hit(self, x: int, y: int, state: int) -> None:
self.hit_ships[x][y] = state
def shoot(self, x: int, y: int) -> int:
"""Shot at own board and return hit or not hit"""
if self.my_ships[x][y] == 1:
return 1
return -1
na = Game(5)
DrawerHelper.draw_columnsnrows(na.my_drawn_board)
na.my_ships[2][2] = 1
na.hit(1, 2, na.shoot(1, 2))
na.draw()

View File

@ -4,7 +4,7 @@ import random, sys
args = sys.argv args = sys.argv
# name, -arguments # name, -arguments
def has_argument(args: list[str], arg: str) -> bool: def has_argument(args, arg):
if len(args) > 1: if len(args) > 1:
if args[1].startswith("-"): if args[1].startswith("-"):
for chr in args[1]: for chr in args[1]:
@ -12,9 +12,9 @@ def has_argument(args: list[str], arg: str) -> bool:
return True return True
return False return False
def num2char(num: int) -> chr: def num2char(num) -> chr:
return chr(num + 65) return chr(num + 65)
def char2num(char: str) -> int: def char2num(char) -> int:
return ord(char) - 65 return ord(char) - 65
posx = [0, 1, 2, 3, 4] posx = [0, 1, 2, 3, 4]
@ -31,17 +31,15 @@ tl.speed(0)
#funções #funções
def linhax(x: int, y: int) -> None: def linhax(x,y):
tl.up() tl.up(); tl.goto(x,y)
tl.goto(x,y) tl.down(); tl.goto(x+600,y)
tl.down() pass
tl.goto(x+600,y)
def linhay(x: int, y: int) -> None: def linhay(x,y):
tl.up() tl.up(); tl.goto(x,y)
tl.goto(x,y) tl.down(); tl.goto(x,y+600)
tl.down() pass
tl.goto(x,y+600)
#radar screen #radar screen
@ -64,14 +62,10 @@ for i in range(5):
#Tabuleiro pronto #Tabuleiro pronto
tl.title("Naval Battle 1943") tl.title("Naval Battle 1943")
tl.hideturtle() tl.hideturtle()
boat = tl.Turtle() boat = tl.Turtle()
boat.shapesize(3, 3) boat.shapesize(3, 3)
boat.up(); boat.goto(0,0); boat.left(90)
boat.up()
boat.goto(0,0)
boat.left(90)
#Enemies #Enemies
AMOUNT_OF_ENEMIES = 3 AMOUNT_OF_ENEMIES = 3
@ -81,12 +75,10 @@ for i in range(AMOUNT_OF_ENEMIES):
enemy = tl.Turtle() enemy = tl.Turtle()
enemy.color("red") enemy.color("red")
enemy.up() enemy.up()
if not has_argument(args, "D"): if not has_argument(args, "D"):
enemy.hideturtle() enemy.hideturtle()
else: else:
enemy.shapesize(3, 3) enemy.shapesize(3, 3)
pos = [random.choice(posy), random.choice(posy)] pos = [random.choice(posy), random.choice(posy)]
enemy.goto((pos[0] - 2) * 100, (pos[1] - 2) * 100) enemy.goto((pos[0] - 2) * 100, (pos[1] - 2) * 100)
@ -110,11 +102,9 @@ while win == False:
hit_enemy: list[tl.Turtle, list[int, int]] = enemy hit_enemy: list[tl.Turtle, list[int, int]] = enemy
acerto = True acerto = True
break break
if acerto: if acerto:
print("Acerto!") print("Acerto!")
boat.write("U-boat Afundado\n Capitão!",False,align="center") boat.write("U-boat Afundado\n Capitão!",False,align="center")
hit_enemy[0].hideturtle() hit_enemy[0].hideturtle()
enemies.remove(hit_enemy) enemies.remove(hit_enemy)
else: else:
@ -126,5 +116,4 @@ while win == False:
win = True win = True
print("Você ganhou!!") print("Você ganhou!!")
tl.exitonclick() tl.exitonclick()

View File

@ -23,11 +23,18 @@ class Networking:
Sends the size of the data before sending the data. Sends the size of the data before sending the data.
""" """
data_len = str(len(bytes(data, "utf-8"))).encode() sock.send(str(len(data)).encode())
sock.send(data_len)
response = sock.recv(1024).decode() response = sock.recv(1024).decode()
sock.send(data.encode()) sock.send(data.encode())
def recv_any_size(self, client):
"""
Receives data from the client without knowing the size of the data.
Client must send the size of the data before sending the data.
"""
data_size = client.recv(1024).decode()
data = client.recv(int(data_size)).decode()
return data
class Server: class Server:
""" """