Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class gamePiece:
- def __init__(self, row, col):
- self.row = row
- self.col = col
- def updateCoordinates(self, row, col):
- self.row = row
- self.col = col
- class FBoard:
- def __init__(self):
- self.board = []
- self.state = "UNFINISHED"
- for i in range(0, 8):
- self.board.append([])
- for j in range(0, 8):
- if i == 0 and j == 3:
- self.board[i].append('x')
- self.gamePieceX = gamePiece(i, j)
- elif i == 7 and (j % 2) == 0:
- self.board[i].append('o')
- else:
- self.board[i].append(' ')
- def __str__(self):
- returnStr = " ---------------------------------\n"
- returnStr += " | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
- returnStr += " ---------------------------------\n"
- for i in range(0, len(self.board)):
- returnStr += str(i) + " | "
- for j in range(0, len(self.board[i])):
- returnStr += self.board[i][j] + " | "
- returnStr += "\b\n"
- returnStr += " ---------------------------------\n"
- returnStr += "Game State: " + self.state + "\n"
- return returnStr
- def get_game_state(self):
- return self.state
- def update_game_state(self):
- if(self.state == "UNFINISHED"):
- if(self.gamePieceX.row == 7):
- self.state = "X_WON"
- if(self.board[self.gamePieceX.row - 1][self.gamePieceX.col - 1] == 'o' and
- self.board[self.gamePieceX.row + 1][self.gamePieceX.col + 1] == 'o' and
- self.board[self.gamePieceX.row - 1][self.gamePieceX.col + 1] == 'o' and
- self.board[self.gamePieceX.row + 1][self.gamePieceX.col - 1] == 'o'):
- self.state = "O_WON"
- def validCoordinates(self, rowBefore, colBefore, rowAfter, colAfter, x):
- if(0 <= rowAfter < 8 and
- 0 <= colAfter < 8 and
- self.board[rowAfter][colAfter] == ' '):
- if(x):
- return (abs(rowAfter - rowBefore) == 1 and
- abs(colAfter - colBefore) == 1)
- else:
- return (self.board[rowBefore][colBefore] == 'o' and
- rowBefore - rowAfter == 1 and
- abs(colAfter - colBefore) == 1)
- def movePiece(self, rowBefore, colBefore, rowAfter, colAfter, x):
- self.board[rowAfter][colAfter] = 'x' if x else 'o'
- self.board[rowBefore][colBefore] = ' '
- if(x):
- self.gamePieceX.updateCoordinates(rowAfter, colAfter)
- self.update_game_state()
- def move_x(self, rowTo, colTo):
- if(self.state == "UNFINISHED"):
- rowBefore = self.gamePieceX.row
- colBefore = self.gamePieceX.col
- if self.validCoordinates(rowBefore, colBefore, rowTo, colTo, True):
- self.movePiece(rowBefore, colBefore, rowTo, colTo, True)
- return True
- return False
- def move_o(self, rowBefore, colBefore, rowTo, colTo):
- if(self.state == "UNFINISHED"):
- if self.validCoordinates(rowBefore, colBefore, rowTo, colTo, False):
- self.movePiece(rowBefore, colBefore, rowTo, colTo, False)
- return True
- return False
- fb = FBoard()
- print(fb)
- fb.move_x(1,4);
- fb.move_x(2,5);
- fb.move_o(7,0,6,1);
- print(fb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement