简体   繁体   English

在python中为棋盘游戏创建2D网格

[英]Creating a 2D grid for a board game in python

I'm taking an introductory course to computer science using Python and we were given an exercise to make a board game(dogems). 我正在学习使用Python进行计算机科学的入门课程,并且我们进行了制作棋盘游戏的练习。 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. 假定该程序采用给定的参数,并使用函数make_board(size)构造具有相等行和列的木板,其底部为数字,侧面为字母。 A function show_board(board) then displays it. 然后,函数show_board(board)显示它。 eg Board size:4 would give: 例如,木板尺寸:4将给出:

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

whereas, a board size:5 would give: 而一块木板尺寸:5会给出:

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. 也许您认为存储列表列表是执行此操作的最佳方法,所以make_board(size)返回类似于字符列表列表的内容,而show_board(board)在for循环中使用for循环将其全部打印出来。出来。

Don't expect the finished solution from StackOverflow, try doing some of this stuff and ask a question when you really get stuck! 不要指望StackOverflow提供完整的解决方案,请尝试做一些这样的事情,并在真正遇到问题时问一个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM