mardi 9 mars 2021

If statement that changes based on variable value

Hello users of the interweb,

I was in the middle of creating a Tic-Tac-Toe program implementing the minimax algorithm and was looking to refactor part of my code to work on any size board - not just 3x3.

The following is a part of my code that loops through the rows of the board looking for a winner:

    for i in range(board_size):
        if board[i][0]["text"] == board[i][1]["text"] == board[i][2]["text"] and board[i][0]["text"] != " ":
            if board[i][0]["text"] == opponent:
                return 10
            elif board [i][0]["text"] == player:
                return -10

I have similar methods of checking the diagonal/columns, but say I want to do this procedure on a 4x4 board, 5x5 board, etc. how would I go about implementing that?

Aucun commentaire:

Enregistrer un commentaire