如何确定某个元素是否已经在列表中的某个位置?

时间:2018-12-11 15:43:46

标签: python arrays python-3.x

我制作了一个由25个0组成的5x5矩阵游戏。玩家1可以将0更改为1,而玩家2可以将0更改为2。

我只是想弄清楚如何验证棋盘上的位置,因此如果玩家已经放置了他们的号码,他们需要输入一个未占用空间的其他号码。

例如:

Player 1 | Please enter a number between 1-25: 3
0 0 3 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Player 2 | Please enter a number between 1-25: 3
This position is already taken! Please enter a different position:

此外,我将如何对游戏进行编程以确定棋盘上是否再没有0了?这样便会平局。

代码:

def player1_turn():
    player1_option = int(input("Player 1 | Please enter a number between 1-25: "))
    if player1_option <= 0:
        print("You can only enter a number between 1 and 25")
        player1_turn()
    elif player1_option > 25:
        print("You can only enter a number between 1 and 25")
        player1_turn()

    player1 = (player1_option - 1) #Counter-acts the elements from starting at 0
    grid[int(player1) // 5][int(player1) % 5] = 1 #places a 1 in inputted position
    for row in grid: #for each row in the grid
        print(*row, sep=" ")
    print()

    for y in range(0,4):
        for x in range(0,4):
            if grid[y][x] == grid[y][x+1] == grid[y+1][x] == grid[y+1][x+1] >0: #if there is a 2x2 of same number in grid:
                print("Player",grid[y][x],"has won!")
                exit()

3 个答案:

答案 0 :(得分:1)

您只需要在更新位置之前检查要更新的位置是否为

def player1_turn():
    player1_option = int(input("Player 1 | Please enter a number between 1-25: "))
    if player1_option <= 0:
        print("You can only enter a number between 1 and 25")
        player1_turn()
    elif player1_option > 25:
        print("You can only enter a number between 1 and 25")
        player1_turn()

    player1 = (player1_option - 1) #Counter-acts the elements from starting at 0

    #You must check if the position is a 0

    if (grid[int(player1) // 5][int(player1) % 5] == 0):
        grid[int(player1) // 5][int(player1) % 5] = 1 #places a 1 in inputted position

    else:
        #Do something else here instead of updating it 


    for row in grid: #for each row in the grid
        print(*row, sep=" ")
    print()

对于已填充的木板,您可以使用常规方法(两次循环)或使用列表理解更直观的方法进行检查

def board_filled():
    for i in grid:
        for j in i:
            if j == 0:
                return False
    return True

def board_filled():
    return (sum([0 in i for i in grid]) == 0)

答案 1 :(得分:0)

如果您为此使用2d数组,则可以检查一个地方是否充满了

filled_array = np.array([[0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0]])

selected_pos = 23
# Get row
row = np.floor(selected_pos)
# Get pos in row
pos = selected_pos % 5 - 1
# Check if pos is not 0
if(filled_array[row][pos] != 0):
    raise ["Place is filled"]

编辑: 用它来检查是否还没有0

if(np.count_nonzero(filled_array) == 25):
     raise ["No 0's left"]

忘记提及:

import numpy as np

使其正常工作

答案 2 :(得分:0)

您很有可能将矩阵表示为列表列表,在您的示例中,您可以使用以下方法创建矩阵:

n = 5
matrix = [[0 for j in range(n)] for i in range(n)]

如果要打印,可以执行:

for i in matrix:
    print i

如果不想使用[]和逗号,请使用print " ".join(map(str, i))

打印

最简单的方法(远离最佳方法)是保持已经进行了多少动作的计数器,您知道在每一回合中都必须将0更改为其他数字,因此整个游戏将有25个动作,您不必检查是否有0,而只需检查已执行了多少个动作。

for i in range(n*n):
    selected = input("Player %i |  Please enter a number between 1-25: " % ((i%2)+1))
    while matrix[selected//n][selected%n] != 0:
        selected = input("This position is already taken! Please enter a different position: ")
    matrix[selected//n][selected%n]=(i%2)+2)