Advertisement
lordloh

XO.py

Sep 10th, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.20 KB | None | 0 0
  1. #! /usr/bin/python3
  2. from enum import Enum
  3. import math
  4.  
  5. class res(Enum):
  6.     OK = 0
  7.     WIN = 1
  8.     DRAW = 2
  9.     OUT_OF_TURN = -1
  10.     INVALID_MOVE = -2
  11.  
  12. def transpose(board):
  13.     transBoard=[[0,0,0],[0,0,0],[0,0,0]]
  14.     c=0;
  15.     for b in board:
  16.         r=2
  17.         for e in b:
  18.             transBoard[r][c]=e;
  19.             r -= 1
  20.         c += 1
  21.     return transBoard
  22.  
  23. class xo:
  24.     def __init__(self):
  25.         self.board = [ [0, 0, 0 ] , [ 0, 0, 0 ] , [ 0, 0, 0 ] ]
  26.         self.sym = [ ' ', '0', 'X' ]
  27.         self.turn = 0
  28.         self.X_TURN_MODULUS = -1
  29.         self.O_TURN_MODULUS = -1
  30.         self.PLAYER_X = 2
  31.         self.PLAYER_O = 1
  32.         self.won = False
  33.         self.game_over = False
  34.         #self.res=enum(OK = 0, WIN = 1, DRAW = 2, OUT_OF_TURN = -1, INVALID_MOVE = -2)
  35.         self.res=res;
  36.         self.empty_positions = 9
  37.        
  38.     def set_pos(self,pos_x,pos_y,player):
  39.         if (player >= 0 & player < 3):
  40.             self.board[pos_x][pos_y]=player
  41.         return 0
  42.        
  43.     def set_absolute(self,pos,player):
  44.         if (self.turn == 0):
  45.             if (player == 1):
  46.                 self.X_TURN_MODULUS = 1
  47.                 self.O_TURN_MODULUS = 0
  48.             elif (player == 2):
  49.                 self.X_TURN_MODULUS = 0
  50.                 self.O_TURN_MODULUS = 1
  51.         if ( self.game_over == True ):
  52.             returnVal=res.INVALID_MOVE
  53.         else:
  54.             pos_x = math.floor( pos / 3 )
  55.             pos_y = pos % 3
  56.             if (player == 1):
  57.                 # if player 1, Check if player is playing out of turn.
  58.                 if (self.turn % 2 == self.O_TURN_MODULUS):
  59.                     # check if we are overwriting a position
  60.                     if (self.board[pos_x][pos_y] == 0):
  61.                         self.board[pos_x][pos_y] = self.PLAYER_O
  62.                         self.turn += 1
  63.                         returnVal = self.has_won(self.PLAYER_O)
  64.                     else:
  65.                         returnVal = self.res.INVALID_MOVE; 
  66.                 else:
  67.                     returnVal = self.res.OUT_OF_TURN
  68.             elif (player == 2):
  69.                 # if player 2, Check if player is playing out of turn.
  70.                 if (self.turn % 2 == self.X_TURN_MODULUS):
  71.                     # check if we are overwriting a position
  72.                     if (self.board[pos_x][pos_y] == 0):
  73.                         self.board[pos_x][pos_y] = self.PLAYER_X
  74.                         self.turn += 1
  75.                         returnVal = self.has_won(self.PLAYER_X)
  76.                     else:
  77.                         returnVal = self.res.INVALID_MOVE;
  78.                 else:
  79.                     returnVal = self.res.OUT_OF_TURN
  80.             else:
  81.                 # actually invalid player
  82.                 returnVal = self.res.INVALID_MOVE
  83.         return returnVal
  84.        
  85.     def set_X(self, pos_x, pos_y):
  86.         if ( self.game_over == False ):
  87.             # check if X is playing first.
  88.             if self.turn == 0:
  89.                 self.X_TURN_MODULUS = 0
  90.                 self.O_TURN_MODULUS = 1
  91.             # check if X is not playing out of turn.
  92.             if self.turn % 2 == self.X_TURN_MODULUS:
  93.                 # check if we are overwriting a position
  94.                 if (self.board[pos_x][pos_y] == 0):
  95.                     self.board[pos_x][pos_y] = self.PLAYER_X
  96.                     self.turn += 1
  97.                     # Check if the last move resulted in a victory
  98.                     return self.has_won(self.PLAYER_X)
  99.                 else:
  100.                     return self.res.INVALID_MOVE
  101.             else:
  102.                 return self.res.OUT_OF_TURN
  103.         else:
  104.             return self.res.INVALID_MOVE;
  105.    
  106.     def set_O(self,pos_x,pos_y):
  107.         # check if O is playing first.
  108.         if ( self.game_over == False ):
  109.             if self.turn == 0:
  110.                 self.X_TURN_MODULUS = 1
  111.                 self.O_TURN_MODULUS = 0
  112.             # check if O is not playing out of turn.
  113.             if self.turn % 2 == self.O_TURN_MODULUS:
  114.                 # check if we are overwriting a position
  115.                 if ( self.board[pos_x][pos_y] == 0 ):
  116.                     self.board[pos_x][pos_y] = self.PLAYER_O
  117.                     self.turn += 1
  118.                     # Check if the last move resulted in a victory
  119.                     return self.has_won(self.PLAYER_O)
  120.                 else:
  121.                     return self.res.INVALID_MOVE
  122.             else:
  123.                 return self.res.OUT_OF_TURN
  124.         else:
  125.             return self.res.INVALID_MOVE
  126.    
  127.     def has_won(self,player):
  128.         self.count_empty_squares();
  129.         transBoard = transpose(self.board);
  130.         result = self.check_board_for_winner( self.board, player ) | self.check_board_for_winner( transBoard, player )
  131.         returnVal = self.res.OK
  132.         # check for draw
  133.         if (result == True):
  134.             # We have a winner
  135.             self.game_over = True
  136.             self.won = True
  137.             returnVal = self.res.WIN
  138.         elif (result == False & self.game_over == True):
  139.             # No winner and no moves left.
  140.             returnVal = self.res.DRAW
  141.         elif (result == False & self.game_over == False):
  142.             # No winner and game not over. Play on.
  143.             returnVal = self.res.OK
  144.         self.won=result
  145.         return returnVal
  146.        
  147.     def count_empty_squares(self):
  148.         # Count the number of empty squares
  149.         self.empty_positions = 0
  150.         for i in self.board:
  151.             for j in i:
  152.                 if j == 0:
  153.                     self.empty_positions += 1;
  154.         if self.empty_positions == 0:
  155.             # If there are no empty squares, then pronounce the game over.
  156.             self.game_over = True;
  157.    
  158.     def check_board_for_winner(self,board,player):
  159.         # Check 3 horizontal and 1 diagonal.
  160.         win = False
  161.         if board[0] == [player, player, player]:
  162.             win = True
  163.         if board[1] == [player, player, player]:
  164.             win = True
  165.         if board[2] == [player, player, player]:
  166.             win = True
  167.         if ( [board[0][0], board[1][1], board[2][2]] == [ player, player, player ] ):
  168.             win = True
  169.         return win
  170.    
  171.     def board2str(self,boardS):
  172.         boardString=""
  173.         for i in range(0, 3):
  174.             for j in range (0,3):
  175.                 if j < 2:
  176.                     boardString += self.sym[boardS[i][j]] + '| '
  177.                 else:
  178.                     boardString += self.sym[boardS[i][j]];
  179.             boardString += "\n"
  180.         return boardString;    
  181.    
  182.     def getBoard(self):
  183.         boardString="";
  184.         for i in range(0, 3):
  185.             for j in range (0,3):
  186.                 if j < 2:
  187.                     boardString += self.sym[self.board[i][j]] + '| ';
  188.                 else:
  189.                     boardString += self.sym[self.board[i][j]];
  190.             boardString += "\n"
  191.         return boardString;
  192.        
  193. def main():
  194.     print("Tic Tac Toe Platform")
  195.     g=xo()
  196.     print(g.getBoard())
  197.    
  198.     print('Play :'+repr(g.set_X(2,2)))
  199.     print(g.getBoard())
  200.     print('----------')
  201.    
  202.     print('Play :'+repr(g.set_O(0,2)))
  203.     print(g.getBoard())
  204.     print('----------')
  205.    
  206.     print('Play :'+str(g.set_X(1,2)))
  207.     print(g.getBoard())
  208.     print('----------')
  209.    
  210.     print('Play :'+str(g.set_O(2,0)))
  211.     print(g.getBoard())
  212.     print('----------')
  213.  
  214.     print('Play :'+str(g.set_X(0,0)))
  215.     print(g.getBoard())
  216.     print('----------')
  217.    
  218.     print('Play :'+str(g.set_O(0,1)))
  219.     print(g.getBoard())
  220.     print('----------')
  221.    
  222.     # out of turn
  223.     print('Play :'+str(g.set_O(0,1)))
  224.     print(g.getBoard())
  225.     print('----------')
  226.    
  227.     # invalid move
  228.     print('Play :'+str(g.set_O(0,1)))
  229.     print(g.getBoard())
  230.     print('----------')
  231.    
  232.    
  233.     print('Play :'+str((g.set_X(1,1))))
  234.     print(g.getBoard())
  235.     print('----------')
  236.  
  237. if __name__ == '__main__':
  238.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement