---[[
math.randomseed(os.time()) --[[为随机数产生器生成一个种子--]]
user_score = 0
comp_score = 0 -- 全局变量存分数
lookup = {}; --输赢对照表
lookup["rock"]={rock = "draw",paper = "lose",scissors ="win"}
lookup["paper"]={rock = "win",paper = "draw",scissors = "lose"}
lookup["scissors"]={rock = "lose",paper = "win",scissors = "draw"}
function GetAiMove() --Ai的函数
local int_to_string = {"rock","paper","scissors"} --局部一个table,对照用
return int_to_string[math.random(3)]
end
function EvaluateTheGuess(user_guess,comp_guess) -- 计算结果的函数
if(lookup[user_guess][comp_guess]=="win") then
print ("user win the game")
--print()
user_score=user_score+1 --小错误 ~已改
elseif (lookup[user_guess][comp_guess]=="lose") then
print ("user lose the game")
--print()
comp_score=comp_score+1
else
print ("draw!")
--print()
end
end
--下面开始
print ("game begin:enter q to guit game")
--print() --换行?
loop = true
while loop==true do
--print()
print("user: "..user_score.." comp: "..comp_score)
print()
print("p--布 r--拳头 s--减")
print("请输入:")
--io.open()
user_guess =io.stdin:read '*l' --出错的地方,这里是l不是1
--user_guess = "r"
print()
local letter_to_string = {r="rock",s="scissors",p="paper"} --亦是局部的一个table 对照用的
if(user_guess == "q") then
loop = false
elseif(user_guess == "r") or (user_guess == "s") or(user_guess =="p") then
comp_guess=GetAiMove()
EvaluateTheGuess(letter_to_string[user_guess],comp_guess)
else
print ("invalid input,try again")
end
end
--]]