Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. # A Tic-Tac-Toe Board
  2. theBoard={'t-l':'t-l','t-m':'t-m','t-r':'t-r',
  3. 'm-l':'m-l','m-m':'m-m','m-r':'m-r',
  4. 'l-l':'l-l','l-m':'l-m','l-r':'l-r'}
  5. def printBoard(board):
  6. """This function prints out the board"""
  7. print(board['t-l']+' | '+board['t-m']+' | '+board['t-r'])
  8. print(' -- + -- + -- ')
  9. print(board['m-l']+' | '+board['m-m']+' | '+board['m-r'])
  10. print(' -- + -- + -- ')
  11. print(board['l-l']+' | '+board['l-m']+' | '+board['l-r'])
  12. def winner(theBoard):
  13. """This function checks the winner"""
  14. for x in ['t','m','l']:
  15. if theBoard[x+'-l']==theBoard[x+'-m'] and theBoard[x+'-m']== theBoard[x+'-r']:
  16. print('The winner is '+theBoard[x+'-l'])
  17. return 1
  18. for y in ['l','m','r']:
  19. if theBoard['t-'+y]==theBoard['m-'+y] and theBoard['m-'+y]== theBoard['l-'+y]:
  20. print('The winner is '+theBoard['l-'+y])
  21. return 1
  22. if theBoard['t-l']==theBoard['m-m'] and theBoard['m-m']== theBoard['l-r']:
  23. print('The winner is '+theBoard['m-m'])
  24. return 1
  25. if theBoard['t-r']==theBoard['m-m'] and theBoard['m-m']== theBoard['l-l']:
  26. print('The winner is '+theBoard['m-m'])
  27. return 1
  28. print("***RULES***\nTo make a move use t,m,l as Top,Mid,Low and l,m,r as Left,Mid,Right in this format" + " 'm-m' for Mid-Mid or 't-l' for Top-Left.")
  29. turn=input('\nWho wants to play first? X or O? : ')
  30. turn=" "+turn+" "
  31. while True:
  32. printBoard(theBoard)
  33. print('Turn for ' + turn +'. Move on which space?')
  34. move=input('Please enter your desired move: ')
  35. theBoard[move]=turn
  36. if turn==' X ':
  37. turn=' O '
  38. else:
  39. turn=' X '
  40. flag=winner(theBoard)
  41. if flag==1:
  42. winner(theBoard)
  43. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement