Python Forum
Word based Game of 21
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word based Game of 21
#1
Hi, I'm new to this forum and to python. I'm studying software development and I created a word based game of 21 with adjusted rules and a point system. This is my first attempt at coding Python and the only experience I have with coding is very minimal entry level C++.

The rules of the game
The player must reach 10 or more points to win, and three rounds to do so.
Each round, the player draws a random card number from 1 to 10, and keeps drawing until they decide to stop.
Once they stop, they will gain points based on their distance from 21, and will lose if they cannot reach 10 points within three rounds. However, f the user does lands on 21, it is an instant win. After that, they can choose to play again or not.

Choosing to stop drawing on the following numbers will reward the following number of points:
20 - 5 points, 19 - 4 points, 18 - 3 points, 17 - 2 points, 16 - 1 point.

The player can win in one round, by stopping on 21, or in two rounds by hitting 20 twice for example.
I created a flowchart to show how the game should work.

[Image: 21_Game_flowchart_Diagram_1_7_2.jpg]

Firstly i should state that I'm not sure if I'm using the software in the best way, so if I'm doing something wrong please let me know.
I'm using Spyder (Python 2.7) on Windows 10.
I run the code, and test the results from the IPython console in the software (is this the correct way/place to do this?)

Here's the code I've written. I included comments, but I can re-upload without them if necessary.

print("Let's play a game of 21!")
print("You have three rounds to get 10 points.")
print("Keep drawing cards from 1-10 until you reach 21! You can choose to stop early however!")
print("You get points based on how close you are to 21 - landing on 20 earns you 5 points and you get 1 less point the further away you are from 21.")
print("Hit 21 though and you immediately win!")
print("\nWant to give it a go?")

import random

total_points=0
current_total=0
round_count=0

def start_game(): #function to reset points and start the game
    total_points -1000 #resets points at game start
    current_total -1000 #resets total at game start
    round_count - 1000 # resets round count at game start
         
    play_again = str(input("Do you want to start?")) #asks to start the game    
    while play_again == "yes": 
       play_game #starts the game if the user types "yes"
    else:
        print("Thank you for playing!") #thanks the user for playing and ends the game if naything other than 'yes' is entered
        
def play_game(): #function that acts main body of the game
     if round_count < 3: #checks that the user hasn't used all available attempts
            if total_points < 10: #checks if user has already reached the required points to win
                print("Your have:", total_points, "points") #tells user their current points
                draw #initiates the draw function
     else:
         if total_points < 10: #after three rounds checks if user has reached 10 points
             print("You didn't get 10 points! you lose! Try again next time!") #prints loss message if user has less than 10 points, 
             start_game #resets the points and asks to start the game again
         else: 
             print("Congratulations! You win!") #prints win message if user has 10 or more points afte rthree rounds
             start_game #resets the points and asks to start the game again
         
def next_round(): #function to increase the round count and continue playing
    round_count + 1 #increases round count
    play_game #plays the game, keeping the total and score
    
def draw(): #function to check total and draw card number from set values, and apply points based on card drawn
    
    if current_total > 21: #first checks if the current total is above 21
        print ("You went over 21! You got 0 points this round!") #tells user their total was too high to score
        next_round #increases round count by 1, then plays the game again keeping round count and points
        
    elif current_total < 21:
        print("Current total:", current_total )          
        draw_card = str(input("Draw a card?")).lower() #asks the user to draw a card
       
        if draw_card == "yes": #when the user chooses to draw a card by typing "yes";
            c = set(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10)
                #declares the set of available cards
            card = random.choice(list(c)) #chooses a random card from the set
            print("You drew the number:", card) #tells the user what card they drew
            current_total + card # adds card drawn to current total
            c.remove(card) #removes card from set
            draw #starts draw process again
            
        elif draw_card == "no": #when the user chooses not to draw by typing "no";
            if current_total == 21: #checks if user already has a total of 21
                print("21! You Win!") #tells the user they have won upon reaching 21 
                start_game #resets the points and asks to start the game again
                
            elif current_total < 16: #checks if the user's total is under 16
                print("You didn't score high enough to get any points! Be a bit more daring next time!") #tell user their total is too low to score
                next_round #increases round count by 1, then plays the game again keeping round count and points
                
            elif current_total == 20: #checks if the user's total is 20
                total_points + 5 #increase point total by 5
                print("20! So close! That's 5 points!") #tells the user how many points they earned
                next_round #increases round count by 1, then plays the game again keeping round count and points
            
            elif current_total == 19: #checks if the user's total is 19
                total_points + 4 #increase point total by 4
                print("4 points.") #tells the user how many points they earned
                next_round #increases round count by 1, then plays the game again keeping round count and points
            
            elif current_total == 18: #checks if the user's total is 18
                total_points + 3 #increase point total by 3
                print("3 points.") #tells the user how many points they earned
                next_round #increases round count by 1, then plays the game again keeping round count and points
                
            elif current_total == 17: #checks if the user's total is 17
                total_points + 2 #increase point total by 2
                print("2 points.") #tells the user how many points they earned
                next_round #increases round count by 1, then plays the game again keeping round count and points
                
            elif current_total == 16: #checks if the user's total is 16
                total_points + 1 #increase point total by 1
                print("1 points.") #tells the user how many points they earned
                next_round #increases round count by 1, then plays the game again keeping round count and points
            
            else: #backup route in total calculation system fails
                print("Sorry, something went wrong.") #prints error message
                start_game #resets the points and asks to start the game again
        else:
             print("You're trolling. Please enter with 'yes' or 'no'.") #tells user to answer 'yes' or 'no' to progress
             draw  #starts draw process again
             
    elif current_total == 21: #checks if user's total is 21
         print("21! You Win!") #tells the user they have won upon reaching 21 
         start_game #resets the points and asks to start the game again
         
start_game #starts the whole process from the start_game functions through to the relevant functions to play the game
Expected Result
When I run the code should print out the opening statements, then the start_game function should start, asking me if I want to start the game, and waiting for my input - proceeding through the function if the input is "yes".

Actual Result
Prints the opening statement (as below) and ends.

Output:
Let's play a game of 21! You have three rounds to get 10 points. Keep drawing cards from 1-10 until you reach 21! You can choose to stop early however! You get points based on how close you are to 21 - landing on 20 earns you 5 points and you get 1 less point the further away you are from 21. Hit 21 though and you immediately win! Want to give it a go?
There are no inline errors shown to be by the software, as I am still fresh to coding so I'm not sure where I've gone wrong. it looks like the start_game function doesn't start.

Any help?
Reply
#2
You need parentheses after start_game. start_game is the function itself, which you can treat like most other objects. start_game() runs the start_game function and returns the result.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Players get lazy and don't want to type in yes, sometimes they only want y or a Y so I try to offer several choices.

    #if choice in ('yes', 'Yes', 'YES', 'y', 'Y'):    #if choice of user is included inside the ()
    if choice == 'yes' or choice == 'Y' or choice == 'y':
You would modify your while statement for this.
I comment one of my if statements out depending on which one I want to use. also be sure that all your functions have parentheses() where needed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help creating a word game wthiierry 4 2,335 Nov-01-2022, 12:29 PM
Last Post: perfringo
  Scramble word game Zatoichi 9 8,320 Sep-20-2021, 03:47 PM
Last Post: jefsummers
  sys.stdin to do a word count based on user entry Kaltex 3 3,632 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  Random Math Text Based Game captainflint 2 4,440 Aug-05-2018, 05:40 PM
Last Post: ahsankhan
  scrabble word game atux_null 4 8,676 Nov-10-2017, 10:00 AM
Last Post: atux_null

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020