Hello,

I have been working on a very simple Python Game so far (Not using pygame atm) and I ran into a problem that is very simple but I just do not know what to use to get it to work.

Basically what I have right now is that the player will deal a set of cards. Each card will be randomized with a simple code:

card1 = random.randrange(1,10)
card2 = random.randrange(1,10)
card3 = random.randrange(1,10)
card4 = random.randrange(1,10)
card5 = random.randrange(1,10)

The problem I am having is that after randomizing the cards, I do not know how to make it so that if the card is equal to 1, it will execute a different function then if it was 5.

For example, a simple tower-tower block battle. Each castle starts with 50 blocks. Using this randomizer with cards, if the random numbers are odd, X number of blocks will be deducted from the 2nd player's tower. If the random numbers are even, X number of blocks will be added to your tower.

I was wondering if this was possible (It probably is), but I still feel I haven't explained this right.

Let me put it this way.

"randomnumber1" will do the following:
Add 5 Blocks to your tower.
Print a message.
Discard the card.

"randomnumber2" will do the following:
Delete 5 Blocks to other tower.
Print a message.
Discard the card.

I "Deal" the cards. My first card is a random number of 2. If I decide to use my first card, it will execute "randomnumber2".

So basically I am wondering if I could:
- Set up executable lines of code such as "randomnumber1" and 2.
- Link the randomized number to the executable lines of code depending on the number.
- "Use" the card to execute the commands of the code.

Please tell me if you need more clarification in what I am trying to say! I would love to get some help on this. I have been googling for a few hours now :@
Thanks!

Recommended Answers

All 4 Replies

I'm not quite sure what you are doing with the whole of the code, but I get what you want. Let's say you have three different lines of code, and a random number between 1 and 3. If it's 1, you execute the first line, if it's 2, you execute the second line, and if it's 3, you execute the third line.

what_will_it_do = random.randrange(1,3)

if what_will_it_do == 1:
	#first thing to do
elif what_will_it_do == 2:
	#second thing to do
elif what_will_it_do == 3:
	#third thing to do

Here's something I just knocked up in a few minutes....This isn't exactly what you're doing, but it should give you a few ideas to use in your game.

My variant uses one tower. The player starts with 50 blocks in their tower.
The computer will then draw random cards (valued from 1-10) and will then add or remove blocks from the players tower until the maximum or minimum number of blocks has been reached.
Odd numbered cards remove blocks, even numbers add blocks. Cards are worth 5,10,15,20 or 25 blocks.

I'm not sure how au fait you are with python, so I've used some pretty simple code and commented it heavily....

Without further ado, here's the listing:

import random

# set up our maximum and minimum number of blocks
MAXBLOCKS=100
MINBLOCKS=0

# set up the initial number of blocks in the tower
userBlocks=50

# count how many cards are dealt in the game
dealCount=0

# some instructions:
print "The aim is to get 100 or more blocks in your tower."
print "The computer will randomly select a card for you (1-10)."
print "The value of the card will affect your towers height."
print "Even numbers add blocks, odd numbers remove blocks."
print "Cards are worth the following:"
print " Card value    -    Block Value"
print "================================"
print "    1,2        -     +/- 05"
print "    3,4        -     +/- 10"
print "    5,6        -     +/- 15"
print "    7,8        -     +/- 20"
print "    9,10       -     +/- 25\n"

# let the user know how many blocks they're starting with
print "You start with", userBlocks, "blocks.\n"
print "Will you be lucky??"
dummy = raw_input("Press enter to start drawing cards and find out...\n")

# create a little space once user has pressed enter
print "\n\n"

# while the players amount of blocks is within the limits
while(userBlocks<MAXBLOCKS and userBlocks>MINBLOCKS):

    # Deal a card and show it to the user
    card = random.randrange(1,10)
    print "The card drawn is:", card

    #now we'll get the blockValue of the card
    blockValue=0;
    if ( card==1 or card==2):
        blockValue=5
    elif (card==3 or card==4):
        blockValue=10
    elif (card==5 or card==6):
        blockValue=15
    elif (card==7 or card==8):
        blockValue=20
    else:
        blockValue=25

    #now should we add or subtract?
    print blockValue, "points have been",

    # if card value is even, add the blockvalue to the users blocks
    if(card%2==0): 
        print "added to",
        userBlocks+=blockValue
    else: # card is odd, so subtract!
        print "subtracted from",
        userBlocks-=blockValue
    print "your tower."

    # if we're still within our limits, inform the player of their
    # current block total...
    if(userBlocks>MINBLOCKS and userBlocks<MAXBLOCKS):
        print "You now have", userBlocks, "blocks in your tower!\n"

    # increment the deal counter
    dealCount+=1

# end of game
print "GAME OVER after", dealCount, "deals!"

# if users total was MINBLOCKS or less
if userBlocks <= MINBLOCKS:
    print "You lost all of your blocks! You lose!"
else: #user must have won!
    print "You won! Your tower is", userBlocks, "high!"

Note: The above is python 2.x code, if you're using 3.x then you'll need to change all of the print statements to print("whatever")!

Anyways, hope this is of some help to you!
Cheers for now,
Jas.

card1 = random.randrange(1,10)
card2 = random.randrange(1,10)
card3 = random.randrange(1,10)
card4 = random.randrange(1,10)
card5 = random.randrange(1,10)

The problem I am having is that after randomizing the cards, I do not know how to make it so that if the card is equal to 1, it will execute a different function then if it was 5.

I was wondering if this was possible (It probably is), but I still feel I haven't explained this right.

Let me put it this way.

"randomnumber1" will do the following:
Add 5 Blocks to your tower.
Print a message.
Discard the card.

"randomnumber2" will do the following:
Delete 5 Blocks to other tower.
Print a message.
Discard the card.

I "Deal" the cards. My first card is a random number of 2. If I decide to use my first card, it will execute "randomnumber2".

So basically I am wondering if I could:
- Set up executable lines of code such as "randomnumber1" and 2.
- Link the randomized number to the executable lines of code depending on the number.
- "Use" the card to execute the commands of the code.

Please tell me if you need more clarification in what I am trying to say! I would love to get some help on this. I have been googling for a few hours now :@
Thanks!

If you put the cards into an array, you could then run a for loop subset with an if loop. this is to learn the prpoperties of the cards as soon as they are randomised, before they are dealt. If you wait until after they are dealt, then you would need to add a way of dealing with cards individually, but to deal with them on generation would make this easier as they are then dealt out with their values and as soon as used the values are already known.

I am fairly new to programming but something like the following (exact code would need to be worked out/found out)

If we assume you have created an array called cards containing each of the cards..

for (card in cards)
    if card = 1:
        do something..
    elif card = 2:
        do something else..
    else:
        do something or nothing

That is just a rough example, and the exact syntax might be slightly off, trying to learn 3 languages at the same time so I get a bit confused at the moment :)

Hope this helps :)

hi can you give me a simple code of lucky 9 game in python i am a first user in daniweb i dont know how to how to create a code so why i ask direct for you..plpz i need A HELP!!can you give me a code???? plz i need now???

Editor's note:
please do not hijack unrelated threads
start your own thread, and title it help with lucky 9 game
explain what the game is and show some effort

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.