I'm working on the following:
Write a python program:
1. Write the definition of the function displaySubMenu that displays the following menu; this function doesn't collect the user's input; only display the following menu.:
[S]top – Press 'S' to stop.
2. Write the definition of the function setNextGenList that creates a pattern of next generation (tempGen) based on the current generation (currentGen); modify the tempGen based on the currentGen by applying the rules of Game of Life.
Conway's Game of Life Rules:
- The neighbors of a given cell are the cells that touch it
vertically, horizontally, or diagonally.
- Any live cell with fewer than two live neighbours dies, as if
caused by underpopulation.
- Any live cell with two or three live neighbours lives on to the
next generation.
- Any live cell with more than three live neighbours dies, as if by
overpopulation.
- Any dead cell with exactly three live neighbours becomes a live
cell, as if by reproduction.
3. Create a loop to generate and next pattern automatically.
When executing your program, the following should happen:
a. Print the menu using the displayMenu
function.
b. Initialize the tempGen using the
setZeroList function.
c. Set the ‘U’ pattern in the tempGen using the
setInitialPatternList function.
d. Copy the tempGen to the
currentGen using the copyList
function.
e. Print the currentGen using the
displayList function.
f. When the user presses ‘P’, loop the following steps:
1). Print the menu using the displaySubMenu
function.
2). Generate a next pattern using the
setNextGenList function.
3). Copy the tempGen to the
currentGen using the copyList
function.
4). Print the currentGen using the
displayList function.
g. When the user presses ‘S’, it will terminate the loop in step f,
and reset the screen to the 'U' pattern.
h. When the user presses ‘Q’, it will terminate the program.
Examples of Outputs:
![Press P to play. [P]lay [Q]uit Press Q to exit. ө000000000000 0000OOOOOOO0000OOOOOOOOO0000OOO ө00000000о0о0000OOOOOOOө000](http://img.homeworklib.com/questions/eb6e3cc0-d1ca-11ea-ade5-ab34cf1eb046.png?x-oss-process=image/resize,w_560)
![Press S to stop [S]top ө0000 eө000OOOOOOO0000ODOOO00000 e000000e ө0000 eө000OOOOOOO0000ODOOO00000 e000000e ө0000 eө000OOOOO](http://img.homeworklib.com/questions/ebe2c660-d1ca-11ea-9943-a97039747155.png?x-oss-process=image/resize,w_560)
![Press S to stop. [S]top Ө00000000000000OOOOOOO000000OOOOOO000000OOOOOOӨ00000OOOOOO0 Ө00000000000000OOOOOOO000000OOOOOO00000](http://img.homeworklib.com/questions/ec4aa620-d1ca-11ea-bde3-6b8b6984bf7e.png?x-oss-process=image/resize,w_560)
I'm stuck on creating a break from the loop using the 'S' key specifically without using input() as that would wait for the user to type it in. I'm trying to make it so that the loop continues until the user presses the specified key. This is what I have so far:
import random
import os
import time
import msvcrt
MAX_ROW = 30 #total number of rows
MAX_COL = 60 #total number of cols
currentGen = [[0]*MAX_COL for i in range(MAX_ROW)] #values to 0
using row*col
tempGen = [[0]*MAX_COL for i in range(MAX_ROW)] #values to 0 using
row*col
def displayMenu(): #menu at top
print("[P]lay - Press 'P' to play.\n[Q]uit -
Press 'Q' to exit.\n")
def setZeroList(tempGen): #initializes list to 0
for rows in range(MAX_ROW):
for cols in
range(MAX_COL):
tempGen[rows][cols] = 0
def setInitialPatternList(tempGen): #creates random U
shape
row=random.randint(0,MAX_ROW-6)
col=random.randint(0,MAX_COL-6)
for iRow in range(row,row+6):
tempGen[iRow][col] =
tempGen[iRow][col+6] = 1 #left and right col of ones
for iRow in range(col,col+7):
tempGen[row+5][iRow] = 1
#bottom row of ones
def copyList(currentGen,tempGen): #copies tempList to
currentList
for iRow in range(MAX_ROW):
for jCol in
range(MAX_COL):
currentGen[iRow][jCol] = tempGen[iRow][jCol]
def displayList(currentGen): #displays within limits with no
spaces
for iRow in range(MAX_ROW):
for jCol in
range(MAX_COL):
print(currentGen[iRow][jCol], end='')
print()
def displaySubMenu():
print("[S]top - Press 'S' to stop\n")
def setNextGenList(nextGen,currentGen):
RowIndex=[0,0,1,1,1,-1,-1,-1]
ColIndex=[1,-1,-1,0,1,-1,0,1]
copyList(currentGen,nextGen)
neighborsCount = 0
for iRow in range(MAX_ROW):
for jCol in
range(MAX_COL):
neighborsCount=0
for kNeighbors in range(8):
neighborRow = iRow + RowIndex[kNeighbors]
neighborCol = jCol + ColIndex[kNeighbors]
if ((neighborRow >= 0) and (neighborRow < MAX_ROW)) and
((neighborCol >= 0) and (neighborCol < MAX_COL)):
if currentGen[neighborRow][neighborCol]==1:
neighborsCount+=1
#Game of Life Rules:
if currentGen[iRow][jCol]==1: #live
if
neighborsCount<2:
#die by underpopulation
nextGen[iRow][jCol]=0
elif
neighborsCount<=3:
#survive
nextGen[iRow][jCol]=1
else:
#die by overpopulation
nextGen[iRow][jCol]=0
else:
#dead
if
neighborsCount==3:
#live by reproduction
nextGen[iRow][jCol]=1
copyList(currentGen,nextGen)
def main():
firstResponse = 'P'
while True:
os.system('cls')
displayMenu()
setZeroList(tempGen)
setInitialPatternList(tempGen)
copyList(currentGen,tempGen)
displayList(currentGen)
firstResponse=input().upper()
while True and
firstResponse=='P':
os.system('cls')
displaySubMenu()
setNextGenList(tempGen,currentGen)
displayList(currentGen)
time.sleep(0.5)
if (msvcrt.kbhit()):
stopKey = ord(msvcrt.getch())
if stopKey == 83:
break
if
firstResponse=='Q':
exit()
main()
# do comment if any problem arises
# Code
#Your code has only one bug which is highlighted below
import random
import os
import time
import msvcrt
MAX_ROW = 30 # total number of rows
MAX_COL = 60 # total number of cols
currentGen = [[0]*MAX_COL for i in range(MAX_ROW)] # values to 0
using row*col
tempGen = [[0]*MAX_COL for i in range(MAX_ROW)] # values to 0 using
row*col
def displayMenu(): # menu at top
print("[P]lay - Press 'P' to play.\n[Q]uit - Press 'Q' to
exit.\n")
def setZeroList(tempGen): # initializes list to 0
for rows in range(MAX_ROW):
for cols in range(MAX_COL):
tempGen[rows][cols] = 0
def setInitialPatternList(tempGen): # creates random U shape
row = random.randint(0, MAX_ROW-6)
col = random.randint(0, MAX_COL-6)
for iRow in range(row, row+6):
# left and right col of ones
tempGen[iRow][col] = tempGen[iRow][col+6] = 1
for iRow in range(col, col+7):
tempGen[row+5][iRow] = 1 # bottom row of ones
def copyList(currentGen, tempGen): # copies tempList to
currentList
for iRow in range(MAX_ROW):
for jCol in range(MAX_COL):
currentGen[iRow][jCol] = tempGen[iRow][jCol]
def displayList(currentGen): # displays within limits with no
spaces
for iRow in range(MAX_ROW):
for jCol in range(MAX_COL):
print(currentGen[iRow][jCol], end='')
print()
def displaySubMenu():
print("[S]top - Press 'S' to stop\n")
def setNextGenList(nextGen, currentGen):
RowIndex = [0, 0, 1, 1, 1, -1, -1, -1]
ColIndex = [1, -1, -1, 0, 1, -1, 0, 1]
copyList(currentGen, nextGen)
neighborsCount = 0
for iRow in range(MAX_ROW):
for jCol in range(MAX_COL):
neighborsCount = 0
for kNeighbors in range(8):
neighborRow = iRow + RowIndex[kNeighbors]
neighborCol = jCol + ColIndex[kNeighbors]
if ((neighborRow >= 0) and (neighborRow < MAX_ROW)) and
((neighborCol >= 0) and (neighborCol < MAX_COL)):
if currentGen[neighborRow][neighborCol] == 1:
neighborsCount += 1
# Game of Life Rules:
if currentGen[iRow][jCol] == 1: # live
if neighborsCount < 2: # die by underpopulation
nextGen[iRow][jCol] = 0
elif neighborsCount <= 3: # survive
nextGen[iRow][jCol] = 1
else: # die by overpopulation
nextGen[iRow][jCol] = 0
else: # dead
if neighborsCount == 3: # live by reproduction
nextGen[iRow][jCol] = 1
copyList(currentGen, nextGen)
def main():
firstResponse = 'P'
while True:
os.system('cls')
displayMenu()
setZeroList(tempGen)
setInitialPatternList(tempGen)
copyList(currentGen, tempGen)
displayList(currentGen)
firstResponse = input().upper()
while True and firstResponse == 'P':
time.sleep(.5)
#When this sleep called before msvrt.kbhit() user enters s within this .5 s time due to which your program is not working
os.system('cls')
displaySubMenu()
setNextGenList(tempGen, currentGen)
displayList(currentGen)
if (msvcrt.kbhit()):
stopKey = ord(msvcrt.getch())
if stopKey == 83 or stopKey == 115:
break
if firstResponse == 'Q':
exit()
main()
Output:
![[P]lay - Press P to play. [Q]uit - Press Q to exit. 000000000000000000000000000000000000000000000000000000000000 00000000](http://img.homeworklib.com/questions/ece3eb60-d1ca-11ea-a727-737c9954606d.png?x-oss-process=image/resize,w_560)
I'm working on the following: Write a python program: 1. Write the definition of the function...
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...
Python Program Only: Write the function definition only of the "get(self, index)" function. Plug your method in the code file shared with you and test it in the main to get the element at index 3 of mylist2 . class Node: def __init__(self, e): self.element = e self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def addFirst(self, e): newNode = Node(e) newNode.next = self.head self.head = newNode self.size = self.size + 1...
Need help with a 2D list - I'm almost there! #Write a function called check_winner which takes #as input a 2D list. It should return "X" if there are four #adjacent "X" values anywhere in the list (row, column, #diagonal); "O" if there are four adjacent "O" values #anywhere in the list; and None if there are neither. # #Here are the ways Connect-4 is different from tic-tac-toe: # # - Connect-4 is played with 6 rows and 7 columns...
Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place...
Write a program that defines a Print Job structure as follows: 1) An integer job Id 2) A string user name (maximum 25 characters) 3) An integer tray (tray will hold the tray number 1 for 8 1/2 by 11 paper, number 2 for 8 1/2 by 11 paper, and number 3 for 8 1/2 by 14 paper) 4) An integer for paper size (this will hold a percentage: 100% is normal, 150% is 1.5 times the size) 5) A...
USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku game and show its result in detail. This is an application program of using 2-dimensional arrays or lists. Each array or list is a game board of 9 x 9. Your program must check the 9 x 9 game board, and report all the problems among 9 rows, 9 columns, and 9 squares. As you can see, you must pre-load the following four games...
I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...
The following function computes by summing the Taylor series
expansion to n terms. Write a program to print a table of using both this function and
the exp() function from the math library, for x = 0 to 1 in steps
of 0.1. The program should ask the user what value of n to use.
(PLEASE WRITE IN PYTHON)
def taylor(x, n):
sum = 1
term = 1
for i in range(1, n):
term = term * x / i...
Write a C program that does the following: Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square,Diamond (with selected height and selected symbol), or Quits if user entered Q.Apart from these 2 other shapes, add a new shape of your choice for any related character. Program then prompts the user to enter a number and...
Please write a Python program to check a tic-tac-toe game and show its winning result in detail. This is an application program of a 3-dimensional list or array. Your complete test run output must look as follows. This test really checks to make sure your program is performing perfectly to check every possible winning situation for X and O. GAME 0 is as follows: OOO OOO OOO O won by row 1 O won by row 2 O won by...