diff --git a/main.py b/main.py index e33a154..350b4a8 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import pygame import sys from constants import * from player import * +import copy def print_board(board): for i in range(BOARD_SIZE): @@ -32,18 +33,19 @@ def main(): while good_inp == False: inp = input() good_inp, player_board, ship_pos = check_valid_input(inp) - enemy_board = player_board.copy() + enemy_board = copy.deepcopy(player_board) - player = Player(player_board, ship_pos) - enemy = Player(enemy_board, ship_pos) + player = Player(player_board, ship_pos.copy()) + enemy = Player(enemy_board, ship_pos.copy()) print_board(player_board) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) time = pygame.time.Clock() - dt = 0 + turn_counter = 0 first_iteration = True + is_enemy_turn = False while True: for event in pygame.event.get(): @@ -54,7 +56,12 @@ def main(): pygame.Surface.fill(screen, (255,255,255)) if first_iteration == False: - player.update(enemy) + if is_enemy_turn == False: + player.update(enemy) + is_enemy_turn = True + else: + enemy.random_shoot(player) + is_enemy_turn = False else: first_iteration = False @@ -62,7 +69,8 @@ def main(): pygame.display.flip() #refresh screen - dt = time.tick(60) / 1000 #converted to ms + turn_counter += 1 + print("turn: ", turn_counter) def check_valid_input(inp): diff --git a/player.py b/player.py index f7ea8c3..c5532dd 100644 --- a/player.py +++ b/player.py @@ -1,6 +1,7 @@ import pygame from pygame import Rect from ship import * +import random class Player(): def __init__(self, board, ships): @@ -23,6 +24,7 @@ class Player(): if enemy.board[ord(position[0])-97][int(position[1])] == 0b00: print("No ship at this position") + self.shot_positions[ord(position[0])-97][int(position[1])] = 0b01 else: enemy.get_hit(position) self.shot_positions[ord(position[0])-97][int(position[1])] = 0b11 @@ -31,19 +33,22 @@ class Player(): if len(position) != 2 and position != None: return False - if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0: - return True - if self.shot_positions[ord(position[0])-97][int(position[1])] == 0b11: print("You already shot at this position!") return False + if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0: + return True + return False def update(self, enemy): inp = input("Please input the position you want to fire.") - while self.__check_valid_input(inp) == False: - inp = input("Please input the position you want to fire.") + check = self.__check_valid_input(inp) + + while check == False: + inp = input("Please input the position you want to fire. ") + check = self.__check_valid_input(inp) self.shoot(inp, enemy) @@ -84,3 +89,10 @@ class Player(): start_pos_y += height - 5 start_pos_x = 125 + + def random_shoot(self, enemy): + position = chr(random.randint(97, 107)) + position += str(random.randint(0,9)) + print("rand pos: ", position) + + self.shoot(position, enemy)