Skip to content

Instantly share code, notes, and snippets.

@TosinAF
Created December 2, 2013 08:10
Show Gist options
  • Save TosinAF/7746503 to your computer and use it in GitHub Desktop.
Save TosinAF/7746503 to your computer and use it in GitHub Desktop.
Game Theory v1.0 This a Pshcychological game, Have a good look at your opponent.
class Player(object):
""" To Create a new Player Instance with name, credit & bet values """
bet = 0
def __init__(self, PlayerName, Credit):
self.PlayerName = PlayerName
self.Credit = Credit
def __str__(self):
return self.PlayerName
class Game(object):
" Accepts two Instances of the Player Object "
GameBoard = ['*','*','*','*','Coin','*','*','*','*']
Player1WinPos = 0
Player2WinPos = len(GameBoard) - 1
def __init__(self, Player1 , Player2):
self.Player1 = Player1
self.Player2 = Player2
def GetPlayersBet(self,Player):
""" Asks for the Players bet & checks that the input is a valid number and not greater than the available credit. """
while(True):
temp_val = input("{0}, You Have {1} credit. Place Your Bet: ".format(Player.PlayerName, Player.Credit))
if ( temp_val.isdigit() ):
temp_val = int(temp_val)
if ( temp_val == 0 ):
print("> {0}, You cannot place a bet of 0. ".format(Player.PlayerName))
continue
if ( temp_val <= Player.Credit):
Player.bet = temp_val
break
else:
print("> {0}, You Do Not Have Enough Credit, Bet a lower amount. ".format(Player.PlayerName))
else:
print("> {0}, You Can Only Input Numbers, Bet Again. ".format(Player.PlayerName))
def UpdateCredit(self):
""" Deducts the bet value from the Players Credit """
#Sometimes the bets made will be ignored hence why this is it's own method
self.Player1.Credit -= self.Player1.bet
self.Player2.Credit -= self.Player2.bet
def UpdateGameBoard(self, WhoWon):
""" Updates & Prints the GameBoard """
CurrentPos = self.GameBoard.index('Coin')
self.GameBoard.remove('Coin')
if( WhoWon == 1 ): CurrentPos -= 1
elif ( WhoWon == 2 ): CurrentPos += 1
self.GameBoard.insert(CurrentPos,'Coin')
print(self.GameBoard)
def is_There_A_Winner_Yet(self):
""" Returns 1 if a Win has occured or one/both of the players have 0 Credit. False, If not. """
if ( self.GameBoard[self.Player1WinPos] == 'Coin' ):
print("\n{0} has won the game. GoodLuck next time, {1}. ".format(self.Player1,self.Player2))
return True
elif ( self.GameBoard[self.Player2WinPos] == 'Coin' ):
print("\n{0} has won the game. GoodLuck next time, {1}. ".format(self.Player2,self.Player1))
return True
elif ( self.Player1.Credit == 0 and self.Player2.Credit == 0 ):
print("\nYou have both managed to reach the unlikely case whereas neither of you have credit. It's a Draw.")
return True
elif ( self.Player1.Credit == 0 ):
print("\n{0} has run out of Credit. {1} Wins. ".format(self.Player1,self.Player2))
return True
elif ( self.Player2.Credit == 0 ):
print("\n{0} has run out of Credit. {1} Wins. ".format(self.Player2,self.Player1))
return True
else:
return False
def StartGameGUI(self):
#Possible Extension of the Game to GUI - Likely with pygame
pass
def StartGameCLI(self):
""" Begin The Command Line Version of the Game, The methods created above are then called here. """
print("="*80 + "\n")
print("Game Theory v1.0")
print("This a Pshcychological game, Have a good look at your opponent.")
print("Below is the inital state of the GameBoard.\n")
print(['Player1WinPos','*','*','*','Coin','*','*','*','Player2WinPos'])
print("\nYou both start off with {0} credit. You will each take turns placing bets.".format(Credit))
print("The Player with the higher bet wins that individual round and the coin takes a step towards the Winning Player's Endzone.")
print("The First Player to get the coin in their endzone wins the entire game.\n")
print("Let the Games Begin & May The Odds Ever Be In Your Favour")
print("Warning: If you run out of credit you lose the game.\n")
print("="*80 + "\n")
while(True):
#Main Game Loop
self.GetPlayersBet(self.Player1)
self.GetPlayersBet(self.Player2)
if( self.Player1.bet == self.Player2.bet ):
print("> Your Bets were equal, no credit will be deducted. Please place your bets again.")
continue
elif ( self.Player1.bet > self.Player2.bet ):
#Player1 Wins the round
self.UpdateCredit()
self.UpdateGameBoard(1)
if ( self.is_There_A_Winner_Yet() ): break
continue
elif ( self.Player1.bet < self.Player2.bet ):
#Player2 Wins the round
self.UpdateCredit()
self.UpdateGameBoard(2)
WinBool = self.is_There_A_Winner_Yet
if ( self.is_There_A_Winner_Yet() ): break
continue
print("\n" + "="*80 )
def main():
global Credit
Credit = 50
Gamer1 = Player("Tosin", Credit)
Gamer2 = Player("Emanuel", Credit)
newGame = Game(Gamer1,Gamer2)
newGame.StartGameCLI()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment