arten_1337

Untitled

Sep 14th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. import time
  2. import os
  3. import textwrap
  4.  
  5.  
  6. def board_display(score):
  7.     print(score[6], '|', score[7], '|', score[8])
  8.     print('-'*10)
  9.     print(score[3], '|', score[4], '|', score[5])
  10.     print('-'*10)
  11.     print(score[0], '|', score[1], '|', score[2])
  12.  
  13.  
  14. def check(char, posi1, posi2, posi3):
  15.     if score[posi1] == char and score[posi2] == char and score[posi3] == char:
  16.         return True
  17.  
  18.  
  19. def checkall(char):
  20.     if check(char, 0, 1, 2):
  21.         return True
  22.     if check(char, 3, 4, 5):
  23.         return True
  24.     if check(char, 6, 7, 8):
  25.         return True
  26.     if check(char, 0, 3, 6):
  27.         return True
  28.     if check(char, 1, 4, 7):
  29.         return True
  30.     if check(char, 2, 5, 8):
  31.         return True
  32.     if check(char, 0, 4, 8):
  33.         return True
  34.     if check(char, 2, 4, 6):
  35.         return True
  36.     return False
  37.  
  38.  
  39. print('Tic Tac Toe\n\n'.center(70))
  40. print('The board is numbered in the form of the numpad keyboard as printed below.\n')
  41. time.sleep(0.01)
  42. score = ['1', '2', '3',
  43.          '4', '5', '6',
  44.          '7', '8', '9']
  45.  
  46. board_display(score)
  47.  
  48. 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.'
  49. wrapper = textwrap.TextWrapper(width=72)
  50. string = wrapper.fill(text=value)
  51. print('\n', string)
  52.  
  53. print('\nChoose your cells accordingly.The game will begin shortly.')
  54. #time.sleep(10)
  55. score = [' ']*9
  56. while True:
  57.     if ' ' not in score:
  58.         print('The game has resulted into a cat game ( ͡° ͜ʖ ͡°)')
  59.         break
  60.     board_display(score)
  61.     while True:
  62.         try:
  63.             player1 = int(input('\nPlayer X, choose your position: '))
  64.             if score[player1-1] != 'X' and score[player1-1] != 'O':
  65.                 score[player1-1] = 'X'
  66.                 break
  67.             else:
  68.                 print('\nThis position is already taken, please try again')
  69.                 continue
  70.         except:
  71.             print('\nKindly enter a valid value')
  72.             continue
  73.     os.system('clear')
  74.     board_display(score)
  75.     if checkall('X'):
  76.         print('Player X is the winner')
  77.         break
  78.     while True:
  79.         try:
  80.             player2 = int(input('\nPlayer O, choose your position: '))
  81.             if score[player2-1] != 'X' and score[player2-1] != 'O':
  82.                 score[player2-1] = 'O'
  83.                 break
  84.             else:
  85.                 print('\nThis position is already taken, please try again')
  86.                 continue
  87.         except:
  88.             print('\nKindly enter a valid value')
  89.             continue
  90.  
  91.     if checkall('O'):
  92.         print('Player O is the winner')
  93.         break
  94.     os.system('clear')
Advertisement
Add Comment
Please, Sign In to add comment