May 30, 2021 Article blog
I believe a lot of little buddies played scissors stone cloth games when they were little, so have you ever played with a computer? Here's a little game of scissors and stone cloth written in python scripts.
1, the computer wins the situation
Computer | Player |
---|---|
Stone | Scissors (scissors) |
Scissors (scissor) | Cloth |
Cloth | Stone |
2, the player wins the situation
Player | Computer |
---|---|
Stone | Scissors (scissors) |
Scissors (scissor) | Cloth |
Cloth | Stone |
According to the above rules we can do this in the form of if, the code is as follows:
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
player = input('please enter your choice(stone,scissor,cloth):')
if computer == 'stone':
if player == 'stone':
print('\033[33mdraw\033[0m')
elif player == 'scissor':
print('\033[32myou lose!!!\033[0m')
else:
print('\033[31myou win!!!\033[0m')
if computer == 'scissor':
if player == 'scissor':
print('\033[33mdraw\033[0m')
elif player == 'stone':
print('\033[31myou win!!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
if computer == 'cloth':
if player == 'cloth':
print('\033[33mdraw\033[0m')
elif player == 'scissor':
print('\033[31myou win!!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
print('your choice:%s' % player,'computer choice:%s' % computer )
Advanced one
You can write the win in a list so that the script above is more streamlined
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]
player = input('please enter your choice(stone,scissor,cloth):')
if computer == player:
print('\033[33mdraw\033[0m')
elif [player,computer] in WinList:
print('\033[33myou win!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
print('your choice:%s' % player,'computer choice:%s' % computer )
Advanced two
To make it easier to play we use the numbers 0, 1, 2 instead: stone, scissors (scissor), cloth (cloth)
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]
choice_memu = '''
(0)stone
(1)scissor
(2)cloth
please choice(0/1/2):'''
number = int(input(choice_memu))
player = C_G[number]
if computer == player:
print('\033[33mdraw\033[0m')
elif [player,computer] in WinList:
print('\033[33myou win!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
print('your choice:%s' % player,'computer choice:%s' % computer )
Advanced three
We use three games and two wins to decide the final winner if it is a draw, then continue to guess until the computer wins two games or the player wins two games to reach the final title.
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]
choice_memu = '''
(0)stone
(1)scissor
(2)cloth
please choice(0/1/2):'''
c_win = 0
p_win = 0
d_win = 1
while c_win < 2 and p_win < 2:
number = int(input(choice_memu))
player = C_G[number]
if computer == player:
d_win+=1
elif [player,computer] in WinList:
p_win+=1
if p_win == 2:
print('\033[31myou win!!!\033[0m')
break
else:
c_win+=1
if c_win == 2:
print('\033[32myou lose!!!\033[0m')
break
total_time = p_win + c_win + d_win
print('you guesse: %s times' % total_time,'you lost: %s times' % c_win,'you win: %s times' % p_win,'draw: %s times' % d_win)
The above is the small editor on the scissors stone cloth small game writing ideas, I hope the small partners can gain something.
Recommended lessons: Python Flask Station Framework, Python Django Framework.