Skip to content

Instantly share code, notes, and snippets.

@honux77
Created February 21, 2013 16:24
Show Gist options
  • Save honux77/5005908 to your computer and use it in GitHub Desktop.
Save honux77/5005908 to your computer and use it in GitHub Desktop.
경민의 야구 게임 리팩토링(?) 해 봤습니다.
import random
#input data and avoid redundent data
def input_from_user(question):
isRedundent=False
while True:
if isRedundent:
print "Numbers must be different."
isRedundent=False
raw = raw_input(question)
input_array = raw.split(" ")
for i in range(0,3):
for j in range(i+1,4):
if input_array[i] == input_array[j]:
isRedundent=True
break
if(isRedundent==False):
break
return input_array
#print title
def print_title():
print("------------------------------------")
print("| basball game with 4 numbers! |")
print("------------------------------------")
#generate random answer
def gen_answer(ea):
answer=[]
for int_0_to_9 in range(0,10):
answer.append(int_0_to_9)
random.shuffle(answer)
return answer[0:ea]
#print result
def print_result(strike,ball):
print("------------------------------------")
print("| strike(s) : %d |")%strike
print("| ball(s) : %d |")%ball
if strike < 4:
print("| Try again! |")
else:
print("| 4 strikes and out! |")
print("| Good Game! |")
print("------------------------------------")
#bs judgement
def judge_bs(input_array,answer):
strike= ball =0
for index in range(0,4):
if int(input_array[index]) == answer[index]:
strike +=1
for add in range(1,4):
if int(input_array[(index+add)%4]) == answer[index]:
ball +=1
return (strike,ball)
#program body start
debug = True
print_title()
answer=gen_answer(4)
#for debug
if debug:
print(answer[0], answer[1], answer[2], answer[3])
#main loop
strike=0
while strike < 4:
input_array = input_from_user("input like [1 2 3 4] : ")
strike=ball=0
(strike,ball) = judge_bs(input_array,answer)
print_result(strike,ball)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment