Text-based game using string indexing

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 8
Reputation: Zebibyte is an unknown quantity at this point 
Solved Threads: 1
Zebibyte's Avatar
Zebibyte Zebibyte is offline Offline
Newbie Poster

Text-based game using string indexing

 
0
  #1
Aug 8th, 2009
I just started learning Python a couple days ago, and my main goal for now is a text-based game, in which the user inputs commands to interact with the virtual world. My problem thus far, however, has been splitting a string up so that I can detect what action the player wants to take, and what the target of that action is. (for example, in my source code below, the user first types an action, punch or kick, then its target, Bob or Alice) In one of the Basic Python tutorials on python.about.com, it mentioned string indexing, where you can call one character out of a string by treating it like a list. The code from the tutorial:

  1. cat = 'dog'
  2. print cat[0]
  3. print cat[1]
  4. print cat[2]
  5.  
  6. output:
  7. d
  8. o
  9. g

So I utilized this technique to detect the actions and targets within the command strings from the user. But my main question is: is this the best way of doing it? If you look in my source code, you'll see that it relies on literal values to look for the words in the string. It fails if there is any whitespace in the string, and while this really is the only problem I can forsee (no player should expect to get away with adding more characters in the command, I think), it still irks me to have to rely on those literal values.

Therefore I ask you: is there a better way of doing this, without relying on those hard-coded values?

(sorry I'm so verbose)

  1. # text_adventure_si.py
  2. # testing a method of detecting text commands
  3. # with string indexing methods
  4.  
  5. print "Bob and Alice stand in front of you."
  6. print "You can either punch or kick them."
  7. print "Type help for command help and quit to exit."
  8.  
  9. action = ""
  10.  
  11. while action.lower() != "quit":
  12. act_in = raw_input("> ")
  13. action = act_in.lower()
  14. if action[0:6] == "punch ":
  15. if action[6:9] == "bob":
  16. print "You punch Bob."
  17. elif action[6:11] == "alice":
  18. print "You punch Alice."
  19. else:
  20. print "You did something wrong."
  21. elif action[0:5] == "kick ":
  22. if action[5:8] == "bob":
  23. print "You kick Bob."
  24. elif action[5:10] == "alice":
  25. print "You kick Alice."
  26. else:
  27. print "You did something wrong."
  28. elif action == "help":
  29. print "Type 'punch' and either Bob or Alice to punch them."
  30. print "You can do the same thing with 'kick'"
  31. print "Type 'quit' to exit the game."
  32. elif action == "quit":
  33. print "Farewell."
  34. else:
  35. print "You did something wrong."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Text-based game using string indexing

 
0
  #2
Aug 8th, 2009
If you were to split the users input into a list you can use list indexing to call out each word individually. Here's an example:
  1. >>> act_in = "punch Alice in the face"
  2. >>> act_in_parts = act_in.split()
  3. >>> action = act_in_parts[0]
  4. >>> target = act_in_parts[1]
  5. >>> action
  6. 'punch'
  7. >>> target
  8. 'Alice'
  9. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: Zebibyte is an unknown quantity at this point 
Solved Threads: 1
Zebibyte's Avatar
Zebibyte Zebibyte is offline Offline
Newbie Poster

Re: Text-based game using string indexing

 
0
  #3
Aug 8th, 2009
Thanks! But I have another question: is it possible to search the resulting list, in case there are extra words in between "punch" and "alice"? It would be especially useful if you could detect if "alice" is further back in the sentence than "punch".
Last edited by Zebibyte; Aug 8th, 2009 at 3:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Text-based game using string indexing

 
0
  #4
Aug 8th, 2009
You can use in to figure out if a list (or string for that matter) contains a particular element:
  1. >>> act_in = "I would love to take alice and punch her in the mouth"
  2. >>> action = act_in.lower().split()
  3. >>> if 'alice' in action:
  4. ... print('This action will be performed on Alice')
  5. ... elif 'bob' in action:
  6. ... print('This action will be performed on Bob')
  7. ...
  8. This action will be performed on Alice
  9. >>> if 'punch' in action:
  10. ... print('The target is getting punched')
  11. ... elif 'kick' in action:
  12. ... print('The target is getting kicked')
  13. ...
  14. The target is getting punched
  15. >>>
I believe that in simply makes use of the list method index (or analogously string.find ), which returns the index of the element you're looking for, and -1 if it does not exist in the list (string). So in would be the true/false version of using list.index (or string.find ).

So in the above code, splitting the string into a list is unnecessary if you're not using indexing to access individual words. Leave the action as a string and you can just ask if the word you're looking for is in the string.
  1. >>> act_in = 'Sometimes, I like to kick people in the teeth. One of the people that I frequently kick is named Bob. That Bob is such an idiot.'
  2. >>> action = act_in.lower()
  3. >>> 'bob' in action
  4. True
  5. >>> 'alice' in action
  6. False
  7. >>> 'punch' in action
  8. False
  9. >>> 'kick' in action
  10. True
  11. >>>
Last edited by jlm699; Aug 8th, 2009 at 3:47 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: Zebibyte is an unknown quantity at this point 
Solved Threads: 1
Zebibyte's Avatar
Zebibyte Zebibyte is offline Offline
Newbie Poster

Re: Text-based game using string indexing

 
0
  #5
Aug 8th, 2009
Wow, thanks! It looks like in is the best option. I'll be sure to switch over to it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC