import random # GLOBAL CONSTANTS SIZE = 8 EMPTY = " " OPPONENT = "O" PLAYER = "P" ERROR = "!" """ @ display() @ argument: 2D list of char(length 1 string), simulated game world. Two integers specifying the current location of the player's playing piece. @ return value: none @ @ Shows the current state of the world with bounding lines above, below, @ left and right of each element. """ def displayBounded(world,cRow,cColumn): r = -1 c = -1 print("Current row=%d, current column=%d" %(cRow,cColumn)) for r in range (0,SIZE,1): for c in range (0,SIZE,1): #Lines before each row print(" -", end = "") print() for c in range (0,SIZE,1): print("|%s" %(world[r][c]), end="") #Bar before each element print("|") #Bar after last element for c in range (0,SIZE,1): #Bounding lines after the last row print(" -", end = "") print() """ @ generateElemement @ a random number argument (precondition 1<=number<=100) @ returns a string of length one (an element to populate world """ def generateElement(randomNumber): element = ERROR if ((randomNumber >= 1) and (randomNumber <= 85)): element = EMPTY elif ((randomNumber >= 51) and (randomNumber <= 100)): element = OPPONENT else: element = ERROR return(element) """ @ getMove() @ int,int (current location of the player's piece) @ returns the row, column to move the player piece. Function checks the @ location is within bounds and valid move """ def getMove(cRow,cColumn): dRow = -1 dColumn = -1 outOfBounds = True moveValid = False while ((outOfBounds == True) or (moveValid == False)): dRow = int(input("Destination row 0-%d: " %(SIZE-1))) dColumn = int(input("Destination column 0-%d: " %(SIZE-1))) outOfBounds = isOutside(dRow,dColumn) if (outOfBounds == True): print("(Row/Col) of (%d/%d) is outside the bounds of 0-%d" %(dRow,dColumn,SIZE-1)) else: moveValid = isMoveValid(cRow,cColumn,dRow,dColumn) if (moveValid == False): print("Moving from current(Row/Col) (%d/%d) to destination (Row/Col) (%d/%d) is not a straight line horizontal or vertical move " %(cRow,cColumn,dRow,dColumn)) return (dRow,dColumn) """ @ initialize() @ no parameters @ returns a fully initialized randomly generated world """ def initialize(): world = [] r = -1 c = -1 randomNumber = -1 newElement = ERROR for r in range (0,SIZE,1): tempRow = [] world.append(tempRow) # Add in new empty row for c in range (0,SIZE,1): randomNumber = random.randrange(1,101) element = generateElement(randomNumber) tempRow.append(element) # Add new element to row end return(world) """ @ isMoveValid() @ argument: four integers which specifying the current (row,column) and the destination (row,column) @ return value: Boolean @ The function checks if the move is within the bounds of the board and if the move is a valid up/down or left/right move. """ def isMoveValid(cRow,cColumn,dRow,dColumn): isValid = False #Check if piece is not moving. # Note to students: Although this approach appears #unsual setting the flag to False when it's already False. However, an alternative #approach could have checked for inequality and contained the following two branches #within the body would likely be harder for students to trace and understand. if ((cRow == dRow) and (cColumn == dColumn)): isValid = False #Recall: the destination (row,column) must be within bounds. Given there exists the #constraint that either the destination row is the same as the source row OR the #destination column is the same that means that given the conditions the move must be #straight up/down or left/right. else: #Check for horizontal move if (cRow == dRow): isValid = True #Check for vertical move elif (cColumn == dColumn): isValid = True return(isValid) """ @ isOutside() @ int,int (a point which may or may not be within the bounds of the board) @ returns a Boolean depending upon whether the location is inside the world """ def isOutside(row,column): outside = False if (row < 0): outside = True elif (row >= SIZE): outside = True if (column < 0): outside = True elif (column >= SIZE): outside = True return(outside) """ @ movePlayer() @ argument: 2D list of char(length 1 string), simulated game world, four integers which specify the current (row,column) and the destination (row,column) @ return value: None @ The current location is cleared and the destination will now contain a space. @ Precondition: checks have already been passed that the destination is: 1) In bounds 2) A valid move. """ def movePlayer(world,cRow,cColumn,dRow,dColumn): world[dRow][dColumn] = PLAYER world[cRow][cColumn] = EMPTY """ @ placePlayer() @ argument: 2D list of char(length 1 string), simulated game world, two integers specifying the location to initially place the player's piece. @ return value: None @ randomly places the player in one of the locations in the game world. returns @ Usage: this function is used to initally place the piece on the board. It is not to be used when the piece is moved off an existing location (has a current row/column) because the previous location will not be cleared. """ def placePlayer(world,playerRow,playerColumn): world[playerRow][playerColumn] = PLAYER """ @ updateLocation() @ argument: four integers which specifying the current (row,column) and the destination (row,column) @ return value: two integers (updated location). @ Updates the current (row,column) to the location that the piece has been moved to (destination row/column) """ """ @ takePieces() @ argument: four integers which specifying the current (row,column) and the destination (row,column) @ return value: None @ The function will 'take' each opposing piece from the current (row,column) to the destination (row,column) @ Precondition: the destination (row,column) is within bounds and a valid move (straight up/down or straight left/right). """ def takePieces(world,cRow,cColumn,dRow,dColumn): r = -1 c = -1 # Students need to write the code for this function so that all pieces will be be taken # (removed from board and replace with empty values). def updateLocation(cRow,cColumn,dRow,dColumn): cRow = dRow cColumn = dColumn return(cRow,cColumn) ############################################################################################# # # Starting execution point # ############################################################################################# def start(): cRow = -1 cColumn = -1 dRow = -1 dColumn = -1 world = initialize(); cRow = random.randrange(0,SIZE) cColumn = random.randrange(0,SIZE) placePlayer(world,cRow,cColumn) displayBounded(world,cRow,cColumn) dRow,dColumn = getMove(cRow,cColumn) movePlayer(world,cRow,cColumn,dRow,dColumn) takePieces(world,cRow,cColumn,dRow,dColumn) cRow,cColumn = updateLocation(cRow,cColumn,dColumn,dRow) print() print("Moving piece...") print() displayBounded(world,cRow,cColumn) start()