0

I'm trying to create a TicTacToe game on Python, and I'm using lists in 3 variables for the moves:

board0 = ["-", "-", "-"]
board1 = ["-", "-", "-"]
board2 = ["-", "-", "-"]

So far I've managed to make an AI which choses random moves, but I'm struggling on finding a compact and efficient way of checking for win conditions. I've got a huge bulk of code which does it, but I want to know if there's a way of streamlining it to get the same job done.

if board0[0] == "X":
    if board0[1] == "X":
        if board0[2] == "X":
            print("PLAYER wins!")
            print(sys.exit())
if board0[0] == "X":
    if board1[1] == "X":
        if board2[2] == "X":
            print("PLAYER wins!")
            print(sys.exit())
if board0[0] == "X":
    if board1[0] == "X":
        if board2[0] == "X":
            print("PLAYER wins!")
            print(sys.exit())
if board0[1] == "X":
    if board1[1] == "X":
        if board2[1] == "X":
           print("PLAYER wins!")
           print(sys.exit())
if board0[2] == "X":
    if board1[1] == "X":
        if board2[0] == "X":
           print("PLAYER wins!")
           print(sys.exit())
if board0[2] == "X":
    if board1[2] == "X":
        if board2[2] == "X":
           print("PLAYER wins!")
           print(sys.exit())
if board1[0] == "X":
    if board1[1] == "X":
        if board1[2] == "X":
           print("PLAYER wins!")
           print(sys.exit())
if board2[0] == "X":
    if board2[1] == "X":
        if board2[2] == "X":
           print("PLAYER wins!")
           print(sys.exit())               
  • 2
    There are patterns in some of your code, no? Couldn't you write loops to take advantage of those patterns? – Charles Duffy Oct 01 '22 at 18:06
  • 1
    ...that said, think about having just one `board` list, where `board[0]` is what you currently call board1, `board[1]` is what you call board2, and `board[2]` is what you call board3. That would let you refer to `board[0][0]`, `board[0][1]`, etc. – Charles Duffy Oct 01 '22 at 18:07
  • you might also consider keeping the board as a single array of 9 entries! – ti7 Oct 01 '22 at 18:11

2 Answers2

0

instead of doing if ... if ... if ... just do if ... and ... and ... you can make a counter for each of the 8 win cons and every time something is placed just increment the counters and if a counter hits 3 then check and you have the winner (this is my approach)

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
nickel
  • 1
  • 5
0

Is this solution suitable for your case?

import sys
board = [
            ["X", "-", "-"],
            ["-", "X", "-"],
            ["-", "-", "X"]
        ]
#report victory
def player_wins():
    print("PLAYER wins!")
    print(sys.exit())
def check_board(b):
    x = ["X", "X", "X"]
    #check diagonal lines
    if [b[0][0], b[1][1], b[2][2]] == x or [b[0][2], b[1][1], b[2][0]] == x:
            player_wins()
    #check rows
    for row in b:
        if row == x:
            player_wins()
    #check colums
    columed_b = zip(*b)
    for col in columed_b:
        if list(col) == x:
            player_wins()
    
check_board(board)
Avo Asatryan
  • 404
  • 8
  • 21