Jump to content

I got bored today


newbi3

Recommended Posts

I got bored today (technically yesterday but I didn't go to sleep yet so it is still today) and decided to write a command line uno game. It is all fully functional you play against the computer and all cards work. Wilds, draw 4s, draw 2s, skips, reverses, everything. I didn't follow an official uno rule book while creating this though so you might think some things are against the rules but it all works!

#!/usr/bin/python
# Newbi3
# 06/09/2013
# Uno

import random
from time import sleep

deck = []
playerHand = []
compHand = []
playedCards = []
currentColor = ""
currentNumber = 0
currentCard = ""

def checkWin():
    ''' Checks if a player has won the game '''
    
    if (len(compHand) == 0):
        print "\n\n\tYou lose!"
        exit()
    elif (len(playerHand) == 0):
        print "\n\n\tCongrats! You win!"
        exit()

    if (len(deck) == 0):
        while len(playedCards) > 0:
            card = random.randint(0,len(cards)-1)
            deck.append(playedCards[card])
            playedCards.remove(playedCards[card])

def checkPlayerHand():
    ''' Checks if the player has any cards to play '''

    checkWin()
    
    if any(currentColor in i for  i in playerHand):
        playerTurn()
    elif any(currentNumber in i for i in playerHand):
        playerTurn()
    elif any("wild" in  i for i in playerHand):
        playerTurn()
    else:
        drawCard(1, "person")
        checkPlayerHand()

def playerTurn():
    ''' Display information to the player '''

    checkWin()
    
    print "\nYou have " + str(len(playerHand)) + " card(s) in your hand."
    print "Your oponent has " + str(len(compHand)) + " card(s) in their hand."
    print str(len(deck)) + " card(s) left in the deck."
        
    print "\n\tLast card played: " + currentCard
    print "\tCurrent color: " + currentColor

    printPlayerHand = []
    for i in range(0,len(playerHand), 1):
        printPlayerHand.append(str(i) + "[" + playerHand[i] + "]")
    print "\n" + ', '.join(printPlayerHand)

    while True:
        try:
            checkCard(input("\nThe number of the card you want to play: "), "person")
            break
        except:
            print "Invalid card!"

def computerTurn():
    ''' Handles the computers turn '''

    checkWin()

    cardToPlay = False

    while cardToPlay == False:
        for i in range(0, len(compHand), 1):
            if (currentColor in compHand[i]):
                checkCard(i, "computer")
                cardToPlay = True
                break

        for i in range(0, len(compHand), 1):
            if (currentNumber in compHand[i]):
                checkCard(i, "computer")
                cardToPlay = True
                break

        for i in range(0, len(compHand), 1):
            if ("wild" in compHand[i]):
                checkCard(i, "computer")
                cardToPlay = True
                break

        drawCard(1, "computer")

def drawCard(amount, player):
    ''' Add cards x amount to the players hand '''

    global playerHand
    global compHand
    global deck

    if (player == "person"):
        for i in range(0, amount , 1):
            checkWin()
            playerHand.append(deck[0])
            deck.remove(deck[0])
    elif (player == "computer"):
        for i in range(0, amount, 1):
            checkWin()
            compHand.append(deck[0])
            deck.remove(deck[0])
            
def playCard(card, player, wild):
    ''' Play the card '''

    global playedCards
    global currentCard
    global playerHand
    global currentColor
    global currentNumber
    global compHand
    
    if (player == "person"):
        skip = False
        playedCards.append(currentCard)
        currentCard = playerHand[card]
        if (wild == False):
            if ("draw two" in playerHand[card]):
                drawCard(2, "computer")
                currentColor, null, null1 = playerHand[card].split(' ')
                currentNumber = "draw two"
            elif ("skip" in playerHand[card] or "reverse" in playerHand[card]):
                currentColor, currentNumber = playerHand[card].split(' ')
                skip = True
            else:
                currentColor, currentNumber = playerHand[card].split(' ')
        else:
            currentColor = ""
            while currentColor == "":
                choosenColor = raw_input("\n\tChoose a color: ")
                if ("blue" in choosenColor.lower() or "green" in choosenColor.lower() or "red" in choosenColor.lower() or "yellow" in choosenColor.lower()):
                    currentColor = choosenColor.lower()
            currentNumber = "17"
            if (playerHand[card] == "wild draw 4"):
                drawCard(4, "computer")
            
        playerHand.remove(playerHand[card])
        if (skip == False):
            computerTurn()
        else:
            checkPlayerHand()

    elif (player == "computer"):
        skip = False
        playedCards.append(currentCard)
        currentCard = compHand[card]
        if (wild == False):
            if ("draw two" in compHand[card]):
                drawCard(2, "person")
                currentColor, null, null1 = compHand[card].split(' ')
                currentNumber = "draw two"
            elif ("skip" in compHand[card] or "reverse" in compHand[card]):
                currentColor, currentNumber = compHand[card].split(' ')
                skip = True
            else:
                currentColor, currentNumber = compHand[card].split(' ')
        else:
            blueCards = 0
            greenCards = 0
            redCards = 0
            yellowCards = 0
            for i in range(0, len(compHand), 1):
                if ("blue" in compHand[i]):
                    blueCards += 1
                elif ("green" in compHand[i]):
                    greenCards += 1
                elif ("red" in compHand[i]):
                    redCards += 1
                elif ("yellow" in compHand[i]):
                    yellowCards += 1

            if (blueCards > greenCards and blueCards > redCards and blueCards > yellowCards):
                currentColor = "blue"
            elif (greenCards > blueCards and greenCards > redCards and greenCards > yellowCards):
                currentColor = "green"
            elif (redCards > blueCards and redCards > greenCards and redCards > yellowCards):
                currentColor = "red"
            elif (yellowCards > blueCards and yellowCards > greenCards and yellowCards > redCards):
                currentColor = "yellow"
            else:
                colors = ["blue", "green", "red", "yellow"]
                currentColor = colors[random.randint(0, len(colors)-1)]

            currentNumber = "17"
            if (compHand[card] == "wild draw 4"):
                drawCard(4, "person")
            
        print "Your opponent played a " + currentCard + " card."
        compHand.remove(compHand[card])
        if (skip == False):
            checkPlayerHand()
        else:
            computerTurn()

def checkCard(card, player):
    ''' Check if the card is a valid card to play '''
    
    if (player == "person"):
        if (currentColor in playerHand[card] or currentNumber in playerHand[card]):
            playCard(card, player, False)
        elif ("wild" in playerHand[card]):
            playCard(card, player, True)
        else:
            print "You can not play this card!"
            checkPlayerHand()
    elif (player == "computer"):
        if ("wild" in compHand[card]):
            playCard(card, player, True)
        else:
            playCard(card, player, False)
        

def dealCards():
    ''' Deal the cards to the players '''
    
    global deck
    global currentCard
    global currentColor
    global currentNumber
    
    for i in range(0,7,1):
        playerHand.append(deck[i])
        deck.remove(deck[i])
        compHand.append(deck[i])
        deck.remove(deck[i])

    while True:
        if "reverse" not in deck[0] and "skip" not in deck[0] and "wild" not in deck[0] and "draw" not in deck[0]:
            currentCard = deck[0]
            currentColor, currentNumber = deck[0].split(' ')
            break
        else:
            deck.append(deck[0])
            deck.remove(deck[0])

def createDeck():
    ''' This function creates the deck of cards for the game '''
    
    global deck
    
    cards = ["blue 0","blue 1","blue 1","blue 2","blue 2","blue 3","blue 3",
             "blue 4","blue 4","blue 5","blue 5","blue 6","blue 6","blue 7",
             "blue 7","blue 8","blue 8","blue 9","blue 9", "green 0","green 1",
             "green 1","green 2","green 2","green 3","green 3","green 4","green 4",
             "green 5","green 5","green 6","green 6","green 7","green 7","green 8",
             "green 8","green 9","green 9","red 0","red 1","red 1","red 2","red 2",
             "red 3","red 3","red 4","red 4","red 5","red 5","red 6","red 6","red 7",
             "red 7","red 8","red 8","red 9","red 9", "yellow 0","yellow 1",
             "yellow 1","yellow 2","yellow 2","yellow 3","yellow 3","yellow 4",
             "yellow 4","yellow 5","yellow 5","yellow 6","yellow 6","yellow 7",
             "yellow 7","yellow 8","yellow 8","yellow 9","yellow 9","blue draw two",
             "blue draw two", "green draw two", "green draw two", "red draw two",
             "red draw two", "yellow draw two", "yellow draw two","blue reverse",
             "blue reverse", "green reverse", "green reverse", "red reverse", "red reverse",
             "yellow reverse", "yellow reverse","blue skip", "blue skip",
             "green skip", "green skip", "red skip", "red skip", "yellow skip", "yellow skip",
             "wild", "wild", "wild", "wild", "wild draw 4", "wild draw 4", "wild draw 4", "wild draw 4"]
    
    while len(cards) > 0:
        card = random.randint(0,len(cards)-1)
        deck.append(cards[card])
        cards.remove(cards[card])

def main():
    ''' The main function that starts the program '''
    
    print "Setting up the game..."
    createDeck()
    dealCards()
    sleep(1)
    checkPlayerHand()

main()

If you discover any bugs let me know!

Link to comment
Share on other sites

  • 4 weeks later...

Thanks! I made some updates to it and optimized it for the Linux shell I should probably update it on here!

Link to comment
Share on other sites

  • 3 weeks later...

Thanks! I made some updates to it and optimized it for the Linux shell I should probably update it on here!

I have to admit I have your game and I have been playing it quite often. Its pretty damn enjoyable. (I was a huge fan of playing text RPG back in the day) I remember writing my first questlike game on an Apple II :D I'm still looking for one to play around with lol.

Link to comment
Share on other sites

  • 5 weeks later...

I have to admit I have your game and I have been playing it quite often. Its pretty damn enjoyable. (I was a huge fan of playing text RPG back in the day) I remember writing my first questlike game on an Apple II :D I'm still looking for one to play around with lol.

I am glad to hear someone is using it!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...