Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # coding: utf-8
- '''
- DEAR VIEWER,
- please if there is something missing,do not hesitate to help improve it.
- I'm a beginner,and i so much need help.
- solomonlekan1@gmail.com.
- THANKS
- '''
- '''
- A TICTACTOE GAME.
- 1. create a function to display board : display_board()
- 2. create a function to collect the marker x or o : marker()
- 3. create a function to assign a place marker : place_marker()
- 4. create a function to check the win , lose or tie(draw) : win_check()
- 5. create a function to randomly choose a player to play : random_player()
- 6. create a function to check valid position (position that are available) : valid_position()
- 7. create a function to check for replay : replay()
- '''
- board = ['#', '1', '2', '3', '4', '5', '6', '7', '8', '9']
- # function to display board
- '''from IPython.display import clear_output'''
- def display_board(board):
- print('\n' * 100)
- print(board[7]+'\t'+board[8]+'\t'+board[9])
- print('_____'+'|'+'_____'+'|'+'_____')
- print(board[4]+'\t'+board[5]+'\t'+board[6])
- print('_____'+'|'+'_____'+'|'+'_____')
- print(board[1]+'\t'+board[2]+'\t'+board[3])
- #display_board(board)
- # function to collect the marker
- def marker():
- # OUTPUT : marker() = ('X', 'O')
- marker = ''
- while not (marker == 'X' or marker == 'O'):
- marker = input('Player 1: choose your marker (X or O):\t').upper()
- player1 = marker
- if player1 == 'X':
- return ('X', 'O')
- else:
- return ('O', 'X')
- #player1_marker, player2_marker = marker()
- #position
- def player_choice(board,player):
- position = 0
- while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
- try:
- position = int(input(f'{player}: Choose your next position: (1-9) '))
- except ValueError:
- print('you\'re required to enter a number between (1-9)')
- return position
- # place marker
- def place_marker(board,mark,position):
- board[position] = mark
- #place_marker()
- # function to win lose or tie check
- def win_check(board,mark):
- return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top
- (board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
- (board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom
- (board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle
- (board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle
- (board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side
- (board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal
- (board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal
- #win_check()
- # func. to randomly choose a player.
- def random_player():
- import random
- if random.randint(0,1) == 0:
- return 'Player 1'
- else:
- return 'Player 2'
- #random_player()
- #func. to check space.
- def space_check(board, position):
- return board[position]
- #space_check()
- # full board check
- def full_board_check(board):
- for i in range(1,10):
- if space_check(board, i):
- return False
- return True
- #full_board_check()
- # func. to replay game
- def replay():
- replay = input('Do you want to replay,and start all over again?\t').lower()
- if replay[0] == 'y':
- return True
- print('welcome to tic tac toe')
- while True:
- #set the board
- Newboard = ['#', '1', '2', '3', '4', '5', '6', '7', '8', '9']
- player1_marker, player2_marker = marker()
- turn = random_player()
- print(turn+' goes first')
- play_game = input('are you ready to play game?').lower()
- if play_game[0] == 'y':
- game_on = True
- else:
- game_on = False
- while game_on:
- if turn == 'Player 1':
- # player1 turn
- display_board(Newboard)
- position = player_choice(Newboard,turn)
- place_marker(Newboard,player1_marker,position)
- if win_check(Newboard,player1_marker):
- display_board(Newboard)
- print('Congratulations, player 1 won.')
- game_on = False
- else:
- if full_board_check(Newboard):
- display_board(Newboard)
- print('The game is a draw!')
- break
- else:
- turn = 'Player 2'
- else:
- # player 2 turn
- display_board(Newboard)
- position = player_choice(Newboard,turn)
- place_marker(Newboard,player2_marker,position)
- if win_check(Newboard,player2_marker):
- display_board(Newboard)
- print('Congratulations, player 2 won.')
- game_on = False
- else:
- if full_board_check(Newboard):
- display_board(Newboard)
- print('The game is a draw!')
- break
- else:
- turn = 'Player 1'
- if not replay():
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement