Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Tasks Weight Awarded Marks deducted for badly commented or badly written code (-4) class GameOfLife init 2 neighbourSum 8 nextPattern 10 print 6 main()function foolproof

Tasks

Weight

Awarded

Marks deducted for badly commented

or badly written code

(-4)

class GameOfLife

init

2

neighbourSum

8

nextPattern

10

print

6

main()function

foolproof prompts

Creation of 3 GameOfLife objects

The games menu

2

3

4

Total

35+(-4)

I also have a code that you need to fix it as soon as possible.

And suggest my other two patterns. i have limited time as i crossed my due date

This is my code

import numpy as np

# Class to define the game of life class GameOfLife: # Constructor to initialize the number of rows and columns and create a board with zeros def __init__(self, rows, cols): self.rows = rows self.cols = cols self.board = board self.board = np.zeros((rows, cols), dtype=int) # Method to start the game def start_game(self): self.n = len(self.board)

# Main function to run the program def main(): # Try to get the number of rows and columns from the user try: rows = int(input("Enter the number of rows: ")) cols = int(input("Enter the number of columns: ")) except ValueError: # If the input is not a valid number, print an error message print("Invalid input. Please enter a valid number.") # Call the main function again to prompt the user for input main()

# Create three instances of the GameOfLife class with different sizes game1 = GameOfLife(20, 20) game2 = GameOfLife(rows, cols) game3 = GameOfLife(rows, cols)

# Loop to give the user a menu to start a game while True: print("1. Start game 1") print("2. Start game 2") print("3. Start game 3") print("4. Exit") choice = int(input("Enter your choice: "))

# If the user chooses 1, start game 1 if choice == 1: game1.start_game(game1.board) # If the user chooses 2, start game 2 elif choice == 2: game2.start_game() # If the user chooses 3, start game 3 elif choice == 3: game3.start_game() # If the user chooses 4, exit the program elif choice == 4: return # If the user enters an invalid choice, print an error message else: print("Invalid choice. Please enter a valid option.")

# Check if the code is being run as the main program and not as a module if __name__ == "__main__": # Call the main function main() #calculating the number of live neighbors def neighbourSum(self, i, j): count = 0 #checking if the top left neighbor is live if (self.board[(i-1+self.n)%self.n][(j-1+self.n)%self.n] == 1): count += 1 #checking if the top center neighbor is live if (self.board[(i-1+self.n)%self.n][j] == 1): count += 1 #checking if the top right neighbor is live if (self.board[(i-1+self.n)%self.n][(j+1)%self.n] == 1): count += 1 #checking if the right neighbor is live if (self.board[i%self.n][(j+1)%self.n] == 1): count += 1 #checking if the bottom right neighbor is live if (self.board[(i+1)%self.n][(j+1)%self.n] == 1): count += 1 #checking if the bottom center neighbor is live if (self.board[(i+1)%self.n][j] == 1): count += 1 #checking if the bottom left neighbor is live if (self.board[(i+1)%self.n][(j-1+self.n)%self.n] == 1): count += 1 #checking if the left neighbor is live if (self.board[i][(j-1+self.n)%self.n] == 1): count += 1 #returning the number of live neighbors return count

#generating the next pattern based on the live neighbors count def nextPattern(self): #initializing a new board with all cells as dead newBoard = [[0 for x in range(self.n)] for x in range(self.n)] #iterating over the cells for i in range(self.n): for j in range(self.n): count = self.neighbourSum(i, j) #checking the live cell rule if (self.board[i][j] == 1 and count < 2): newBoard[i][j] = 0 elif (self.board[i][j] == 1 and count > 3): newBoard[i][j] = 0 elif (self.board[i][j] == 0 and count == 3): newBoard[i][j] = 1 elif (self.board[i][j] == 1 and (count == 2 or count == 3)): newBoard[i][j] = 1 #printing the pattern self.printBoard() #taking the user input to proceed to next pattern or to end the game userInput = input("To print the next pattern enter y/Y. To end the game enter any other symbol:") # If the user enters 'y' or 'Y', update the board and repeat the process if (userInput == "y" or userInput == "Y"): self.board = newBoard self.nextPattern() # If the user enters anything else, end the game and display a message else: print("Thanks for playing with us !!!") def printBoard(self): # Loop through the board for i in range(self.n): for j in range (self.n): # If the current cell is 0, print a blank space if ( self.board[i][j] == 0): print("", end=" ") # If the current cell is 1, print an asterisk else: print("*", end=" ") # Move to the next line for the next row print("", end=" ") board = np.zeros((20, 20)) # create a 20x20 board filled with zeros board[2, 4:7] = 1 # set elements in the specified range to 1 board[4:7, 7] = 1 # set elements in the specified range to 1 board += board.T # add transposed board to the original board board += board[:, ::-1] # add the board flipped along the vertical axis to the original board board += board[::-1, :] # add the board flipped along the horizontal axis to the original board

gol = GameOfLife(board) # create a GameOfLife object with the updated board

gol.nextPattern()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions