Przeglądaj źródła

Added a guessing game.

Stan Hebben 6 lat temu
rodzic
commit
4669cc7d62

+ 11
- 0
scripts/guessing.zs Wyświetl plik

@@ -0,0 +1,11 @@
1
+var correct = false;
2
+while !correct {
3
+	val value = randomInt(10);
4
+	val guess = readLine("Guess a number 0-9: ");
5
+	if value == int.parse(guess) {
6
+		println("You were right!");
7
+		correct = true;
8
+	} else {
9
+		println("Nope, try again...");
10
+	}
11
+}

+ 10
- 1
src/main/java/org/openzen/javascriptingexample/Globals.java Wyświetl plik

@@ -8,6 +8,7 @@ package org.openzen.javascriptingexample;
8 8
 import java.io.BufferedReader;
9 9
 import java.io.IOException;
10 10
 import java.io.InputStreamReader;
11
+import java.util.Random;
11 12
 import org.openzen.zencode.java.ZenCodeGlobals;
12 13
 
13 14
 /**
@@ -15,6 +16,8 @@ import org.openzen.zencode.java.ZenCodeGlobals;
15 16
  * @author Hoofdgebruiker
16 17
  */
17 18
 public class Globals implements ZenCodeGlobals {
19
+	private static final Random RANDOM = new Random();
20
+	
18 21
 	@Global
19 22
 	public static void println(String message) {
20 23
 		System.out.println(message);
@@ -23,11 +26,17 @@ public class Globals implements ZenCodeGlobals {
23 26
 	@Global
24 27
 	public static String readLine(String prompt) {
25 28
 		System.out.print(prompt);
26
-		try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
29
+		try {
30
+			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
27 31
 			return reader.readLine();
28 32
 		} catch (IOException ex) {
29 33
 			ex.printStackTrace();
30 34
 			return "";
31 35
 		}
32 36
 	}
37
+	
38
+	@Global
39
+	public static int randomInt(int limit) {
40
+		return RANDOM.nextInt(limit);
41
+	}
33 42
 }

Ładowanie…
Anuluj
Zapisz