""" Game of life prep lab """ import random def build_board_random(row, col): """ This function fills the board with random numbers 0 and 1. A live organism is represented by 1 and a dead organism is represented by 0. The sizes of the board will be user input and determine in main """ return [[random.randint(0,1) for i in range(col)] for j in range(row)] def build_board_user(row, col): """ This function fills the board with user input: 0 and 1. A live organism is represented by 1 and a dead organism is represented by 0. """ print("enter",row,"x",col,"0's and 1'") return [[int(input("")) for i in range(col)] for j in range(row)] def printBoard(board): """ prints the board (prints 2-d list in matrix form) - we wrote this function already """ for list in board: print(list) def copyBoard(board, row, col): """ creates and returns the copy of the board Pay attention! You have to allocate a separate space for the copy board. First you need to write the following statement in this function: copy_boad=[[ 0 for i in range(col)] for j in range(row)] and then you would need to do the nested loop to copy each element board[i][j] to copy_board[i][j] """ copy_board=[[ 0 for i in range(col)] for j in range(row)] for i in range(row): for j in range(col): copy_board[i][j]=board[i][j] return copy_board def change(oldBoard, newBoard, row, col): #returns 0 if not the same and 1 if the same for i in range(row): for j in range(col): if(oldBoard[i][j]!=newBoard[i][j]): return 0 return 1 """ solution using counter count=0 for i in range(row): for j in range(col): if(oldBoard[i][j]!=newBoard[i][j]): count+=1 if(count>0): return 0 else: return 1 """ def count(board, row, col): #counts and returns the number of live organisms count=0 for list in board: for item in list: if(item==1): count+=1 return count def neigh(board, row, col, i, j): #finds and returns the number of live organisms around the cell position i, j #corners sum=0 if(i==0 and j==0): #upper left corner sum=board[i][j+1]+board[i+1][j+1]+board[i+1][j] elif(i==0 and j==col-1): #upper right corner sum=board[i][j-1]+board[i+1][j-1]+board[i+1][j] elif(i==row-1 and j==0): #bottom left corner sum=board[i-1][j]+board[i-1][j+1]+board[i][j+1] elif(i==row-1 and j==col-1): #bottom right corner sum=board[i-1][j-1]+board[i-1][j]+board[i][j-1] #borders elif(i==0): #first row sum=board[i][j+1]+board[i+1][j+1]+board[i+1][j]+board[i][j-1]+board[i+1][j-1] elif(i==row-1): #last row sum=board[i][j+1]+board[i-1][j+1]+board[i-1][j]+board[i][j-1]+board[i-1][j-1] elif(j==0): #first column sum=board[i-1][j]+board[i][j+1]+board[i+1][j+1]+board[i-1][j+1]+board[i+1][j] elif(j==col-1): #last colum sum=board[i-1][j]+board[i+1][j]+board[i-1][j-1]+board[i][j-1]+board[i+1][j-1] else: #middle one sum=board[i-1][j]+board[i+1][j]+board[i-1][j-1]+board[i][j-1]+board[i+1][j-1] sum+=board[i][j+1]+board[i+1][j+1]+board[i-1][j+1] return sum def all_neigh(board, row, col): #this function creates a 2-d list, with each element equals #to the number of live organisms of 2-d list scores. The function returns a new list. neigh_list=[[ 0 for i in range(col)] for j in range(row)] for i in range(row): for j in range(col): neigh_list[i][j]=neigh(board, row, col, i,j) return neigh_list def main(): row=int(input("enter row ")) col=int(input("enter col ")) choice=int(input("enter 1 for random board and 0 for user board ")) if(choice==1): board=build_board_random(row, col) else: board=build_board_user(row, col) print("T0 configuration") printBoard(board) print("testing copy board") copy_board=copyBoard(board, row, col) printBoard(copy_board) print("testing function change") result=change(board,copy_board,row,col) if(result==1): print("boards the same") else: print("not the same") print("test function count") print(count(board, row, col)) print("test functions neigh and all_neigh") neigh_list=all_neigh(board,row,col) print("neighbors") printBoard(neigh_list) main()