Skip to content

Instantly share code, notes, and snippets.

@nathanesau
Created June 5, 2015 23:47
Show Gist options
  • Save nathanesau/b674f618bc3f5a9afe9b to your computer and use it in GitHub Desktop.
Save nathanesau/b674f618bc3f5a9afe9b to your computer and use it in GitHub Desktop.
Rook
# in progress
import random
class card:
def __init__(self, num, suit):
self.num = num # card num
self.suit = suit # card suit
self.value = self.points()
def rank(card):
if(card.suit=="R"):
return(0+card.num)
elif(card.suit=="G"):
return(14+card.num)
elif(card.suit=="B"):
return(28+card.num)
elif(card.suit=="Y"):
return(42+card.num)
else: # Rk
return(57) # max
def points(self): # point value associated with a card num
if(self.num == 5):
return(5)
elif(self.num == 10 or self.num == 14):
return(10)
elif(self.num == 0): # rook
return(20)
else: # card is worth no points
return(0)
def __str__(self):
msg = str(self.num) + self.suit
if len(msg) == 2:
msg = " " + msg
return(msg)
def printHand(cards): # print array of card objects
msg = ""
for i in range(len(cards)):
msg += str(cards[i]) + " "
print(msg)
def sort(cards): # sort the array of card objects using bubble sort
for i in range(len(cards)):
for j in range(len(cards)-1-i): # descending
if card.rank(cards[j]) < card.rank(cards[j+1]):
# swap
cards[j], cards[j+1] = cards[j+1], cards[j]
return(cards)
def handPoints(cards): # num points in a hand of cards
total = 0
for card in cards:
total += card.value
return(total)
class player:
def __init__(self, num):
self.num = num # player order
self.cards = [] # no cards yet
self.bid = 0 # no bid yet
self.wonBid = 0 # hasn't won bid yet
self.score = 0 # 0 since game hasn't started yet
self.scoreRound = 0 # 0 since round hasn't started yet
def __str__(self):
return("player #" + str(self.num))
def getBid(self, currentBid):
if(currentBid < 80):
return(currentBid+5)
else:
return(0)
def getTrump(self): # getTrump for winning bidder
return("G")
def getPartner(self): # getPartner for winning bidder
return("14Y")
def getCard(self): # getCard to be played
index = 0
cardStr = str(self.cards[index]) # choose self.cards[0]
self.cards = self.cards[:index] + self.cards[index+1:]
return(cardStr)
def nextBidder(self, currentBidder):
return((currentBidder+1)%4)
def swapNest(self, nest):
# assume 13 cards for now
cardsSwapped = 0
while(cardsSwapped < 3):
for i in range(13):
for j in range(5): # iterate through nest
if self.cards[i].num < nest.cards[j].num:
# swap
self.cards[i],nest.cards[j]= \
nest.cards[j],self.cards[i]
cardsSwapped += 1 # once swapped three cards break
return(nest)
# global variables
numPlayers = 4
gamesPlayed = 0
highestCard = 14 # 1 - 14
suits = ["R","G","B","Y"]
startingBidder = 0
players = []
for i in range(numPlayers): # create players
players.append(player(i))
deck = []
for i in range(highestCard): # create deck
for suit in suits:
deck.append(card(i+1,suit))
deck.append(card(0,"Rk"))
#def playGame():
random.shuffle(deck) # shuffle deck
for i in range(numPlayers): # deal deck
for j in range(highestCard-1):
players[i].cards.append(deck[i*(highestCard-1)+j])
nest = player(5) # create nest
for i in range(numPlayers*(highestCard-1), numPlayers*highestCard+1, 1):
nest.cards.append(deck[i]) # remaining cards go into nest
for player in players: # sort cards for players
player.cards = card.sort(player.cards)
card.sort(nest.cards)
for i in range(numPlayers): # print hands
pass
#card.printHand(players[i].cards)
#card.printHand(nest.cards)
currentBidder = startingBidder
bid = 0
passed = 0
while(bid < 120 and passed < 3): # bidding phase
tmpBid = players[currentBidder].getBid(bid)
if(tmpBid > bid):
bid = tmpBid
else:
passed += 1
if(passed==3):
winningBidder = currentBidder
players[winningBidder].wonBid = 1
players[winningBidder].bid = bid
currentBidder = player.nextBidder(currentBidder)
nest = players[winningBidder].swapNest(nest) # calculate points in nest
#card.printHand(nest.cards)
nestValue = card.handPoints(nest.cards)
trump = players[winningBidder].getTrump()
partnerCard = players[winningBidder].getPartner()
partner = 0 # unknown at this point
print("\nplayer #" + str(currentBidder) + " won the bid at " + str(bid))
if(nestValue > 0):
print(" there are points in the middle")
else:
print(" there are no points in the middle")
print(" the trump is " + str(trump))
print(" the partner card is the " + partnerCard)
# figure out who the partner is
for i in range(numPlayers):
for j in range(13): # assume 13 cards for now
if(str(players[i].cards[j])==partnerCard):
players[i].bid = players[winningBidder].bid # same bid as partner
partner = i
# play the game
print("\nStarting game!")
for i in range(13): # 13 turns
for j in range(numPlayers): # 4 players
tmpCard = players[j].getCard()
# print("player #" + str(j) + ", card: " + tmpCard)
# reset variables and calculate scores
for i in range(numPlayers):
if(i==partner or i==winningBidder): # deal with bidding team
players[i].scoreRound = 90
if(players[i].scoreRound > players[i].bid):
players[i].score += players[i].scoreRound
else: # broke -> score = score - bid
players[i].score -= players[i].bid
else:
players[i].scoreRound = 30
players[i].score = players[i].scoreRound
# reset variables
players[i].wonBid = 0
players[i].scoreRound = 0
players[i].bid = 0
# print summary of game
print("\nSummary of Scores \n---------------------")
for i in range(numPlayers):
print("player #" + str(i) + ", score: " + str(players[i].score))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment