黑白棋游戏法律移动

时间:2015-03-20 00:29:25

标签: python

我的反转逻辑代码中的函数出现问题。该函数是isLegalMove我被要求“返回一个布尔值,指示当前玩家是否可以将他们的筹码放在正方形的位置(row,col)。行和col都必须是有效的索引。” 所以我在下面提出了我的代码,但是我在游戏中做出的举动是错误的:不是合法的举动。 请帮忙!

代码:

from ezarrays import Array2D

# Values representing the color of the chips on the board.
EMPTY = 0
BLACK = 1
WHITE = 2

class ReversiGameLogic :

 # Creates an instance of Reversi game logic with the board correctly
 # initialized and the current player set to black.
 def __init__(self) :
 # Use a 2-D array to represent the board.
  self._gameBoard = Array2D(8, 8)
  self._gameBoard.clear(EMPTY)

 # Set the initial configuration of the board.
  self._gameBoard[4,3] = BLACK
  self._gameBoard[3,4] = BLACK
  self._gameBoard[3,3] = WHITE
  self._gameBoard[4,4] = WHITE

 # Maintain the number of the current player.
  self._currentPlayer = BLACK

 # Keep track of the number of each players chips.
  self._numBlackChips = 2
  self._numWhiteChips = 2

 # A flag that is set when the game is over. That is, when there are
 # no empty squares on the board or neither player can make a move.
  self._gameOver = False

 # Returns a boolean indicating whether the game is over.
  def isOver(self) :
   isOver = 0
   for i in range(8) :
    for j in range(8) :
      if self._gameBoard[i, j] != 0 :
      isOver + 1
   if isOver == 64 :
     self._gameOver = True
     return True
   else:
     return False

 # Returns the player number of the current player.
 def whoseTurn(self) :
  if self._currentPlayer == 1:
   return 1
  else:
   self._curentPlayer == 2
  return 2


 # Returns the number of chips on the board for the given player.
 def numChips(self, player) :
  chipCounter = 0
  if player == 1 :
   for i in range(8) :
     for j in range(8) :
      if self._gameBoard[i, j] == BLACK :
        chipCounter = chipCounter + 1
  else : 
   for i in range(8) :
     for j in range(8) :
       if self._gameBoard[i, j] == WHITE :
        chipCounter = chipCounter + 1 
  return chipCounter

 # Returns the number of open squares on the board.
 def numOpenSquares(self) :
  numOpenSquares = 0
  for i in range(8) :
   for j in range(8) :
     if self._gameBoard[i, j] == EMPTY :
       numOpenSquares =  numOpenSquares + 1
  return numOpenSquares

 # Returns the player number of the winner or 0 if it's a draw.
  def getWinner( self ):
  player1 = 0
  player2 = 0
  if self._gameOver is True :
  for i in range(8) :
    for j in range(8) :
      if self._gameBoard[i, j] == BLACK :
        player1 = player1 + 1
      else :
        player2 = player2 + 1
  if player1 > player2 :
    return 1
  if player2 > player1 :
    return 2
  else:
    return 0

 #  
 def isLegalMove( self, row, col):
  if row < 8 and col < 8:
   if self._gameBoard[row,col] != EMPTY:
     return True
  else:
    return False



 # Returns the player number whose chip occupies the given square.
 def occupiedBy(self, row, col):
  if self._gameBoard[row, col] == BLACK :
    return 1
  if self._gameBoard[row, col] == WHITE :
    return 2
  else:
    return 0

 # Performs an actual move in the game. That is the current player places
 # one of his chips in the square at position (row, col).
 def makeMove( row, col ):
  if isALineOfAttack(row, col, 1, 1) is True :
   if self._currentPlayer == 1 :
     self._gameBoard[row, col] = BLACK
   else :
     self._gameBoard[row, col] = WHITE 










# Helper method that returns a Boolean indicating if there is a line of
# attack from cell (row, col) in the direction offset given by rowInc
# and colInc. The direction offsets should be, 0, 1, or -1.
def _isALineOfAttack(self, row, col, rowInc, colInc) :
 row += rowInc
 col += colInc
# The next cell in the line must contain the opponents chip.  
 if self.occupiedBy(row, col) == self._currentPlayer :
   return False

# Traverse along the line and determine if it's a line of attack.
 while row >= 0 and col >= 0 and row < 8 and col < 8 :
   if self.occupiedBy(row, col) == self._currentPlayer :
     return True
   elif self.occupiedBy(row, col) == EMPTY :
     return False
   else :
     row += rowInc
     col += colInc
     if row < 0 or row > 7 or col < 0 or col > 7 :
          return False      
 return False

1 个答案:

答案 0 :(得分:0)

我对isLegalMove()缩进的解释:

def isLegalMove( self, row, col ):
    if row < 8 and col < 8:
        if self._gameBoard[row,col] != EMPTY:
            return True
    else:
        return False

值得注意的问题:

  1. 如果row和col都小于8,但self._gameBoard[row,col] == EMPTY在这种情况下你的函数不会返回任何内容。
  2. True时正在返回self._gameBoard[row,col] != EMPTY,这是否意味着将筹码置于主板上的现有芯片之上是合法的?
  3. 我建议的解决方案:

    1. 首先在rowcol上执行输入验证,以确保它们在您的电路板范围内。如果不是,请立即返回False
    2. 接下来,如果True处的电路板上没有芯片,则返回[row, col]。否则False
    3. 编辑:

      假设您的电路板尺寸固定,8x8。

      def isLegalMove( self, row, col ):
          if row < 0 or row >= 8 or col < 0 or col >= 8:
              return False
      
          if self._gameBoard[row,col] == EMPTY:
              return True
          else:
              return False