Question

Please use Java for this question.

​​​​​​​Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first

2. Grid Game Given a grid with n rows and m columns of binary integers, 0 or 1, and a set of rules, simulate k turns of the g

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

'''

Python version : 3.6

'''

# The function is expected to return a 2D_INTEGER_ARRAY

# The function accepts the following parameters:

# 1. 2D_INTEGER_ARRAY grid

# 2. INTEGER k

# 3. STRING_ARRAY rules

def gridGame(grid, k, rules):

  

# create outGrid of same elements as grid

outGrid = [[grid[i][j] for j in range(len(grid[i]))] for i in range(len(grid))]

  

# loop that runs k times

for i in range(k):

# create a tempGrid the contains the same elements as outGrid

tempGrid = [[outGrid[i][j] for j in range(len(outGrid[i]))] for i in range(len(outGrid))]

# loop that updates each cell of tempGrid

for row in range(len(tempGrid)):

for col in range(len(tempGrid[row])):

# call the function to calculate the number of alive neighbors

aliveNeighbors = countAliveNeighbors(outGrid,row,col)

# if the rules for that number of alive neighbours is alive set it to 1

if rules[aliveNeighbors] == "alive":

tempGrid[row][col] = 1

else: # if rule is dead set it to 0

tempGrid[row][col] = 0

# copy the tempGrid elements to outGrid

outGrid = [[tempGrid[i][j] for j in range(len(tempGrid[i]))] for i in range(len(tempGrid))]

return outGrid # return the outGrid

# function that calculates and returns the number of alive neighbors for specified row and col

# the function accepts the following parameters as inputs:

# 1. 2D_INTEGER_ARRAY grid

# 2. INTEGER row

# 3. INTEGER col

def countAliveNeighbors(grid, row, col):

  

count = 0

if((row -1) >= 0):

if(((col-1) >= 0) and (grid[row-1][col-1] == 1)):

count += 1

if(grid[row-1][col] == 1):

count += 1

if((col+1) < len(grid[row])) and (grid[row-1][col+1] == 1):

count += 1

if((col-1) >= 0 and (grid[row][col-1] == 1)):

count += 1

if((col+1) < len(grid[row]) and grid[row][col+1] == 1):

count += 1

if((row+1) < len(grid)):

if(((col-1) >= 0) and (grid[row+1][col-1] == 1)):

count += 1

if(grid[row+1][col] == 1):

count += 1

if((col+1) < len(grid[row])) and (grid[row+1][col+1] == 1):

count += 1

return count

  

# main function

if __name__ == "__main__":

  

# read the value fro n and m

n = int(input(''))

m = int(input(''))

# create a grid of n rows and m columns and set the elements to 0

grid = [[0 for i in range(m)] for j in range(n)]

  

# loop to read the values for the grid

for i in range(n):

line = input('')

line_list = line.split()

for j in range(m):

grid[i][j] = int(line_list[j])

  

# read the value for k   

k = int(input(''))

  

# read 9

numRules = int(input(''))

  

# read the 9 values for rules

rules = ["" for i in range(numRules)]

for i in range(numRules):

rules[i] = input('')

rules[i] = rules[i].strip()

  

# call and get the modified grid

mod_grid = gridGame(grid,k,rules)

  

# output the resultant grid

print(mod_grid)

  

#end of program

Add a comment
Know the answer?
Add Answer to:
Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in Python This program: 1. Correct the compute_cells_state function which receives as parameter an array...

    Write in Python This program: 1. Correct the compute_cells_state function which receives as parameter an array of 2-dimensional booleans. The table is indexed online then in column.    A cell is called "alive" if the Boolean is true otherwise it is said that it is "dead".    Each cell to 8 "neighbors" (the 4 adjacent cells and the 4 cells diagonally)    The function modifies each cell according to the following rule:      - if the cell has 2 or 3 living neighbors she...

  • C++ there is an issue with my if/else statements, I have tried several different ways to...

    C++ there is an issue with my if/else statements, I have tried several different ways to make it match the instructions but each time i get different errors. Need help geting this to match the instructions peoperly. *******************instructions***************************** Function: nextGeneration This function has no parameters. This function returns an int. The purpose of this function is to modify the grid to represent the next generation. Here's how you are supposed to do that for this assignment: Set each element of...

  • JAVA. Threading the Game of Life Write a Java program to implement the Game of Life,...

    JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? I need c++ codes. Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by randomly setting 20 of...

  • 1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. L...

    1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. Live cells are shown in grey and dead cells are shown in white. The rules are as follows Two cells are considered to be neighbours if they share an edge. A dead cell comes to life it if has exactly 2 living neighbours. .A live cell remains alive if it has 2 or 3 living...

  • Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT...

    Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT DESCRIBED BELOW). Title: Game of Life Description In this assignment, you will code a simulation called "Game of Life". It is a very famous 'game' and the formed the basis of an entire field of simulation models called "cellular automata". Before continuing reading this assignment, I suggest you read a bit more about Game of Life at: https://en.wikipedia.org/wiki/Conway's_Game_of_Life or http://www.math.com/students/wonders/life/life.html (the latter also has...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's...

    Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's Game of Life (for details and ex amples, see https://en.vikipedia.org/wiki/Convay's.Game of Life). The basic gam ts played on each generation, each cell changes according to the following instructions: nal grid of square cells, each of which is either alive or dead. With 1. Any dead cell with exactly three live neighbors (out of its eight nearest neighbors) comes to life [reproduction]. 2. Any live...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT