Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. class TicTacToe:
  2.     X = 'X'
  3.     O = 'O'
  4.  
  5.     def __init__(self):
  6.         self.board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  7.         self.player1_turn = None
  8.         self.player2_turn = None
  9.         self.turn = self.X
  10.         self.game_over = False
  11.  
  12.     def display_board(self):
  13.         print(f'{self.board[0]} | {self.board[1]} | {self.board[2]}')
  14.         print('---------')
  15.         print(f'{self.board[3]} | {self.board[4]} | {self.board[5]}')
  16.         print('---------')
  17.         print(f'{self.board[6]} | {self.board[7]} | {self.board[8]}')
  18.  
  19.     def is_legal(self, move):
  20.         if self.board[move] == ' ':
  21.             return True
  22.         else:
  23.             print('This side is not empty!')
  24.             return False
  25.  
  26.     def piece_p_1(self):
  27.         if self.turn == self.X:
  28.             self.player1_turn = self.X
  29.         else:
  30.             self.player1_turn = self.O
  31.         return self.player1_turn
  32.  
  33.     def piece_p_2(self, player):
  34.         if player == self.X:
  35.             self.player2_turn = self.X
  36.         else:
  37.             self.player2_turn = self.O
  38.         return self.player2_turn
  39.  
  40.     def set_player1(self, index):
  41.         try:
  42.             while (index > 8 or index < 0) and not self.is_legal(index):
  43.                 print('Input not correct!!!')
  44.                 print('Player 1 (0 - 8): ')
  45.                 index = int(input())
  46.             else:
  47.                 self.board[index] = self.player1_turn
  48.         except:
  49.             print('ERROR')
  50.  
  51.     def set_player2(self, index):
  52.         try:
  53.             while (index > 8 or index < 0) and not self.is_legal(index):
  54.                 print('Input not correct!!!')
  55.                 print('Player 2 (0 - 8): ')
  56.                 index = int(input())
  57.             else:
  58.                 self.board[index] = self.player2_turn
  59.         except:
  60.             print('ERROR')
  61.  
  62.     def game_loop(self):
  63.         while True:
  64.             p1 = self.piece_p_1()
  65.             p2 = self.piece_p_2(p1)
  66.  
  67.             self.display_board()
  68.  
  69.             if p1 == self.X:
  70.                 print('Player 1 is first (0 - 8): ')
  71.                 index = int(input())
  72.                 self.set_player1(index)
  73.             else:
  74.                 print('Player 2 is first (0 - 8): ')
  75.                 index = int(input())
  76.                 self.set_player2(index)
  77.  
  78.             if self.turn == self.X:
  79.                 self.turn = self.O
  80.             else:
  81.                 self.turn = self.X
  82.  
  83.  
  84. game = TicTacToe()
  85. game.game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement