대학생 코딩 과제 대행 java, python, oracle 스물 세 번째
https://open.kakao.com/o/s3aMpbA
요즘 회사다니면서 과제 의뢰가 들어와도 못했었는데,
그래도 짬이 나서 오랜만에 도전..!
이번 의뢰자도 외국대학교 인데 어디 학교인지는 모르겠다.
먼저 요구사항이다.
python으로 생물학적인(?) 세포를 이차원배열에서 지지고 볶고 하는 과제이다.
graphics.py 사용해야하는게 필수 조건이다.







실행영상이다. 코드는 지저분해서.. 올리지 말까하다가 올린다.

요즘 회사다니면서 과제 의뢰가 들어와도 못했었는데,
그래도 짬이 나서 오랜만에 도전..!
이번 의뢰자도 외국대학교 인데 어디 학교인지는 모르겠다.
먼저 요구사항이다.
python으로 생물학적인(?) 세포를 이차원배열에서 지지고 볶고 하는 과제이다.
graphics.py 사용해야하는게 필수 조건이다.







실행영상이다. 코드는 지저분해서.. 올리지 말까하다가 올린다.

from graphics import * import random # makeControl()function creates Control Panel Graphics window as specifieddef makeControl(): window = GraphWin("Control Panel", 300, 460) # GENERATIONS meesage box = Rectangle(Point(0, 20), Point(300, 40)) box.draw(window) box.setFill("black") messages = Text(Point(150, 30), "GENERATIONS") messages.draw(window) messages.setTextColor("white") # START,RANDOM, MANUAL, RESTART, PAUSE, QUIT button START = Rectangle(Point(30, 50), Point(130, 80)) START.draw(window) START.setFill("green") Smessages = Text(Point(80, 65), "START") Smessages.draw(window) RANDOM = Rectangle(Point(30, 120), Point(130, 150)) RANDOM.draw(window) RANDOM.setFill("light blue") Rmessages = Text(Point(80, 135), "RANDOM") Rmessages.draw(window) PAUSE = Rectangle(Point(170, 50), Point(270, 80)) PAUSE.draw(window) PAUSE.setFill("orange") Pmessages = Text(Point(220, 65), "PAUSE") Pmessages.draw(window) MANUAL = Rectangle(Point(30, 85), Point(130, 115)) MANUAL.draw(window) MANUAL.setFill("dark blue") Mmessages = Text(Point(80, 100), "MANUAL") Mmessages.draw(window) RESTART = Rectangle(Point(170, 85), Point(270, 115)) RESTART.draw(window) RESTART.setFill("yellow") REmessages = Text(Point(220, 100), "RESTART") REmessages.draw(window) QUIT = Rectangle(Point(170, 120), Point(270, 150)) QUIT.draw(window) QUIT.setFill("red") Qmessages = Text(Point(220, 135), "QUIT") Qmessages.draw(window) # Living Cells,Generation Num text livingcells = Text(Point(80, 180), "Living Cells:") livingcells.draw(window) generationnum = Text(Point(80, 220), "Generation Num:") generationnum.draw(window) countcell = Text(Point(200, 180), "0") countcell.draw(window) gennumber = Text(Point(200, 220), "0") gennumber.draw(window) # Highest Living Cell Count Text highestNumberLiving = Text(Point(80, 260), "HighestNumberLiving") highestNumberLiving.draw(window) # Lowest Living Cell Count Text lowestNumberLiving = Text(Point(80, 300), "LowestNumberLiving") lowestNumberLiving.draw(window) highestNumber = Text(Point(200, 260), "0") highestNumber.draw(window) lowestNumber = Text(Point(200, 300), "0") lowestNumber.draw(window) # RedAffect, BlueAffect, Tie Breaker Count Text redAffect = Text(Point(80, 340), "RedAffectCount") blueAffect = Text(Point(80, 380), "BlueAffectCount") tieBreaker = Text(Point(80, 420), "TieBreakerCount") redAffect.draw(window) blueAffect.draw(window) tieBreaker.draw(window) redCount = Text(Point(200, 340), "0") blueCount = Text(Point(200, 380), "0") tieCount = Text(Point(200, 420), "0") redCount.draw(window) blueCount.draw(window) tieCount.draw(window) stability = Text(Point(150, 435), "") stability.draw(window) # makeControl()function returns return window, START, RANDOM, MANUAL, RESTART, PAUSE, QUIT, countcell, gennumber, highestNumber, lowestNumber, redCount, blueCount, tieCount, stability # makeGrid()function creates Generations Grid Graphics window as specifieddef makeGrid(): win2 = GraphWin("Generations Grid", 400, 500) win2.setBackground("light grey") gridList = [] # Create and draw List of 10 x 10 Rectangle cells for j in range(0, 40): for i in range(0, 40): grid = Rectangle(Point(i * 10, j * 10), Point(i * 10 + 10, j * 10 + 10)) # edge if i == 0 or i == 39: grid.setFill("black") grid.setOutline("black") elif j == 0 or j == 39: grid.setFill("black") grid.setOutline("black") else: grid.setFill("white") grid.draw(win2) gridList.append(grid) selectFinshButton = Rectangle(Point(0, 400), Point(400, 500)).draw(win2) selectFinshButton.setFill("yellow") stopMessage = Text(Point(200, 450), "I want to stop Selecting cell") stopMessage.draw(win2) # makeGrid()function returns the List of Rectangle cells return gridList, win2 # getStatistics() function return Highest Living Cell Count, Lowest Living Cell Countdef getStatistics(tempList): maxVal = tempList[0] minVal = tempList[0] returnList = [] for i in tempList: if i < minVal: minVal = i if i > maxVal: maxVal = i returnList.append(maxVal) returnList.append(minVal) return returnList, maxVal, minVal # makeGridToWhite() function sets the fill color for every cell in the griddef makeGridToWhite(gridList, window2): for grid in gridList: if grid.config["fill"] != "black": grid.setFill("white") return gridList, window2 # manualGridSetting() function sets the fill color for pointed cell in the griddef manualGridSetting(gridList, point): xPoint = int(point.getX() // 10) yPoint = int(point.getY() // 10) if 1 <= xPoint <= 38 and 1 <= yPoint <= 38: gridList[yPoint * 40 + xPoint].setFill("red") return gridList # life() function sets the fill color for each cell in the griddef life(gridList): # life()function uses loop to assign an initial alive/dead status to the cells in the grid for grid in gridList: if grid.config["fill"] != "black": red = random.uniform(0, 100) blue = random.uniform(0, 100) # get 18% living cells if red < 18: grid.setFill("red") elif blue < 10: grid.setFill("blue") else: grid.setFill("white") # life() function returns return gridList # cycle() function counts redAffect, blueAffect, tie Breakerdef cycle(gridList): stateList = [] redAffectCnt = 0 blueAffectCnt = 0 tieBreakerCnt = 0 for i in range(0, 1600): stateList.append("N") # define living cells and count living cells for index, grid in enumerate(gridList): redLivingCellCount = 0 blueLivingCellCount = 0 if grid.config["fill"] != "black": neighbors = getNeighbor(index, gridList) for n in neighbors: if gridList[n].config["fill"] == "red": redLivingCellCount += 1 elif gridList[n].config["fill"] == "blue": blueLivingCellCount += 1 if grid.config["fill"] == "red": if redLivingCellCount < 2: stateList[index] = "dead" elif redLivingCellCount > 3: stateList[index] = "dead" elif redLivingCellCount == 3: redAffectCnt += 1 stateList[index] = "aliveRed" else: stateList[index] = "aliveRed" elif grid.config["fill"] == "blue": if blueLivingCellCount < 2: stateList[index] = "dead" elif blueLivingCellCount > 3: stateList[index] = "dead" elif blueLivingCellCount == 3: blueAffectCnt += 1 stateList[index] = "aliveBlue" else: stateList[index] = "aliveBlue" elif grid.config["fill"] == "white": if redLivingCellCount == 3: stateList[index] = "aliveRed" tieBreakerCnt += 1 elif blueLivingCellCount == 3: stateList[index] = "aliveBlue" tieBreakerCnt += 1 totalLivingCellCount = 0 for index, state in enumerate(stateList): grid = gridList[index] if state == "dead": grid.setFill("white") elif state == "aliveBlue": grid.setFill("blue") totalLivingCellCount += 1 elif state == "aliveRed": grid.setFill("red") totalLivingCellCount += 1 return gridList, totalLivingCellCount, redAffectCnt, blueAffectCnt, tieBreakerCnt # getNeighbor function define Neighbors for each celldef getNeighbor(target, gridList): neighborList = [] size = 40 max = 1600 index = target if index - 1 >= 0 and (index - 1) // size == index // size: l = index - 1 neighborList.append(l) if index + 1 < max and (index + 1) // size == index // size: r = index + 1 neighborList.append(r) if index - size >= 0: top = index - size neighborList.append(top) if top - 1 >= 0 and (top - 1) // size == (index // size) - 1: tl = top - 1 neighborList.append(tl) if top + 1 < max and (top + 1) // size == (index // size) - 1: tr = top + 1 neighborList.append(tr) if index + size < max: bottom = index + size neighborList.append(bottom) if bottom - 1 >= 0 and (bottom - 1) // size == (index // size) + 1: bl = bottom - 1 neighborList.append(bl) if bottom + 1 < max and (bottom + 1) // size == (index // size) + 1: br = bottom + 1 neighborList.append(br) return neighborList # Is point inside rectangle def control(point, rectangle): ll = rectangle.getP1() # assume p1 is ll (lower left) ur = rectangle.getP2() # assume p2 is ur (upper right) return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY() < ur.getY() def main(): # main() function calls makeControl() and saves returned objects to variables window, START, RANDOM, MANUAL, RESTART, PAUSE, QUIT, countcell, gennumber, highestNumber, lowestNumber, redCount, blueCount, tieCount, stability = makeControl() point = window.getMouse() simulation_state = "NOT RUNNING" simulation = "NOT STARTED" generationNumber, totalLivingCellCount, highest, lowest, stopCondition, minVal = 0, 0, 0, 0, 0, 1 redAffectCntSum, blueAffectCntSum, tieBreakerCntSum, redAffectCnt, blueAffectCnt, tieBreakerCnt = 0, 0, 0, 0, 0, 0 window2 = '' # gridPoint = '' choice = True highestAndLowest = [] while True: if point is not None: # start window if START control clicked if control(point, START): if simulation == "NOT STARTED" and simulation_state == "NOT RUNNING": stopCondition = 0 redAffectCntSum = 0 blueAffectCntSum = 0 tieBreakerCntSum = 0 redCount.setText(redAffectCntSum) blueCount.setText(blueAffectCntSum) tieCount.setText(tieBreakerCntSum) gridList, window2 = makeGrid() gridList = life(gridList) simulation_state = "RUNNING" simulation = "STARTED" elif simulation == "STARTED" and simulation_state == "NOT RUNNING": simulation_state = "RUNNING" # reset window if RESET control clicked # rename RESET to RANDOM if control(point, RANDOM): redAffectCntSum = 0 blueAffectCntSum = 0 tieBreakerCntSum = 0 redCount.setText(redAffectCntSum) blueCount.setText(blueAffectCntSum) tieCount.setText(tieBreakerCntSum) stopCondition = 0 if simulation == "STARTED": highestAndLowest = [] highestNumber.setText("0") lowestNumber.setText("0") life(gridList) generationNumber = 0 totalLivingCellCount = 0 countcell.setText(totalLivingCellCount) gennumber.setText(generationNumber) stability.setText("") # pause window if PAUSE control clicked if control(point, PAUSE): stopCondition = 0 simulation_state = "NOT RUNNING" # reset the grid if control(point, RESTART): redAffectCntSum = 0 blueAffectCntSum = 0 tieBreakerCntSum = 0 redCount.setText(redAffectCntSum) blueCount.setText(blueAffectCntSum) tieCount.setText(tieBreakerCntSum) stopCondition = 0 highestAndLowest = [] highestNumber.setText("0") lowestNumber.setText("0") simulation_state = "NOT RUNNING" generationNumber = 0 gennumber.setText(generationNumber) cc = 0 countcell.setText(cc) gridList, window2 = makeGridToWhite(gridList, window2) stability.setText("") # pause and user can select cell that would die or live if control(point, MANUAL): stopCondition = 0 if simulation == "STARTED" and simulation_state == "RUNNING" or simulation == "STARTED" and simulation_state == "NOT RUNNING": redAffectCntSum = 0 blueAffectCntSum = 0 tieBreakerCntSum = 0 redCount.setText(redAffectCntSum) blueCount.setText(blueAffectCntSum) tieCount.setText(tieBreakerCntSum) highestAndLowest = [] highestNumber.setText("0") lowestNumber.setText("0") simulation_state = "NOT RUNNING" generationNumber = 0 gennumber.setText(generationNumber) cc = 0 countcell.setText(cc) gridList, window2 = makeGridToWhite(gridList, window2) stability.setText("") while choice: gridPoint = window2.getMouse() if gridPoint.getY() > 400: break gridList = manualGridSetting(gridList, gridPoint) cc += 1 countcell.setText(cc) window2.checkMouse() # closes all windowsif QUIT control clicked if control(point, QUIT): window.close() return if simulation_state == "RUNNING": gridList, totalLivingCellCount, redAffectCnt, blueAffectCnt, tieBreakerCnt = cycle(gridList) redAffectCntSum += redAffectCnt blueAffectCntSum += blueAffectCnt tieBreakerCntSum += tieBreakerCnt highestAndLowest.append(totalLivingCellCount) highestAndLowest, highest, lowest = getStatistics(highestAndLowest) generationNumber += 1 gennumber.setText(generationNumber) countcell.setText(totalLivingCellCount) highestNumber.setText(highest) lowestNumber.setText(lowest) redCount.setText(redAffectCntSum) blueCount.setText(blueAffectCntSum) tieCount.setText(tieBreakerCntSum) if minVal == lowest: stopCondition += 1 if stopCondition > 320: simulation_state = "NOT RUNNING" stability.setText("Stability has been achieved") minVal = lowest point = window.checkMouse() return if __name__ == "__main__": main()
댓글
댓글 쓰기