Guest User

Untitled

a guest
Apr 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. from IPython.display import clear_output
  2.  
  3. def display_board(board):
  4. clear_output() # Remember, this only works in jupyter!
  5.  
  6. print(' | |')
  7. print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
  8. print(' | |')
  9. print('-----------')
  10. print(' | |')
  11. print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
  12. print(' | |')
  13. print('-----------')
  14. print(' | |')
  15. print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
  16. print(' | |')
  17.  
  18. def player_input():
  19. marker = ''
  20.  
  21. while not (marker == 'X' or marker == 'O'):
  22. marker = input('Player 1: Do you want to be X or O? ').upper()
  23.  
  24. if marker == 'X':
  25. return ('X', 'O')
  26. else:
  27. return ('O', 'X')
  28.  
  29.  
  30. def place_marker(board, marker, position):
  31. board[position] = marker
  32.  
  33.  
  34. def win_check(board,mark):
  35.  
  36. return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top
  37. (board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
  38. (board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom
  39. (board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle
  40. (board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle
  41. (board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side
  42. (board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal
  43. (board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal
  44.  
  45.  
  46. import random
  47.  
  48. def choose_first():
  49. if random.randint(0, 1) == 0:
  50. return 'Player 2'
  51. else:
  52. return 'Player 1'
  53.  
  54.  
  55. def space_check(board, position):
  56.  
  57. return board[position] == ' '
  58.  
  59.  
  60.  
  61. def full_board_check(board):
  62. for i in range(1,10):
  63. if space_check(board, i):
  64. return False
  65. return True
  66.  
  67.  
  68. def player_choice(board):
  69. position = 0
  70.  
  71. while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
  72. position = int(input('Choose your next position: (1-9) '))
  73.  
  74. return position
  75.  
  76.  
  77.  
  78.  
  79. def replay():
  80.  
  81. return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')
  82.  
  83.  
  84. print('Welcome to Tic Tac Toe!')
  85.  
  86. while True:
  87. # Reset the board
  88. theBoard = [' '] * 10
  89. player1_marker, player2_marker = player_input()
  90. turn = choose_first()
  91. print(turn + ' will go first.')
  92.  
  93. play_game = input('Are you ready to play? Enter Yes or No.')
  94.  
  95. if play_game.lower()[0] == 'y':
  96. game_on = True
  97. else:
  98. game_on = False
  99.  
  100. while game_on:
  101. if turn == 'Player 1':
  102. # Player1's turn.
  103.  
  104. display_board(theBoard)
  105. position = player_choice(theBoard)
  106. place_marker(theBoard, player1_marker, position)
  107.  
  108. if win_check(theBoard, player1_marker):
  109. display_board(theBoard)
  110. print('Congratulations! You have won the game!')
  111. game_on = False
  112. else:
  113. if full_board_check(theBoard):
  114. display_board(theBoard)
  115. print('The game is a draw!')
  116. break
  117. else:
  118. turn = 'Player 2'
  119.  
  120. else:
  121. # Player2's turn.
  122.  
  123. display_board(theBoard)
  124. position = player_choice(theBoard)
  125. place_marker(theBoard, player2_marker, position)
  126.  
  127. if win_check(theBoard, player2_marker):
  128. display_board(theBoard)
  129. print('Player 2 has won!')
  130. game_on = False
  131. else:
  132. if full_board_check(theBoard):
  133. display_board(theBoard)
  134. print('The game is a draw!')
  135. break
  136. else:
  137. turn = 'Player 1'
  138.  
  139. if not replay():
  140. break
Add Comment
Please, Sign In to add comment