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

View file

@ -3,8 +3,6 @@ import random
class GameNumber(): class GameNumber():
# create game's numbers # create game's numbers
#s = {}
#s = set()
# create number set for game # create number set for game
def createNumberSet(self, amount, gamesize): def createNumberSet(self, amount, gamesize):
@ -29,8 +27,6 @@ class GameNumber():
class CheckNumber(): class CheckNumber():
# check number whether it is right # check number whether it is right
#strike = 0
#ball = 0
# compare number between a guess answer and a right answer # compare number between a guess answer and a right answer
def compareNumber(self, gameNumber, guessNumber): def compareNumber(self, gameNumber, guessNumber):
@ -56,21 +52,58 @@ class CheckNumber():
class InputNumber(): class InputNumber():
# user inputs numbers # user inputs numbers
#userGuess = []
#amountNumber = []
def getGameSize(self): def getGameSize(self):
amountNumber = [] amountNumber = []
amountNumber.append(int(input('put a game size : '))) while True: # check wrong input
amountNumber.append(int(input('put a max of number : '))) 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
return amountNumber 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): def getNumber(self, gameSize):
userGuess = [] userGuess = []
for i in range(gameSize): for i in range(gameSize):
print('put ', i+1, ' of numbers : ', end='') while True: # check wrong input
userGuess.append(int(input())) print('put ', i+1, ' of numbers : ', end='')
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 return userGuess