Last active
September 9, 2023 05:05
-
-
Save T31337/eb23f5cfa7a9a91ba9e1cfa613ec04f3 to your computer and use it in GitHub Desktop.
Pig Game Written In Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
class PigGame: | |
MAX_SCORE = 100 | |
scores = [0, 0] | |
@staticmethod | |
def roll(): | |
points = random.randint(1, 6) | |
# We Will Check For 1 Being Rolled Elsewhere | |
return points | |
def computerTurn(self): | |
rollAgain = 1 | |
points = 0 | |
while rollAgain == 1: | |
diceValue = self.roll() | |
if diceValue == 1: | |
print("Computer Rolled A 1, Turn Over!") | |
points = 0 | |
break | |
else: | |
points += diceValue | |
print(f"Computer Rolled: {diceValue}") | |
print(f"Computer Points: {points}") | |
willRoll = random.randint(0, 1) | |
if willRoll == 0: | |
print("Computer Stopped Rolling") | |
self.scores[1] += points | |
break | |
print(f"Computer Score: {self.scores[1]}") | |
def playerTurn(self): | |
rollAgain = 1 | |
points = 0 | |
print(f"Your Current Score Is: {self.scores[0]}") | |
while rollAgain == 1: | |
isRolling = input("Press Enter To Roll Or Type (n) To Stop Rolling: ") | |
if isRolling.lower() != "n": | |
diceValue = self.roll() | |
print(f"Your Rolled A: {diceValue}") | |
if diceValue == 1: | |
print("You Rolled A 1, Turn Over!") | |
points = 0 | |
break | |
else: | |
points += diceValue | |
print(f"You Have {points} Points!") | |
continue | |
else: | |
self.scores[0] += points | |
print(f"You Have A Score Of: {self.scores[0]}") | |
break | |
def playGame(self): | |
print("Welcome To PygGame,\nRoll To Score Points,\nHowever, Roll A 1 And It Is Turn Over") | |
currentMax = max(self.scores) | |
while currentMax < self.MAX_SCORE: | |
print("Starting Player Turn\n") | |
self.playerTurn() | |
print("Starting Computer Turn\n") | |
self.computerTurn() | |
currentMax = max(self.scores) | |
if self.scores[0] > self.scores[1]: | |
print(f"Player Wins!\nPlayer Score: {self.scores[0]}\nComputer Score: {self.scores[1]}") | |
else: | |
print(f"Computer Wins!\nComputer Score: {self.scores[1]}\nPlayer Score: {self.scores[0]}") | |
playAgain = input("Press Enter To Play Again Or Type (q) To Quit: ") | |
if playAgain == "": | |
newGame = PigGame() | |
newGame.playGame() | |
else: | |
sys.exit() | |
game = PigGame() | |
game.playGame() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment