Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tic tac toe by Naman Pujari
- # using NumPy arrays for aesthetics!
- # let's begin...
- """ UPDATE 1: 05/15/2017 ~~~~~~~~~~
- NEED TO IMPLEMENT ERROR CHECKING FUNCTION
- NEED TO ALLOW FOR TWO PLAYER COMPATIBILITY (EASY)
- * THIS WILL ALSO AFFECT THE ERROR CHECK FUNCTION AND USER INTERFACE
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """
- import numpy as np
- def user_interface():
- valid_choice = False
- while(valid_choice == False):
- user_x = int(input('Enter x-coordinate of your turn'))
- user_y = int(input('Enter y-coordinate of your turn'))
- if((user_x >= 1 and user_x <= 3) and (user_y >= 1 and user_y <=3)):
- valid_choice = True
- else:
- print('Invalid choice (OUT OF BOUNDS)')
- to_continue = error_check(user_x,user_y)
- if(to_continue == 1):
- print('Position is taken, try again...')
- else:
- # np.array elements are rows,columns; so y,x not x,y
- board[user_y][user_x] = user_side
- print(board)
- return win(board)
- def error_check(x_query, y_query
- ):
- if(board[y_query][x_query] == ' '):
- return 0
- else:
- return 1
- def win(array):
- win_case = 0
- for i in [1,2,3]:
- if((array[i,1] == 'o') and (array[i,2] == 'o') and (array[i,3] == 'o')):
- win_case = 1
- break
- if(win_case == 1):
- return True
- else:
- return False
- if __name__ == '__main__':
- winner = False
- board = np.array([[' ','1','2','3'],
- ['1',' ',' ',' '],
- ['2',' ',' ',' '],
- ['3',' ',' ',' ']])
- print('Welcome to tic tac toe')
- user_side = ''
- while(user_side != 'o' and user_side != 'x'):
- user_side = input('Enter "o" or "x" to pick sides... ')
- if(user_side != 'o' and user_side != 'x'):
- print('ERROR (pick an appropriate side please)')
- print()
- print('You have picked', user_side, 'as your side.')
- while(winner == False):
- winner = user_interface()
- print(winner)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement