1

I'm writing a Connect Four script. The script takes a list of moves as input ("A_Red, "B_Yellow", "G_Red", etc), which I'm sorting into a matrix. Each nested list represents a column of the board with "A" being the first nested list (or column), and "G" being the last nested list (or column). Moves will then get sorted like so:

A board with some moves will resemble something like:

[ ["Red, "Yellow"], ["Red"], ["Yellow", "Yellow"], [], [], [], [] ]

Once all of the moves are sorted into their respective nested list, I fill each nested list with empty strings ("") to prevent any 'list index out of range errors' with for loops used to perform 4-in-a-row checks.

How can I check if the 4th element (index[3]) of each nested list contains either "Red" or "Yellow" WITHOUT a for loop?

If this condition is True, the diagonal checks will then be performed, and I only need them performed once.

board = [["Red", "Yellow", "Red", "Yellow"], [], [], ["Red", "Yellow", "Red", "Yellow"], [], [], []]
for i in range (0, 7):
   if board[i][3]:
       # Perform Diagonal Check

The issue with the above code is that I only need the diagonal check to be performed one time when any "Red" or "Yellow" piece is identified in the fourth position in any nested list. With the above for loop, the check will be performed 7 times for each item in the fourth position.

Is it possible to check a range of list positions without going:

if board[0][3] or board[1][3] or board[2][3]... == "Yellow" or "Red": #etc...
10
  • I'm not sure why beginning Python programmers often develop an obsession with avoiding for loops? Is there some online curriculum telling you that for is evil? It's true that there may often be ways of doing something that are more efficient than writing a for loop, but sometimes such a loop is just what's needed. Here, if you're using basic lists, there are better solutions than what you're doing, but for is likely to show up. In most cases where people will tell you for is bad, the alternative tends to be to use something optimised hidden away in a library like numpy. Commented Jan 12, 2023 at 22:47
  • Commenting on the actual question: you're not use arrays but lists here, so your question should probably be updated to reflect that, or you should share more of your code that shows how you use actual arrays. If you are in fact using lists, there are nicer and more efficient ways to write a check like the one you're describing, but they will include for - however, it would make more sense to look at the actual problem you're trying to solve, which is deciding whether 4 have been connected yet? Commented Jan 12, 2023 at 22:50
  • I honestly have no issue with for loops, and I actually love using them - probably one of my favourite things if I'm being honest. The issue with using a for loop is I don't know how to use it in such a manner that will only perform the diagonal check once. For instance, if board[0][3] is "Yellow" and board[1][3] is "Red", then the diagonal check will be performed twice for each piece present in the 4th position of each column. Unless there's another way to do it. Commented Jan 12, 2023 at 23:11
  • I've already programmed the diagonal checks, which work. I'm trying to make it more efficient. Two of the ways I'm doing that are: 1) If less than 10 moves have been made, skip diagonal checks, and 2) if there are no pieces in the 4th row, skip diagonal checks. These are conditions necessary for a diagonal 4-in-a-row win to be possible. I'm trying to check that there are no pieces in the 4th row before performing a diagonal check. And I'm happy to use a for loop if there's a way to stop it from performing diagonal checks for each piece present in the 4th row. Commented Jan 12, 2023 at 23:11
  • If efficiency is the most important aspect, you could simply only perform checks that involve the last played piece - after all, the only 4s will involve that piece, otherwise the game would have ended sooner. If you have a situation where you have to start with a board in an ongoing game, and you cannot get data about what column was played last, then you have to wonder if performing a lot of logic to avoid doing more checks is any more efficient than just performing a really efficient check of the entire board. Commented Jan 12, 2023 at 23:18

1 Answer 1

0

To check the 4th element in each sub-list, use the following code:

mylist = [[1, 2, 3, 0, 4], [5, 6, 7, 0, 8], [9, 1, 2, 0, 3]]

fourth_element_check = [x[3] for x in mylist]

if 0 in fourth_element_check:
    print ("0 is the fourth element of one of the nested lists")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.