Gameplay
Gameplay
class Gameplay:
def __init__(self, player1_name, player2_name):
self.players = [Player(player1_name), Player(player2_name)]
self.current_player = 0 # The index for the current player (0 = player1, 1
= player2)
def take_turn(self):
current_player = self.players[self.current_player]
print(f"{current_player.name}'s turn") # Accessing the 'name' attribute of
Player object
guess = input(f"{current_player.name}, enter your guess (e.g., A,1):
").split(',')
def switch_turn(self):
self.current_player = 1 - self.current_player # Switch between player 0
and 1
def check_winner(self):
# Check if the current player's ships are all sunk
current_player = self.players[self.current_player]
if current_player.is_all_ships_sunk():
# The other player is the winner
winner = self.players[1 - self.current_player]
print(f"Game Over! {winner.name} is the winner!")
return True
return False