Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --"I guess your number" not "guess a number"
- function cheat(rsmall,rbig,number)
- local small=rsmall;
- local big=rbig;
- local num=small+(big-small)/2
- for attempt=0,math.ceil(math.log(rbig-rsmall)/math.log(2)),1 do local num=small+(big-small)/2
- io.write("Attempt ",attempt,'/',math.ceil(math.log(rbig-rsmall)/math.log(2)),'\n')
- io.write("N: ",math.floor(num),'\n')
- if (math.floor(num)==number) then io.write(math.floor(num)," = ",math.floor(number),'\n');return true;
- elseif (math.floor(num) < number) then io.write(math.floor(num),"< ",math.floor(number),'\n');small=num;
- elseif (math.floor(num) > number) then io.write(math.floor(num)," > ",math.floor(number),'\n');big=num;
- end
- end
- return false;
- end
- function main()
- io.write("Enter number range\nfrom>");smaller = io.read('*n');
- io.write("to>");bigger = io.read('*n');
- local win=false;local attempt=0;local bg=bigger;local sm=smaller
- while(win==false) do attempt=attempt+1; io.write("Attempt ",attempt,'/',math.ceil(math.log(bg-sm) / math.log(2)),'\n')
- if (attempt > math.ceil(math.log(bg-sm) / math.log(2))) then print("I lost");
- io.write("Type your number\n>")if cheat(sm,bg,io.read('*n')) then print("You are a cheater"); end
- return 1;
- end
- n=smaller+(bigger-smaller)/2
- print("i guess",math.floor(n));
- io.write("Is that your number?\n1 - Guess = Number\n2 - Guess < Number\n3 - Guess > Number\n>");
- while(1) do choice= io.read('*n');
- if (choice==1) then win=true; break;
- elseif (choice==2) then smaller=n break
- elseif (choice==3) then bigger=n; break
- else print("Invalid Choice");
- end
- end
- end
- io.write("I guessed it")
- return 0;
- end
- main()
Add Comment
Please, Sign In to add comment