Skip to content

Commit

Permalink
Slight rework of check win, not final yet
Browse files Browse the repository at this point in the history
  • Loading branch information
JanEricNitschke committed Nov 10, 2024
1 parent 1268b1d commit 6ce8723
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions tictactoe_mojo/tictactoe/game.🔥
Original file line number Diff line number Diff line change
Expand Up @@ -121,53 +121,42 @@ fn _is_full[size: Int](board: BOARD[size*size]) -> Bool:
return all(board != OPEN)

fn _is_winner_param[size: Int, //, player: UInt8](board: BOARD[size*size]) -> Bool:

var reference: SIMD[DType.uint8, board.size]
@parameter
for row in range(size):
row_win = True
reference = SIMD[DType.uint8, board.size](0)
@parameter
for col in range(size):
if board[row * size + col] != player:
row_win = False
break
if row_win:
print("Row win")
reference[row * size + col] = player
if (board & reference).reduce_bit_count() == size:
return True

# Check columns
@parameter
for col in range(size):
col_win = True
reference = SIMD[DType.uint8, board.size](0)
@parameter
for row in range(size):
if board[row * size + col] != player:
col_win = False
break
if col_win:
print("Col win")
reference[row * size + col] = player
if (board & reference).reduce_bit_count() == size:
return True

# Check main diagonal
main_diag_win = True
reference = SIMD[DType.uint8, board.size](0)
@parameter
for i in range(size):
if board[i * size + i] != player:
main_diag_win = False
break
if main_diag_win:
print("Main diag win")
reference[i * size + i] = player
if (board & reference).reduce_bit_count() == size:
return True

# Check anti-diagonal
anti_diag_win = True
reference = SIMD[DType.uint8, board.size](0)
@parameter
for i in range(size):
if board[i * size + (size - 1 - i)] != player:
anti_diag_win = False
break
if anti_diag_win:
print("Anti diag win")
reference[i * size + (size - 1 - i)] = player
if (board & reference).reduce_bit_count() == size:
return True
print("No win")
return False


Expand Down

0 comments on commit 6ce8723

Please sign in to comment.