在Python中为棋盘游戏创建2D网格 [英] Creating a 2D grid for a board game in python
问题描述
我正在使用Python为计算机科学带来入门课程,并且我们给了一个练习来制作棋盘游戏(dogems)。我有麻烦构建董事会。该程序假定采用给定的参数,并使用函数make_board(size)构造一个具有相同行和列的电路板,其中底部有数字,侧面有字母。一个函数show_board(板)然后显示它。
例如董事会规模:4将给:
a。 。 。
b。 。 。
c。 。 。
。 1 2 3
然而,董事会规模:5会给:
a。 。 。 。
b。 。 。 。
c。 。 。 。
d。 。 。 。
。 1 2 3 4
我的问题基本上是,我会如何写这些函数来构建一个板这种性质?
尝试从一些非常简单的事情开始,比如只打印出最下面的一行:
。 1 2 3 4 5
这很简单
print'。','1','2','3','4','5'
现在如果我想要一个可变大小的板子?
让我们试一下循环
我在范围内(长度+ 1):
if i == 0:
print'。'
else:
print i
请注意,您需要一个可变长度。
好的列怎么样?这些是字母,我们如何才能打印一个可变长度的字母列表?
当你逐个处理这些小问题时,你会开始意识到变量变得明显。也许你认为存储列表的列表是最好的方式,所以 make_board(size)
返回类似于字符列表的列表,并且 show_board(board)
在for循环中使用for循环将其全部打印出来。
不要期望完成的解决方案从StackOverflow,尝试做一些这些东西,当你真的陷入困境时问一个问题!
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?
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!
这篇关于在Python中为棋盘游戏创建2D网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!