Skip to content

Instantly share code, notes, and snippets.

@StephanieSunshine
Created January 9, 2022 14:35
Show Gist options
  • Save StephanieSunshine/7d9de41b58ae7ed4d053bf4ad7f85472 to your computer and use it in GitHub Desktop.
Save StephanieSunshine/7d9de41b58ae7ed4d053bf4ad7f85472 to your computer and use it in GitHub Desktop.
Python circular link list dice game in Python
#!/usr/bin/env python3
# first one to zero, wins!
# 2022 Stephanie Sunshine -- MIT License
from random import randrange
class Player:
next_player = None
name = ""
score = 0
def __init__(self, name):
self.score = randrange(100)
self.name = name
def check_win(self):
if self.score <= 0:
print(f"{self.name} is the Winner!")
exit(0)
def roll(self):
r = randrange(3)
print(f"{self.name} score: {self.score} rolls: {r}")
self.score -= r
def play_turn(self):
self.roll()
self.check_win()
self.next_player.play_turn()
players = [ Player("Player 1"), Player("Player 2"), Player("Player 3"), Player("Player 4"), Player("Player 5") ]
# link everyone
for i in range(0,len(players)-1):
players[i].next_player = players[i+1]
players[-1].next_player=players[0]
players[0].play_turn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment