python/리스트

미니 윷놀이

june__Park 2021. 2. 28. 00:08
# 미니 윷놀이
# 1. 플레이어는 p1과 p2 2명이다.
# 2. 번갈아가며 1~3 사이의 숫자를 입력해 이동한다.
# 3. 이동하다가 다음 플레이어와 같은 위치에 놓이게 되면,
#    상대 플레이어는 잡히게 되어 원점으로 되돌아간다.
# 4. 먼저 3바퀴를 돌면 이긴다.

#   [p1]1~3 입력 : 2
#   1 2 3 4 5 6 7 8
#   0 1 0 0 0 0 0 0
#   0 0 0 2 0 0 0 0
#
#   [p1]이 p2를 잡았다!
#   1 2 3 4 5 6 7 8
#   0 0 0 1 0 0 0 0
#   2 0 0 0 0 0 0 0


game = [0, 1, 2, 3, 4, 5, 6, 7, 8]
p1 = [0, 0, 0, 0, 0, 0, 0, 0, 0]
p2 = [0, 0, 0, 0, 0, 0, 0, 0, 0]

turn = 0

idx1 = 0
idx2 = 0

p1[idx1] = 1
p2[idx2] = 2

count1 = 0
count2 = 0

while True:
    if count1 == 3:
        print("win: [p1]")
        break
    elif count2 == 3:
        print("win: [p2]")
        break
    
    print(game)
    print(p1)
    print(p2)

    if turn%2 == 0:
        mov = int(input("[p1] 1~3: "))
        p1[idx1] = 0
        idx1 += mov
        if idx1 >= 9:
            count1 += 1
            idx1 %= 9
        p1[idx1] = 1
        if p2[idx1] == 2:
            print("[p1]이 [p2]를 잡았다!")
            p2[0] = 2
            p2[idx1] = 0
        turn += 1

    elif turn%2 == 1:
        mov = int(input("[p2] 1~3: "))
        p2[idx2] = 0
        idx2 += mov
        if idx2 >= 9:
            count2 += 1
            idx2 %= 9
        p2[idx2] = 2
        if p1[idx2] == 1:
            print("[p2]가 [p1]을 잡았다!")
            p1[0] = 1
            p1[idx2] = 0
        turn += 1