Making a small grid: Difference between revisions

From Computer Science Wiki
 
Line 102: Line 102:


# -- and you can try this one also:  
# -- and you can try this one also:  
 
import random
grid = []


for i in range(0, 625):
for i in range(0, 625):

Latest revision as of 19:56, 31 August 2021

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 25 x 25 grid. Replace the numbers with ascii characters. Use different ascii characters to represent different types of terrain, such as water, sand, lakes, people, villages, roads, etc.

Some Code to Get You Started[edit]

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))

def draw_board():
    row = 0
    for i in range(0,625):
        print(str(i).zfill(3),end=' ')
        row = row + 1
        if row == 25:
            print ("\n")
            row = 0 
    return

draw_board()

Take This Farther[edit]

You want to go farther?

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

How you will be assessed[edit]

Your solution will be graded using the following axis:


Scope

  • To what extent does your code implement the features required by our specification?
  • To what extent is there evidence of effort?

Correctness

  • To what extent did your code meet specifications?
  • To what extent did your code meet unit tests?
  • To what extent is your code free of bugs?

Design

  • To what extent is your code written well (i.e. clearly, efficiently, elegantly, and/or logically)?
  • To what extent is your code eliminating repetition?
  • To what extent is your code using functions appropriately?

Style

  • To what extent is your code readable?
  • To what extent is your code commented?
  • To what extent are your variables well named?
  • To what extent do you adhere to style guide?


A possible solution[edit]

Click the expand link to see one possible solution, but NOT before you have tried and failed!

import random
grid = []

for i in range(0,625):
    grid.append([" W ", " T ", " L "][random.randint(0, 2)])

def draw_board():
    row = 0
    for i in range(0,625):
        print(grid[i].zfill(1),end=' ')
        row = row + 1
        if row == 25:
            print ("\n")
            row = 0 
    return

draw_board()




# -- and you can try this one also: 
import random
grid = []

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


def draw_board():
    row = 0
    for i in range(0, 625):
        ElementRan = random.randint(1, 100)
        if 1 < ElementRan < 65:
            element = u"\U0001F332"
        if 66 <= ElementRan <= 94:
            element = u"\U0001F4A7"
        if 95 <= ElementRan <= 100:
            element = u"\U0001F43A"
        print(element, end=' ')
        row = row + 1
        if row == 25:
            row = 0
    return


draw_board()

References[edit]