0% found this document useful (0 votes)
9 views1 page

Gameplay

The Gameplay class manages a two-player game, initializing players and handling turns. It includes methods for taking turns, switching players, and checking for a winner based on whether a player's ships are sunk. The game prompts players for their guesses and determines the outcome of the game accordingly.

Uploaded by

jy2723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Gameplay

The Gameplay class manages a two-player game, initializing players and handling turns. It includes methods for taking turns, switching players, and checking for a winner based on whether a player's ships are sunk. The game prompts players for their guesses and determines the outcome of the game accordingly.

Uploaded by

jy2723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

from player import Player

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(',')

# Additional logic to process the guess...


# (check if guess is correct, hit/miss, etc.)
pass

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

You might also like