Compare commits

...

4 Commits

Author SHA1 Message Date
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 100 additions and 21 deletions

75
src/game.py Normal file
View File

@ -0,0 +1,75 @@
def make_board(size: int, filler = 0) -> list:
return [[filler for _ in range(size)] for _ in range(size)]
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 setup_drawn_board(self) -> None:
max_num_len = len(str(len(self.my_drawn_board) - 1))
# Currently inserts the columns and rows into the board
for i in range(len(self.my_drawn_board)):
for j in range(len(self.my_drawn_board[i])):
if i == 0 and j == 0:
self.my_drawn_board[i][j] = " " * max_num_len
elif i == 0:
self.my_drawn_board[i][j] = chr(j + 64)
elif j == 0:
self.my_drawn_board[i][j] = str(i).rjust(max_num_len)
else:
self.my_drawn_board[i][j] = " "
def draw_ships(self) -> None:
for i in range(len(self.ships)):
for j in range(len(self.ships[i])):
if self.ships[i][j] == 1:
self.my_drawn_board[i + 1][j + 1] = "B"
def draw_shots(self) -> None:
for i in range(len(self.hit_ships)):
for j in range(len(self.hit_ships[i])):
if self.hit_ships[i][j] == 1:
self.my_drawn_board[i + 1][j + 1] = "O"
if self.hit_ships[i][j] == -1:
self.my_drawn_board[i + 1][j + 1] = "X"
def draw_board(self) -> None:
for i in range(len(self.my_drawn_board)):
for j in range(len(self.my_drawn_board[i])):
print(self.my_drawn_board[i][j], end=" ")
print()
def draw(self) -> None:
self.draw_ships()
self.draw_shots()
self.draw_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.ships[x][y] == 1:
return 1
return -1
na = Game(5)
na.setup_drawn_board()
na.ships[2][2] = 1
na.draw()

View File

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

View File

@ -23,18 +23,11 @@ class Networking:
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()
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:
"""