Godot GDScript 剪刀石頭布

2022/07/08修改
Windows 10 專業版 21H1
Godot v3.4.4.stable.official [419e713a2]



以三個按鈕作為輸入,與隨機產生的電腦對手進行猜拳
輸贏的狀態有三種(平手,勝利或失敗)
判定輸贏的方式,先以IF排除平手的結果
再將手動輸入與電腦隨機兩個變數加入陣列
以MATCH語句將勝利的結果列出
最後以_將失敗的結果列出


scenemaster.gd

extends Control

var moraType = ["rock", "paper", "scissrs"]
var moraTypeTc = {"rock":"石頭", "paper":"布", "scissrs":"剪刀"}

func _ready():
randomize()

func mora(playerChoice):
var computerChoice = moraType[randi() % 3]
var drawWinLose
if playerChoice == computerChoice:
drawWinLose = "平手"
else:
match [playerChoice,computerChoice]:
["rock","scissrs"],["paper","rock"],["scissrs","paper"]:
drawWinLose = "你贏"
_:
drawWinLose = "你輸"
$LabelResult.text="你選擇%s, 電腦選擇%s, %s"%[moraTypeTc[playerChoice],moraTypeTc[computerChoice],drawWinLose]


func _on_ButtonScissors_pressed():
mora("scissrs")


func _on_ButtonRock_pressed():
mora("rock")


func _on_ButtonPaper_pressed():
mora("paper")