149 lines
5 KiB
Python
149 lines
5 KiB
Python
import random
|
|
from pyscript import document
|
|
|
|
class RPSGame:
|
|
def __init__(self, best_of = 3):
|
|
self.user_score = 0
|
|
self.ai_score = 0
|
|
self.deck = self.build_deck()
|
|
self.refresh_hand()
|
|
self.rounds_to_win = (best_of // 2) + 1
|
|
self.update_ui()
|
|
|
|
def rock(self, lst, n):
|
|
lst.extend(["Rock"] * n)
|
|
return lst
|
|
|
|
def paper(self, lst, n):
|
|
lst.extend(["Paper"] * n)
|
|
return lst
|
|
|
|
def scissors(self, lst, n):
|
|
lst.extend(["Scissors"] * n)
|
|
return lst
|
|
|
|
def generate_deck(self):
|
|
cards = [self.rock, self.paper, self.scissors]
|
|
deck = []
|
|
|
|
random.shuffle(cards) # This is done, so the card types are not added in the same order everytime, is scissors is last everytime it could result in scissors always being low or high since the last card just are all sofar minus 52¨
|
|
for i in range(2):
|
|
n = random.randint(5, 20)
|
|
deck = cards[i](deck, n)
|
|
|
|
missing_cards = 52 - len(deck)
|
|
deck = cards[2](deck, missing_cards)
|
|
|
|
random.shuffle(deck)
|
|
return deck
|
|
|
|
def check_deck(self, lst):
|
|
card_types = ["Rock", "Paper", "Scissors"]
|
|
for card_type in card_types:
|
|
if lst.count(card_type) >= 25:
|
|
return False
|
|
return True
|
|
|
|
def build_deck(self):
|
|
acceptable_deck = False
|
|
while not acceptable_deck:
|
|
deck = self.generate_deck()
|
|
acceptable_deck = self.check_deck(deck)
|
|
return deck
|
|
|
|
def give_cards(self):
|
|
return [self.deck.pop(0) for _ in range(3)], [self.deck.pop(0) for _ in range(3)]
|
|
|
|
def battle(self, user_card, ai_card):
|
|
if user_card == ai_card:
|
|
return 0
|
|
if (user_card == "Rock" and ai_card == "Scissors") or \
|
|
(user_card == "Paper" and ai_card == "Rock") or \
|
|
(user_card == "Scissors" and ai_card == "Paper"):
|
|
return 1
|
|
return -1
|
|
|
|
def refresh_hand(self):
|
|
self.user_hand, self.ai_hand = self.give_cards()
|
|
print("New cards")
|
|
print(f"AI cards: {self.ai_hand}")
|
|
print(f"{len(self.deck)} Cards left.")
|
|
|
|
def play_card(self, event):
|
|
index = int(event.target.getAttribute("index"))
|
|
if index >= len(self.user_hand):
|
|
return
|
|
|
|
user_card = self.user_hand.pop(index)
|
|
ai_card = self.ai_hand.pop(random.randint(0, len(self.ai_hand) - 1))
|
|
|
|
result = self.battle(user_card, ai_card)
|
|
if result == 1:
|
|
self.user_score += 1
|
|
round_result = f"You win! {user_card} beats {ai_card}."
|
|
self.refresh_hand()
|
|
elif result == -1:
|
|
self.ai_score += 1
|
|
round_result = f"AI wins! {ai_card} beats {user_card}."
|
|
self.refresh_hand()
|
|
else:
|
|
round_result = "It's a tie! Try again"
|
|
|
|
if not self.user_hand or not self.ai_hand:
|
|
self.refresh_hand()
|
|
if self.user_score < self.rounds_to_win and self.ai_score < self.rounds_to_win:
|
|
pass
|
|
else:
|
|
if self.user_score < self.ai_score:
|
|
round_result = "You lost D: Better luck next time"
|
|
self.update_ui(round_result)
|
|
self.disable_buttons()
|
|
return
|
|
else:
|
|
round_result = "You won great :D"
|
|
self.update_ui(round_result)
|
|
self.disable_buttons()
|
|
return
|
|
|
|
self.update_ui(round_result)
|
|
|
|
def update_ui(self, result_text="Waiting for your move..."):
|
|
print("Update UI")
|
|
document.querySelector("#user-score").innerText = str(self.user_score)
|
|
document.querySelector("#ai-score").innerText = str(self.ai_score)
|
|
document.querySelector("#result").innerText = result_text
|
|
document.querySelector("#reset-game").disabled = False
|
|
for i in range(3):
|
|
btn = document.querySelector(f"#card-{i}")
|
|
if i < len(self.user_hand):
|
|
card_name = self.user_hand[i].lower() # Convert "Rock" -> "rock"
|
|
btn.style.backgroundImage = f"url('images/{card_name}.png')"
|
|
btn.disabled = False
|
|
else:
|
|
btn.style.backgroundImage = "url('images/card-back.png')" # Unknown card
|
|
btn.disabled = True
|
|
|
|
def disable_buttons(self):
|
|
print("disable button function")
|
|
for i in range(3):
|
|
btn = document.querySelector(f"#card-{i}")
|
|
btn.style.backgroundImage = "url('images/card-back.png')"
|
|
btn.disabled = True
|
|
|
|
def reset_game(self, event=None):
|
|
print("Game Reset")
|
|
self.user_score = 0
|
|
self.ai_score = 0
|
|
self.deck = self.build_deck()
|
|
self.refresh_hand()
|
|
self.deck_overview()
|
|
self.update_ui("Game reset! Pick a card to start.")
|
|
|
|
def deck_overview(self):
|
|
print(f"{self.deck.count('Rock')} Rock cards")
|
|
print(f"{self.deck.count('Paper')} Paper cards")
|
|
print(f"{self.deck.count('Scissors')} Scissors cards")
|
|
|
|
# Start the game
|
|
game = RPSGame(5)
|
|
game.deck_overview()
|