LoginSignup
2
0

More than 1 year has passed since last update.

ワンライナーじゃんけんプログラム in Python

Last updated at Posted at 2022-11-23

入力

入力はGCP(グー、チョキ、パー)でおなしゃす

プログラム

結論から行くスタイル

import random; cpu=random.choice(["G","C","P"]); man=input(); print(f"{man} vs {cpu}"); gcp={'G':0,'C':1,'P':2}; result={0:"あいこ",1:"CPUの勝ち",2:"人の勝ち"}; print(result[(gcp[man]-gcp[cpu])%3])

必要最低限の解説

GCPを012にそれぞれ置き換えて、(man-cpu)%3を計算するとどちらが勝者か求まります

        %3  勝者
GG = 0   0  あいこ
GC = -1  2  man
GP = -2  1  cpu

CG = 1   1  cpu
CC = 0   0  あいこ
CP = -1  2  man

PG = 2   2  man
PC = 1   1  cpu
PP = 0   0  あいこ

余り2のときにmanが勝利
余り1のときにcpuが勝利

蛇足

ワンライナーとしては邪道かもしれない

2
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0