본문 바로가기
프로그램

[파이썬] 가위바위보 게임(승부 판단 로직)

by 오디세이99 2022. 11. 1.
728x90
반응형

가위바위보 게임에서 승부의 판단 로직은 

가위-바위 => 바위 승리

바위:보 => 보 승리

가위:보 => 가위 승리

가 되고, 2명의 게이머가 있으면 각각의 조건을 사용해서 승부를 판단해야 하지만 

['가위', '바위', '보']

와 같이 리스트로 해서 인덱스를 보면 항시 +1 인덱스가 승리하게 됩니다.

 

from random import *
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

root = Tk()  # tkinter 설정


def select(human_choice):  # 버튼 클리시 인수 넘어 옴
    lst = ['가위', '바위', '보']
    computer_choice = choice(lst)  # 램덤하게 리스트에서 하나를 선택. ramdon.choice()
    print()
    print('Computer choice =', computer_choice)
    print('Human choice =', human_choice)

    c = computer_choice
    h = human_choice
    ci = lst.index(c)
    hi = lst.index(h)

    # print(c, ci, h, hi)
    win = ''
    if ci == hi + 1 or ci + 2 == hi:
        win = '컴퓨터 승리'
        # print(c, h, win)
    elif ci + 1 == hi:
        win = '휴먼 승리'
        # print(c, h, win)
    else:
        win = '무승부'
        # print('무승부')

    result["text"] = win


root.geometry('600x200')
frame = Frame(root)  # tkinter 위젯들을 포함할 틀(Frame) 설정
frame.pack(pady=10)  # 좌우 여백 10 설정

result = ttk.Label(frame, text="게임시작", font=("굴림체", 16))
# result.pack(side=RIGHT,padx=10,pady=10, anchor=CENTER)
result.pack(anchor=CENTER)

image = Image.open("computer.png")
resize_image = image.resize((100, 100))
img_computer = ImageTk.PhotoImage(resize_image)
computer = ttk.Label(frame, text="computer", image=img_computer)
computer.pack(side='left', anchor=CENTER)

image = Image.open("VS.png")
resize_image = image.resize((50, 50))
img_vs = ImageTk.PhotoImage(resize_image)
computer = ttk.Label(frame, text="", image=img_vs)
computer.pack(side='left', anchor=CENTER)

image = Image.open("gawebawebo_gawe.png")
resize_image = image.resize((70, 70))
img_gawe = ImageTk.PhotoImage(resize_image)
b1 = ttk.Button(frame, text='가위', image=img_gawe, command=lambda: select('가위'))
b1.pack(side='left', pady=2)

image = Image.open("gawebawebo_bawe.png")
resize_image = image.resize((70, 70))
img_bawe = ImageTk.PhotoImage(resize_image)
b2 = ttk.Button(frame, text='바위', image=img_bawe, command=lambda: select('바위'))
b2.pack(side='left', pady=2)

image = Image.open("gawebawebo_bo.png")
resize_image = image.resize((70, 70))
img_bo = ImageTk.PhotoImage(resize_image)
b3 = ttk.Button(frame, text='보', image=img_bo, command=lambda: select('보'))
b3.pack(side='left', pady=2)

root.mainloop()

computer.png
0.03MB

 

gawebawebo_bawe.png
0.01MB
gawebawebo_bo.png
0.01MB
gawebawebo_gawe.png
0.01MB
VS.png
0.12MB

728x90
반응형

댓글