SHARE
TWEET

3x3 TicTacToe evaluation

danielfajt Feb 29th, 2020 (edited) 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Simple TicTacToe evaluation
  2. # - Player 0 wins if vertical, horizontal or diagonal sum == 0
  3. # - Player 1 wins if == 3
  4. # - Board should be initialized with different values than 1 and 0 (eg. None)
  5. # - Just simple concept
  6.  
  7. import numpy as np
  8.  
  9. n1 = np.array([0,1,1])
  10. n2 = np.array([0,1,0])
  11. n3 = np.array([0,0,1])
  12. n_diag_descending = np.diagonal([n1,n2,n3])
  13. n_diag_ascending = np.diagonal(np.fliplr([n1,n2,n3]))
  14.  
  15. # Vertical sum
  16. vs = np.sum([n1,n2,n3], axis=0)
  17. # Horizontal sum
  18. hs = np.sum([n1,n2,n3], axis=1)
  19. # Diagonal sum descending
  20. dsd = np.sum([n_diag_descending])
  21. # Diagonal sum ascending
  22. dsa = np.sum([n_diag_ascending])
  23.  
  24. def winner(*args):
  25.     vals = np.concatenate((args))
  26.     print(vals)
  27.  
  28.     if 3 in vals:
  29.         print('Player 1 win')
  30.     elif 0 in vals:
  31.         print('Player 0 win')
  32.     else:
  33.         print('No one won this round')
  34.  
  35. winner(hs, vs, [dsd], [dsa])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top