0

I want to make a tic tac toe game user enter input one line string of all columns and rows of Xs and Os like this 'O_OOXOOXX' i turn them into nested list like this [['O', '_', 'O'], ['O', 'X', 'O'], ['O', 'X', 'X']]

My question is how can replace all the if elif below? because it seems a lot.

for i in range(3):
   if nested_list[i][0] == nested_list[i][1] == nested_list[i][2] == 'O':
      print('O wins')
   elif nested_list[0][i] == nested_list[1][i] == nested_list[2][i] == 'O':
      print('O wins')
   elif nested_list[0][0] == nested_list[1][1] == nested_list[2][2] == 'O':
      print('O wins')
   elif nested_list[0][2] == nested_list[1][1] == nested_list[2][0] == 'O':
      print('O wins')

   elif nested_list[i][0] == nested_list[i][1] == nested_list[i][2] == 'X':
      print('X wins')
   elif nested_list[0][i] == nested_list[1][i] == nested_list[2][i] == 'X':
      print('X wins')
   elif nested_list[0][0] == nested_list[1][1] == nested_list[2][2] == 'X':
      print('X wins')
   elif nested_list[0][2] == nested_list[1][1] == nested_list[2][0] == 'X':
      print('X wins')
8
  • 1
    You could use or between conditions that have the same effect. Commented Jan 13, 2022 at 10:41
  • 1
    You have to come up with a different algorithm, it's not a specific quiestion, "how to make less ifs" Commented Jan 13, 2022 at 10:43
  • 2
    If your game works and you just need some help in improving the code, you could post it to Code review Stack Exchange Commented Jan 13, 2022 at 10:44
  • 2
    Look at the patterns between all these different checks, and how each one is different, and how each one is the same. How do they vary? What are the variables in each case? Can you express those variables in an automated fashion? Commented Jan 13, 2022 at 10:48
  • 1
    At least you can do this with 4 ifs instead of 8. As you can see, patterns for X and O are same Commented Jan 13, 2022 at 10:49

2 Answers 2

1

Probabily there's a nicer solution, but maybe something like this?

for i in range(3):
    a = set(nested_list[i,:])
    b = set(nested_list[:,i])
    if(len(a) == 1 && nested_list[i,0] != '_')
        print(nested_list[i,0], " wins")
    elif(len(b) == 1 && nested_list[0,i] != '_')
        print(nested_list[0,i], " wins")
if (((nested_list[0][0] == nested_list[1][1] == nested_list[2][2]) || nested_list[2][0] == nested_list[1][1] == nested_list[0][2])) && nested_list[1][1] != '_'):
    print(nested_list[1][1], " wins")
Sign up to request clarification or add additional context in comments.

2 Comments

a[0] does not work when a is a set.
@kaya3 You're right, my bad. Fixed it
0

I figured another solution:

arr = ['X', 'X', 'O', 'O', 'O', 'X', 'X', 'O', 'X']

I determine a nested list of possible wins

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

for i in range(8):
if(arr[matches[i][0]] == 'X' and
   arr[matches[i][1]] == 'X' and
   arr[matches[i][2]] == 'X'):
     print(X wins)
else:
  print(O wins)

and now i'm trying to figure out the other possibilities for example if the game not finished if the there is 3 Xs and Os in row then the game is impossible...

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.