网格中的移动坐标

问题描述 投票:0回答:1

这些是我的全局变量

number_black_balls= 8
black_balls=[[0,0], [1,0], [1,1], [0,1], [6,6], [6,5], [5,5], [5,6]]

number_white_balls= 8
white_balls=[[6,0], [6,1], [5,1], [5,0], [0,6], [0,5], [1,5], [1,6]]

number_red_balls= 13
red_balls=[[1,3], [2,2], [2,3], [2,4], [3,1], [3,2], [3,3], [3,4], [3,5], [4,2], [4,3], [4,4], [5,3]]

## Movements: ##
column= ["   ", "1", "2", "3", "4", "5", "6", "7"]
dict_movement= {"up":[-1,0],"down":[1,0],"left":[0,-1],"right":[0,1]}  ##direction in which I'm going
##########################

这是功能:

def grid():
    res=[0]*7
    for i in range(7):
        res[i]= ["*"]*7            
    return res
    print (res[i]," ",end="\n")


def show_grid(g):
    print()
    for i in column:
        print(i, end="  ")
    print()
    print("   -----------------------  ")
    for i, line in enumerate(g, 1):
        print (i,"| ", "  ".join(line), " |",i)
    print("   -----------------------  ")

    for i in column:
        print(i, end="  ")
    print("\n")

def balls_location(g):
    for r in red_balls:
        g[r[0]][r[1]]="R"
    for b in white_balls:
        g[b[0]][b[1]]="B"
    for n in black_balls:
        g[n[0]][n[1]]="N"
    return g

def initGame():          
    g=grid()
    g=balls_location(g)
    return g

g=initgame()
show_grid(g)

我如何移动棋子,知道只有在棋子后面什么也没有的时候才能移动。这是一个两人游戏。如果您要检查规则,则游戏有三个名称(秋叶,库巴,traboulet)。

最终结果应该是这样的:

     1  2  3  4  5  6  7  
   -----------------------  
1 |  N  N  *  *  *  B  B  | 1
2 |  N  N  *  R  *  B  B  | 2
3 |  *  *  R  R  R  *  *  | 3
4 |  *  R  R  R  R  R  *  | 4
5 |  *  *  R  R  R  *  *  | 5
6 |  B  B  *  R  *  N  N  | 6
7 |  B  B  *  *  *  N  N  | 7
   -----------------------  
     1  2  3  4  5  6  7 

### the command
>>> player 1 > 1(column) 1(row) down(direction)

并且之后应该看起来像这样。

     1  2  3  4  5  6  7  
   -----------------------  
1 |  *  N  *  *  *  B  B  | 1
2 |  N  N  *  R  *  B  B  | 2
3 |  N  *  R  R  R  *  *  | 3
4 |  *  R  R  R  R  R  *  | 4
5 |  *  *  R  R  R  *  *  | 5
6 |  B  B  *  R  *  N  N  | 6
7 |  B  B  *  *  *  N  N  | 7
   -----------------------  
     1  2  3  4  5  6  7 

>>> player 2>
python grid
1个回答
1
投票

[这是一种非常“ hacky”且凌乱的方式,这只是我“工作”的一个示例:

def players_move(r, c, dir):
    if dir == 'down':
        if g[r+1][c] == '*':
            for b in black_balls:
                if b[1] == 0:
                    black_balls[b[0]][0] += 1
        g[r-1][c-1] = '*'

g=initGame()
show_grid(g)

players_move(1, 1, "down")
balls_location(g)
show_grid(g)

这给出了您显示的结果。

并且由于您对代码和所涉及的逻辑有了更好的了解,因此您可以对此进行很多改进。

© www.soinside.com 2019 - 2024. All rights reserved.