首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Reversi游戏合法行动

Reversi游戏合法行动
EN

Stack Overflow用户
提问于 2015-03-20 00:29:24
回答 1查看 1.3K关注 0票数 2

我的reversi逻辑代码中的一个函数有问题。函数是isLegalMove --我被要求“返回一个布尔值,指示当前播放器是否可以将其芯片放置在位置(行、行)的方格中。行和行都必须是有效的索引​。”所以我在下面给出了我的代码,但是我在游戏中的一个动作是错误的:不是合法的移动。请帮帮我!

代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-03-20 02:23:18

我对你对isLegalMove()缩进的解释

代码语言:javascript
复制
def isLegalMove( self, row, col ):
    if row < 8 and col < 8:
        if self._gameBoard[row,col] != EMPTY:
            return True
    else:
        return False

值得注意的问题:

  1. 如果行和col都小于8,但是self._gameBoard[row,col] == EMPTY,在这种情况下,函数不会返回任何内容。
  2. True返回self._gameBoard[row,col] != EMPTY时,这是否意味着将芯片放在板上现有芯片之上是合法的?

我建议的解决办法是:

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

编辑:

假设你的棋盘是固定大小的,8x8。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29157412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档