GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jan-2009, 15:31
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Black Jack game


I have a Black Jack game but it's not working properly. It keeps repeating the same numbers. Does any one know how to make it not repeat the numbers?

Python Code:
import random as r
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 , 11]*4
computer = []
player = []
c = 'y'

def clear():
    import os
    if os.name == 'nt':
        os.system('cls')
    if os.name == 'posix':
        os.system('clear')
        
def showHand():
    hand = 0
    for i in player: hand += i
    print "the computer is showing a %d" % computer[0]
    print "your hand totals: %d (%s)" % (hand, player)

def setup():
    for i in range(2):
        dealcomputer = deck[r.randint(1, len(deck) -1)]
        dealPlayer = deck[r.randint(1, len(deck)-1)]
        computer.append(dealcomputer)
        player.append(dealPlayer)
        deck.pop(dealcomputer)
        deck.pop(dealPlayer)
setup()
while c != 'q':
    showHand()
    c = raw_input("[D]ealt [S]tick [Q]uit: ").lower()
    clear()
    if c == 'd':
        dealcomputer = deck[r.randint(1, len(deck)-1)]
        dealPlayer = deck[r.randint(1,len(deck)-1)]
        computer.append(dealPlayer)
        deck.pop(dealPlayer)
        hand = 0
        for i in player: hand += i
        if hand > 21:
            print "BUST!"
            player = []
            computer = []
            setup ()
            hand = 0
        for i in computer: hand += i
        if hand > 21:
            print "computer Busts!"
            player = []
            computer = []
            setup = ()
        
    elif c == 's':
        cHand = 0
        pHand = 0

        for i in computer: cHand += i
        for i in player: pHand += i
        if pHand > cHand:
            print "You win!"
            computer = []
            player = []
            setup = ()
        else:
            print "computer win!"
            computer = []
            player = []
            setup = ()
    else:
        if c == 'q':

         gb= raw_input("Press Enter (q to quit): ").lower()
         if 'q' in exit:
             break
Last edited by admin : 12-Jan-2009 at 02:48. Reason: Please insert your Python code between [py] & [/py] tags
  #2  
Old 12-Jan-2009, 01:08
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


Can't you focus rand on range of like 1-52 and it will select subsequent values without repeating?

If not , what if you used an array of [1 thu 52]
Use rand to choose an indice, get that value into a separate variable, and change the value at that indice to zero.
Then take that variable value and divide by 13 to detemine suit and then %13 to determine value (I think)

Like if rand chooses indice 35: 35 / 13 = 2 , , 35 % 13 = 9 , , so you have a 9 of suit 2. (suits are 0 thru 3)

I think there will need to be an adjustment where if( (x%13) == 0 ) suit = suit-1 or something?
Like: 54 % 13 = 0 and 54 / 13 = 4 hmmm need to think about that some...

On subsequent rand selects, if you get a zero, increment up (or down ) until you get a value.
Guess you'll need a counter in there to count 52.
To re-shuffle just load the array with 1 thru 52 again.

Seems like a lot to go thu though doesn't it. Maybe there's a better way.
I made this Cprog to see how the values break out: (sorry about C No time to figure it out in py right now)
CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  int i, j;
  for(i= 0; i < 13; i++)
  {
    for(j= 1; j <= 52; j++)
    {
      if((j % 13) == i )
      {
        printf("%d %% 13 = %d \n", j,  i);
      }
    }
  }
  return 0;
}
/* 
$ gcc -Wall -W -pedantic 090112_bjack.c -o 090112_bjack
$  ./090112_bjack
13 % 13 = 0 
26 % 13 = 0 
39 % 13 = 0 
52 % 13 = 0 
1 % 13 = 1 
14 % 13 = 1 
27 % 13 = 1 
40 % 13 = 1 
2 % 13 = 2 
15 % 13 = 2 
28 % 13 = 2 
41 % 13 = 2 
3 % 13 = 3 
16 % 13 = 3 
29 % 13 = 3 
42 % 13 = 3 
4 % 13 = 4 
17 % 13 = 4 
30 % 13 = 4 
43 % 13 = 4 
5 % 13 = 5 
18 % 13 = 5 
31 % 13 = 5 
44 % 13 = 5 
6 % 13 = 6 
19 % 13 = 6 
32 % 13 = 6 
45 % 13 = 6 
7 % 13 = 7 
20 % 13 = 7 
33 % 13 = 7 
46 % 13 = 7 
8 % 13 = 8 
21 % 13 = 8 
34 % 13 = 8 
47 % 13 = 8 
9 % 13 = 9 
22 % 13 = 9 
35 % 13 = 9 
48 % 13 = 9 
10 % 13 = 10 
23 % 13 = 10 
36 % 13 = 10 
49 % 13 = 10 
11 % 13 = 11 
24 % 13 = 11 
37 % 13 = 11 
50 % 13 = 11 
12 % 13 = 12 
25 % 13 = 12 
38 % 13 = 12 
51 % 13 = 12 
*/
Last edited by Howard_L : 12-Jan-2009 at 02:05.
  #3  
Old 13-Jan-2009, 00:39
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


Wow, check this out:
Python Code:
#!/usr/bin/env python

import random

deck = range(52)

print "shuffling"

random.shuffle(deck)

for i in deck:
  print i

print "reshuffling"
random.shuffle(deck)
for i in deck:
  print i
That could come in very handy for the above! This language is somethong else...
  #4  
Old 16-Jan-2009, 09:56
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Re: Black jack game


its not working its givin me an error..av you got your own version of a black jack game...this pytho is killin me head
  #5  
Old 16-Jan-2009, 10:57
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


No , I haven't made a blackjack game. Good project though. Is this for a class?

What's not working?
Where is the error occuring?
What does it say?
Post the your exact code so I can see where the problem is and help you.
  #6  
Old 16-Jan-2009, 11:23
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Re: Black jack game


Python Code:
import random as r
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 , 11]*4
computer = []
player = []
c = 'y'

def clear():
    import os
    if os.name == 'nt':
        os.system('cls')
    if os.name == 'posix':
        os.system('clear')
        
def showHand():
    hand = 0
    for i in player: hand += i
    print "the computer is showing a %d" % computer[0]
    print "your hand totals: %d (%s)" % (hand, player)

def setup():
    for i in range(2):
        dealcomputer = deck[r.randint(1, len(deck) -1)]
        dealPlayer = deck[r.randint(1, len(deck)-1)]
        computer.append(dealcomputer)
        player.append(dealPlayer)
        deck.pop(dealcomputer)
        deck.pop(dealPlayer)
setup()
while c != 'q':
    showHand()
    c = raw_input("[D]ealt [S]tick [Q]uit: ").lower()
    clear()
    if c == 'd':
        dealcomputer = deck[r.randint(1, len(deck)-1)]
        dealPlayer = deck[r.randint(1,len(deck)-1)]
        computer.append(dealPlayer)
        deck.pop(dealPlayer)
        hand = 0
        for i in player: hand += i
        if hand > 21:
            print "BUST!"
            player = []
            computer = []
            setup ()
            hand = 0
        for i in computer: hand += i
        if hand > 21:
            print "computer Busts!"
            player = []
            computer = []
            setup = ()
        
    elif c == 's':
        cHand = 0
        pHand = 0

        for i in computer: cHand += i
        for i in player: pHand += i
        if pHand > cHand:
            print "You win!"
            computer = []
            player = []
            setup = ()
        else:
            print "computer win!"
            computer = []
            player = []
            setup = ()
    else:
        if c == 'q':

         gb= raw_input("Press Enter (q to quit): ").lower()
         if 'q' in exit:
             break

ITS GIVIN ME AN ERROR MESG..SEE BELOW:
the computer is showing a 10
your hand totals: 18 ([8, 10])
[D]ealt [S]tick [Q]uit: d
computer Busts!

Traceback (most recent call last):
File "C:/Python26/testn", line 30, in <module>
showHand()
File "C:/Python26/testn", line 17, in showHand
print "the computer is showing a %d" % computer[0]
IndexError: list index out of range
>>>
Last edited by LuciWiz : 16-Jan-2009 at 11:49. Reason: Please insert your Python code between [py] & [/py] tags
  #7  
Old 16-Jan-2009, 13:11
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


dude, it is EXTREMELY important to show you indents in python.
That is how python understands scope.

Please read the guidelines to learn how to enclose your code in the code tags
next time (as LuciWiz has done for you above) so that the indents will show.

What OS are you running this on?
I have just done the windows install and am learning how to use it...

So is that saying that computer[0] is out of range?

When I first start that line prints, after I make choose [Dealt] I get the error you show...
hmmm so arrays can become empty?
I am new to python so it will take me a while to figure anything out and I won't be able to do that until tonight. sorry
But you could try playing with an array and see if you can mimic what changes computer[] goes through during that part of the program. Just on the command line try stuff like:
Python Code:
c:\python26\python
Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> comp = []
>>>
>>> print comp[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
#### same basic error message! list index out of range
>>>
>>> comp[0] = 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>>
>>> comp = [10]
>>>
>>> comp[0] = 11
>>>
>>> print comp[0]
back later
Last edited by Howard_L : 16-Jan-2009 at 14:31.
  #8  
Old 16-Jan-2009, 23:42
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Black jack game


And so I see that you redefine computer and player in each of these if's:
Python Code:
        if hand > 21:
            print "BUST!"
            player = []
            computer = []
            setup ()
            hand = 0
As seen above , you can't print the value stored in an empty set , because there isn't one!
Maybe you want to give them some value instead, maybe zero?
also setup doesn't look quite right there does it? (there are other typo's like that)
  #9  
Old 17-Jan-2009, 10:11
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Re: Black jack game


I Am New To Python As Well And Only Trying To Understand How To Make This Game Work....its Takin Me Looooooooooong To Understand
  #10  
Old 17-Jan-2009, 10:16
SYLE SYLE is offline
New Member
 
Join Date: Jan 2009
Posts: 7
SYLE is on a distinguished road

Re: Black jack game


i tried is again its givin me
(12.1.09)
(Cam-81288)
the computer is showing a 11
your hand totals: 21 ([10, 11])
[D]eal [Q]uit [S]tick: s
Youn win!
Traceback (most recent call last):
File "E:\test.py", line 33, in ?
showHand()
File "E:\test.py", line 20, in showHand
print "the computer is showing a %d" % computer[0]
IndexError: list index out of range
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with a simple "dragon" game. ddaigle C++ Forum 13 04-Dec-2007 07:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.