Creating a 2D grid for a board game in python

眉间皱痕 提交于 2019-12-12 01:36:23

问题


I'm taking an introductory course to computer science using Python and we were given an exercise to make a board game(dogems). I'm having troubles constructing the board. The program is suppose to take a given argument and, using a function make_board(size), constructs a board of equal rows and columns with numbers along the bottom and letters along the side. A function show_board(board) then displays it. e.g. Board size:4 would give:

a . . .
b . . .
c . . .
. 1 2 3

whereas, a board size:5 would give:

a . . . .
b . . . .
c . . . .
d . . . .
. 1 2 3 4

My question is basically, how would I go about writing these functions to construct a board of this nature?


回答1:


Try starting with something really simple, like printing out just the bottom row:

. 1 2 3 4 5

That's pretty easy

print '.', '1', '2', '3', '4', '5'

Now what if I want to have a variable sized board?

Let's try a loop

for i in range(length+1):
    if i == 0:
        print '.'
    else:
        print i

Note that you need a variable length.

Ok what about the columns? These are letters, how can we print a variable length list of letters?

As you tackle these little problems one by one, you will start to realize what variables become apparent. Maybe you decide that storing a list of lists is the best way to do it, so make_board(size) returns something like a list of of lists of characters, and show_board(board) uses a for loop within a for loop to print it all out.

Don't expect the finished solution from StackOverflow, try doing some of this stuff and ask a question when you really get stuck!



来源:https://stackoverflow.com/questions/5999751/creating-a-2d-grid-for-a-board-game-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!