Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. class gamePiece:
  2.     def __init__(self, row, col):
  3.         self.row = row
  4.         self.col = col
  5.  
  6.     def updateCoordinates(self, row, col):
  7.         self.row = row
  8.         self.col = col
  9.  
  10. class FBoard:
  11.     def __init__(self):
  12.         self.board = []
  13.         self.state = "UNFINISHED"
  14.  
  15.         for i in range(0, 8):
  16.             self.board.append([])
  17.             for j in range(0, 8):
  18.                 if i == 0 and j == 3:
  19.                     self.board[i].append('x')
  20.                     self.gamePieceX = gamePiece(i, j)
  21.                 elif i == 7 and (j % 2) == 0:
  22.                     self.board[i].append('o')
  23.                 else:
  24.                     self.board[i].append(' ')
  25.  
  26.     def __str__(self):
  27.         returnStr = "  ---------------------------------\n"
  28.         returnStr += "  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
  29.         returnStr += "  ---------------------------------\n"
  30.         for i in range(0, len(self.board)):
  31.             returnStr += str(i) + " | "
  32.             for j in range(0, len(self.board[i])):
  33.                 returnStr += self.board[i][j] + " | "
  34.             returnStr += "\b\n"
  35.         returnStr += "  ---------------------------------\n"
  36.         returnStr += "Game State: " + self.state + "\n"
  37.         return returnStr
  38.  
  39.     def get_game_state(self):
  40.         return self.state
  41.  
  42.     def update_game_state(self):
  43.         if(self.state == "UNFINISHED"):
  44.             if(self.gamePieceX.row == 7):
  45.                 self.state = "X_WON"
  46.             if(self.board[self.gamePieceX.row - 1][self.gamePieceX.col - 1] == 'o' and
  47.                self.board[self.gamePieceX.row + 1][self.gamePieceX.col + 1] == 'o' and
  48.                self.board[self.gamePieceX.row - 1][self.gamePieceX.col + 1] == 'o' and
  49.                self.board[self.gamePieceX.row + 1][self.gamePieceX.col - 1] == 'o'):
  50.                 self.state = "O_WON"
  51.            
  52.     def validCoordinates(self, rowBefore, colBefore, rowAfter, colAfter, x):
  53.         if(0 <= rowAfter < 8 and
  54.            0 <= colAfter < 8 and
  55.            self.board[rowAfter][colAfter] == ' '):
  56.             if(x):
  57.                return (abs(rowAfter - rowBefore) == 1 and
  58.                        abs(colAfter - colBefore) == 1)
  59.             else:
  60.                 return (self.board[rowBefore][colBefore] == 'o' and
  61.                         rowBefore - rowAfter == 1 and
  62.                         abs(colAfter - colBefore) == 1)
  63.     def movePiece(self, rowBefore, colBefore, rowAfter, colAfter, x):
  64.         self.board[rowAfter][colAfter] = 'x' if x else 'o'
  65.         self.board[rowBefore][colBefore] = ' '
  66.         if(x):
  67.             self.gamePieceX.updateCoordinates(rowAfter, colAfter)
  68.         self.update_game_state()
  69.  
  70.     def move_x(self, rowTo, colTo):
  71.         if(self.state == "UNFINISHED"):
  72.             rowBefore = self.gamePieceX.row
  73.             colBefore = self.gamePieceX.col
  74.             if self.validCoordinates(rowBefore, colBefore, rowTo, colTo, True):
  75.                 self.movePiece(rowBefore, colBefore, rowTo, colTo, True)
  76.                 return True
  77.         return False
  78.  
  79.     def move_o(self, rowBefore, colBefore, rowTo, colTo):
  80.         if(self.state == "UNFINISHED"):
  81.             if self.validCoordinates(rowBefore, colBefore, rowTo, colTo, False):
  82.                 self.movePiece(rowBefore, colBefore, rowTo, colTo, False)
  83.                 return True
  84.         return False
  85.        
  86. fb = FBoard()
  87. print(fb)
  88. fb.move_x(1,4);
  89. fb.move_x(2,5);
  90. fb.move_o(7,0,6,1);
  91. print(fb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement