Jump to content

Poker program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
jivkoss

jivkoss

    Newbie

  • Members
  • PipPip
  • 28 posts
from random import choice as rc


print("LET'S PLAY POKER")

while True:

    a = raw_input("Do you want to play poker? y/n: ")

    if a == 'y':

        cards = ['2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', '10h', 'Jh', 'Qh', 'Kh', 'Ah', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', '10d', 'Jd', 'Qd', 'Kd', 'Ad', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', '10c', 'Jc', 'Qc', 'Kc', 'Ac', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', '10s', 'Js', 'Qs', 'Ks', 'As']

        player = []

        player.append(rc(cards))

        player.append(rc(cards))

        print("You have {0}".format(player))

        flop = []

        flop.append(rc(cards))

        flop.append(rc(cards))

        flop.append(rc(cards))

        print("Dealing the flop: {0} ".format(flop))

        turn = []

        turn.append(rc(cards))

        print("Dealing the turn: {0} ".format(turn))

        river = []

	river.append(rc(cards))

        print("Dealing the river: {0} ".format(river))

        print("The board is {0} {1} {2} ".format(flop, turn, river))

        computer = []

        computer.append(rc(cards))

	computer.append(rc(cards))

        print("Computer has {0} ".format(computer))

        print("Guess who wins")

    elif a == 'n':

        print("I gues you are to tired of losing")

        break

    else:

        print("Invalid command try again")


print("Done")

raw_input("Press<enter>to exit")


What I don't like is that it is possible the same card to be dealt to the player and also appear on the board. How can I make my program in such a way that after i take an object from the 'cards' list it removes itself from the list and wont be available as random choice for the 'flop' list for example.
Also tell me what you think for my program. I've been learning python for a month and this code is the best come up with.

#2
Excited

Excited

    Newbie

  • Members
  • PipPip
  • 27 posts
Add this method:

def add(target, card):
    target.append(card)
    cards.remove(card)

now instead of this:

player.append(rc(cards))
flop.append(rc(cards))

use:

add(player, rc(cards))
add(flop, rc(cards))


#3
jivkoss

jivkoss

    Newbie

  • Members
  • PipPip
  • 28 posts
@Excited
Thanks a lot. It's very useful to define function and call it. I'll use this in my blackjack program too.

#4
jivkoss

jivkoss

    Newbie

  • Members
  • PipPip
  • 28 posts
So here is my final poker program:
from random import choice as rc

print("LET'S PLAY POKER")
while True:
    a = raw_input("Do you want to play poker? y/n: ")
    def add(target, card):
        target.append(card)
        cards.remove(card)    
    if a == 'y':
        cards = ['2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', '10h', 'Jh', 'Qh', 'Kh', 'Ah', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', '10d', 'Jd', 'Qd', 'Kd', 'Ad', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', '10c', 'Jc', 'Qc', 'Kc', 'Ac', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', '10s', 'Js', 'Qs', 'Ks', 'As']
        player = []
        add(player, rc(cards))
        add(player, rc(cards))
        print("You have {0}".format(player))
        flop = []
        add(flop, rc(cards))
        add(flop, rc(cards))
        add(flop, rc(cards))
        print("Dealing the flop: {0} ".format(flop))
        turn = []
        add(turn, rc(cards))
        print("Dealing the turn: {0} ".format(turn))
        river = []
        add(river, rc(cards))
        print("Dealing the river: {0} ".format(river))
        print("The board is {0} {1} {2} ".format(flop, turn, river))
        computer = []
        add(computer, rc(cards))
	add(computer, rc(cards))
        print("Computer has {0} ".format(computer))
        print("Guess who wins")
    elif a == 'n':
        print("I gues you are to tired of losing")
        break
    else:
        print("Invalid command try again")

print("Done")
raw_input("Press<enter>to exit")

I want to make it in such a way, that when i run it in the command prompt the user will have to press <space> in order to continue the program. I mean after dealing the flop the user has to press <space> for dealing the turn and then again <space> for dealing the river. Anyone has an idea how this can be done?

#5
jivkoss

jivkoss

    Newbie

  • Members
  • PipPip
  • 28 posts
PS: I know I can use
raw_input("press<enter>to continue")
but I want to make it with <press space>.

#6
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
write a function that checks for whitespace.

def whitespace(input)
if input==" ":
continue
else:
break

#7
jivkoss

jivkoss

    Newbie

  • Members
  • PipPip
  • 28 posts
You probably mean I could write this:
def whitespace():
    while True:
        a = raw_input()
        if a == ' ': break
        else: continue

But i still have to press enter.

#8
yamman13

yamman13

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
I see your point. The only way I can think of, is to use Tkinter, and set up a handler function for spacebar. Whenever its pressed, the handler function should then pass on the argument, e.g. 1 for being pressed, to another function within your program.