user9763648
user9763648

Reputation:

How to determine if an element is already in a list at a certain position?

I've made a 5x5 matrix game, consisting of 25 0's. Player 1 can change any 0 to a 1 and player 2 can change any 0 to a 2.

I'm just having trouble figuring out how to validate the positions on the board so that if a player has already placed their number, they need to input a different number which space hasn't been taken.

For example:

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:

Also, how would I program the game to determine if there are no longer any 0's on the board? As it will then be a draw.

Code:

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

Upvotes: 1

Views: 285

Answers (3)

Abhishek Patel
Abhishek Patel

Reputation: 615

You simply need to check if the position you are updating is 0 before updating it

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

As for the board filled, you can check it using the common way (double for loop) or a more intuitive way using list comprehension

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)

Upvotes: 1

Germ&#225;n Ruelas
Germ&#225;n Ruelas

Reputation: 125

Most likely, you are representing your matrix as a list of list, in your example you can create it with:

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

if you want to print it, you can do:

for i in matrix:
    print i

if you don't want the [ ] and commas, print it with print " ".join(map(str, i))

the simpler way to do it (far away from the best) is maintain a counter of how many movements have been made, you know that in each turn you must change a 0 to other number, so the total game is going to have 25 movements, you don't have to check if there are 0s, you just have to check how many movements hove been made.

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)

Upvotes: 0

Sven
Sven

Reputation: 57

If you are using an 2d array for this you could check if a place is filled with,

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"]

Edit: Use this to check if there are no 0's left

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

forgot to mention:

import numpy as np

for it to work

Upvotes: 0

Related Questions