본문 바로가기

Programming/Algorithm

[Python3] 이진수 게임!

[출처 - 네이버웹툰: 공대생 너무 만화 44화]

주로 공대쪽 특히 컴퓨터공학과에서 유명한 게임이다. 서로 번갈아가며 진수를 말하는 게임인데 5자리수정도인 16을 넘어가기 힘들다. 정확한 게임규칙은 다음과 같다.

이진수로 0, 1, 10, 11, 100, ... 을 차례로 돌아가며 한 숫자씩 말한다.

예를 들어 A와 B가 게임을 한다고 하면,

A: 0
B: 1
A: 1
B: 0
A: 1
B: 1

...

이런식으로 돌아가는 것이다.

import sys
while(1): #먼저할지 나중에 할지
order = input("First or Last?? (F/L) ")

if (order.lower() == 'f'):
i = 0
break

elif (order.lower() == 'l'):
print(0)
i = 1
break

print("Wrong input!!")

usr_turn = True #유저의 순서라면 참 아니라면 거짓

while(i >= 0):
bina = bin(i)[2:]

for j in bina:
if(usr_turn):
while(1): #유저입력
try:
usr_in = int(input())
if(len(str(usr_in)) != 1): #여러개입력
print("Wront Input!")
continue
break

except ValueError: #숫자외에 입력
print("Wrong input!")

if(usr_in == int(j)): #컴퓨터차례
usr_turn = False

else: #오답
print("You wrong It's", j)
print(i, bin(i)[2:])
sys.exit()

else: #컴퓨터차례
print(j)
usr_turn = True

i += 1

막상 프로그래밍은 어렵지 않으나 여러 오류케이스를 제외하는 프로그램을 짜다보니 길어져서 복잡해 보일 수도 있겠다.

First or Last?? (F/L) L

0

1

1

0

1

1

1

0

0

1

0

1

1

1

0

1

1

1

1

0

0

1

You wrong It's 0

8 1000


Process finished with exit code 0



'Programming > Algorithm' 카테고리의 다른 글

[Python3] 단어 순서 바꾸기  (1) 2017.04.03
[Python3] 글자 순서 바꾸기  (2) 2017.04.01
[Python3] 31게임! 필승전략  (1) 2017.03.22
[Python3] 31게임!  (1) 2017.03.21
[Python3] 제곱해서 같은 수 - 2  (0) 2017.03.12