# Author: James Tam # Version: June 3, 2020 # A program that reads the information for starting positions for a chess # board into a 2D list that is dynamically created using append(). # Limitation: no functionality implemented aside from reading the starting positions from file. NEWLINE = "\n" """ @display *Arguments: list reference,integer,integer (the last two reflect the size of the list) *Return: nothing Displays the contents of the 2D list with each row appearing on its own line. It's followed by a separator a line of stars and two blank lines. """ def display(aBoard,numRows,numColumns): currentRow = 0 currentColumn = 0 print("DISPLAY BOARD") while (currentRow < numRows): currentColumn = 0 while (currentColumn < numColumns): print("%s" %(aBoard[currentRow][currentColumn]),end="") currentColumn = currentColumn + 1 currentRow = currentRow + 1 print() for currentColumn in range (0,numColumns,1): print("*", end="") print(NEWLINE) """ @displayWithGrid *Arguments: list reference,integer,integer (the last two reflect the size of the list) *Return: nothing Displays the contents of the 2D list with each row appearing on its own line. Each element in the list is bound: above, below, left and right with a line to visually separate it from other elements. """ def displayWithGrid(aBoard,numRows,numColumns): currentRow = 0 currentColumn = 0 print("DISPLAY BOARD WITH GRID") while (currentRow < numRows): for currentColumn in range(0,numColumns,1): print(" -", end="") print() currentColumn = 0 while (currentColumn < numColumns): print("|%s" %(aBoard[currentRow][currentColumn]),end="") currentColumn = currentColumn + 1 currentRow = currentRow + 1 print("|") for currentColumn in range(0,numColumns,1): print(" -", end="") """ @readBoardFromFile *Arguments: None *Return: list reference,integer,integer (the last two reflect the size of the list) Reads the starting positions from a text file. Each piece or an empty space is represented by a character. A line in the file will be a row in the chess board. """ def readBoardFromFile(): inputFileOK = False aBoard = [] while (inputFileOK == False): try: inputFileName = input("Enter name of input file: ") inputFile = open(inputFileName,"r") print("Opening file "+ inputFileName + \ " for reading.") currentRow = 0 for line in inputFile: aBoard.append([]) #currentColumn = 0 for ch in line: if (ch != NEWLINE): aBoard[currentRow].append(ch) currentRow = currentRow + 1 inputFileOK = True print("Completed reading of file " + inputFileName) except IOError: print("Error: File", inputFileName, "couldn't" + \ "be opened.") numRows = len(aBoard) numColumns = len(aBoard[0]) return(aBoard,numRows,numColumns) ######################### # Execution begins here # ######################### def start(): aBoard,numRows,numColumns = readBoardFromFile() display(aBoard,numRows,numColumns) displayWithGrid(aBoard,numRows,numColumns) start()