Compare commits

..

No commits in common. "0ee1082c6d62b30d5a1e795602c26f6c8d32b828" and "055e4fa22489290a8bbccd2bb78f87a3ee0a135a" have entirely different histories.

3 changed files with 21 additions and 100 deletions

View File

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

View File

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