서양 중세를 실제 배경으로 한 게임 Kingdom come deliverance에서는 다음과 같은 규칙의 주사위 게임이 등장한다.
1부터 6까지 적힌 여섯 면의 주사위 6개가 주어지고, 이것을 무작위로 한 번에 다 던진다. 다음과 같은 규칙에 따라 점수를 획득하여 목표 점수에 먼저 도달한 사람이 게임에 이긴다.
윗면의 숫자가
(1) 1 하나 당 점수 100을 더한다. 그러나 1이 3개라면 1000을 더한다. 여기서 4개, 5개, 6개이면 각각 x2를 한다(4개 = 2000, 5개 = 4000, 6개 = 8000).
(2) 5 하나 당 점수 50을 더한다. 5가 3개이면 500을 더한다. 여기서 위와 같이 4개, 5개, 6개이면 각각 1000, 2000, 4000이 된다.
(3) 2, 3, 4, 6이 각각 3개이면 해당 숫자의 x100한 값을 더한다. 예) 3이 3개 = 300, 6이 3개 = 600. 위와 비슷하게 같은 숫자가 3개보다 많으면 하나 당 곱하기 2를 한다. 예) 3이 4개 = 600. 6이 5개 = 2400
(4) 숫자 1, 2, 3, 4, 5의 조합은 750, 그리고 2, 3, 4, 5, 6은 1250, 또 1, 2, 3, 4, 5, 6은 2000을 더한다.
(5) 한 턴에 6개 모든 주사위로 점수를 획득하면 주사위를 한 번 더 굴릴 수 있는 선택이 주어진다. 예를 들어 1) 1,2,3,4,5 + 1이면 750점 + 100점을 얻고 모든 주사위를 한 번 더 굴려서 점수를 더 얻을 수 있다. 여기서 턴을 종료하면 850점을 얻고, 주사위를 한 번 더 굴렸으나 어떠한 조합도 나오지 않는 "꽝"이 나오면 이전에 얻은 점수를 모두 날리고 0점으로 초기화 되며, 턴은 종료 된다.
이 규칙을 구현해보시오. 인풋은 Array 형태로 주어지고, 되도록이면 알고리즘 내에서 Array의 숫자 순서를 재배열해서는 안된다. 6개 숫자 생성은 Pseudo-random-number generator를 사용하여 무작위로 생성할 수 있다. 예) Python - Random library. 목표 점수는 사용자가 임의로 지정하면 된다.
입력 - 출력 예) [1, 3, 2, 6, 2, 2] -> 200(2가 3개) + 100(1이 1개) = 300점
[3, 6, 6, 3, 4, 2] -> 0점
[2, 6, 1, 1, 6, 3] -> 200점
[1, 5, 2, 3, 6, 4] -> 2000점
[3, 5, 1, 5, 3, 3], [2, 4, 6, 3, 3, 4] -> 0점
[5, 2, 1, 2, 5, 2] -> 400점
[1, 2, 3, 4, 5, 5], [5, 2, 1, 2, 1, 2], [4, 6, 4, 1, 4, 4] -> 800 +たす 450 +たす 900 =わ 2150
킹덤 컴 딜리버런스 2가 나온다길래 생각이 나서 올려보았습니다, 재미있게 풀어보세용~
#랜덤 라이브러리 호출
import random
#주사위 숫자 생성 함수
def kingdome_dice():
a=[]
for i in range(6):
a.append(random.randrange(1,7))
return a
# rule 1 - 첫번쨰와 두번째 규칙에서 사용한 주사위의 개수와 점수를 반환한다.
def rule_1(dice):
a=dice.count(1)
b=dice.count(5)
if a < 3:
point=a*100
if a >= 3:
point=1000*2**(a-3)
if b < 3:
point=point+(b*50)
if b >= 3:
point=point+(500*2**(b-3))
return a+b,point
# rule 2 - 2,3,4,6의 갯수 탐색
def rule_2(dice,n):
a=dice.count(n)
if a < 3:
point=0
a=0
return a,point
if a >= 3:
point=n*100*2**(a-3)
return a,point
# rule 3 - 5개 이상의 연속된 숫자 탐색
def rule_3(dice):
a=set(dice)
if len(a)<5:
return 0,0
if len(a)==6:
return 6,2000
if len(a)==5:
if 1 in a and 6 in a:
return 0,0
else:
if 1 in a:
return 5,750
if 6 in a:
return 5,1250
# 주사위를 돌리고 점수 확인 후 다시 돌리는지 여부 확인
def diceplay(sppoint):
point=sppoint
while True:
dice=kingdome_dice()
print(dice)
a16=rule_3(dice)
if a16[0]==6:
point=point+a16[1]
print(point)
continue
if a16[0]==5 and dice.count(1)==2:
point=point+a16[1]+100
print(point)
continue
if a16[0]==5 and dice.count(5)==2:
point=point+a16[1]+50
print(point)
continue
if a16[0]==5:
point=point+a16[1]
print(point)
return point
break
else:
a15=rule_1(dice)
a2=rule_2(dice,2)
a3=rule_2(dice,3)
a4=rule_2(dice,4)
a6=rule_2(dice,6)
point=point+a15[1]+a2[1]+a3[1]+a4[1]+a6[1]
count=a15[0]+a2[0]+a3[0]+a4[0]+a6[0]
if count==6:
print(point)
else:
print(point)
return point
break
#주사위 목표 점수 입력 받기
total_point=int(input("목표 점수를 입력하시오:"))
pointlist=[]
totalpoint=0
count=1
while totalpoint<total_point:
a=diceplay(totalpoint)
totalpoint=int(a)
count=count+1
print("플레이어",count,"의 게임을 하여 ",totalpoint,"의 점수를 획득하였습니다")
2024年07月20日 21:12
import random
def getDiceNum():
return [random.randint(1, 6) for _ in range(6)]
def listToDict(diceNum):
diceDict = dict()
for n in diceNum:
if n not in diceDict:
diceDict[n] = 1
else:
diceDict[n] += 1
return diceDict
def isStraigt(diceDict, start, end):
if False not in [True if x in diceDict else False for x in range(start, end)]:
return True
else:
return False
def get1and5Score(diceDict):
addScore = 0
if 1 in diceDict and diceDict[1] == 2:
addScore = 100
if 5 in diceDict and diceDict[5] == 2:
addScore += 50
return addScore
def addTurnScoreInfo(info, tag, score):
info[tag] = score
def getCountAndMoreScore(info, diceDict):
addScore = 0
for x in range(1, 7):
if x in diceDict:
baseScore = x * 100
if x == 5:
baseScore =50
if x in [1, 5] and diceDict[x] == 2:
baseScore += baseScore
if x == 1 and diceDict[x] >= 3:
baseScore = 1000
if x == 5 and diceDict[x] >= 3:
baseScore = 500
if diceDict[x] >= 4:
for y in range(4, diceDict[x]+1):
baseScore = baseScore *2
if (x in [1, 5]) or (x in [2, 3, 4, 6] and diceDict[x] >= 3):
addScore += baseScore
addTurnScoreInfo(scoreInfo, f'{x} is {diceDict[x]}', baseScore)
return addScore
if __name__ == '__main__':
totalScore = 0
goCount = 1
isGo = True
while isGo:
turnScore = 0
#diceNum = [4, 6, 4, 1, 4, 4]
diceNum = getDiceNum()
print(diceNum)
scoreInfo = {}
diceDict = listToDict(diceNum)
if isStraigt(diceDict, 1, 7):
turnScore += 2000
addTurnScoreInfo(scoreInfo, '1to6', turnScore)
else:
if isStraigt(diceDict, 1, 6):
turnScore += 750
addTurnScoreInfo(scoreInfo, '1to5', turnScore)
score1or5 = get1and5Score(diceDict)
turnScore += score1or5
addTurnScoreInfo(scoreInfo, '1or5 more', score1or5)
elif isStraigt(diceDict, 2, 7):
turnScore += 1250
addTurnScoreInfo(scoreInfo, '2to6', turnScore)
score1or5 = get1and5Score(diceDict)
turnScore += score1or5
addTurnScoreInfo(scoreInfo, '1or5 more', score1or5)
else:
turnScore +=getCountAndMoreScore(scoreInfo, diceDict)
print(f'{goCount} - turnScore : {turnScore}')
print(f'=> {scoreInfo}')
if turnScore == 0:
totalScore = 0
isGo = False
else:
totalScore += turnScore
print(f'totalScore : {totalScore}')
if isGo == True:
go_yn = input('go and stop?(go: anykey, stop: n )')
if 'n' == go_yn:
isGo = False
print('stop!!')
else:
print('go!!')
goCount += 1
print("finish")
2025年01月17日 13:58
import random
class dice:
def __init__(self) -> None:
self.result = 0
def throw(self) :
self.result = random.randint(1,6)
return self.result
def getDiceValue(self) :
return self.result
#점수 계산하는 함수
def calculatePoint(result) :
point = 0
idx = 0
zeroIdx = []
serialType = 'N'
diceResult = {}
for i in range(1,7) :
diceResult[i] = result.count(i)
print("주사워 숫자별 통계 : " , diceResult)
for v in diceResult :
if diceResult[v] > 0 :
idx += 1
else :
idx += 0
zeroIdx.append(v)
print("serial : ", idx, "/", zeroIdx)
#연속된 숫자 점수 주기
if idx == 6 :
point = 2000
serialType = 'F'
elif idx == 5 and 1 in zeroIdx :
point = 1250
serialType = 'P'
elif idx == 5 and 6 in zeroIdx :
point = 750
serialType = 'P'
else :
serialType = 'N'
retPoint = calculatePointNum(diceResult, serialType)
point += retPoint[0]
print('serialType : ', serialType, '/Point : ', point)
retPoint[0] = point
return retPoint
## end calculatePoint
# 숫자별 점수 계산
def calculatePointNum(result, serialType):
point = 0
p = 0
isAllDicePoint = False
mat = {1:[0,100,200,1000,2000,4000,8000],
2:[0,0,0,200,400,800,1200],
3:[0,0,0,300,600,1200,2400],
4:[0,0,0,400,800,1600,3200],
5:[0,50,100,500,1000,2000,4000],
6:[0,0,0,600,1200,2400,4800]}
for i in result :
if serialType != 'N' and result[i]>0 :
p = mat[i][result[i]-1]
else :
p = mat[i][result[i]]
if serialType == 'P' and p>0 :
isAllDicePoint = isAllDicePoint | True
elif serialType == 'N' and result[i] > 0 and p == 0 :
isAllDicePoint = isAllDicePoint | False
elif serialType == 'N' and result[i] > 0 and p > 0 :
isAllDicePoint = isAllDicePoint & True
elif serialType == 'F' :
isAllDicePoint = True
point += p
print('key : ' , i, '/ Point : ',p, '/ TotolPoint : ', point, '/isAllDiceType : ', isAllDicePoint)
return [point, isAllDicePoint]
## end calculatePointEachNum
################# Main Program ######################################
diceCount = 6 #주사위 갯수
finalPoint = 0
isAgain = True
answerAgain = 'Y'
targetPoint = 1000
targetPoint = input("목표점수를 입력하세요 : ")
#주사위 객체 생성
dice1 = dice()
# 모든 주사위가 점수를 획득하면 한번더 던질지를 선택할 수 있다.
# 사용자가 한번더를 원하지 않거나 모든 주사위가 점수를 획득하지 못하면 그때까지의 점수를 최종으로 한다.
# 한번 더 던진 결과가가 0점일 경우 최종 점수는 0으로 세팅
while isAgain:
diceValue = []
#6번 던져서 결과 가져오기
for i in range(0,6) :
diceValue.append(dice1.throw())
print('주사위 결과 : ' , diceValue)
point = calculatePoint(diceValue)
isAgain = point[1]
print ('Point : ' , point)
if point[0] > 0 :
finalPoint += point[0]
else :
finalPoint = 0
if isAgain and targetPoint > finalPoint:
answer = input("게임을 한번더 하시겠습니까(Y/N)?").strip().upper()
isAgain = True if answer == 'Y' else False
else : None
print ('당신의 최종 점수는 : ' , finalPoint)
풀이 작성