在 python 中为棋盘游戏创建 2D 网格
我正在学习使用 Python 的计算机科学入门课程,我们得到了制作棋盘游戏(dogems)的练习。我在构建董事会时遇到了麻烦。该程序假定采用给定的参数,并使用函数 make_board(size) 构造一个具有相等行和列的板,底部为数字,侧面为字母。然后函数 show_board(board) 显示它。 例如,板尺寸:4 会给出:
a . . .
b . . .
c . . .
. 1 2 3
而板尺寸:5 会给出:
a . . . .
b . . . .
c . . . .
d . . . .
. 1 2 3 4
我的问题基本上是,我将如何编写这些函数来构建这种性质的板?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从一些非常简单的事情开始,例如仅打印最后一行:
这非常简单
现在,如果我想要一个可变大小的板怎么办?
让我们尝试一个循环,
请注意,您需要一个可变长度。
好的,那么列呢?这些是字母,我们如何打印可变长度的字母列表?
当您一一解决这些小问题时,您将开始意识到哪些变量变得明显。也许您认为存储列表列表是最好的方法,因此
make_board(size)
返回类似字符列表列表的内容,并且show_board(board)
code> 在 for 循环中使用 for 循环将其全部打印出来。不要指望 StackOverflow 会给出最终的解决方案,尝试做一些这样的事情,并在你真正遇到困难时提出问题!
Try starting with something really simple, like printing out just the bottom row:
That's pretty easy
Now what if I want to have a variable sized board?
Let's try a loop
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, andshow_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!