AcastaTeacup
AcastaTeacup

Reputation: 15

How to compare the nth element of different lists in Python?

I am making a tic-tac-toe game where an input decides the size of the board (to make it fun). There are two players, player 1 using X and player 2 using O to mark their spot on the board.

The code for a 3 x 3 board looks like this: (I want to replace the board making code for some less hard-coded stuff but I'm fine with that now, this is not the question, this is just for clarification)

grid = "-  -  -\n-  -  -\n-  -  -"                                           
print(grid)                                                                  
                                                                             
listed_grid = list(grid)                                                     
while "\n" in listed_grid:                                                   
    listed_grid.remove("\n")                                                 
while " " in listed_grid:                                                    
    listed_grid.remove(" ")                                                  
listed_grid = [listed_grid[n: n + 3] for n in range(0, len(listed_grid), 3)] 
print(listed_grid)  

                                                     

So basically I have x lists of the same length (x is decided by the input and the length as well). These lists represent the rows of the board. (I know in the code above it is not decided as an input)

I want the code to print a message saying "Congratulations! Player A (1 or 2) won!!!" when for each list, the nth element is the same for every list. Actually, in this case, there is a whole column that got occupied by the same mark type (X or O).

So the list "listed_grid" has 3 sublists for each row if it's a classic 3x3 game. How can I do so that for every nth element in the sublists in "listed_grid", if the marks are the same and not a '-' (hyphen) since the hyphen just shows the players that this spot is not taken, it prints a congratulations message to whoever won?

Thank you very much! Please go easy on me, I'm still pretty bad at Python.

Upvotes: 1

Views: 223

Answers (2)

Guy
Guy

Reputation: 50819

To check the columns you can use zip(), this will give you a list of tuples with the columns. You can insert the result to set() and check if there is only one element and that it's not -

for col in zip(*listed_grid):
    s = set(col)
    if len(s) == 1:
        x = s.pop()
        if x != '-':
            print(f'Congratulations! Player {x} won!!!')

Or if you use Python 3.8 or newer you can use Assignment Expressions :=

for col in zip(*listed_grid):
    s = set(col)
    if len(s) == 1 and (x := s.pop()) != '-':
        print(f'Congratulations! Player {x} won!!!')

Output:

grid = "-  Y  -\nX  Y  -\nX  Y  -"
# Congratulations! Player Y won!!!

grid = "X  Y  -\nX  Y  -\nX  -  -"
Congratulations! Player X won!!!

Upvotes: 1

Oxin
Oxin

Reputation: 265

They're may be a more refined answer to this question, but the first solution that comes to mind is to loop through each list in 'listed_grid' simultaneously:

winner = False

#if i = j and j = k (i=j=k) and one of them is != '-', winner can be announced
for i,j,k in zip(listed_grid[0], listed_grid[1], listed_grid[2]):
        if i == j and j == k and i != '-':
            winner = True
            break
            
        if winner == True:
            print('Winner')

This of course only covers the vertical winning patterns in Xs and Os, which I believe is all you asked for. To check for horizontal wins you just need to check that every element in one of the lists in 'listed_grid' is equal to each other, and for diagonal patterns you just need to check two conditions for a 3x3 game, that listed_grid[0][0] = listed_grid[1][1] = listed_grid[2][2] and listed_grid[2][2] = listed_grid[1][1] = listed_grid[0][0].

Upvotes: 0

Related Questions