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, "niyanax...@gmail.com" <niyanax...@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@python.org





On 20/03/2015 00:50, niyanax...@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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
# reversilogic.py
#
# Created by: R. Necaise
# Modified by: 
#
#

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 and col < 8:
      if self._gameBoard[row,col] != EMPTY:
        return False
    else:
      return True
  
     
  
   # 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
from reversiguiapp import ReversiGameApp

theApp = ReversiGameApp()
theApp.run()
# reversi.py 
#
# created by: R. Necaise
#
# A text-based front end for playing the game of Reversi. This driver module
# needs a correct implementation of the ReversiGameLogic ADT.
#
# The grading version prints out the user entered moves that are extracted
# from a file using redirected standard input.
#

from reversilogic import ReversiGameLogic

PLAYERS = [None, "Black (o)", "White (x)"]
THE_CHIPS = [".", "o", "x"]

def main():
  theGame = ReversiGameLogic()
  showBoard( theGame )
  
  while not theGame.isOver() :
    player = theGame.whoseTurn()    
    row, col = getMove( player )
    if theGame.isLegalMove( row, col ) :
      theGame.makeMove( row, col )
      player = theGame.whoseTurn()
      showBoard( theGame )
    else :
      print( "\nError!! not a legal move." )    
  
  if theGame.getWinner() == 0 :
    print( "The game is a draw." )
  else :
    print( PLAYERS[theGame.getWinner()] + " is the Winner!!!" )
    
def getMove( player ):
  while True :
    print( PLAYERS[ player ] + ": select the cell for your move." )    
    temp = input("   row (1-8) => ")
    row = ord(temp[0]) - 49    
    if row < 0 or row > 7 :
      print( "Error!! invalid row number." )
    else :
      temp = input("   col (a-h) => ")
      col = ord(temp[0]) - 97
      if col < 0 or col > 7 :
        print( "Error!! invalid column letter." )
      else :
        return row, col
  
def showBoard( game ):
  blackChips = game.numChips( 1 )
  whiteChips = game.numChips( 2 )
  print( "" )
  print( "Score: Black (o): %d, White (x): %d" % (blackChips, whiteChips) )
  print( "    a  b  c  d  e  f  g  h" )
  for row in range(8) :
    print( row+1, " ", end="" )
    for col in range(8) :
      chip = game.occupiedBy( row, col )
      if chip is None :
        chip = 0
      print( " " + THE_CHIPS[chip] + " ", end="" )
    print( "" )
  print( "" )

main()
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to