ungleich-learning-circle/kjg/python-the-hard-way/baseballgameclass.py

116 lines
3 KiB
Python
Raw Permalink Normal View History

2020-06-24 15:52:32 +00:00
import random
class GameNumber():
# create game's numbers
# create number set for game
def createNumberSet(self, amount, gamesize):
s = {}
s = set()
while len(s) < gamesize:
# add set as gamesize
s.add(random.randint(0, amount-1))
print(s)
return s
# create game number list
def createGameNumber(self, gameSet):
numbers = []
for k in gameSet:
numbers.append(k)
print(k)
return numbers
class CheckNumber():
# check number whether it is right
# compare number between a guess answer and a right answer
def compareNumber(self, gameNumber, guessNumber):
result = []
strike = 0
ball = 0
for i in range(len(gameNumber)):
print(guessNumber)
if guessNumber[i] == gameNumber[i]:
# check number of strke
strike += 1
elif guessNumber[i] in gameNumber:
# check number of ball
ball += 1
# add results of strike and ball
result.append(strike)
result.append(ball)
return result
class InputNumber():
# user inputs numbers
def getGameSize(self):
amountNumber = []
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
2020-06-24 15:52:32 +00:00
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
2020-06-24 15:52:32 +00:00
def getNumber(self, gameSize):
userGuess = []
for i in range(gameSize):
while True: # check wrong 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
2020-06-24 15:52:32 +00:00
return userGuess
class PrintResult():
# print result
def printResult(self, result):
print(result[0], ' strike, ', result[1], ' ball')