在网格中移动cooridnates

时间:2017-04-17 15:58:07

标签: python grid

这些是我的全局变量

function phoneNumber(tel) {
var toString = String(tel),
    phoneNumber = toString.replace(/[^0-9]/g, ""),
    countArrayStr = phoneNumber.split(""),
    numberVar = countArrayStr.length,
    closeStr = countArrayStr.join("");
if (numberVar == 10) {
    var phone = closeStr.replace(/(\d{3})(\d{3})(\d{4})/, "$1.$2.$3"); // Change number symbols here for numbers 10 digits in length. Just change the periods to what ever is needed.
} else if (numberVar > 10) {
    var howMany = closeStr.length,
        subtract = (10 - howMany),
        phoneBeginning = closeStr.slice(0, subtract),
        phoneExtention = closeStr.slice(subtract),
        disX = "x", // Change the extension symbol here
        phoneBeginningReplace = phoneBeginning.replace(/(\d{3})(\d{3})(\d{4})/, "$1.$2.$3"), // Change number symbols here for numbers greater than 10 digits in length. Just change the periods and to what ever is needed. 
        array = [phoneBeginningReplace, disX, phoneExtention],
        afterarray = array.splice(1, 0, " "),
        phone = array.join("");

} else {
    var phone = "invalid number US number";
}
return phone;
}

phoneNumber("1234567891"); // Your phone number here

以下是功能:

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
##########################

我知道如果这件作品背后没有任何东西我只能移动,我该如何移动这些碎片。这是一场双人比赛。如果你想查看规则,游戏有三个名字(Akiba,Kuba,traboulet)。

最终结果应该是这样的:

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)

之后看起来应该是这样的。

     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 个答案:

答案 0 :(得分: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)

这给出了你展示的结果。

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