Moving around a small grid: Difference between revisions
(Created page with "right|frame|This a problem set for you to work through This is a problem set. Some of these are easy, others are far more difficult. The purpose of these...") |
|||
Line 18: | Line 18: | ||
# that would be pretty cool, huh? | # that would be pretty cool, huh? | ||
current_player_y_position = | current_player_y_position = 5 | ||
current_player_x_position = 5 | current_player_x_position = 5 | ||
def move(direction,pos_y,pos_x): | def move(direction,pos_y,pos_x): | ||
global current_player_x_position | |||
global current_player_y_position | |||
if direction == "up": | if direction == "up": | ||
if pos_y == 1: | if pos_y == 1: | ||
Line 32: | Line 34: | ||
print("You walk up one square. ") | print("You walk up one square. ") | ||
print("-----------------------") | print("-----------------------") | ||
current_player_y_position = current_player_y_position - 1 | current_player_y_position = current_player_y_position - 1 | ||
foo = raw_input("Press enter to continue...") | foo = raw_input("Press enter to continue...") | ||
Line 46: | Line 46: | ||
print("You walk right one square. ") | print("You walk right one square. ") | ||
print("---------------------------") | print("---------------------------") | ||
current_player_x_position = current_player_x_position +1 | current_player_x_position = current_player_x_position +1 | ||
foo = raw_input("Press enter to continue...") | foo = raw_input("Press enter to continue...") | ||
Line 60: | Line 58: | ||
print("You walk left one square. ") | print("You walk left one square. ") | ||
print("---------------------------") | print("---------------------------") | ||
current_player_x_position = current_player_x_position -1 | current_player_x_position = current_player_x_position -1 | ||
foo = raw_input("Press enter to continue...") | foo = raw_input("Press enter to continue...") | ||
Line 74: | Line 70: | ||
print("You walk down one square. ") | print("You walk down one square. ") | ||
print("-----------------------") | print("-----------------------") | ||
current_player_y_position = current_player_y_position + 1 | current_player_y_position = current_player_y_position + 1 | ||
foo = raw_input("Press enter to continue...") | foo = raw_input("Press enter to continue...") |
Revision as of 19:55, 16 March 2016
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.
The Problem[edit]
This problem is designed to test your skill and knowledge of strings, specifically your knowledge and understanding of combining (concatenating) strings.
- The web is built with HTML strings like "
<i>Yay</i>
" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word [1]
Some Code to Get You Started[edit]
#
# this is the start of a simple game on a 10 x 10 grid that let's players move around in a fairly simple way.
#
# currently, all we have is movement and updating the board. But if a student wanted to add TERRAIN, BAD GUYS, COMBAT, and maybe even BASIC AI
# that would be pretty cool, huh?
current_player_y_position = 5
current_player_x_position = 5
def move(direction,pos_y,pos_x):
global current_player_x_position
global current_player_y_position
if direction == "up":
if pos_y == 1:
print("-----------------------")
print("You cannot go that way.")
print("-----------------------")
foo = raw_input("Press enter to continue...")
else:
print("-----------------------")
print("You walk up one square. ")
print("-----------------------")
current_player_y_position = current_player_y_position - 1
foo = raw_input("Press enter to continue...")
elif direction == "right":
if pos_x == 10:
print("-----------------------")
print("You cannot go that way.")
print("-----------------------")
foo = raw_input("Press enter to continue...")
else:
print("---------------------------")
print("You walk right one square. ")
print("---------------------------")
current_player_x_position = current_player_x_position +1
foo = raw_input("Press enter to continue...")
elif direction == "left":
if pos_x == 1:
print("-----------------------")
print("You cannot go that way.")
print("-----------------------")
foo = raw_input("Press enter to continue...")
else:
print("---------------------------")
print("You walk left one square. ")
print("---------------------------")
current_player_x_position = current_player_x_position -1
foo = raw_input("Press enter to continue...")
elif direction == "down":
if pos_y == 10:
print("-----------------------")
print("You cannot go that way.")
print("-----------------------")
foo = raw_input("Press enter to continue...")
else:
print("-----------------------")
print("You walk down one square. ")
print("-----------------------")
current_player_y_position = current_player_y_position + 1
foo = raw_input("Press enter to continue...")
return
def update_board(x_position, y_position):
if x_position == current_player_x_position and y_position == current_player_y_position:
token = "T"
else:
token = "_"
return token
def draw_board():
y_counter = 1
x_counter = 1
counter_heading = 1
print("\n")
print(" " * 4),
while counter_heading < 11:
print(str(counter_heading).zfill(2) + " "),
counter_heading = counter_heading + 1
print("\n")
while y_counter < 11:
print(str(y_counter).zfill(2) + " |"),
while x_counter < 11:
print("_" + update_board(x_counter, y_counter) + "_ |"),
x_counter = x_counter + 1
print("\n")
y_counter = y_counter + 1
x_counter = 1
def help():
print("\n")
print("Type 99 to quit. Type up, down, left, right to move around.")
print("-----------------------------------------------------------")
foo = raw_input("Press enter to continue...")
return
def main():
global current_player_y_position
global current_player_x_position
while True:
draw_board()
choice = raw_input("What do you want to do (type help for help) > ")
if choice == "99":
break
elif choice == "help":
help()
elif choice == "up":
move('up',current_player_y_position,current_player_x_position)
elif choice == "right":
move('right',current_player_y_position,current_player_x_position)
elif choice == "left":
move('left',current_player_y_position,current_player_x_position)
elif choice == "down":
move('down',current_player_y_position,current_player_x_position)
return
main()
Take This Farther[edit]
This simple function is very useful, and it is likely that you have used a function like this to add bold face text to a google doc.
- Please create a function that creates a link.
<a href="http://reddit.com/r/python">Click here</a>
. You will need the link and text to display to the user. - Add some logic which rejects invalid HTML tags
How you will be assessed[edit]
Every problem set is a formative assignment. Please click here to see how you will be graded
References[edit]
- Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
One Possible Solution[edit]
Click the expand link to see one possible solution, but NOT before you have tried and failed!
#
# this function returns a HTML opening and closing pair of tag
#
def make_tags(tag, word):
print("<" + tag + ">" + word + "</" + tag + ">")
return
# example input:
# make_tags('i','Hello world')
# example output:
# <i>Hello world</i>