python / 라이프게임 구현

Geewon Kim·2024년 1월 17일

Python

목록 보기
4/7

1차원 라이프게임

양옆의 숫자가 동일하면 가운데에 있는 세포의 상태가 변경되도록 구현

코드

def nextGen(curList):
    nextList = curList.copy()

    for i in range(len(curList)):
        pre_i = i-1
        next_i = i+1

        if next_i == len(curList):
            next_i = 0

        if currentList[pre_i] == currentList[next_i]:
            nextList[i] = currentList[i]

        else:
            if curList[i] == '0':
                nextList[i] = '1'
            else:
                nextList[i] = '0'

    return nextList



start = input("초기패턴입력 :")
currentList = list(start)


while True:
    print("현재상태 :")
    print(currentList)

    userInput = input("다음세대 y/n :")
    if userInput == "y":
        currentList = nextGen(currentList)

    else:
        print("The End")
        break

결과

profile
내 지식의 외장하드

0개의 댓글