win check

This commit is contained in:
2025-07-28 09:20:41 +02:00
parent 428e7e83f0
commit e0319a2fda
2 changed files with 40 additions and 8 deletions

28
main.py
View File

@@ -35,8 +35,8 @@ def main():
good_inp, player_board, ship_pos = check_valid_input(inp)
enemy_board = copy.deepcopy(player_board)
player = Player(player_board, ship_pos.copy())
enemy = Player(enemy_board, ship_pos.copy())
player = Player(player_board, ship_pos.copy(), "Player")
enemy = Player(enemy_board, ship_pos.copy(), "Enemy")
print_board(player_board)
@@ -65,7 +65,15 @@ def main():
else:
first_iteration = False
player.draw(screen)
if check_win(player, enemy) == True:
print(f"{player.name} won!")
sys.exit(0)
if check_win(enemy, player) == True:
print(f"{player.name} won!")
sys.exit(0)
player.draw(screen, enemy)
pygame.display.flip() #refresh screen
@@ -73,6 +81,16 @@ def main():
print("turn: ", turn_counter)
def check_win(player, enemy):
print(f"checking win for: {player.name}")
for i in range(0, BOARD_SIZE):
for j in range(0, BOARD_SIZE):
if enemy.board[i][j] == 0b01:
return False
return True
def check_valid_input(inp):
ship_pos = []
s = inp.split(' ')
@@ -96,7 +114,7 @@ def check_valid_input(inp):
print("t", t)
ship_pos.append((t[0], t[1]))
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107: #check if the fist chars match
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 106 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 106: #check if the fist chars match
if int(t[0][1]) >= 0 and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and int(t[0][1]) != int(t[1][1]) and abs(int(t[0][1]) - int(t[1][1]))+1 < 6: #check if number is in correct range
if int(t[0][1]) > int(t[1][1]):
step = -1
@@ -114,7 +132,7 @@ def check_valid_input(inp):
print("e1")
return (False, board, ship_pos)
elif t[0][1] == t[1][1] and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and abs(int(t[0][1]) - int(t[1][1]))+1 < 6:
if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107 and t[0][0] != t[1][0]:
if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 106 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 106 and t[0][0] != t[1][0]:
if ord(t[0][0]) > ord(t[1][0]):
step = -1
else:

View File

@@ -4,8 +4,9 @@ from ship import *
import random
class Player():
def __init__(self, board, ships):
def __init__(self, board, ships, player_name):
self.board = board
self.name = player_name
self.shot_positions = []
for i in range(0, len(board)):
self.shot_positions.append([])
@@ -52,7 +53,7 @@ class Player():
self.shoot(inp, enemy)
def draw(self, screen):
def draw(self, screen, enemy):
start_pos_x = 125
start_pos_y = 30
width = 80
@@ -84,6 +85,9 @@ class Player():
elif self.board[i][j] == 0b11:
pygame.draw.circle(screen, (255,0,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
if enemy.shot_positions[i][j] == 0b01:
pygame.draw.circle(screen, (0,255,0), (start_pos_x+width//2, start_pos_y + height // 2), 20)
start_pos_x += width - 5
start_pos_y += height - 5
@@ -91,8 +95,18 @@ class Player():
def random_shoot(self, enemy):
position = chr(random.randint(97, 107))
position = chr(random.randint(97, 106))
position += str(random.randint(0,9))
check = self.__check_valid_input(position)
while check == False:
print("rand check loop")
position = chr(random.randint(97, 106))
position += str(random.randint(0,9))
check = self.__check_valid_input(position)
print("rand pos: ", position)
self.shoot(position, enemy)