LUA program: Guessing numbers

Source: Internet
Author: User

The rule of guessing a number game is that the rule is that the computer and the person take turns guessing, guessing the game stops
--Computer generated four non-repeating numbers
tb1={0,1,2,3,4,5,6,7,8,9}
I=1 tbn={}
If I<5 Then
For i=1,4 do
N=math.random (1,11-i)
G=tb1[n]
Table.insert (TBN,G)
Tb1[11-i],tb1[n]=tb1[n],tb1[11-i]-The position of the number and the last number of the interchange generation
I=i+1

End
End
Print (Tbn[1],tbn[2],tbn[3],tbn[4])
TBN_NUMBER=TBN[1]*1000+TBN[2]*100+TBN[3]*10+TBN[4]

--Judgment part
Q=1
While True does
A=0 b=0
If Q==1 Then
Print ("Please enter four digits")
While True does
P=io.read ("*line")
if (not Tonumber (p)) or (String.len (p) ~=4) then
Print ("You are not entering a 4-digit number, please re-enter")
Else
P=tonumber (P)
Break
End
End

While True does
P1=MATH.MODF (p/1000)
If p1<0 or p1>9 then
Print ("The number you entered is not four bits, please re-enter")
else break
End
End
P2=MATH.MODF (p/100)-p1*10
P3=MATH.MODF (P/10)-p2*10-p1*100
p4=p-p3*10-p2*100-p1*1000
TBP={P1,P2,P3,P4}
Print (P1,P2,P3,P4)
Q=0
For i=1,4 do
For j=1,4 do
If TBP[I]==TBN[J] Then
If I~=j Then
B=b+1
Else
A=a+1
End
End

End
End
If a==4 and b==0 then
Print ("player wins")
Break
End
Print (A..) A ".. B.. " B ")

Else
--Lack of computer generated number can not use the player has entered the judgment
K=1
While K==1 do
M1=math.random (0,9)
M2=math.random (0,9)
M3=math.random (0,9)
M4=math.random (0,9)
TBM={M1,M2,M3,M4}
If TBM~=TBP Then
Break
End
End
Print (M1,M2,M3,M4)
Q=1
For i=1,4 do
For j=1,4 do
If TBM[I]==TBN[J] Then
If I~=j Then
B=b+1
Else
A=a+1
End
End
End
End
If a==4 and b==0 then
Print ("Computer wins")
Break
End
Print (A..) A ".. B.. " B ")
End
End

Lua program: guessing numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.