Advertisement
Guest User

Untitled

a guest
May 14th, 2017
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. # tic tac toe by Naman Pujari
  2. # using NumPy arrays for aesthetics!
  3. # let's begin...
  4.  
  5. """ UPDATE 1: 05/15/2017 ~~~~~~~~~~
  6.     NEED TO IMPLEMENT ERROR CHECKING FUNCTION
  7.     NEED TO ALLOW FOR TWO PLAYER COMPATIBILITY (EASY)
  8.         * THIS WILL ALSO AFFECT THE ERROR CHECK FUNCTION AND USER INTERFACE
  9.     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """
  10.  
  11. import numpy as np  
  12.  
  13. def user_interface():
  14.     valid_choice = False
  15.     while(valid_choice == False):
  16.         user_x = int(input('Enter x-coordinate of your turn'))
  17.         user_y = int(input('Enter y-coordinate of your turn'))
  18.         if((user_x >= 1 and user_x <= 3) and (user_y >= 1 and user_y <=3)):
  19.             valid_choice = True
  20.         else:
  21.             print('Invalid choice (OUT OF BOUNDS)')
  22.  
  23.     to_continue = error_check(user_x,user_y)
  24.     if(to_continue == 1):
  25.         print('Position is taken, try again...')
  26.     else:
  27.         # np.array elements are rows,columns; so y,x not x,y
  28.         board[user_y][user_x] = user_side
  29.         print(board)
  30.  
  31.     return win(board)
  32.  
  33. def error_check(x_query, y_query
  34.     ):
  35.     if(board[y_query][x_query] == ' '):
  36.         return 0
  37.     else:
  38.         return 1
  39.  
  40. def win(array):
  41.     win_case = 0
  42.     for i in [1,2,3]:
  43.         if((array[i,1] == 'o') and (array[i,2] == 'o') and (array[i,3] == 'o')):
  44.             win_case = 1
  45.             break
  46.     if(win_case == 1):
  47.         return True
  48.     else:
  49.         return False
  50.  
  51.  
  52. if __name__ == '__main__':
  53.     winner = False
  54.  
  55.     board = np.array([[' ','1','2','3'],
  56.                       ['1',' ',' ',' '],
  57.                       ['2',' ',' ',' '],
  58.                       ['3',' ',' ',' ']])  
  59.     print('Welcome to tic tac toe')
  60.  
  61.     user_side = ''
  62.     while(user_side != 'o' and user_side != 'x'):
  63.         user_side = input('Enter "o" or "x" to pick sides... ')
  64.         if(user_side != 'o' and user_side != 'x'):
  65.             print('ERROR (pick an appropriate side please)')
  66.             print()
  67.  
  68.     print('You have picked', user_side, 'as your side.')
  69.  
  70.     while(winner == False):
  71.         winner = user_interface()
  72.         print(winner)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement