Advertisement
SolomonExcel

TICTACTOE GAME

Feb 27th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. '''
  4. DEAR VIEWER,
  5.    please if there is something missing,do not hesitate to help improve it.
  6. I'm a beginner,and i so much need help.
  7. solomonlekan1@gmail.com.
  8.                                                        
  9.                                                        THANKS
  10. '''
  11. '''
  12. A TICTACTOE GAME.
  13. 1. create a function to display board : display_board()
  14. 2. create a function to collect the marker x or o : marker()
  15. 3. create a function to assign a place marker : place_marker()
  16. 4. create a function to check the win , lose or tie(draw) : win_check()
  17. 5. create a function to randomly choose a player to play : random_player()
  18. 6. create a function to check valid position (position that are available) : valid_position()
  19. 7. create a function to check for replay : replay()
  20. '''
  21. board = ['#', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  22.  
  23. # function to display board
  24. '''from IPython.display import clear_output'''
  25. def display_board(board):
  26.     print('\n' * 100)
  27.     print(board[7]+'\t'+board[8]+'\t'+board[9])
  28.     print('_____'+'|'+'_____'+'|'+'_____')
  29.     print(board[4]+'\t'+board[5]+'\t'+board[6])
  30.     print('_____'+'|'+'_____'+'|'+'_____')
  31.     print(board[1]+'\t'+board[2]+'\t'+board[3])
  32.    
  33. #display_board(board)
  34.    
  35. # function to collect the marker
  36. def marker():
  37.     # OUTPUT : marker() = ('X', 'O')
  38.     marker = ''
  39.     while not (marker == 'X' or marker == 'O'):
  40.         marker = input('Player 1: choose your marker (X or O):\t').upper()
  41.     player1 = marker
  42.     if player1 == 'X':
  43.         return ('X', 'O')
  44.     else:
  45.         return ('O', 'X')
  46.    
  47. #player1_marker, player2_marker = marker()
  48. #position
  49. def player_choice(board,player):
  50.     position = 0
  51.    
  52.     while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
  53.         try:
  54.             position = int(input(f'{player}: Choose your next position: (1-9) '))
  55.         except ValueError:
  56.             print('you\'re required to enter a number between (1-9)')
  57.        
  58.     return position
  59.  
  60. # place marker
  61. def place_marker(board,mark,position):
  62.     board[position] = mark
  63.        
  64. #place_marker()
  65.  
  66. # function to win lose or tie check
  67.  
  68. def win_check(board,mark):
  69.     return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top
  70.     (board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
  71.     (board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom
  72.     (board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle
  73.     (board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle
  74.     (board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side
  75.     (board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal
  76.     (board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal
  77. #win_check()
  78.  
  79. # func. to randomly choose a player.
  80. def random_player():
  81.    
  82.     import random
  83.     if random.randint(0,1) == 0:
  84.         return 'Player 1'
  85.     else:
  86.         return 'Player 2'
  87.    
  88. #random_player()
  89.  
  90. #func. to check space.
  91. def space_check(board, position):
  92.    
  93.     return board[position]
  94.        
  95. #space_check()
  96.  
  97.  
  98. # full board check
  99. def full_board_check(board):
  100.     for i in range(1,10):
  101.         if space_check(board, i):
  102.             return False
  103.     return True
  104.  
  105. #full_board_check()
  106.  
  107. # func. to replay game
  108.  
  109. def replay():
  110.     replay = input('Do you want to replay,and start all over again?\t').lower()
  111.     if replay[0] == 'y':
  112.         return True
  113.    
  114. print('welcome to tic tac toe')
  115.  
  116. while True:
  117.    
  118.     #set the board
  119.     Newboard = ['#', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  120.     player1_marker, player2_marker = marker()
  121.     turn = random_player()
  122.     print(turn+' goes first')
  123.    
  124.     play_game = input('are you ready to play game?').lower()
  125.     if play_game[0] == 'y':
  126.         game_on = True
  127.     else:
  128.         game_on = False
  129.        
  130.     while game_on:
  131.         if turn == 'Player 1':
  132.             # player1 turn
  133.             display_board(Newboard)
  134.             position = player_choice(Newboard,turn)
  135.             place_marker(Newboard,player1_marker,position)
  136.            
  137.             if win_check(Newboard,player1_marker):
  138.                 display_board(Newboard)
  139.                 print('Congratulations, player 1 won.')
  140.                 game_on = False
  141.             else:
  142.                 if full_board_check(Newboard):
  143.                     display_board(Newboard)
  144.                     print('The game is a draw!')
  145.                     break
  146.                 else:
  147.                     turn = 'Player 2'
  148.  
  149.         else:
  150.    
  151.             # player 2 turn
  152.             display_board(Newboard)
  153.             position = player_choice(Newboard,turn)
  154.             place_marker(Newboard,player2_marker,position)
  155.            
  156.             if win_check(Newboard,player2_marker):
  157.                 display_board(Newboard)
  158.                 print('Congratulations, player 2 won.')
  159.                 game_on = False
  160.             else:
  161.                 if full_board_check(Newboard):
  162.                     display_board(Newboard)
  163.                     print('The game is a draw!')
  164.                     break
  165.                 else:
  166.                     turn = 'Player 1'
  167.                
  168.     if not replay():
  169.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement