sam-frank
6/12/2019 - 2:14 AM

pig project

import random 
import sys

class Player:
    def __init__(self, name, roll, score, bank):
        self.name = name
        self.score = score
        self.roll = roll
        self.bank = bank

    def __str__(self):
        return f"{self.name}"

    def addroll(self):
        self.score += self.bank
        self.check_for_win()
        self.bank = 0
        self.switchturn()

    def switchturn(self):
        print("\n SCOREBOARD:  |", player1.name, player1.score, "|", player2.name, player2.score, "|")
        print("\n")
        if self.name == "Sam":
            Player.game_logic(player2) 
        else:
            Player.game_logic(player1)
        
    def rolldice(self):
        self.roll = random.randint(1,6)
        print("\n", self.name, "  rolled a   |" , self.roll , "|")
        if self.roll == 1:
                print(" ================ switch turns ==================")
                self.bank = 0
                self.switchturn()
        else:
            self.addtobank()
        return self.roll

    def addtobank(self):
        roll = (self.roll) 
        self.bank += roll
        print("your current BANK total is", self.bank)
        again = input(str("Press R to roll again or S to stay "))
        if (str(again) == "R"):
            self.rolldice()
        if (str(again) == "S"):
            self.addroll()
        else:
            self.bank -= roll 
            print(" \n ------ invalid command , try again ----- ")
            self.addtobank()

    def game_logic(self):
        print("\n", self.name, "'s turn")
        play = input(str("Press R to Roll "))
        if (str(play) == "R"):
            self.rolldice()    
        else:
            print("\n  ----- Invalid Option, try again ----")
            self.game_logic()

    def check_for_win(self):
        if self.score >= 15:
            print("\n +++++++++++++++", self.name, "Is the Winner!++++++++++++++++++ ")
            print("\n SCOREBOARD:  |", player1.name, player1.score, "|", player2.name, player2.score, "|")
            sys.exit()
        
# Create My Players, initital values set to 0
player1 = Player("Sam", 0, 0, 0)
player2 = Player("James", 0, 0, 0)

#Kick off the game 
print(" \n =========== Wecome to PIG, the best game in the world ========= ")
print("\n ------", player1.name, "will start ----------")

Player.game_logic(player1)