Hello everyone! I need help creating a visual sequence model for my two-player memory game. I have created three classes on Python. I will attach my code and my UML model as well to get an understanding. Please help me as I am struggling to implement this relationship. Feel free to DM or point me to another group that is more focused on OOD. Thanks!
#Object-Oriented Design: Sequence Modeling with Python Code
18 messages · Page 1 of 1 (latest)
!format
Using code blocks makes reading the code much easier
Also, what exactly are you struggling with?
I'll post my code now, but basically I need to create a visual sequence model using the objects/classes and methods in my Python code. It's suppose to look like this...I'm basically designing a two-player memory game
I'm not sure if anyone is familiar with this but i'm struggling creating it
import random
import time
from player import Player
from gameboard import GameBoard
class MemoryGame:
def __init__(self, symbols, board_size, players):
self.board_size = board_size
self.players = [Player(player) for player in players]
self.current_round = 0
self.selected_indices = []
self.tiles = symbols * 2
random.shuffle(self.tiles)
def display_board(self, selected_indices):
print("\nCurrent Board:")
print(' '.join(str(tile) if index in selected_indices else 'x' for index, tile in enumerate(self.tiles)))
print("\n")
def player_turn(self, player):
self.current_round += 1
print(f"{player.name}'s turn (Round {self.current_round})")
self.display_board([])
selected_indices = []
for _ in range(2):
while True:
try:
index = int(input(f"{player.name}, please enter the index to flip a tile: "))
if 0 <= index < len(self.tiles) and index not in selected_indices:
selected_indices.append(index)
break
else:
print('Invalid index. Please choose a valid index.')
except ValueError:
print('Invalid input. Please enter a number.')
self.display_board(selected_indices)
if self.tiles[selected_indices[0]] == self.tiles[selected_indices[1]]:
print("It's a match!")
player.matches += 1
else:
print('Not a match!')
time.sleep(5)
cont.
def determine_winner(self):
scores = [player.matches for player in self.players]
max_score = max(scores)
min_score = min(scores)
if max_score > min_score:
return f"Player {scores.index(max_score) + 1} wins with {max_score} matches!"
elif max_score < min_score:
return f"Player {scores.index(min_score) + 1} wins with {min_score} matches!"
else:
return "It's a draw!"
def start_game(self):
for _ in range(5):
for player in self.players:
self.player_turn(player)
print("\nGame Over. Thanks for playing!")
print("Match Results:")
for player in self.players:
player.display_matches()
player.points = player.matches
print(self.determine_winner())
import random
import time
class GameBoard:
def __init__(self, symbols, board_size):
self.symbols = symbols
self.tiles = symbols * 2
random.shuffle(self.tiles)
self.board_size = board_size
def display_board(self, selected_indices):
print("\nCurrent Board:")
print(' '.join(str(tile) if index in selected_indices else 'x' for index, tile in enumerate(self.tiles)))
print("\n")
class Player:
def __init__(self, name):
self.name = name
self.matches = 0
self.points = 0
def display_matches(self):
print(f"{self.name}: {self.matches} matches")
def get_score(self):
return self.points
def take_turn(self):
print(f"{self.name}'s turn.")
def final_score(self):
return f"{self.name}'s final score: {self.points}"
in case anyone decides to run the code, this is my main.py which calls all these classes:
import random
import time
from memorygame import MemoryGame
from player import Player
from gameboard import GameBoard
if __name__ == "__main__":
symbols = ['!', '@', '#', '$', '%']
board_size = 10
player_names = ['Player 1', 'Player 2']
players = player_names
game = MemoryGame(symbols, board_size, players)
game.start_game()
Is GameBoard not being used?
GameBoard is being used as instance from the MemoryGame class
Maybe I'm blind but I don't see it being used in the code