Advertisement
berinkaq

Untitled

Mar 12th, 2021
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. class TicTacGame:
  2.     player = 'X'
  3.     winner = ''
  4.     board = {'7': ' ', '8': ' ', '9': ' ',
  5.              '4': ' ', '5': ' ', '6': ' ',
  6.              '1': ' ', '2': ' ', '3': ' '}
  7.  
  8.     def show_board(self):
  9.         print('     |     |')
  10.         print('  {}  |  {}  |  {}'.format(self.board['7'], self.board['8'], self.board['9']))
  11.         print('_____|_____|_____')
  12.         print('     |     |')
  13.         print('  {}  |  {}  |  {}'.format(self.board['4'], self.board['5'], self.board['6']))
  14.         print('_____|_____|_____')
  15.         print('     |     |')
  16.         print('  {}  |  {}  |  {}'.format(self.board['1'], self.board['2'], self.board['3']))
  17.         print('     |     |')
  18.  
  19.     def validate_input(self, turn):
  20.         try:
  21.             move = int(turn)
  22.         except ValueError:
  23.             return 0
  24.         if move > 9 or move < 1:
  25.             return 0
  26.         if self.board[turn] != ' ':
  27.             return 0
  28.         return 1
  29.  
  30.     def make_turn(self):
  31.         while True:
  32.             print('Player', self.player, 'turn:')
  33.             turn = input()
  34.             if self.validate_input(turn):
  35.                 self.board[turn] = self.player
  36.                 if self.player == 'X':
  37.                     self.player = 'O'
  38.                 else:
  39.                     self.player = 'X'
  40.                 break
  41.             else:
  42.                 print('Wrong input! Try again!')
  43.  
  44.     def check_winner(self):
  45.         winnings = [['7', '8', '9'], ['4', '5', '6'], ['1', '2', '3'],
  46.                     ['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9'],
  47.                     ['1', '5', '9'], ['3', '5', '7']]
  48.         for comb in winnings:
  49.             if self.board[comb[0]] != ' ':
  50.                 if self.board[comb[0]] == self.board[comb[1]] and self.board[comb[0]] == self.board[comb[2]]:
  51.                     return 1
  52.         return 0
  53.  
  54.     def start_game(self):
  55.         turn = 0
  56.         while True:
  57.             self.show_board()
  58.             if turn == 9:
  59.                 print('Draw!')
  60.                 return
  61.             self.make_turn()
  62.             if self.check_winner():
  63.                 self.show_board()
  64.                 if self.player == 'X':
  65.                     self.winner = 'O'
  66.                 else:
  67.                     self.winner = 'X'
  68.                 print('Player', self.winner, 'has won!!!')
  69.                 return
  70.             turn += 1
  71.  
  72.  
  73. if __name__ == '__main__':
  74.     game = TicTacGame()
  75.     game.start_game()
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement