我最近在我的comp sci课程中制作了康威的生活游戏。几天前我们的班级结束了,我想让我的游戏看起来更好一些。目前这就是我用0和X打印板的方法,并且每次迭代都会打印一个新板,这在python IDLE中要么太慢而在命令提示符下太快甚至看不到发生了什么。如何在一块板上显示它,该板重复使用相同的板来打印每次迭代,而不是为每次迭代创建一个新板。关于代码本身的任何建议都表示赞赏。
def main():
rows = input("Please enter the number of rows: ") # asks the user for number of rows
try :
rows = int(rows) # checks for a valid input
except ValueError:
rows = validChoice(rows)
columns = input("Please enter the number of columns: ") # asks the user for number of columns
try :
columns = int(columns) # checks for valid column input
except ValueError:
columns = validChoice(columns)
print ("")
board = (makeBoard(rows, columns)) # calls makeboard function and saves in board variable as list
print ("Indexing starts at 0 ")
print ("")
getMove = move(board, rows, columns) # asks the user for which cells to turn on initially
print ("")
while getMove != "q" : # while the user hasn't selected it loops through for the user to turn on multiple cells
getMove = move(board, rows, columns)
print ("")
# Asks for the number of iterations to run
iterations = input("How many iterations should I run? ")
try:
iterations = int(iterations) # Asks for a valid iterations input if needed.
except ValueError:
iterations = validChoice(iterations)
print ("")
print ("Starting Board: ")
print ("")
printBoard(board) #Prints the starting board
print ("")
iteration = 1 # iteration starts at 1
while iterations > 0 : # Uses while loop to run through the iterations
print ("Iteration", iteration, ":")
print ("")
iteration = iteration + 1 # adds iteration everytime
board = nextIteration(board) # runs board through iteration to see which cells will live and which will die
board = fixBoard(board) # corrects the board up to 0s and 1s
printBoard(board) # prints board
print ("")
iterations = iterations - 1 # subtracts 1 iteration each time the program runs through each iteration
# turns 2 into a 0 because those cells died
# turns 3 into a 1 because those cells became alive
# returns boad with just 0s and 1s
def fixBoard(board):
for i in range(len(board)): # Uses for loop to go through each list inside list
for x in range(len(board[0])): # Uses for loop to go through each index inside the list
if board[i][x] == 2:
board[i][x] = 0
if board[i][x] == 3:
board[i][x] = 1
return (board)
# This function runs the game through each iteration
# Converts cells that will become alive next round to 3
# Converts cells that will become dead next round to 2
# Only checks specific cells for for cells around the edge of the board
# Uses nested for loops to go through each index
# Uses nested if statements to check which cells around it are alive
# Input board with 0s and 1s returns board with 0s 1s 2s and 3s
def nextIteration(board):
rose = len(board)
cols = len(board[0])
for x in range(rose):
for i in range(cols):
counter = 0
if (x == 0) and (i == 0):
if (board[x][i+1] == 1) or (board[x][i+1] ==2):
counter = counter + 1
if (board[x+1][i+1] == 1) or (board[x+1][i+1] ==2):
counter = counter + 1
if (board[x+1][i] == 1) or (board[x+1][i] ==2):
counter = counter + 1
if (board[x][i]==1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (x == 0) and (0 < i < (len(board[0])-1)):
if (board[x][i+1] == 1) or (board[x][i+1] ==2):
counter = counter +1
if (board[x+1][i+1] == 1) or (board[x+1][i+1] ==2):
counter = counter +1
if (board[x+1][i] == 1) or (board[x+1][i] ==2):
counter = counter +1
if (board[x+1][i-1] == 1) or (board[x+1][i-1] ==2):
counter = counter +1
if (board[x][i-1] == 1) or (board[x][i-1] ==2):
counter = counter +1
if (board[x][i] == 1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
elif (counter > 3):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (x == 0) and (i == (len(board[0])-1)):
if (board[x][i-1] == 1) or (board[x][i-1] ==2):
counter = counter + 1
if (board[x+1][i-1] == 1) or (board[x+1][i-1] ==2):
counter = counter + 1
if (board[x+1][i] == 1) or (board[x+1][i] ==2):
counter = counter + 1
if (board[x][i]==1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (0 < x < (len(board)-1)) and (i == 0):
if (board[x-1][i] == 1) or (board[x-1][i] ==2):
counter = counter + 1
if (board[x-1][i+1] == 1) or (board[x-1][i+1] ==2):
counter = counter + 1
if (board[x][i+1] == 1) or (board[x][i+1] ==2):
counter = counter + 1
if (board[x+1][i+1] == 1) or (board[x+1][i+1] ==2):
counter = counter + 1
if (board[x+1][i] == 1) or (board[x+1][i] ==2):
counter = counter + 1
if (board[x][i] == 1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
elif (counter > 3):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (0 < x < (len(board)-1)) and (0 < i < (len(board[0])-1)):
if (board[x-1][i-1] == 1) or (board[x-1][i-1] ==2):
counter = counter +1
if (board[x-1][i] == 1) or (board[x-1][i] ==2):
counter = counter +1
if (board[x-1][i+1] == 1) or (board[x-1][i+1] ==2):
counter = counter +1
if (board[x][i+1] == 1) or (board[x][i+1] ==2):
counter = counter +1
if (board[x+1][i+1] == 1) or (board[x+1][i+1] ==2):
counter = counter +1
if (board[x+1][i] == 1) or (board[x+1][i] ==2):
counter = counter +1
if (board[x+1][i-1] == 1) or (board[x+1][i-1] ==2):
counter = counter +1
if (board[x][i-1] == 1) or (board[x][i-1] ==2):
counter = counter +1
if (board[x][i] == 1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
elif (counter > 3):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (0 < x < (len(board)-1)) and (i == (len(board[0])-1)):
if (board[x-1][i] == 1) or (board[x-1][i] ==2):
counter = counter + 1
if (board[x-1][i-1] == 1) or (board[x-1][i-1] ==2):
counter = counter + 1
if (board[x][i-1] == 1) or (board[x][i-1] ==2):
counter = counter + 1
if (board[x+1][i-1] == 1) or (board[x+1][i-1] ==2):
counter = counter + 1
if (board[x+1][i] == 1) or (board[x+1][i] ==2):
counter = counter + 1
if (board[x][i] == 1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
elif (counter > 3):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (x == (len(board)-1)) and (i == 0):
if (board[x-1][i] == 1) or (board[x-1][i] ==2):
counter = counter + 1
if (board[x-1][i+1] == 1) or (board[x-1][i+1] ==2):
counter = counter + 1
if (board[x][i+1] == 1) or (board[x][i+1] ==2):
counter = counter + 1
if (board[x][i]==1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if ( x == (len(board)-1)) and ( i == (len(board[0])-1)):
if (board[x-1][i] == 1) or (board[x-1][i] ==2):
counter = counter + 1
if (board[x-1][i-1] == 1) or (board[x-1][i-1] ==2):
counter = counter + 1
if (board[x][i-1] == 1) or (board[x][i-1] ==2):
counter = counter + 1
if (board[x][i]==1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
if (x == (len(board)-1)) and (0 < i < (len(board[0])-1)):
if (board[x][i-1] == 1) or (board[x][i-1] ==2):
counter = counter + 1
if (board[x-1][i-1] == 1) or (board[x-1][i-1] ==2):
counter = counter + 1
if (board[x-1][i] == 1) or (board[x-1][i] ==2):
counter = counter + 1
if (board[x-1][i+1] == 1) or (board[x-1][i+1] ==2):
counter = counter + 1
if (board[x][i+1] == 1) or (board[x][i+1] ==2):
counter = counter + 1
if (board[x][i] == 1):
if (counter == 2) or (counter == 3):
board[x][i] = board[x][i]
elif (counter < 2):
board[x][i] = 2
elif (counter > 3):
board[x][i] = 2
if (board[x][i] == 0):
if (counter == 3):
board[x][i] = 3
return (board)
# Asks the user for while cells they want to turn on
# Input board return Board
# Returns q if user doesn't want to turn on any more cells
def move(board, rows, columns):
#Asks for the row
row = input("Please enter the row of a cell to turn on or q to exit: ")
if row == "q" :
return ("q") # Checks for a valid choice of input by the user
else:
try :
row = int(row)
except ValueError:
row = validChoice(row)
while row >= rows :
row = validChoice(row)
#Asks for the column
col = input("Please enter a column for that cell: ")
try :
col = int(col)
except ValueError: # Checks for valid input
col = validChoice(col)
while col >= columns:
col = validChoice(col)
board[row][col] = 1
return (board)
# Takes the board and prints it out to the user
# Leaves 0s and 0s and 1s to Xs
def printBoard(board):
for i in range(len(board)): # uses for loop to go through each index
for x in range(len(board[0])):
if (board[i][x]) == 0:
print (0, end="")
elif (board[i][x]) == 1:
print ('X', end="")
print ("")
# Makes the board by using append method and lists
def makeBoard(rows, columns):
list2 = [] # this is the main list
column = columns
while rows > 0:
list1 = [] # this is each list that will be inside the list
while column > 0:
list1.append(0)
column = column - 1
list2.append(list1)
rows = rows - 1
column = columns
return (list2)
# Checks for valid user Input
# If letters keeps asking until a number is input
def validChoice(number):
number = input("Please enter a valid choice: ")
try :
number = int(number)
except ValueError:
number = validChoice(number)
return (number)
main()