Moving around a small grid: Difference between revisions

From Computer Science Wiki
No edit summary
Line 238: Line 238:
             move('up')
             move('up')
         elif choice == "right":
         elif choice == "right":
             move('right',current_player_y_position,current_player_x_position)
            # not yet implemented!
             move('right')
         elif choice == "left":
         elif choice == "left":
             move('left',current_player_y_position,current_player_x_position)  
            # not yet implemented!
             move('left')
         elif choice == "down":
         elif choice == "down":
             move('down',current_player_y_position,current_player_x_position)                      
            # not yet implemented!
             move('down')                    
     return
     return



Revision as of 08:20, 21 March 2016

This a problem set for you to work through [1]

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 functions, variables and computational thinking.

You have a simple 10 x 10 grid, with your character at the center (5,5). You can move up, down, left and right. Add:

  • terrain
  • a bad guy
  • combat
  • basic AI
  • basic statistics for the bad guy (or you) like Hit Points, Armour, Power, etc...

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]

You want to go farther?

  • add color to this.
  • add many types of different entities (google nethack)
  • move the board to 50 x 50
  • add an option to SAVE / RESTORE a game

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]

a slightly better approach to this...[edit]

Click to see a much better (but imperfect) example

import random

grid = []


# let's make our map.We assume a 25 x 25 map

for i in range(0,625):
    grid.append(random.randrange(1,4))

# now we place the player 

grid.insert(317,5)

# and let's keep track of where the player is. 

current_player_location = 317

#
# remember we have a 25 x 25 grid, which we are representing in a simple list. 
# this means row 1 is 0 to 24, row 2 is 25 to 49, row 3 is 50 to 74, etc...
# You should remember this when you work on the move code. 
#    

def draw_board():
    row = 0
    for i in range(0,625):
        if grid[i] == 1:
            print("..."),
        elif grid[i] == 2:
            print("..."),
        elif grid[i] == 3:
            print(".*."),
        elif grid[i] == 4:
            print(".^."),
        elif grid[i] == 5:
            print("[T]"),
        else:
            print("ERR"),
        row = row + 1
        if row == 25:
            print ("\n")
            row = 0 
    return

def move(where):
    global current_player_location
    if where == "up":
        #
        # let's make sure we are not at the top row, which is 0 to 24
        #
        if grid.index(5) <=25:
            print("You can't go up anymore.")
        else:
            print("The player is at position " +  str(grid.index(5)))
            current_player_position = grid.index(5)
            grid.insert(current_player_position - 25, grid.pop(current_player_position)) 
            print("The new player is at position " +  str(grid.index(5)))      
    return
    


def main():
    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')
        elif choice == "right":
             # not yet implemented!
             move('right')
        elif choice == "left":
             # not yet implemented!
             move('left')
        elif choice == "down":
            # not yet implemented!
             move('down')                     
    return

main()