[Tutor] Reversi Game Logic

niyanaxx95 at gmail.com niyanaxx95 at gmail.com
Fri Mar 20 18:28:41 CET 2015


I am very new to Comp Science and am still learning.

I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project.

Please help me out!






Sent from Windows Mail





From: Ni'Yana Morgan
Sent: ‎Friday‎, ‎March‎ ‎20‎, ‎2015 ‎11‎:‎16‎ ‎AM
To: Mark Lawrence






I am very new to Comp Science and am still learning.

I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project.

Please help me out!






Sent from Windows Mail





From: Mark Lawrence
Sent: ‎Friday‎, ‎March‎ ‎20‎, ‎2015 ‎11‎:‎11‎ ‎AM
To: Ni'Yana Morgan






please use reply all so everybody can benefit


 


Kindest regards. 

Mark Lawrence.









On Friday, 20 March 2015, 15:04, "niyanaxx95 at gmail.com" <niyanaxx95 at gmail.com> wrote:
 








Thank you Mark for replying. I fixed the note you provided on the isLegalMove. However the lineOfAttack function is a function my Professor did so students are not allowed to touch it.




For the isOver function, are you able to guide me on that?

I am very new to Comp Science and am still learning.

I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project.

Please help me out!
















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
  
  # Returns the 
  def isLegalMove( self, row, col):
    if row < 8 > col:
      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







Sent from Windows Mail





From: Mark Lawrence
Sent: ‎Thursday‎, ‎March‎ ‎19‎, ‎2015 ‎11‎:‎12‎ ‎PM
To: tutor at python.org





On 20/03/2015 00:50, niyanaxx95 at gmail.com wrote:

 From just a quick glance seeing it's 3 in the morning here.

> I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices​." So I came up with my code below, however a move I make in the game says Error: not a legal move. Please help!
>
> Code:
>
> 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

The above line does nothing.

>      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

The above line has two errors.

>      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
>

You can greatly simplify the above function - I'll let you think about it :)

>    # 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
>
>    # Returns the
>    def isLegalMove( self, row, col):
>      if row < 8 and col < 8:

In Python the above line can be written.

if row < 8 > col:

>        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 :

Again Python comparisons don't need to be written like this.

>        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



-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: reversilogic.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20150320/ba22fde1/attachment-0003.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: reversigui.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20150320/ba22fde1/attachment-0004.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: reversi.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20150320/ba22fde1/attachment-0005.ksh>


More information about the Tutor mailing list