Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import os
- import textwrap
- def board_display(score):
- print(score[6], '|', score[7], '|', score[8])
- print('-'*10)
- print(score[3], '|', score[4], '|', score[5])
- print('-'*10)
- print(score[0], '|', score[1], '|', score[2])
- def check(char, posi1, posi2, posi3):
- if score[posi1] == char and score[posi2] == char and score[posi3] == char:
- return True
- def checkall(char):
- if check(char, 0, 1, 2):
- return True
- if check(char, 3, 4, 5):
- return True
- if check(char, 6, 7, 8):
- return True
- if check(char, 0, 3, 6):
- return True
- if check(char, 1, 4, 7):
- return True
- if check(char, 2, 5, 8):
- return True
- if check(char, 0, 4, 8):
- return True
- if check(char, 2, 4, 6):
- return True
- return False
- print('Tic Tac Toe\n\n'.center(70))
- print('The board is numbered in the form of the numpad keyboard as printed below.\n')
- time.sleep(0.01)
- score = ['1', '2', '3',
- '4', '5', '6',
- '7', '8', '9']
- board_display(score)
- value = 'The object of Tic Tac Toe is to get three in a row. You play on a three by three game board. The first player is known as X and the second is O. Players alternate placing Xs and Os on the game board until either opponent has three in a row or all nine squares are filled. X always goes first, and in the event that no one has three in a row, the stalemate is called a cat game.'
- wrapper = textwrap.TextWrapper(width=72)
- string = wrapper.fill(text=value)
- print('\n', string)
- print('\nChoose your cells accordingly.The game will begin shortly.')
- #time.sleep(10)
- score = [' ']*9
- while True:
- if ' ' not in score:
- print('The game has resulted into a cat game ( ͡° ͜ʖ ͡°)')
- break
- board_display(score)
- while True:
- try:
- player1 = int(input('\nPlayer X, choose your position: '))
- if score[player1-1] != 'X' and score[player1-1] != 'O':
- score[player1-1] = 'X'
- break
- else:
- print('\nThis position is already taken, please try again')
- continue
- except:
- print('\nKindly enter a valid value')
- continue
- os.system('clear')
- board_display(score)
- if checkall('X'):
- print('Player X is the winner')
- break
- while True:
- try:
- player2 = int(input('\nPlayer O, choose your position: '))
- if score[player2-1] != 'X' and score[player2-1] != 'O':
- score[player2-1] = 'O'
- break
- else:
- print('\nThis position is already taken, please try again')
- continue
- except:
- print('\nKindly enter a valid value')
- continue
- if checkall('O'):
- print('Player O is the winner')
- break
- os.system('clear')
Advertisement
Add Comment
Please, Sign In to add comment