The scene was so free that I remembered an acquaintance making a rock-paper-scissors game.
@ sandream's article "Python rock-paper-scissors poi for beginners (answers and explanations) " It is a rule of winning three times against a rock-paper-scissors kid. How @sandream came to write an article about rock-paper-scissors, Please refer to the above article for a detailed explanation of the overall function.
janken.py
#_*_ coding: utf-8 _*_
import random
import time
dic = {'a':'Goo','b':'Choki','c':'Par'}
#Get a list of hands you can put out
def clist(hands):
ls =[]
for i in hands:
ls.append(i)
return ls
#Hand message that can be issued
comm_mess = str(','.join(dic))+'Please enter one of them!'
#Victory end message
def good_end():
mess = str(\
'Stupid! !!\n'\
'Rock-paper-scissors-rock-paper-scissors is a rock-paper-scissors game. !!\n'\
'Hidebu!\n'\
)
return mess
#Defeat end message
def bad_end():
mess = str(\
'It ’s your bonus! !!\n'\
'Please clarify the cause and submit the defeat deterrence checklist by 10 am tomorrow morning.\n'\
)
return mess
#Rock-paper-scissors kid Esper mode
def kozou_tsuyoi(uch,pch,uco,pco):
#Rock-paper-scissors kid Esper mode message
kozou_mess1 = ('Shit ~! Don't mess with rock-paper-scissors! !!')
#Rock-paper-scissors kid super esper mode message
kozou_mess2 = ('You can see it! I can see you! !!')
_random =random.random()
#Branch to the player's winning star, the kid's winning star, and the hand
if uco == 2 and pco <= 1 and uch == dic['a']:
print(kozou_mess1)
hands = dic['c']
elif uco == 2 and pco <= 1 and uch == dic['b']:
print(kozou_mess1)
hands = dic['a']
elif uco == 2 and pco <= 1 and uch == dic['c']:
print(kozou_mess1)
hands = dic['b']
#Rock-paper-scissors kid goes into super esper mode
elif _random < 0.01 and uch == dic['a']:
print(kozou_mess2)
hands = dic['c']
elif _random < 0.01 and uch == dic['b']:
print(kozou_mess2)
hands = dic['a']
elif _random < 0.01 and uch == dic['c']:
print(kozou_mess2)
hands = dic['b']
else:
hands = pch
return hands
choice_list = clist(dic)
judge = None
player_win ='You WIN !!\n'
player_lose ='You LOSE !!\n'
player_draw ='DRAW !!\n'
user_co = 0
pc_co = 0
print(\
'I'm rock-paper-scissors\n'\
'Let's play rock-paper-scissors\n'\
'It ’s better to have 3 before\n'\
)
for i in clist(dic):
print (i+'Is'+' '+dic[i])
time.sleep(2)
print(comm_mess+'\n')
while True:
if judge == player_draw:
print('Ah! Kodetsu! !!')
else:
print('Jean! Ken! !!')
#Player chooses a hand
print('(What are you going to do !?')
user = input(':')
user_choice = user.lower()
try:
user_choice = dic[user]
except:
print(comm_mess)
continue
#Rock-paper-scissors kid chooses a hand
pc_choice = dic[str(random.choice(clist(dic)))]
#Rock-paper-scissors kid Esper mode
_random = random.random()
if _random < 0.5:
pc_choice = kozou_tsuyoi(user_choice,pc_choice,user_co,pc_co)
if judge == player_draw:
print('Shock! !!')
else:
print('Poit! !!')
time.sleep(1.5)
#Check your hands
print(\
'Player's choice:'+str(user_choice)+'\n'\
'Rock-paper-scissors elephant's hand:'+str(pc_choice)+'\n'\
)
time.sleep(1.5)
#Win / loss judgment
if user_choice == pc_choice:
judge = player_draw
print(player_draw)
elif user_choice == dic['a']:
if pc_choice ==dic['b']:
judge = player_win
print(player_win)
user_co += 1
elif pc_choice == dic['c']:
judge = player_lose
print(player_lose)
pc_co += 1
else:
print('Rock-paper-scissors law is disturbed! A')
elif user_choice == dic['b']:
if pc_choice ==dic['c']:
judge = player_win
print(player_win)
user_co += 1
elif pc_choice == dic['a']:
judge = player_lose
print(player_lose)
pc_co += 1
else:
print('Rock-paper-scissors law is disturbed! B')
elif user_choice == dic['c']:
if pc_choice ==dic['a']:
judge = player_win
print(player_win)
user_co += 1
elif pc_choice == dic['b']:
judge = player_lose
print(player_lose)
pc_co += 1
else:
print('Rock-paper-scissors law is disturbed! C')
else:
print('Rock-paper-scissors law is disturbed! D')
print(\
'Number of player wins:'+str(user_co)+'\n'\
'Number of rock-paper-scissors wins:'+str(pc_co)+'\n'\
)
time.sleep(1.5)
#Progress-based event (thinking)
#Ending judgment
if user_co == 3:
print(good_end())
break
elif pc_co == 3:
print(bad_end())
break
print('THE END')
When I tried it with Repl.it, it seemed to work as expected, so Yoshi!
Choosing a simple choice is chaotic enough, which is interesting. Speaking of rock-paper-scissors games that were in the arcade, it's *** the unreasonable strength of the opponent ***. It is a thing that was often rolled up by medal games in the past. I added such a function.
Name it *** Esper function *** It executes a function at random and activates when certain conditions are met. Around ↓
def kozou_tsuyoi(uch,pch,uco,pco):
_random =random.random()
#Branch to the player's winning star, the kid's winning star, and the hand
if uco == 2 and pco <= 1 and uch == dic['a']:
hands = dic['c']
#Random value
elif _random < 0.01 and uch == dic['a']:
hands = dic['c']
else:
hands = pch
return hands
The conditions are divided, but the design is as follows.
*** I think that you can make it unreasonable strength by loosening the activation conditions and branching conditions. *** ***
It's not fun to change it 100% according to the player's hand. I changed it a little.
Because the condition of loop exit is *** win or lose ***, which is a masculine specification I left the input restrictions for the articles I referred to.
Since there are a lot of branches, I am doing something like exception handling by giving if else else. Well, even so, the game won't end unless you win or lose ***.
Those who lost to rock-paper-scissors kid in rock-paper-scissors Please clarify the cause and submit the defeat deterrence checklist by 10 am tomorrow morning.
Recommended Posts