[Python #12] create game
This commit is contained in:
parent
3c7bc7cd1a
commit
99470b4f53
2 changed files with 146 additions and 0 deletions
64
kjg/python-the-hard-way/baseballgame.py
Normal file
64
kjg/python-the-hard-way/baseballgame.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
import baseballgameclass
|
||||
|
||||
|
||||
|
||||
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)
|
||||
|
||||
guess = []
|
||||
|
||||
a_test2 = baseballgameclass.InputNumber()
|
||||
#guess = a_test2.getNumber(size[0])
|
||||
|
||||
|
||||
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:
|
||||
# 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:
|
||||
while True:
|
||||
guess = a_test2.getNumber(size[0])
|
||||
result3 = a_test3.compareNumber(number, guess)
|
||||
a_test4.printResult(result3)
|
||||
print(number)
|
||||
if result3[0] == size[0]:
|
||||
print('You win.')
|
||||
b = input('continue a game (y or n) : ')
|
||||
if b == 'y':
|
||||
a = 0
|
||||
break
|
||||
else:
|
||||
a = 3
|
||||
break
|
||||
elif a == 3:
|
||||
break
|
||||
|
||||
|
||||
|
||||
|
||||
|
82
kjg/python-the-hard-way/baseballgameclass.py
Normal file
82
kjg/python-the-hard-way/baseballgameclass.py
Normal file
|
@ -0,0 +1,82 @@
|
|||
import random
|
||||
|
||||
|
||||
class GameNumber():
|
||||
# create game's numbers
|
||||
#s = {}
|
||||
#s = set()
|
||||
|
||||
# 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
|
||||
#strike = 0
|
||||
#ball = 0
|
||||
|
||||
# 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
|
||||
#userGuess = []
|
||||
#amountNumber = []
|
||||
|
||||
def getGameSize(self):
|
||||
amountNumber = []
|
||||
amountNumber.append(int(input('put a game size : ')))
|
||||
amountNumber.append(int(input('put a max of number : ')))
|
||||
|
||||
return amountNumber
|
||||
|
||||
def getNumber(self, gameSize):
|
||||
userGuess = []
|
||||
for i in range(gameSize):
|
||||
print('put ', i+1, ' of numbers : ', end='')
|
||||
userGuess.append(int(input()))
|
||||
|
||||
return userGuess
|
||||
|
||||
|
||||
class PrintResult():
|
||||
# print result
|
||||
|
||||
def printResult(self, result):
|
||||
print(result[0], ' strike, ', result[1], ' ball')
|
Loading…
Reference in a new issue