Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
6
Task/Guess-the-number/00-META.yaml
Normal file
6
Task/Guess-the-number/00-META.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
category:
|
||||
- Conditional loops
|
||||
- Randomness
|
||||
from: http://rosettacode.org/wiki/Guess_the_number
|
||||
note: Games
|
||||
17
Task/Guess-the-number/00-TASK.txt
Normal file
17
Task/Guess-the-number/00-TASK.txt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
;Task:
|
||||
Write a program where the program chooses a number between '''1''' and '''10'''.
|
||||
|
||||
A player is then prompted to enter a guess. If the player guesses wrong, then the prompt appears again until the guess is correct.
|
||||
|
||||
When the player has made a successful guess the computer will issue a "Well guessed!" message, and the program exits.
|
||||
|
||||
A [[:Category:Conditional loops|conditional loop]] may be used to repeat the guessing until the user is correct.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Bulls and cows]]
|
||||
* [[Bulls and cows/Player]]
|
||||
* [[Guess the number/With Feedback]]
|
||||
* [[Mastermind]]
|
||||
<br><br>
|
||||
|
||||
5
Task/Guess-the-number/11l/guess-the-number.11l
Normal file
5
Task/Guess-the-number/11l/guess-the-number.11l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
V t = random:(1..10)
|
||||
V g = Int(input(‘Guess a number that's between 1 and 10: ’))
|
||||
L t != g
|
||||
g = Int(input(‘Guess again! ’))
|
||||
print(‘That's right!’)
|
||||
106
Task/Guess-the-number/AArch64-Assembly/guess-the-number.aarch64
Normal file
106
Task/Guess-the-number/AArch64-Assembly/guess-the-number.aarch64
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program guessNumber.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
.equ BUFFERSIZE, 100
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessNum: .asciz "I'm thinking of a number between 1 and 10. \n Try to guess it:\n"
|
||||
|
||||
szMessError: .asciz "That's not my number. Try another guess:\n"
|
||||
szMessSucces: .asciz "Correct.\n"
|
||||
|
||||
szCarriageReturn: .asciz "\n"
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
sBuffer: .skip BUFFERSIZE
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
mov x0,1
|
||||
mov x1,10
|
||||
bl extRandom // generate random number
|
||||
mov x5,x0
|
||||
|
||||
ldr x0,qAdrszMessNum
|
||||
bl affichageMess
|
||||
loop:
|
||||
mov x0,#STDIN // Linux input console
|
||||
ldr x1,qAdrsBuffer // buffer address
|
||||
mov x2,#BUFFERSIZE // buffer size
|
||||
mov x8,#READ // request to read datas
|
||||
svc 0 // call system
|
||||
ldr x1,qAdrsBuffer // buffer address
|
||||
mov x2,#0 // end of string
|
||||
sub x0,x0,#1 // replace character 0xA
|
||||
strb w2,[x1,x0] // store byte at the end of input string (x0 contains number of characters)
|
||||
ldr x0,qAdrsBuffer
|
||||
bl conversionAtoD // call routine conversion ascii to décimal
|
||||
cmp x0,x5
|
||||
beq 1f
|
||||
ldr x0,qAdrszMessError // not Ok
|
||||
bl affichageMess
|
||||
b loop
|
||||
1:
|
||||
ldr x0,qAdrszMessSucces // ok
|
||||
bl affichageMess
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0, #0 // return code
|
||||
mov x8, #EXIT // request to exit program
|
||||
svc 0 // perform system call
|
||||
qAdrszMessNum: .quad szMessNum
|
||||
qAdrszMessError: .quad szMessError
|
||||
qAdrszMessSucces: .quad szMessSucces
|
||||
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
qAdrsBuffer: .quad sBuffer
|
||||
/******************************************************************/
|
||||
/* random number */
|
||||
/******************************************************************/
|
||||
/* x0 contains inferior value */
|
||||
/* x1 contains maxi value */
|
||||
/* x0 return random number */
|
||||
extRandom:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x8,[sp,-16]! // save registers
|
||||
stp x19,x20,[sp,-16]! // save registers
|
||||
sub sp,sp,16 // reserve 16 octets on stack
|
||||
mov x19,x0
|
||||
add x20,x1,1
|
||||
mov x0,sp // store result on stack
|
||||
mov x1,8 // length 8 bytes
|
||||
mov x2,0
|
||||
mov x8,278 // call system Linux 64 bits Urandom
|
||||
svc 0
|
||||
mov x0,sp // load résult on stack
|
||||
ldr x0,[x0]
|
||||
sub x2,x20,x19 // calculation of the range of values
|
||||
udiv x1,x0,x2 // calculation range modulo
|
||||
msub x0,x1,x2,x0
|
||||
add x0,x0,x19 // and add inferior value
|
||||
100:
|
||||
add sp,sp,16 // alignement stack
|
||||
ldp x19,x20,[sp],16 // restaur 2 registers
|
||||
ldp x2,x8,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // retour adresse lr x30
|
||||
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
25
Task/Guess-the-number/ABAP/guess-the-number.abap
Normal file
25
Task/Guess-the-number/ABAP/guess-the-number.abap
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
REPORT guess_the_number.
|
||||
|
||||
DATA prng TYPE REF TO cl_abap_random_int.
|
||||
|
||||
cl_abap_random_int=>create(
|
||||
EXPORTING
|
||||
seed = cl_abap_random=>seed( )
|
||||
min = 1
|
||||
max = 10
|
||||
RECEIVING
|
||||
prng = prng ).
|
||||
|
||||
DATA(number) = prng->get_next( ).
|
||||
|
||||
DATA(field) = VALUE i( ).
|
||||
|
||||
cl_demo_input=>add_field( EXPORTING text = |Choice one number between 1 and 10| CHANGING field = field ).
|
||||
cl_demo_input=>request( ).
|
||||
|
||||
WHILE number <> field.
|
||||
cl_demo_input=>add_field( EXPORTING text = |You miss, try again| CHANGING field = field ).
|
||||
cl_demo_input=>request( ).
|
||||
ENDWHILE.
|
||||
|
||||
cl_demo_output=>display( |Well Done| ).
|
||||
19
Task/Guess-the-number/ALGOL-68/guess-the-number.alg
Normal file
19
Task/Guess-the-number/ALGOL-68/guess-the-number.alg
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
main:
|
||||
(
|
||||
INT n;
|
||||
INT g;
|
||||
n := ENTIER (random*10+1);
|
||||
PROC puts = (STRING string)VOID: putf(standout, ($gl$,string));
|
||||
puts("I'm thinking of a number between 1 and 10.");
|
||||
puts("Try to guess it! ");
|
||||
DO
|
||||
readf(($g$, g));
|
||||
IF g = n THEN break
|
||||
ELSE
|
||||
puts("That's not my number. ");
|
||||
puts("Try another guess!")
|
||||
FI
|
||||
OD;
|
||||
break:
|
||||
puts("You have won! ")
|
||||
)
|
||||
19
Task/Guess-the-number/AWK/guess-the-number.awk
Normal file
19
Task/Guess-the-number/AWK/guess-the-number.awk
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# syntax: GAWK -f GUESS_THE_NUMBER.AWK
|
||||
BEGIN {
|
||||
srand()
|
||||
n = int(rand() * 10) + 1
|
||||
print("I am thinking of a number between 1 and 10. Try to guess it.")
|
||||
while (1) {
|
||||
getline ans
|
||||
if (ans !~ /^[0-9]+$/) {
|
||||
print("Your input was not a number. Try again.")
|
||||
continue
|
||||
}
|
||||
if (n == ans) {
|
||||
print("Well done you.")
|
||||
break
|
||||
}
|
||||
print("Incorrect. Try again.")
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
15
Task/Guess-the-number/Action-/guess-the-number.action
Normal file
15
Task/Guess-the-number/Action-/guess-the-number.action
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
PROC Main()
|
||||
BYTE x,n,min=[1],max=[10]
|
||||
|
||||
PrintF("Try to guess a number %B-%B: ",min,max)
|
||||
x=Rand(max-min+1)+min
|
||||
DO
|
||||
n=InputB()
|
||||
IF n=x THEN
|
||||
PrintE("Well guessed!")
|
||||
EXIT
|
||||
ELSE
|
||||
Print("Incorrect. Try again: ")
|
||||
FI
|
||||
OD
|
||||
RETURN
|
||||
92
Task/Guess-the-number/Ada/guess-the-number.ada
Normal file
92
Task/Guess-the-number/Ada/guess-the-number.ada
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
with Ada.Numerics.Discrete_Random;
|
||||
with Ada.Text_IO;
|
||||
procedure Guess_Number is
|
||||
subtype Number is Integer range 1 .. 10;
|
||||
package Number_IO is new Ada.Text_IO.Integer_IO (Number);
|
||||
package Number_RNG is new Ada.Numerics.Discrete_Random (Number);
|
||||
Generator : Number_RNG.Generator;
|
||||
My_Number : Number;
|
||||
Your_Guess : Number;
|
||||
begin
|
||||
Number_RNG.Reset (Generator);
|
||||
My_Number := Number_RNG.Random (Generator);
|
||||
Ada.Text_IO.Put_Line ("Guess my number!");
|
||||
loop
|
||||
Ada.Text_IO.Put ("Your guess: ");
|
||||
Number_IO.Get (Your_Guess);
|
||||
exit when Your_Guess = My_Number;
|
||||
Ada.Text_IO.Put_Line ("Wrong, try again!");
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line ("Well guessed!");
|
||||
end Guess_Number;
|
||||
|
||||
-------------------------------------------------------------------------------------------------------
|
||||
-- Another version ------------------------------------------------------------------------------------
|
||||
-------------------------------------------------------------------------------------------------------
|
||||
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
||||
with Ada.Numerics.Discrete_Random;
|
||||
|
||||
-- procedure main - begins program execution
|
||||
procedure main is
|
||||
guess : Integer := 0;
|
||||
counter : Integer := 0;
|
||||
theNumber : Integer := 0;
|
||||
|
||||
-- function generate number - creates and returns a random number between the
|
||||
-- ranges of 1 to 100
|
||||
function generateNumber return Integer is
|
||||
type randNum is new Integer range 1 .. 100;
|
||||
package Rand_Int is new Ada.Numerics.Discrete_Random(randNum);
|
||||
use Rand_Int;
|
||||
gen : Generator;
|
||||
numb : randNum;
|
||||
|
||||
begin
|
||||
Reset(gen);
|
||||
|
||||
numb := Random(gen);
|
||||
|
||||
return Integer(numb);
|
||||
end generateNumber;
|
||||
|
||||
-- procedure intro - prints text welcoming the player to the game
|
||||
procedure intro is
|
||||
begin
|
||||
Put_Line("Welcome to Guess the Number");
|
||||
Put_Line("===========================");
|
||||
New_Line;
|
||||
|
||||
Put_Line("Try to guess the number. It is in the range of 1 to 100.");
|
||||
Put_Line("Can you guess it in the least amount of tries possible?");
|
||||
New_Line;
|
||||
end intro;
|
||||
|
||||
begin
|
||||
New_Line;
|
||||
|
||||
intro;
|
||||
theNumber := generateNumber;
|
||||
|
||||
-- main game loop
|
||||
while guess /= theNumber loop
|
||||
Put("Enter a guess: ");
|
||||
guess := integer'value(Get_Line);
|
||||
|
||||
counter := counter + 1;
|
||||
|
||||
if guess > theNumber then
|
||||
Put_Line("Too high!");
|
||||
elsif guess < theNumber then
|
||||
Put_Line("Too low!");
|
||||
end if;
|
||||
end loop;
|
||||
|
||||
New_Line;
|
||||
|
||||
Put_Line("CONGRATULATIONS! You guessed it!");
|
||||
Put_Line("It took you a total of " & integer'image(counter) & " attempts.");
|
||||
|
||||
New_Line;
|
||||
end main;
|
||||
21
Task/Guess-the-number/Aime/guess-the-number.aime
Normal file
21
Task/Guess-the-number/Aime/guess-the-number.aime
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
file f;
|
||||
integer n;
|
||||
text s;
|
||||
|
||||
f.stdin;
|
||||
|
||||
n = irand(1, 10);
|
||||
o_text("I'm thinking of a number between 1 and 10.\n");
|
||||
o_text("Try to guess it!\n");
|
||||
while (1) {
|
||||
f_look(f, "0123456789");
|
||||
f_near(f, "0123456789", s);
|
||||
if (atoi(s) != n) {
|
||||
o_text("That's not my number.\n");
|
||||
o_text("Try another guess!\n");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
o_text("You have won!\n");
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
on run
|
||||
-- define the number to be guessed
|
||||
set numberToGuess to (random number from 1 to 10)
|
||||
-- prepare a variable to store the user's answer
|
||||
set guessedNumber to missing value
|
||||
-- start a loop (will be exited by using "exit repeat" after a correct guess)
|
||||
repeat
|
||||
try
|
||||
-- ask the user for his/her guess
|
||||
set usersChoice to (text returned of (display dialog "Guess the number between 1 and 10 inclusive" default answer "" buttons {"Check"} default button "Check"))
|
||||
-- try to convert the given answer to an integer
|
||||
set guessedNumber to usersChoice as integer
|
||||
on error
|
||||
-- something gone wrong, overwrite user's answer with a non-matching value
|
||||
set guessedNumber to missing value
|
||||
end try
|
||||
-- decide if the user's answer was the right one
|
||||
if guessedNumber is equal to numberToGuess then
|
||||
-- the user guessed the correct number and gets informed
|
||||
display dialog "Well guessed! The number was " & numberToGuess buttons {"OK"} default button "OK"
|
||||
-- exit the loop (quits this application)
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
end run
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
-- GUESS THE NUMBER ----------------------------------------------------------
|
||||
|
||||
on run
|
||||
-- isMatch :: Int -> Bool
|
||||
script isMatch
|
||||
on |λ|(x)
|
||||
tell x to its guess = its secret
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- challenge :: () -> {secret: Int, guess: Int}
|
||||
script challenge
|
||||
on response()
|
||||
set v to (text returned of (display dialog ¬
|
||||
"Guess the number in range 1-10" default answer ¬
|
||||
"" buttons {"Esc", "Check"} default button ¬
|
||||
"Check" cancel button "Esc"))
|
||||
|
||||
if isInteger(v) then
|
||||
v as integer
|
||||
else
|
||||
-1
|
||||
end if
|
||||
end response
|
||||
|
||||
on |λ|(rec)
|
||||
{secret:(random number from 1 to 10), guess:response() ¬
|
||||
of challenge, attempts:(attempts of rec) + 1}
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
|
||||
-- MAIN LOOP -------------------------------------------------------------
|
||||
set rec to |until|(isMatch, challenge, {secret:-1, guess:0, attempts:0})
|
||||
|
||||
display dialog (((guess of rec) as string) & ": Well guessed ! " & ¬
|
||||
linefeed & linefeed & "Attempts: " & (attempts of rec))
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- isInteger :: a -> Bool
|
||||
on isInteger(e)
|
||||
try
|
||||
set n to e as integer
|
||||
on error
|
||||
return false
|
||||
end try
|
||||
true
|
||||
end isInteger
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
on |until|(p, f, x)
|
||||
set mp to mReturn(p)
|
||||
set v to x
|
||||
|
||||
tell mReturn(f)
|
||||
repeat until mp's |λ|(v)
|
||||
set v to |λ|(v)
|
||||
end repeat
|
||||
end tell
|
||||
return v
|
||||
end |until|
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
10 N% = RND(1) * 10 + 1
|
||||
20 PRINT "A NUMBER FROM 1 ";
|
||||
30 PRINT "TO 10 HAS BEEN ";
|
||||
40 PRINT "RANDOMLY CHOSEN."
|
||||
50 FOR Q = 0 TO 1 STEP 0
|
||||
60 INPUT "ENTER A GUESS. "; G%
|
||||
70 Q = G% = N%
|
||||
80 NEXT
|
||||
90 PRINT "WELL GUESSED!"
|
||||
7
Task/Guess-the-number/Arturo/guess-the-number.arturo
Normal file
7
Task/Guess-the-number/Arturo/guess-the-number.arturo
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
n: random 1 10
|
||||
|
||||
while [notEqual? to :integer input "Guess the number: " n] [
|
||||
print "Wrong!"
|
||||
]
|
||||
|
||||
print "Well guessed!"
|
||||
14
Task/Guess-the-number/AutoHotkey/guess-the-number.ahk
Normal file
14
Task/Guess-the-number/AutoHotkey/guess-the-number.ahk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Random, rand, 1, 10 ; This stores a number between 1 and 10 in the var rand using the Mersenne Twister
|
||||
msgbox I am thinking of a number between 1 and 10.
|
||||
|
||||
loop
|
||||
{
|
||||
InputBox, guess, Guess the number, Type in a number from 1 to 10
|
||||
If (guess = rand)
|
||||
{
|
||||
msgbox Well Guessed!
|
||||
Break ; exits loop
|
||||
}
|
||||
Else
|
||||
Msgbox Try again.
|
||||
}
|
||||
6
Task/Guess-the-number/AutoIt/guess-the-number.autoit
Normal file
6
Task/Guess-the-number/AutoIt/guess-the-number.autoit
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$irnd = Random(1, 10, 1)
|
||||
$iinput = -1
|
||||
While $input <> $irnd
|
||||
$iinput = InputBox("Choose a number", "Please chosse a Number between 1 and 10")
|
||||
WEnd
|
||||
MsgBox(0, "Success", "Well guessed!")
|
||||
9
Task/Guess-the-number/BASIC256/guess-the-number.basic
Normal file
9
Task/Guess-the-number/BASIC256/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
n = int(rand * 10) + 1
|
||||
print "I am thinking of a number from 1 to 10"
|
||||
do
|
||||
input "Guess it > ",g
|
||||
if g <> n then
|
||||
print "No luck, Try again."
|
||||
endif
|
||||
until g = n
|
||||
print "Yea! You guessed my number."
|
||||
10
Task/Guess-the-number/BBC-BASIC/guess-the-number.basic
Normal file
10
Task/Guess-the-number/BBC-BASIC/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
choose% = RND(10)
|
||||
REPEAT
|
||||
INPUT "Guess a number between 1 and 10: " guess%
|
||||
IF guess% = choose% THEN
|
||||
PRINT "Well guessed!"
|
||||
END
|
||||
ELSE
|
||||
PRINT "Sorry, try again"
|
||||
ENDIF
|
||||
UNTIL FALSE
|
||||
7
Task/Guess-the-number/Batch-File/guess-the-number.bat
Normal file
7
Task/Guess-the-number/Batch-File/guess-the-number.bat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
@echo off
|
||||
set /a answer=%random%%%(10-1+1)+1
|
||||
set /p guess=Pick a number between 1 and 10:
|
||||
:loop
|
||||
if %guess%==%answer% (echo Well guessed!
|
||||
pause) else (set /p guess=Nope, guess again:
|
||||
goto loop)
|
||||
18
Task/Guess-the-number/Befunge/guess-the-number.bf
Normal file
18
Task/Guess-the-number/Befunge/guess-the-number.bf
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
v RNG anthouse
|
||||
> v ,,,,,,<
|
||||
v?v ,
|
||||
vvvvv ,
|
||||
v?????v ,
|
||||
vvvvvvvvv ,
|
||||
>?????????v ,
|
||||
>vvvvvvvvvv< ,
|
||||
>??????????< ,
|
||||
>vvvvvvvvvv< ,
|
||||
>??????????< ,
|
||||
>vvvvvvvvvv< ^"Well guessed!"<
|
||||
>??????????< >"oN",,91v actual game unit
|
||||
1234567899 ^_91+"!" ^
|
||||
1 ^-g22<&<>+,v
|
||||
+>,,,,,,,,,,,,,,,,^
|
||||
>>>>>>>>>v^"guessthenumber!"+19<
|
||||
RNG unit > 22p ^
|
||||
12
Task/Guess-the-number/Bracmat/guess-the-number.bracmat
Normal file
12
Task/Guess-the-number/Bracmat/guess-the-number.bracmat
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
( ( GuessTheNumber
|
||||
= mynumber
|
||||
. clk$:?mynumber
|
||||
& mod$(!mynumber*den$!mynumber.10)+1:?mynumber
|
||||
& whl
|
||||
' ( put'"Guess my number:"
|
||||
& get':~!mynumber:?K
|
||||
)
|
||||
& out'"Well guessed!"
|
||||
)
|
||||
& GuessTheNumber$
|
||||
);
|
||||
9
Task/Guess-the-number/Brat/guess-the-number.brat
Normal file
9
Task/Guess-the-number/Brat/guess-the-number.brat
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
number = random 10
|
||||
|
||||
p "Guess a number between 1 and 10."
|
||||
|
||||
until {
|
||||
true? ask("Guess: ").to_i == number
|
||||
{ p "Well guessed!"; true }
|
||||
{ p "Guess again!" }
|
||||
}
|
||||
21
Task/Guess-the-number/C++/guess-the-number.cpp
Normal file
21
Task/Guess-the-number/C++/guess-the-number.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(0));
|
||||
int n = 1 + (rand() % 10);
|
||||
int g;
|
||||
std::cout << "I'm thinking of a number between 1 and 10.\nTry to guess it! ";
|
||||
while(true)
|
||||
{
|
||||
std::cin >> g;
|
||||
if (g == n)
|
||||
break;
|
||||
else
|
||||
std::cout << "That's not my number.\nTry another guess! ";
|
||||
}
|
||||
std::cout << "You've guessed my number!";
|
||||
return 0;
|
||||
}
|
||||
14
Task/Guess-the-number/C-Shell/guess-the-number.csh
Normal file
14
Task/Guess-the-number/C-Shell/guess-the-number.csh
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#!/bin/csh -f
|
||||
# Guess the number
|
||||
|
||||
# jot(1) a random number. If jot(1) not found, exit now.
|
||||
@ number = `jot -r 1 1 10` || exit
|
||||
|
||||
echo 'I have thought of a number. Try to guess it!'
|
||||
echo 'Enter an integer from 1 to 10.'
|
||||
@ guess = "$<"
|
||||
while ( $guess != $number )
|
||||
echo 'Sorry, the guess was wrong! Try again!'
|
||||
@ guess = "$<"
|
||||
end
|
||||
echo 'Well done! You guessed it.'
|
||||
19
Task/Guess-the-number/C-sharp/guess-the-number-1.cs
Normal file
19
Task/Guess-the-number/C-sharp/guess-the-number-1.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
|
||||
class GuessTheNumberGame
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
int randomNumber = new Random().Next(1, 11);
|
||||
|
||||
Console.WriteLine("I'm thinking of a number between 1 and 10. Can you guess it?");
|
||||
while(true)
|
||||
{
|
||||
Console.Write("Guess: ");
|
||||
if (int.Parse(Console.ReadLine()) == randomNumber)
|
||||
break;
|
||||
Console.WriteLine("That's not it. Guess again.");
|
||||
}
|
||||
Console.WriteLine("Congrats!! You guessed right!");
|
||||
}
|
||||
};
|
||||
26
Task/Guess-the-number/C-sharp/guess-the-number-2.cs
Normal file
26
Task/Guess-the-number/C-sharp/guess-the-number-2.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using System; using System.Text; //daMilliard.cs
|
||||
namespace DANILIN
|
||||
|
||||
{ class Program
|
||||
{ static void Main(string[] args)
|
||||
{ Random rand = new Random();int t=0;
|
||||
int h2=100000000; int h1=0; int f=0;
|
||||
int comp = rand.Next(h2);
|
||||
int human = rand.Next(h2);
|
||||
|
||||
while (f<1)
|
||||
{ Console.WriteLine();Console.Write(t);
|
||||
Console.Write(" ");Console.Write(comp);
|
||||
Console.Write(" ");Console.Write(human);
|
||||
|
||||
if(comp < human)
|
||||
{ Console.Write(" MORE");
|
||||
int a=comp; comp=(comp+h2)/2; h1=a; }
|
||||
|
||||
else if(comp > human)
|
||||
{ Console.Write(" less");
|
||||
int a=comp; comp=(h1+comp)/2; h2=a;}
|
||||
|
||||
else {Console.Write(" win by ");
|
||||
Console.Write(t); Console.Write(" steps");f=1;}
|
||||
t++; }}}}
|
||||
30
Task/Guess-the-number/C/guess-the-number.c
Normal file
30
Task/Guess-the-number/C/guess-the-number.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int n;
|
||||
int g;
|
||||
char c;
|
||||
|
||||
srand(time(NULL));
|
||||
n = 1 + (rand() % 10);
|
||||
|
||||
puts("I'm thinking of a number between 1 and 10.");
|
||||
puts("Try to guess it:");
|
||||
|
||||
while (1) {
|
||||
if (scanf("%d", &g) != 1) {
|
||||
/* ignore one char, in case user gave a non-number */
|
||||
scanf("%c", &c);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (g == n) {
|
||||
puts("Correct!");
|
||||
return 0;
|
||||
}
|
||||
puts("That's not my number. Try another guess:");
|
||||
}
|
||||
}
|
||||
25
Task/Guess-the-number/COBOL/guess-the-number.cobol
Normal file
25
Task/Guess-the-number/COBOL/guess-the-number.cobol
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Guess-The-Number.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 Random-Num PIC 99.
|
||||
01 Guess PIC 99.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
COMPUTE Random-Num = 1 + (FUNCTION RANDOM * 10)
|
||||
DISPLAY "Guess a number between 1 and 10:"
|
||||
|
||||
PERFORM FOREVER
|
||||
ACCEPT Guess
|
||||
|
||||
IF Guess = Random-Num
|
||||
DISPLAY "Well guessed!"
|
||||
EXIT PERFORM
|
||||
ELSE
|
||||
DISPLAY "That isn't it. Try again."
|
||||
END-IF
|
||||
END-PERFORM
|
||||
|
||||
GOBACK
|
||||
.
|
||||
10
Task/Guess-the-number/Clojure/guess-the-number.clj
Normal file
10
Task/Guess-the-number/Clojure/guess-the-number.clj
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(def target (inc (rand-int 10))
|
||||
|
||||
(loop [n 0]
|
||||
(println "Guess a number between 1 and 10 until you get it right:")
|
||||
(let [guess (read)]
|
||||
(if (= guess target)
|
||||
(printf "Correct on the %d guess.\n" n)
|
||||
(do
|
||||
(println "Try again")
|
||||
(recur (inc n))))))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
num = Math.ceil(Math.random() * 10)
|
||||
guess = prompt "Guess the number. (1-10)"
|
||||
while parseInt(guess) isnt num
|
||||
guess = prompt "YOU LOSE! Guess again. (1-10)"
|
||||
alert "Well guessed!"
|
||||
19
Task/Guess-the-number/CoffeeScript/guess-the-number-2.coffee
Normal file
19
Task/Guess-the-number/CoffeeScript/guess-the-number-2.coffee
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# This shows how to do simple REPL-like I/O in node.js.
|
||||
readline = require "readline"
|
||||
|
||||
do ->
|
||||
number = Math.ceil(10 * Math.random())
|
||||
interface = readline.createInterface process.stdin, process.stdout
|
||||
|
||||
guess = ->
|
||||
interface.question "Guess the number between 1 and 10: ", (answer) ->
|
||||
if parseInt(answer) == number
|
||||
# These lines allow the program to terminate.
|
||||
console.log "GOT IT!"
|
||||
interface.close()
|
||||
process.stdin.destroy()
|
||||
else
|
||||
console.log "Sorry, guess again"
|
||||
guess()
|
||||
|
||||
guess()
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
10 n% = int(rnd(1)*10)+1
|
||||
20 print chr$(147);chr$(14)
|
||||
30 print "I have chosen a number from 1 to 10."
|
||||
40 print
|
||||
50 for q = 0 TO -1 step 0
|
||||
60 input "What is your guess";g%
|
||||
70 q = g% = n%
|
||||
80 next
|
||||
90 print "WELL GUESSED!"
|
||||
9
Task/Guess-the-number/Common-Lisp/guess-the-number.lisp
Normal file
9
Task/Guess-the-number/Common-Lisp/guess-the-number.lisp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun guess-the-number (max)
|
||||
(format t "Try to guess a number from 1 to ~a!~%Guess? " max)
|
||||
(loop with num = (1+ (random max))
|
||||
for guess = (read)
|
||||
as num-guesses from 1
|
||||
until (and (numberp guess) (= guess num))
|
||||
do (format t "Your guess was wrong. Try again.~%Guess? ")
|
||||
(force-output)
|
||||
finally (format t "Well guessed! You took ~a ~:*~[~;try~:;tries~].~%" num-guesses)))
|
||||
4
Task/Guess-the-number/Crystal/guess-the-number.crystal
Normal file
4
Task/Guess-the-number/Crystal/guess-the-number.crystal
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
n = rand(1..10)
|
||||
puts "Guess the number: 1..10"
|
||||
until gets.to_s.to_i == n; puts "Wrong! Guess again: " end
|
||||
puts "Well guessed!"
|
||||
8
Task/Guess-the-number/D/guess-the-number.d
Normal file
8
Task/Guess-the-number/D/guess-the-number.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
void main() {
|
||||
immutable num = uniform(1, 10).text;
|
||||
|
||||
do write("What's next guess (1 - 9)? ");
|
||||
while (readln.strip != num);
|
||||
|
||||
writeln("Yep, you guessed my ", num);
|
||||
}
|
||||
6
Task/Guess-the-number/DCL/guess-the-number.dcl
Normal file
6
Task/Guess-the-number/DCL/guess-the-number.dcl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ time = f$time()
|
||||
$ number = f$extract( f$length( time ) - 1, 1, time ) + 1
|
||||
$ loop:
|
||||
$ inquire guess "enter a guess (integer 1-10) "
|
||||
$ if guess .nes. number then $ goto loop
|
||||
$ write sys$output "Well guessed!"
|
||||
9
Task/Guess-the-number/Dart/guess-the-number.dart
Normal file
9
Task/Guess-the-number/Dart/guess-the-number.dart
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import 'dart:math';
|
||||
import 'dart:io';
|
||||
|
||||
main() {
|
||||
final n = (1 + new Random().nextInt(10)).toString();
|
||||
print("Guess which number I've chosen in the range 1 to 10");
|
||||
do { stdout.write(" Your guess : "); } while (n != stdin.readLineSync());
|
||||
print("\nWell guessed!");
|
||||
}
|
||||
19
Task/Guess-the-number/Delphi/guess-the-number.delphi
Normal file
19
Task/Guess-the-number/Delphi/guess-the-number.delphi
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
program GuessTheNumber;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils;
|
||||
|
||||
var
|
||||
theDigit : String ;
|
||||
theAnswer : String ;
|
||||
|
||||
begin
|
||||
Randomize ;
|
||||
theDigit := IntToStr(Random(9)+1) ;
|
||||
while ( theAnswer <> theDigit ) do Begin
|
||||
Writeln('Please enter a digit between 1 and 10' ) ;
|
||||
Readln(theAnswer);
|
||||
End ;
|
||||
Writeln('Congratulations' ) ;
|
||||
end.
|
||||
29
Task/Guess-the-number/ERRE/guess-the-number.erre
Normal file
29
Task/Guess-the-number/ERRE/guess-the-number.erre
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
PROGRAM GUESS_NUMBER
|
||||
|
||||
!
|
||||
! for rosettacode.org
|
||||
!
|
||||
|
||||
BEGIN
|
||||
|
||||
RANDOMIZE(TIMER)
|
||||
N=0
|
||||
R=INT(RND(1)*100+1) ! RND function gives a random number from 0 to 1
|
||||
G=0
|
||||
C$=""
|
||||
|
||||
WHILE G<>R DO
|
||||
INPUT("Pick a number between 1 and 100";G)
|
||||
IF G=R THEN
|
||||
PRINT("You got it!")
|
||||
N+=1
|
||||
PRINT("It took";N;"tries to pick the right Number.")
|
||||
ELSIF G<R THEN
|
||||
PRINT("Try a larger number.")
|
||||
N+=1
|
||||
ELSE
|
||||
PRINT("Try a smaller number.")
|
||||
N+=1
|
||||
END IF
|
||||
END WHILE
|
||||
END PROGRAM
|
||||
10
Task/Guess-the-number/EasyLang/guess-the-number.easy
Normal file
10
Task/Guess-the-number/EasyLang/guess-the-number.easy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
n = random 10
|
||||
write "Guess a number between 1 and 10: "
|
||||
repeat
|
||||
g = number input
|
||||
write g
|
||||
until g = n
|
||||
print " is wrong"
|
||||
write "try again: "
|
||||
.
|
||||
print " is correct. Well guessed!"
|
||||
26
Task/Guess-the-number/Eiffel/guess-the-number-1.e
Normal file
26
Task/Guess-the-number/Eiffel/guess-the-number-1.e
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
number_to_guess: INTEGER
|
||||
do
|
||||
number_to_guess := (create {RANDOMIZER}).random_integer_in_range (1 |..| 10)
|
||||
from
|
||||
print ("Please guess the number!%N")
|
||||
io.read_integer
|
||||
until
|
||||
io.last_integer = number_to_guess
|
||||
loop
|
||||
print ("Please, guess again!%N")
|
||||
io.read_integer
|
||||
end
|
||||
print ("Well guessed!%N")
|
||||
end
|
||||
|
||||
end
|
||||
42
Task/Guess-the-number/Eiffel/guess-the-number-2.e
Normal file
42
Task/Guess-the-number/Eiffel/guess-the-number-2.e
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
class
|
||||
RANDOMIZER
|
||||
|
||||
inherit
|
||||
ANY
|
||||
redefine
|
||||
default_create
|
||||
end
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
default_create
|
||||
-- <Precursor>
|
||||
local
|
||||
time: TIME
|
||||
do
|
||||
sequence.do_nothing
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
random_integer_in_range (a_range: INTEGER_INTERVAL): INTEGER
|
||||
do
|
||||
Result := (sequence.double_i_th (1) * a_range.upper).truncated_to_integer + a_range.lower
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
sequence: RANDOM
|
||||
local
|
||||
seed: INTEGER_32
|
||||
time: TIME
|
||||
once
|
||||
create time.make_now
|
||||
seed := time.hour *
|
||||
(60 + time.minute) *
|
||||
(60 + time.second) *
|
||||
(1000 + time.milli_second)
|
||||
create Result.set_seed (seed)
|
||||
end
|
||||
|
||||
end
|
||||
22
Task/Guess-the-number/Elena/guess-the-number.elena
Normal file
22
Task/Guess-the-number/Elena/guess-the-number.elena
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
int randomNumber := randomGenerator.eval(1,10);
|
||||
console.printLine("I'm thinking of a number between 1 and 10. Can you guess it?");
|
||||
bool numberCorrect := false;
|
||||
until(numberCorrect)
|
||||
{
|
||||
console.print("Guess: ");
|
||||
int userGuess := console.readLine().toInt();
|
||||
if (randomNumber == userGuess)
|
||||
{
|
||||
numberCorrect := true;
|
||||
console.printLine("Congrats!! You guessed right!")
|
||||
}
|
||||
else
|
||||
{
|
||||
console.printLine("That's not it. Guess again.")
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Task/Guess-the-number/Elixir/guess-the-number.elixir
Normal file
21
Task/Guess-the-number/Elixir/guess-the-number.elixir
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
defmodule GuessingGame do
|
||||
def play do
|
||||
play(Enum.random(1..10))
|
||||
end
|
||||
|
||||
defp play(number) do
|
||||
guess = Integer.parse(IO.gets "Guess a number (1-10): ")
|
||||
case guess do
|
||||
{^number, _} ->
|
||||
IO.puts "Well guessed!"
|
||||
{n, _} when n in 1..10 ->
|
||||
IO.puts "That's not it."
|
||||
play(number)
|
||||
_ ->
|
||||
IO.puts "Guess not in valid range."
|
||||
play(number)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
GuessingGame.play
|
||||
4
Task/Guess-the-number/Emacs-Lisp/guess-the-number.l
Normal file
4
Task/Guess-the-number/Emacs-Lisp/guess-the-number.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(let ((number (1+ (random 10))))
|
||||
(while (not (= (read-number "Guess the number ") number))
|
||||
(message "Wrong, try again."))
|
||||
(message "Well guessed! %d" number))
|
||||
18
Task/Guess-the-number/Erlang/guess-the-number.erl
Normal file
18
Task/Guess-the-number/Erlang/guess-the-number.erl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
% Implemented by Arjun Sunel
|
||||
-module(guess_the_number).
|
||||
-export([main/0]).
|
||||
|
||||
main() ->
|
||||
io:format("Guess my number between 1 and 10 until you get it right:\n"),
|
||||
N = random:uniform(10),
|
||||
guess(N).
|
||||
|
||||
guess(N) ->
|
||||
{ok, [K]} = io:fread("Guess number : ","~d"),
|
||||
|
||||
if
|
||||
K=:=N ->
|
||||
io:format("Well guessed!!\n");
|
||||
true ->
|
||||
guess(N)
|
||||
end.
|
||||
17
Task/Guess-the-number/Euphoria/guess-the-number.euphoria
Normal file
17
Task/Guess-the-number/Euphoria/guess-the-number.euphoria
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
include get.e
|
||||
|
||||
integer n,g
|
||||
n = rand(10)
|
||||
|
||||
puts(1,"I have thought of a number from 1 to 10.\n")
|
||||
puts(1,"Try to guess it!\n")
|
||||
|
||||
while 1 do
|
||||
g = prompt_number("Enter your guess: ",{1,10})
|
||||
if n = g then
|
||||
exit
|
||||
end if
|
||||
puts(1,"Your guess was wrong. Try again!\n")
|
||||
end while
|
||||
|
||||
puts(1,"Well done! You guessed it.")
|
||||
20
Task/Guess-the-number/Factor/guess-the-number.factor
Normal file
20
Task/Guess-the-number/Factor/guess-the-number.factor
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
USING: io random math math.parser kernel formatting ;
|
||||
IN: guess-the-number
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: gen-number ( -- n )
|
||||
10 random 1 + ;
|
||||
|
||||
: make-guess ( n -- n ? )
|
||||
dup readln string>number = ;
|
||||
|
||||
: play-game ( n -- n )
|
||||
[ make-guess ]
|
||||
[ "Guess a number between 1 and 10:" print flush ] do until ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: guess-the-number ( -- )
|
||||
gen-number play-game
|
||||
"Yes, the number was %d!\n" printf ;
|
||||
20
Task/Guess-the-number/Fantom/guess-the-number.fantom
Normal file
20
Task/Guess-the-number/Fantom/guess-the-number.fantom
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
Str target := (1..10).random.toStr
|
||||
Str guess := ""
|
||||
while (guess != target)
|
||||
{
|
||||
echo ("Enter a guess: ")
|
||||
guess = Env.cur.in.readLine
|
||||
if (guess.trim == target) // 'trim' to remove any spaces/newline
|
||||
{
|
||||
echo ("Well guessed!")
|
||||
break
|
||||
}
|
||||
else
|
||||
echo ("Failed - try again")
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Task/Guess-the-number/Forth/guess-the-number.fth
Normal file
11
Task/Guess-the-number/Forth/guess-the-number.fth
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
\ tested with GForth 0.7.0
|
||||
: RND ( -- n) TIME&DATE 2DROP 2DROP DROP 10 MOD ; \ crude random number
|
||||
: ASK ( -- ) CR ." Guess a number between 1 and 10? " ;
|
||||
: GUESS ( -- n) PAD DUP 4 ACCEPT EVALUATE ;
|
||||
: REPLY ( n n' -- n) 2DUP <> IF CR ." No, it's not " DUP . THEN ;
|
||||
|
||||
: GAME ( -- )
|
||||
RND
|
||||
BEGIN ASK GUESS REPLY OVER = UNTIL
|
||||
CR ." Yes it was " .
|
||||
CR ." Good guess!" ;
|
||||
34
Task/Guess-the-number/Fortran/guess-the-number.f
Normal file
34
Task/Guess-the-number/Fortran/guess-the-number.f
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
program guess_the_number
|
||||
implicit none
|
||||
|
||||
integer :: guess
|
||||
real :: r
|
||||
integer :: i, clock, count, n
|
||||
integer,dimension(:),allocatable :: seed
|
||||
|
||||
real,parameter :: rmax = 10
|
||||
|
||||
!initialize random number generator:
|
||||
call random_seed(size=n)
|
||||
allocate(seed(n))
|
||||
call system_clock(count)
|
||||
seed = count
|
||||
call random_seed(put=seed)
|
||||
deallocate(seed)
|
||||
|
||||
!pick a random number between 1 and rmax:
|
||||
call random_number(r) !r between 0.0 and 1.0
|
||||
i = int((rmax-1.0)*r + 1.0) !i between 1 and rmax
|
||||
|
||||
!get user guess:
|
||||
write(*,'(A)') 'I''m thinking of a number between 1 and 10.'
|
||||
do !loop until guess is correct
|
||||
write(*,'(A)',advance='NO') 'Enter Guess: '
|
||||
read(*,'(I5)') guess
|
||||
if (guess==i) exit
|
||||
write(*,*) 'Sorry, try again.'
|
||||
end do
|
||||
|
||||
write(*,*) 'You''ve guessed my number!'
|
||||
|
||||
end program guess_the_number
|
||||
14
Task/Guess-the-number/FreeBASIC/guess-the-number.basic
Normal file
14
Task/Guess-the-number/FreeBASIC/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Randomize
|
||||
Dim n As Integer = Int(Rnd * 10) + 1
|
||||
Dim guess As Integer
|
||||
Print "Guess which number I've chosen in the range 1 to 10"
|
||||
Print
|
||||
Do
|
||||
Input " Your guess : "; guess
|
||||
If n = guess Then
|
||||
Print "Well guessed!"
|
||||
End
|
||||
End If
|
||||
Loop
|
||||
13
Task/Guess-the-number/Frink/guess-the-number.frink
Normal file
13
Task/Guess-the-number/Frink/guess-the-number.frink
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// Guess a Number
|
||||
target = random[1,10] // Min and max are both inclusive for the random function
|
||||
guess = 0
|
||||
println["Welcome to guess a number! I've picked a number between 1 and 10. Try to guess it!"]
|
||||
while guess != target
|
||||
{
|
||||
guessStr = input["What is your guess?"]
|
||||
guess = parseInt[guessStr]
|
||||
if guess == undef
|
||||
println["$guessStr is not a valid guess. Please enter a number from 1 to 10."]
|
||||
else
|
||||
guess == target ? println["$guess is correct. Well guessed!"] : println["$guess is not correct. Guess again!"]
|
||||
}
|
||||
29
Task/Guess-the-number/FutureBasic/guess-the-number.basic
Normal file
29
Task/Guess-the-number/FutureBasic/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
void local fn BuildWindow
|
||||
window 1, @"Guess the number (1-10)", (0,0,480,270), NSWindowStyleMaskTitled
|
||||
textfield 1,,, (220,124,40,21)
|
||||
ControlSetAlignment( 1, NSTextAlignmentCenter )
|
||||
WindowMakeFirstResponder( 1, 1 )
|
||||
AppSetProperty( @"Number", @(rnd(10)) )
|
||||
end fn
|
||||
|
||||
void local fn DoDialog( ev as long, tag as long )
|
||||
select ( ev )
|
||||
case _btnClick
|
||||
select ( tag )
|
||||
case 1
|
||||
if ( fn ControlIntegerValue( 1 ) == fn NumberIntegerValue( fn AppProperty( @"Number" ) ) )
|
||||
alert 1,, @"Well guessed!",, @"Exit"
|
||||
end
|
||||
else
|
||||
textfield 1,, @""
|
||||
alert 1,, @"Wrong number!",, @"Try again"
|
||||
end if
|
||||
end select
|
||||
end select
|
||||
end fn
|
||||
|
||||
fn BuildWindow
|
||||
|
||||
on dialog fn DoDialog
|
||||
|
||||
HandleEvents
|
||||
9
Task/Guess-the-number/GML/guess-the-number.gml
Normal file
9
Task/Guess-the-number/GML/guess-the-number.gml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
var n, g;
|
||||
n = irandom_range(1,10);
|
||||
show_message("I'm thinking of a number from 1 to 10");
|
||||
g = get_integer("Please enter guess", 1);
|
||||
while(g != n)
|
||||
{
|
||||
g = get_integer("I'm sorry "+g+" is not my number, try again. Please enter guess", 1);
|
||||
}
|
||||
show_message("Well guessed!");
|
||||
6
Task/Guess-the-number/GW-BASIC/guess-the-number.basic
Normal file
6
Task/Guess-the-number/GW-BASIC/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
10 RANDOMIZE TIMER:N=INT(RND*10+1):G=0
|
||||
20 PRINT "Guess the number between 1 and 10."
|
||||
30 WHILE N<>G
|
||||
40 INPUT "Your guess? ",G
|
||||
50 WEND
|
||||
60 PRINT "That's correct!"
|
||||
15
Task/Guess-the-number/Gambas/guess-the-number.gambas
Normal file
15
Task/Guess-the-number/Gambas/guess-the-number.gambas
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Public Sub Form_Open()
|
||||
Dim byGuess, byGos As Byte
|
||||
Dim byNo As Byte = Rand(1, 10)
|
||||
Dim sHead As String = "Guess the number"
|
||||
|
||||
Repeat
|
||||
Inc byGos
|
||||
byGuess = InputBox("Guess the number between 1 and 10", sHead)
|
||||
sHead = "Sorry, have another go"
|
||||
Until byGuess = byNo
|
||||
|
||||
Message.Info("Well guessed! You took " & Str(byGos) & " gos to guess the number was " & Str(byNo), "OK")
|
||||
Me.Close
|
||||
|
||||
End
|
||||
23
Task/Guess-the-number/Go/guess-the-number.go
Normal file
23
Task/Guess-the-number/Go/guess-the-number.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Print("Guess number from 1 to 10: ")
|
||||
rand.Seed(time.Now().Unix())
|
||||
n := rand.Intn(10) + 1
|
||||
for guess := n; ; fmt.Print("No. Try again: ") {
|
||||
switch _, err := fmt.Scan(&guess); {
|
||||
case err != nil:
|
||||
fmt.Println("\n", err, "\nSo, bye.")
|
||||
return
|
||||
case guess == n:
|
||||
fmt.Println("Well guessed!")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Task/Guess-the-number/Groovy/guess-the-number.groovy
Normal file
10
Task/Guess-the-number/Groovy/guess-the-number.groovy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def random = new Random()
|
||||
def keyboard = new Scanner(System.in)
|
||||
def number = random.nextInt(10) + 1
|
||||
println "Guess the number which is between 1 and 10: "
|
||||
def guess = keyboard.nextInt()
|
||||
while (number != guess) {
|
||||
println "Guess again: "
|
||||
guess = keyboard.nextInt()
|
||||
}
|
||||
println "Hurray! You guessed correctly!"
|
||||
16
Task/Guess-the-number/Haskell/guess-the-number-1.hs
Normal file
16
Task/Guess-the-number/Haskell/guess-the-number-1.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import Control.Monad
|
||||
import System.Random
|
||||
|
||||
-- Repeat the action until the predicate is true.
|
||||
until_ act pred = act >>= pred >>= flip unless (until_ act pred)
|
||||
|
||||
answerIs ans guess
|
||||
| ans == guess = putStrLn "You got it!" >> return True
|
||||
| otherwise = putStrLn "Nope. Guess again." >> return False
|
||||
|
||||
ask = liftM read getLine
|
||||
|
||||
main = do
|
||||
ans <- randomRIO (1,10) :: IO Int
|
||||
putStrLn "Try to guess my secret number between 1 and 10."
|
||||
ask `until_` answerIs ans
|
||||
10
Task/Guess-the-number/Haskell/guess-the-number-2.hs
Normal file
10
Task/Guess-the-number/Haskell/guess-the-number-2.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import System.Random
|
||||
|
||||
main = randomRIO (1,10) >>= gameloop
|
||||
|
||||
gameloop :: Int -> IO ()
|
||||
gameloop r = do
|
||||
i <- fmap read getLine
|
||||
if i == r
|
||||
then putStrLn "You got it!"
|
||||
else putStrLn "Nope. Guess again." >> gameloop r
|
||||
17
Task/Guess-the-number/HolyC/guess-the-number.holyc
Normal file
17
Task/Guess-the-number/HolyC/guess-the-number.holyc
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
U8 n, *g;
|
||||
|
||||
n = 1 + RandU16 % 10;
|
||||
|
||||
Print("I'm thinking of a number between 1 and 10.\n");
|
||||
Print("Try to guess it:\n");
|
||||
|
||||
while(1) {
|
||||
g = GetStr;
|
||||
|
||||
if (Str2I64(g) == n) {
|
||||
Print("Correct!\n");
|
||||
break;
|
||||
}
|
||||
|
||||
Print("That's not my number. Try another guess:\n");
|
||||
}
|
||||
7
Task/Guess-the-number/IS-BASIC/guess-the-number.basic
Normal file
7
Task/Guess-the-number/IS-BASIC/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
100 PROGRAM "Guess.bas"
|
||||
110 RANDOMIZE
|
||||
120 LET N=RND(10)+1
|
||||
130 DO
|
||||
140 INPUT PROMPT "Guess a number that's between 1-10: ":G
|
||||
150 LOOP UNTIL N=G
|
||||
160 PRINT "Well guessed!"
|
||||
8
Task/Guess-the-number/Icon/guess-the-number.icon
Normal file
8
Task/Guess-the-number/Icon/guess-the-number.icon
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
procedure main()
|
||||
n := ?10
|
||||
repeat {
|
||||
writes("Pick a number from 1 through 10: ")
|
||||
if n = numeric(read()) then break
|
||||
}
|
||||
write("Well guessed!")
|
||||
end
|
||||
10
Task/Guess-the-number/J/guess-the-number-1.j
Normal file
10
Task/Guess-the-number/J/guess-the-number-1.j
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
require 'misc'
|
||||
game=: verb define
|
||||
n=: 1 + ?10
|
||||
smoutput 'Guess my integer, which is bounded by 1 and 10'
|
||||
whilst. -. guess -: n do.
|
||||
guess=. {. 0 ". prompt 'Guess: '
|
||||
if. 0 -: guess do. 'Giving up.' return. end.
|
||||
smoutput (guess=n){::'no.';'Well guessed!'
|
||||
end.
|
||||
)
|
||||
6
Task/Guess-the-number/J/guess-the-number-2.j
Normal file
6
Task/Guess-the-number/J/guess-the-number-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
game''
|
||||
Guess my integer, which is bounded by 1 and 10
|
||||
Guess: 1
|
||||
no.
|
||||
Guess: 2
|
||||
Well guessed!
|
||||
10
Task/Guess-the-number/Java/guess-the-number.java
Normal file
10
Task/Guess-the-number/Java/guess-the-number.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
public class Guessing {
|
||||
public static void main(String[] args) throws NumberFormatException{
|
||||
int n = (int)(Math.random() * 10 + 1);
|
||||
System.out.print("Guess the number between 1 and 10: ");
|
||||
while(Integer.parseInt(System.console().readLine()) != n){
|
||||
System.out.print("Wrong! Guess again: ");
|
||||
}
|
||||
System.out.println("Well guessed!");
|
||||
}
|
||||
}
|
||||
12
Task/Guess-the-number/JavaScript/guess-the-number.js
Normal file
12
Task/Guess-the-number/JavaScript/guess-the-number.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function guessNumber() {
|
||||
// Get a random integer from 1 to 10 inclusive
|
||||
var num = Math.ceil(Math.random() * 10);
|
||||
var guess;
|
||||
|
||||
while (guess != num) {
|
||||
guess = prompt('Guess the number between 1 and 10 inclusive');
|
||||
}
|
||||
alert('Congratulations!\nThe number was ' + num);
|
||||
}
|
||||
|
||||
guessNumber();
|
||||
3
Task/Guess-the-number/Jq/guess-the-number-1.jq
Normal file
3
Task/Guess-the-number/Jq/guess-the-number-1.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{prompt: "Please enter your guess:", random: (1+rand(9)) }
|
||||
| ( (while( .guess != .random; .guess = (input|tonumber) ) | .prompt),
|
||||
"Well done!"
|
||||
15
Task/Guess-the-number/Jq/guess-the-number-2.jq
Normal file
15
Task/Guess-the-number/Jq/guess-the-number-2.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# LCG::Microsoft generates 15-bit integers using the same formula
|
||||
# as rand() from the Microsoft C Runtime.
|
||||
# Input: [ count, state, random ]
|
||||
def next_rand_Microsoft:
|
||||
.[0] as $count
|
||||
| ((214013 * .[1]) + 2531011) % 2147483648 # mod 2^31
|
||||
| [$count+1 , ., (. / 65536 | floor) ];
|
||||
|
||||
def rand_Microsoft(seed):
|
||||
[0,seed]
|
||||
| next_rand_Microsoft # the seed is not so random
|
||||
| next_rand_Microsoft | .[2];
|
||||
|
||||
# A random integer in [0 ... (n-1)]:
|
||||
def rand(n): n * (rand_Microsoft($seed|tonumber) / 32768) | trunc;
|
||||
34
Task/Guess-the-number/Jsish/guess-the-number.jsish
Normal file
34
Task/Guess-the-number/Jsish/guess-the-number.jsish
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
function guessNumber() {
|
||||
// Get a random integer from 1 to 10 inclusive
|
||||
var num = Math.ceil(Math.random() * 10);
|
||||
var guess;
|
||||
var tries = 0;
|
||||
|
||||
while (guess != num) {
|
||||
tries += 1;
|
||||
printf('%s', 'Guess the number between 1 and 10 inclusive: ');
|
||||
guess = console.input();
|
||||
}
|
||||
printf('Congratulations!\nThe number was %d it took %d tries\n', num, tries);
|
||||
}
|
||||
|
||||
if (Interp.conf('unitTest')) {
|
||||
// Set a predictable outcome
|
||||
Math.srand(0);
|
||||
guessNumber();
|
||||
}
|
||||
|
||||
/*
|
||||
=!INPUTSTART!=
|
||||
5
|
||||
9
|
||||
2
|
||||
=!INPUTEND!=
|
||||
*/
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
Guess the number between 1 and 10 inclusive: Guess the number between 1 and 10 inclusive: Guess the number between 1 and 10 inclusive: Congratulations!
|
||||
The number was 2 it took 3 tries
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
10
Task/Guess-the-number/Julia/guess-the-number.julia
Normal file
10
Task/Guess-the-number/Julia/guess-the-number.julia
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function guess()
|
||||
number = dec(rand(1:10))
|
||||
print("Guess my number! ")
|
||||
while readline() != number
|
||||
print("Nope, try again... ")
|
||||
end
|
||||
println("Well guessed!")
|
||||
end
|
||||
|
||||
guess()
|
||||
8
Task/Guess-the-number/Kotlin/guess-the-number.kotlin
Normal file
8
Task/Guess-the-number/Kotlin/guess-the-number.kotlin
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val n = (1 + java.util.Random().nextInt(10)).toString()
|
||||
println("Guess which number I've chosen in the range 1 to 10\n")
|
||||
do { print(" Your guess : ") } while (n != readLine())
|
||||
println("\nWell guessed!")
|
||||
}
|
||||
19
Task/Guess-the-number/LFE/guess-the-number-1.lfe
Normal file
19
Task/Guess-the-number/LFE/guess-the-number-1.lfe
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(defmodule guessing-game
|
||||
(export (main 0)))
|
||||
|
||||
(defun get-player-guess ()
|
||||
(let (((tuple 'ok (list guessed)) (: io fread '"Guess number: " '"~d")))
|
||||
guessed))
|
||||
|
||||
(defun check-guess (answer guessed)
|
||||
(cond
|
||||
((== answer guessed)
|
||||
(: io format '"Well-guessed!!~n"))
|
||||
((/= answer guessed)
|
||||
(check-guess answer (get-player-guess)))))
|
||||
|
||||
(defun main ()
|
||||
(: io format '"Guess the number I have chosen, between 1 and 10.~n")
|
||||
(check-guess
|
||||
(: random uniform 10)
|
||||
(get-player-guess)))
|
||||
8
Task/Guess-the-number/LFE/guess-the-number-2.lfe
Normal file
8
Task/Guess-the-number/LFE/guess-the-number-2.lfe
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> (slurp '"guessing-game.lfe")
|
||||
#(ok guessing-game)
|
||||
> (main)
|
||||
Guess the number I have chosen, between 1 and 10.
|
||||
Guess number: 10
|
||||
Guess number: 5
|
||||
Well-guessed!!
|
||||
ok
|
||||
23
Task/Guess-the-number/LOLCODE/guess-the-number.lol
Normal file
23
Task/Guess-the-number/LOLCODE/guess-the-number.lol
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
HAI 1.3
|
||||
|
||||
VISIBLE "SEED ME, FEMUR! "!
|
||||
I HAS A seed, GIMMEH seed
|
||||
|
||||
HOW IZ I randomizin
|
||||
seed R MOD OF SUM OF 1 AN PRODUKT OF 69069 AN seed AN 10
|
||||
IF U SAY SO
|
||||
|
||||
I IZ randomizin MKAY
|
||||
I HAS A answer ITZ SUM OF seed AN 1
|
||||
I HAS A guess
|
||||
|
||||
IM IN YR guesser
|
||||
VISIBLE "WUTS MY NUMBR? "!
|
||||
GIMMEH guess, guess IS NOW A NUMBR
|
||||
|
||||
BOTH SAEM guess AN answer, O RLY?
|
||||
YA RLY, VISIBLE "U WIN!", GTFO
|
||||
OIC
|
||||
IM OUTTA YR guesser
|
||||
|
||||
KTHXBYE
|
||||
15
Task/Guess-the-number/Langur/guess-the-number.langur
Normal file
15
Task/Guess-the-number/Langur/guess-the-number.langur
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
writeln "Guess a number from 1 to 10"
|
||||
|
||||
val .n = toString random 10
|
||||
for {
|
||||
val .guess = read ">> ", RE/^0*(?:[1-9]|10)(?:\.0+)?$/, "bad data\n", 7, ""
|
||||
if .guess == "" {
|
||||
writeln "too much bad data"
|
||||
break
|
||||
}
|
||||
if .guess == .n {
|
||||
writeln "That's it."
|
||||
break
|
||||
}
|
||||
writeln "not it"
|
||||
}
|
||||
28
Task/Guess-the-number/Lasso/guess-the-number-1.lasso
Normal file
28
Task/Guess-the-number/Lasso/guess-the-number-1.lasso
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
local(
|
||||
number = integer_random(10, 1),
|
||||
status = false,
|
||||
guess
|
||||
)
|
||||
|
||||
// prompt for a number
|
||||
stdout('Guess a number between 1 and 10: ')
|
||||
|
||||
while(not #status) => {
|
||||
#guess = null
|
||||
|
||||
// the following bits wait until the terminal gives you back a line of input
|
||||
while(not #guess or #guess -> size == 0) => {
|
||||
#guess = file_stdin -> readSomeBytes(1024, 1000)
|
||||
}
|
||||
#guess = integer(#guess)
|
||||
|
||||
if(not (range(#guess, 1, 10) == #guess)) => {
|
||||
stdout('Input not of correct type or range. Guess a number between 1 and 10: ')
|
||||
else(#guess == #number)
|
||||
stdout('Well guessed!')
|
||||
#status = true
|
||||
else
|
||||
stdout('You guessed wrong number. Guess a number between 1 and 10: ')
|
||||
}
|
||||
|
||||
}
|
||||
41
Task/Guess-the-number/Lasso/guess-the-number-2.lasso
Normal file
41
Task/Guess-the-number/Lasso/guess-the-number-2.lasso
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?LassoScript
|
||||
|
||||
local(
|
||||
number = integer(web_request -> param('number') or integer_random(10, 1)),
|
||||
status = false,
|
||||
guess = web_request -> param('guess'),
|
||||
_guess = integer(#guess),
|
||||
message = 'Guess a number between 1 and 10'
|
||||
)
|
||||
|
||||
|
||||
|
||||
if(#guess) => {
|
||||
if(not (range(#_guess, 1, 10) == #_guess)) => {
|
||||
#Message = 'Input not of correct type or range. Guess a number between 1 and 10'
|
||||
else(#_guess == #number)
|
||||
#Message = 'Well guessed!'
|
||||
#status = true
|
||||
else
|
||||
#Message = 'You guessed wrong number. Guess a number between 1 and 10'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?><!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Guess the number - Rosetta Code</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>[#message]</h3>
|
||||
[if(not #status)]
|
||||
<form method="post">
|
||||
<label for="guess">Guess:</label><br/ >
|
||||
<input type="number" name="guess" />
|
||||
<input name="number" type="hidden" value="[#number]" />
|
||||
<input name="submit" type="submit" />
|
||||
</form>
|
||||
[/if]
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
number = int(rnd(0) * 10) + 1
|
||||
input "Guess the number I'm thinking of between 1 and 10. "; guess
|
||||
while guess <> number
|
||||
input "Incorrect! Try again! "; guess
|
||||
wend
|
||||
print "Congratulations, well guessed! The number was "; number;"."
|
||||
15
Task/Guess-the-number/LiveCode/guess-the-number.livecode
Normal file
15
Task/Guess-the-number/LiveCode/guess-the-number.livecode
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
command guessTheNumber
|
||||
local tNumber, tguess
|
||||
put random(10) into tNumber
|
||||
repeat until tguess is tNumber
|
||||
ask question "Please enter a number between 1 and 10" titled "Guess the number"
|
||||
if it is not empty then
|
||||
put it into tguess
|
||||
if tguess is tNumber then
|
||||
answer "Well guessed!"
|
||||
end if
|
||||
else
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
end guessTheNumber
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
10 RANDOMIZE TIME:num=INT(RND*10+1):guess=0
|
||||
20 PRINT "Guess the number between 1 and 10."
|
||||
30 WHILE num<>guess
|
||||
40 INPUT "Your guess? ", guess
|
||||
50 WEND
|
||||
60 PRINT "That's correct!"
|
||||
14
Task/Guess-the-number/Lua/guess-the-number.lua
Normal file
14
Task/Guess-the-number/Lua/guess-the-number.lua
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
math.randomseed( os.time() )
|
||||
n = math.random( 1, 10 )
|
||||
|
||||
print( "I'm thinking of a number between 1 and 10. Try to guess it: " )
|
||||
|
||||
repeat
|
||||
x = tonumber( io.read() )
|
||||
|
||||
if x == n then
|
||||
print "Well guessed!"
|
||||
else
|
||||
print "Guess again: "
|
||||
end
|
||||
until x == n
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
Module QBASIC_Based {
|
||||
supervisor:
|
||||
GOSUB initialize
|
||||
GOSUB guessing
|
||||
GOTO continue
|
||||
|
||||
initialize:
|
||||
\\ Not need to RANDOMIZE TIMER
|
||||
\\ we can use Random(1, 100) to get a number from 1 to 100
|
||||
n = 0: r = INT(RND * 100 + 1): g = 0: c$ = ""
|
||||
RETURN
|
||||
|
||||
guessing:
|
||||
WHILE g <> r {
|
||||
INPUT "Pick a number between 1 and 100:"; g
|
||||
IF g = r THEN {
|
||||
PRINT "You got it!"
|
||||
n ++
|
||||
PRINT "It took "; n; " tries to pick the right number."
|
||||
} ELSE.IF g < r THEN {
|
||||
PRINT "Try a larger number."
|
||||
n ++
|
||||
} ELSE {
|
||||
PRINT "Try a smaller number."
|
||||
n++
|
||||
}
|
||||
}
|
||||
RETURN
|
||||
|
||||
continue:
|
||||
WHILE c$ <> "YES" AND c$ <> "NO" {
|
||||
INPUT "Do you want to continue? (YES/NO)"; c$
|
||||
c$ = UCASE$(c$)
|
||||
IF c$ = "YES" THEN {
|
||||
GOTO supervisor
|
||||
} ELSE.IF c$ = "NO" THEN {
|
||||
Goto End
|
||||
}
|
||||
}
|
||||
End:
|
||||
}
|
||||
QBASIC_Based
|
||||
7
Task/Guess-the-number/MATLAB/guess-the-number.m
Normal file
7
Task/Guess-the-number/MATLAB/guess-the-number.m
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
number = ceil(10*rand(1));
|
||||
[guess, status] = str2num(input('Guess a number between 1 and 10: ','s'));
|
||||
|
||||
while (~status || guess ~= number)
|
||||
[guess, status] = str2num(input('Guess again: ','s'));
|
||||
end
|
||||
disp('Well guessed!')
|
||||
8
Task/Guess-the-number/MAXScript/guess-the-number.max
Normal file
8
Task/Guess-the-number/MAXScript/guess-the-number.max
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
rand = random 1 10
|
||||
clearListener()
|
||||
while true do
|
||||
(
|
||||
userval = getKBValue prompt:"Enter an integer between 1 and 10: "
|
||||
if userval == rand do (format "\nWell guessed!\n"; exit)
|
||||
format "\nChoose another value\n"
|
||||
)
|
||||
56
Task/Guess-the-number/MIPS-Assembly/guess-the-number.mips
Normal file
56
Task/Guess-the-number/MIPS-Assembly/guess-the-number.mips
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# WRITTEN: August 26, 2016 (at midnight...)
|
||||
|
||||
# This targets MARS implementation and may not work on other implementations
|
||||
# Specifically, using MARS' random syscall
|
||||
.data
|
||||
take_a_guess: .asciiz "Make a guess:"
|
||||
good_job: .asciiz "Well guessed!"
|
||||
|
||||
.text
|
||||
#retrieve system time as a seed
|
||||
li $v0,30
|
||||
syscall
|
||||
|
||||
#use the high order time stored in $a1 as the seed arg
|
||||
move $a1,$a0
|
||||
|
||||
#set the seed
|
||||
li $v0,40
|
||||
syscall
|
||||
|
||||
#generate number 0-9 (random int syscall generates a number where):
|
||||
# 0 <= $v0 <= $a1
|
||||
li $a1,10
|
||||
li $v0,42
|
||||
syscall
|
||||
|
||||
#increment the randomly generated number and store in $v1
|
||||
add $v1,$a0,1
|
||||
|
||||
loop: jal print_take_a_guess
|
||||
jal read_int
|
||||
|
||||
#go back to beginning of loop if user hasn't guessed right,
|
||||
# else, just "fall through" to exit_procedure
|
||||
bne $v0,$v1,loop
|
||||
|
||||
exit_procedure:
|
||||
#set syscall to print_string, then set good_job string as arg
|
||||
li $v0,4
|
||||
la $a0,good_job
|
||||
syscall
|
||||
|
||||
#exit program
|
||||
li $v0,10
|
||||
syscall
|
||||
|
||||
print_take_a_guess:
|
||||
li $v0,4
|
||||
la $a0,take_a_guess
|
||||
syscall
|
||||
jr $ra
|
||||
|
||||
read_int:
|
||||
li $v0,5
|
||||
syscall
|
||||
jr $ra
|
||||
10
Task/Guess-the-number/Maple/guess-the-number-1.maple
Normal file
10
Task/Guess-the-number/Maple/guess-the-number-1.maple
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
GuessNumber := proc()
|
||||
local number;
|
||||
randomize():
|
||||
printf("Guess a number between 1 and 10 until you get it right:\n:");
|
||||
number := rand(1..10)();
|
||||
while parse(readline()) <> number do
|
||||
printf("Try again!\n:");
|
||||
end do:
|
||||
printf("Well guessed! The answer was %d.\n", number);
|
||||
end proc:
|
||||
1
Task/Guess-the-number/Maple/guess-the-number-2.maple
Normal file
1
Task/Guess-the-number/Maple/guess-the-number-2.maple
Normal file
|
|
@ -0,0 +1 @@
|
|||
GuessNumber();
|
||||
3
Task/Guess-the-number/Mathematica/guess-the-number.math
Normal file
3
Task/Guess-the-number/Mathematica/guess-the-number.math
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
number = RandomInteger[{1, 10}];
|
||||
While[guess =!= number, guess = Input["Guess my number"]];
|
||||
Print["Well guessed!"]
|
||||
33
Task/Guess-the-number/Mercury/guess-the-number.mercury
Normal file
33
Task/Guess-the-number/Mercury/guess-the-number.mercury
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
:- module guess.
|
||||
:- interface.
|
||||
:- import_module io.
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
:- implementation.
|
||||
:- import_module random, string.
|
||||
|
||||
main(!IO) :-
|
||||
time(Time, !IO),
|
||||
random.init(Time, Rand),
|
||||
random.random(1, 10, N, Rand, _),
|
||||
main(from_int(N) ++ "\n", !IO).
|
||||
|
||||
:- pred main(string::in, io::di, io::uo) is det.
|
||||
main(N, !IO) :-
|
||||
io.write_string("Guess the number: ", !IO),
|
||||
io.read_line_as_string(Res, !IO),
|
||||
(
|
||||
Res = ok(S),
|
||||
( if S = N then io.write_string("Well guessed!\n", !IO)
|
||||
else main(N, !IO) )
|
||||
;
|
||||
Res = error(E)
|
||||
;
|
||||
Res = eof
|
||||
).
|
||||
|
||||
:- pred time(int::out, io::di, io::uo) is det.
|
||||
|
||||
:- pragma foreign_decl("C", "#include <time.h>").
|
||||
:- pragma foreign_proc("C", time(Int::out, _IO0::di, _IO1::uo),
|
||||
[will_not_call_mercury, promise_pure],
|
||||
"Int = time(NULL);").
|
||||
9
Task/Guess-the-number/Min/guess-the-number.min
Normal file
9
Task/Guess-the-number/Min/guess-the-number.min
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
randomize
|
||||
|
||||
9 random succ
|
||||
|
||||
"Guess my number between 1 and 10." puts!
|
||||
|
||||
("Your guess" ask int over ==) 'pop ("Wrong." puts!) () linrec
|
||||
|
||||
"Well guessed!" puts!
|
||||
8
Task/Guess-the-number/MiniScript/guess-the-number.mini
Normal file
8
Task/Guess-the-number/MiniScript/guess-the-number.mini
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
num = ceil(rnd*10)
|
||||
while true
|
||||
x = val(input("Your guess?"))
|
||||
if x == num then
|
||||
print "Well guessed!"
|
||||
break
|
||||
end if
|
||||
end while
|
||||
4
Task/Guess-the-number/NS-HUBASIC/guess-the-number.basic
Normal file
4
Task/Guess-the-number/NS-HUBASIC/guess-the-number.basic
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
10 NUMBER=RND(10)+1
|
||||
20 INPUT "I'M THINKING OF A NUMBER BETWEEN 1 AND 10. WHAT IS IT? ",GUESS
|
||||
30 IF GUESS<>NUMBER THEN PRINT "INCORRECT GUESS. TRY AGAIN.": GOTO 20
|
||||
40 PRINT "CORRECT NUMBER."
|
||||
10
Task/Guess-the-number/Nanoquery/guess-the-number.nanoquery
Normal file
10
Task/Guess-the-number/Nanoquery/guess-the-number.nanoquery
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
random = new(Nanoquery.Util.Random)
|
||||
target = random.getInt(9) + 1
|
||||
guess = 0
|
||||
|
||||
println "Guess a number between 1 and 10."
|
||||
while not target = guess
|
||||
guess = int(input())
|
||||
end
|
||||
|
||||
println "That's right!"
|
||||
20
Task/Guess-the-number/Nemerle/guess-the-number.nemerle
Normal file
20
Task/Guess-the-number/Nemerle/guess-the-number.nemerle
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Guess
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
def rand = Random();
|
||||
def x = rand.Next(1, 11); // returns 1 <= x < 11
|
||||
mutable guess = 0;
|
||||
|
||||
do
|
||||
{
|
||||
WriteLine("Guess a nnumber between 1 and 10:");
|
||||
guess = Int32.Parse(ReadLine());
|
||||
} while (guess != x);
|
||||
|
||||
WriteLine("Well guessed!");
|
||||
}
|
||||
}
|
||||
23
Task/Guess-the-number/NetRexx/guess-the-number.netrexx
Normal file
23
Task/Guess-the-number/NetRexx/guess-the-number.netrexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* NetRexx */
|
||||
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
guessThis = (Math.random * 10 + 1) % 1
|
||||
guess = -1
|
||||
prompt = [ -
|
||||
'Try guessing a number between 1 and 10', -
|
||||
'Wrong; try again...' -
|
||||
]
|
||||
promptIdx = int 0
|
||||
|
||||
loop label g_ until guess = guessThis
|
||||
say prompt[promptIdx]
|
||||
promptIdx = 1
|
||||
parse ask guess .
|
||||
if guess = guessThis then do
|
||||
say 'Well guessed!' guess 'is the correct number.'
|
||||
leave g_
|
||||
end
|
||||
end g_
|
||||
|
||||
return
|
||||
13
Task/Guess-the-number/NewLISP/guess-the-number.l
Normal file
13
Task/Guess-the-number/NewLISP/guess-the-number.l
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
; guess-number.lsp
|
||||
; oofoe 2012-01-19
|
||||
; http://rosettacode.org/wiki/Guess_the_number
|
||||
|
||||
(seed (time-of-day)) ; Initialize random number generator from clock.
|
||||
(setq number (+ 1 (rand 10)))
|
||||
|
||||
(println "I'm thinking of a number between 1 and 10. Can you guess it?")
|
||||
(print "Type in your guess and hit [enter]: ")
|
||||
(while (!= number (int (read-line))) (print "Nope! Try again: "))
|
||||
(println "Well guessed! Congratulations!")
|
||||
|
||||
(exit)
|
||||
13
Task/Guess-the-number/Nim/guess-the-number.nim
Normal file
13
Task/Guess-the-number/Nim/guess-the-number.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import strutils, random
|
||||
|
||||
randomize()
|
||||
var chosen = rand(1..10)
|
||||
echo "I have thought of a number. Try to guess it!"
|
||||
|
||||
var guess = parseInt(readLine(stdin))
|
||||
|
||||
while guess != chosen:
|
||||
echo "Your guess was wrong. Try again!"
|
||||
guess = parseInt(readLine(stdin))
|
||||
|
||||
echo "Well guessed!"
|
||||
18
Task/Guess-the-number/OCaml/guess-the-number.ocaml
Normal file
18
Task/Guess-the-number/OCaml/guess-the-number.ocaml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#!/usr/bin/env ocaml
|
||||
|
||||
let () =
|
||||
Random.self_init();
|
||||
let n =
|
||||
if Random.bool () then
|
||||
let n = 2 + Random.int 8 in
|
||||
print_endline "Please guess a number between 1 and 10 excluded";
|
||||
(n)
|
||||
else
|
||||
let n = 1 + Random.int 10 in
|
||||
print_endline "Please guess a number between 1 and 10 included";
|
||||
(n)
|
||||
in
|
||||
while read_int () <> n do
|
||||
print_endline "The guess was wrong! Please try again!"
|
||||
done;
|
||||
print_endline "Well guessed!"
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue