为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕森王 2021-09-14 15:44:45
我制作了一个 5x5 矩阵游戏,由 25 个 0 组成。玩家 1 可以将任何 0 更改为 1,玩家 2 可以将任何 0 更改为 2。我只是在弄清楚如何验证板上的位置时遇到了麻烦,这样如果玩家已经放置了他们的号码,他们就需要输入一个不同的号码,而这个号码没有被占用。例如:Player 1 | Please enter a number between 1-25: 30 0 3 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0Player 2 | Please enter a number between 1-25: 3This 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()
查看完整描述

2 回答

?
交互式爱情

TA贡献1712条经验 获得超3个赞

您只需要在更新之前检查您正在更新的位置是否为 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


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


查看完整回答
反对 回复 2021-09-14
?
万千封印

TA贡献1891条经验 获得超3个赞

如果您为此使用二维数组,您可以检查一个地方是否充满,


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

让它工作


查看完整回答
反对 回复 2021-09-14
  • 2 回答
  • 0 关注
  • 231 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信