[Python #11] update game for comment and exception

This commit is contained in:
kjg 2020-06-26 22:49:19 +09:00
parent 99470b4f53
commit 67e7a3ac62
2 changed files with 68 additions and 51 deletions

View file

@ -2,60 +2,44 @@ import baseballgameclass
# initial fucntion
size = []
a_test2 = baseballgameclass.InputNumber()
#size = a_test2.getGameSize()
#print(size)
a_test = baseballgameclass.GameNumber()
#a_test.createNumberSet(size[1], size[0])
#number = a_test.createGameNumber()
#print(number)
cGameNumber = baseballgameclass.GameNumber()
guess = []
checknewgame = 0
inputset = {}
inputset = set()
a_test2 = baseballgameclass.InputNumber()
#guess = a_test2.getNumber(size[0])
# assign class
cInputNumber = baseballgameclass.InputNumber()
cCheckNumber = baseballgameclass.CheckNumber()
cPrintResult = baseballgameclass.PrintResult()
a_test3 = baseballgameclass.CheckNumber()
#result3 = a_test3.compareNumber(number, guess)
a_test4 = baseballgameclass.PrintResult()
#a_test4.printResult(result3)
#print(result3)
a = 0
b = {}
b = set()
while True:
if a == 0:
if checknewgame == 0: # check new game
# get game size and max of number
size = a_test2.getGameSize()
b = a_test.createNumberSet(size[1], size[0])
number = a_test.createGameNumber(b)
a = 1
elif a != 3:
size = cInputNumber.getGameSize()
inputset = cGameNumber.createNumberSet(size[1], size[0])
number = cGameNumber.createGameNumber(inputset)
checknewgame = 1
elif checknewgame != 3: # running game
while True:
guess = a_test2.getNumber(size[0])
result3 = a_test3.compareNumber(number, guess)
a_test4.printResult(result3)
print(number)
guess = cInputNumber.getNumber(size[0]) # get created gaem number
result3 = cCheckNumber.compareNumber(number, guess) # compare input number and game number
cPrintResult.printResult(result3) # print result
# print(number)
if result3[0] == size[0]:
print('You win.')
b = input('continue a game (y or n) : ')
if b == 'y':
a = 0
newgame = input('continue a game (y or n) : ')
if newgame == 'y':
checknewgame = 0 # continue game
break
else:
a = 3
checknewgame = 3 # stop game
break
elif a == 3:
elif checknewgame == 3: # exit game
break

View file

@ -3,8 +3,6 @@ import random
class GameNumber():
# create game's numbers
#s = {}
#s = set()
# create number set for game
def createNumberSet(self, amount, gamesize):
@ -29,8 +27,6 @@ class GameNumber():
class CheckNumber():
# check number whether it is right
#strike = 0
#ball = 0
# compare number between a guess answer and a right answer
def compareNumber(self, gameNumber, guessNumber):
@ -56,21 +52,58 @@ class CheckNumber():
class InputNumber():
# user inputs numbers
#userGuess = []
#amountNumber = []
def getGameSize(self):
amountNumber = []
amountNumber.append(int(input('put a game size : ')))
amountNumber.append(int(input('put a max of number : ')))
while True: # check wrong input
temp = input('put a game size : ')
try:
if len(temp) > 1:
raise
for i in temp:
if i not in '0123456789':
raise
except:
print('it is not number or too big, input again')
else:
amountNumber.append(int(temp))
break
while True: # check wrong input
temp = input('put a max of number : ')
try:
if len(temp) > 1:
raise
for i in temp:
if i not in '0123456789':
raise
except:
print('it is not number or too big, input again')
else:
amountNumber.append(int(temp))
return amountNumber
def getNumber(self, gameSize):
userGuess = []
for i in range(gameSize):
while True: # check wrong input
print('put ', i+1, ' of numbers : ', end='')
userGuess.append(int(input()))
temp2 = input()
try:
if len(temp2) > 1:
raise
for k in temp2:
if k not in '0123456789':
raise
except:
print('it is not number or too big, input again')
else:
userGuess.append(int(temp2))
break
return userGuess