Compare commits

..

5 Commits

Author SHA1 Message Date
Guilherme Aleixo de Oliveira Martins 05a26bd99d Move drawing functions into static class 2024-12-05 17:39:58 -03:00
20241144010020 0ee1082c6d Start abstracting game into a class 2024-12-03 11:37:01 -03:00
20241144010020 393c1b23b4 Change ugly code 2024-12-03 10:14:25 -03:00
20241144010020 6dd9557681 Add function type hints 2024-12-03 10:07:18 -03:00
20241144010020 649b5b27f7 Use byte length as data_size 2024-11-30 10:09:17 -03:00
3 changed files with 104 additions and 21 deletions

79
src/game.py Normal file
View File

@ -0,0 +1,79 @@
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, arg): def has_argument(args: list[str], arg: str) -> bool:
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, arg):
return True return True
return False return False
def num2char(num) -> chr: def num2char(num: int) -> chr:
return chr(num + 65) return chr(num + 65)
def char2num(char) -> int: def char2num(char: str) -> int:
return ord(char) - 65 return ord(char) - 65
posx = [0, 1, 2, 3, 4] posx = [0, 1, 2, 3, 4]
@ -31,15 +31,17 @@ tl.speed(0)
#funções #funções
def linhax(x,y): def linhax(x: int, y: int) -> None:
tl.up(); tl.goto(x,y) tl.up()
tl.down(); tl.goto(x+600,y) tl.goto(x,y)
pass tl.down()
tl.goto(x+600,y)
def linhay(x,y): def linhay(x: int, y: int) -> None:
tl.up(); tl.goto(x,y) tl.up()
tl.down(); tl.goto(x,y+600) tl.goto(x,y)
pass tl.down()
tl.goto(x,y+600)
#radar screen #radar screen
@ -62,10 +64,14 @@ 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
@ -75,10 +81,12 @@ 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)
@ -102,9 +110,11 @@ 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:
@ -116,4 +126,5 @@ while win == False:
win = True win = True
print("Você ganhou!!") print("Você ganhou!!")
tl.exitonclick() tl.exitonclick()

View File

@ -23,18 +23,11 @@ class Networking:
Sends the size of the data before sending the data. Sends the size of the data before sending the data.
""" """
sock.send(str(len(data)).encode()) data_len = str(len(bytes(data, "utf-8"))).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:
""" """