The document describes a number guessing game implemented in different programming languages and discusses refactoring the code into reusable utility classes. The code provided implements a number guessing game where a random number is generated between 0-100 and the user has 5 attempts to guess the number. Feedback is provided on each guess if the number is too high or too low. After 5 attempts or a correct guess, a message is displayed thanking the user for playing. Later implementations split the code into utility classes for generating random numbers, getting user input, comparing guesses, and tracking attempts. The discussion focuses on removing state from the utility classes.
3. /13@yegor256 3
10 N = INT(RND(1) * 100)
20 T = T + 1
30 IF T > 5 THEN GOTO 120
40 PRINT "Guess a number in 0..100 range: "
50 INPUT X
60 IF X < N THEN PRINT "Too small."
70 IF X > N THEN PRINT "Too big."
80 IF X = N THEN GOTO 100
90 GOTO 20
100 PRINT "Bingo!"
110 GOTO 130
120 PRINT "You lost, sorry. It was: ", N
130 PRINT "Thanks for playing with me!"
4. /13@yegor256 4
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
srand(time(NULL));
int x, t=0, n=rand() % 100;
while (++t <= 5) {
printf("Guess a number in 0..100 range: ");
scanf("%d", &x);
if (x > n) {
printf("Too big.n");
} else if (x < n) {
printf("Too small.n");
} else {
break;
}
}
if (t < 5) {
printf("Bingo!n");
} else {
printf("You lost, sorry. It was: %dn", n);
}
printf("Thanks for playing with me!n");
}
5. /13@yegor256 5
import java.util.Scanner;
public class Main {
public static void main(String... args) {
int n = (int) (Math.random() * 100.0d);
int t = 0;
while (true) {
if (++t > 5) {
System.out.println("You lost, sorry. It was: " + n);
break;
}
System.out.print("Guess a number in 0..100 range: ");
int x = new Scanner(System.in).nextInt();
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
break;
}
}
System.out.println("Thanks for playing with me!");
}
}
6. /13@yegor256 6
import java.util.Scanner;
public class Main {
public static void main(String... args) {
int n = (int) (Math.random() * 100.0d);
int t = 0;
while (true) {
if (++t > 5) {
System.out.println("You lost, sorry. It was: " + n);
break;
}
System.out.print("Guess a number in 0..100 range: ");
int x = new Scanner(System.in).nextInt();
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
break;
}
}
System.out.println("Thanks for playing with me!");
}
}
System.out.print("Guess a number in 0..100 range: ");
int x = new Scanner(System.in).nextInt();
7. /13@yegor256 7
import java.util.Scanner;
public class Main {
public static void main(String... args) {
int n = (int) (Math.random() * 100.0d);
int t = 0;
while (true) {
if (++t > 5) {
System.out.println("You lost, sorry. It was: " + n);
break;
}
int x = InputUtils.read();
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
break;
}
}
System.out.println("Thanks for playing with me!");
}
}
class InputUtils {
public static int read() {
System.out.print(
"Guess a number in 0..100 range: “
);
return new Scanner(System.in).nextInt();
}
}
9. /13@yegor256 9
class InputUtils {
public static int read() {
System.out.print(
"Guess a number in 0..100 range: “
);
return new Scanner(System.in).nextInt();
}
}
class NumberUtils {
public static int compare(int x, int n) {
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
}
}
}
class RandomUtils {
public static int make() {
return (int) (Math.random() * 100.0d);
}
}
class WhateverUtils {
public static int build() {
return ?;
}
}
n
t
x
10. /13@yegor256 10
import java.util.Scanner;
public class Main {
public static void main(String... args) {
int n = (int) (Math.random() * 100.0d);
int t = 0;
while (true) {
if (++t > 5) {
System.out.println("You lost, sorry. It was: " + n);
break;
}
new Guess();
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
break;
}
}
System.out.println("Thanks for playing with me!");
}
}
class Guess implements Integer {
@Override
public int value() {
System.out.print(
"Guess a number in 0..100 range: “
);
return new Scanner(System.in).nextInt();
}
}
12. /13@yegor256 12
n
t
x System.out.print(
"Guess a number in 0..100 range: “
);
new Scanner(System.in).nextInt();
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
}(int) (Math.random() * 100.0d);
13. /13@yegor256 13
import java.util.Scanner;
public class Main {
public static void main(String... args) {
int n = (int) (Math.random() * 100.0d);
int t = 0;
while (true) {
if (++t > 5) {
System.out.println("You lost, sorry. It was: " + n);
break;
}
System.out.print("Guess a number in 0..100 range: ");
int x = new Scanner(System.in).nextInt();
if (x < n) {
System.out.println("Too small.");
} else if (x > n) {
System.out.println("Too big.");
} else {
System.out.println("Bingo!");
break;
}
}
System.out.println("Thanks for playing with me!");
}
}
new Farewell(
new Attempts(
new VerboseDiff(
new Diff(
new Secret() as secret,
new Guess()
)
), 5
),
secret
).say();