aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-03-15 20:04:47 +0100
committerPacien TRAN-GIRARD2014-03-15 20:04:47 +0100
commit967f40d710eb07568b6753c6f02d6897f8728999 (patch)
tree05a2e255261185806e142d97355e0aff85c83b39
parent48b29ade0ae071401e3c4c69f70506088f28adaa (diff)
downloadesieequest-967f40d710eb07568b6753c6f02d6897f8728999.tar.gz
Implement command input from file
-rwxr-xr-xsrc/esieequest/Main.java13
-rw-r--r--src/esieequest/view/text/Console.java25
-rw-r--r--src/esieequest/view/text/FileReader.java45
-rw-r--r--src/esieequest/view/text/TextInterface.java (renamed from src/esieequest/view/console/Console.java)28
-rw-r--r--src/esieequest/view/text/package-info.java (renamed from src/esieequest/view/console/package-info.java)2
-rw-r--r--test/commands.txt2
6 files changed, 93 insertions, 22 deletions
diff --git a/src/esieequest/Main.java b/src/esieequest/Main.java
index 2ad11b1..59ac568 100755
--- a/src/esieequest/Main.java
+++ b/src/esieequest/Main.java
@@ -1,6 +1,7 @@
1package esieequest; 1package esieequest;
2 2
3import java.util.Arrays; 3import java.util.Arrays;
4import java.util.List;
4 5
5import javax.swing.JApplet; 6import javax.swing.JApplet;
6 7
@@ -9,7 +10,8 @@ import esieequest.model.Game;
9import esieequest.view.View; 10import esieequest.view.View;
10import esieequest.view.app.Applet; 11import esieequest.view.app.Applet;
11import esieequest.view.app.Window; 12import esieequest.view.app.Window;
12import esieequest.view.console.Console; 13import esieequest.view.text.Console;
14import esieequest.view.text.FileReader;
13 15
14/** 16/**
15 * The main class of the program. 17 * The main class of the program.
@@ -44,10 +46,17 @@ public class Main extends JApplet {
44 * the command line arguments 46 * the command line arguments
45 */ 47 */
46 public static void main(final String[] args) { 48 public static void main(final String[] args) {
49 final List<String> arguments = Arrays.asList(args);
47 final Game game = new Game(); 50 final Game game = new Game();
48 View view; 51 View view;
49 52
50 if (Arrays.asList(args).contains("--nogui")) { 53 if (arguments.contains("--file")) {
54 if (arguments.size() < 2) {
55 System.out.println("Error: no input file specified.");
56 return;
57 }
58 view = new FileReader(arguments.get(1));
59 } else if (arguments.contains("--console")) {
51 view = new Console(); 60 view = new Console();
52 } else { 61 } else {
53 view = new Window(); 62 view = new Window();
diff --git a/src/esieequest/view/text/Console.java b/src/esieequest/view/text/Console.java
new file mode 100644
index 0000000..e7a71dd
--- /dev/null
+++ b/src/esieequest/view/text/Console.java
@@ -0,0 +1,25 @@
1package esieequest.view.text;
2
3import java.util.Scanner;
4
5/**
6 * The textual console view.
7 *
8 * @author Pacien TRAN-GIRARD
9 */
10public class Console extends TextInterface {
11
12 /**
13 * Runs the console input scanner.
14 */
15 @Override
16 protected void run() {
17 final Scanner scanner = new Scanner(System.in);
18 while (this.running) {
19 System.out.print("> ");
20 this.gameEngine.interpret(scanner.nextLine());
21 }
22 scanner.close();
23 }
24
25}
diff --git a/src/esieequest/view/text/FileReader.java b/src/esieequest/view/text/FileReader.java
new file mode 100644
index 0000000..0bf3d3d
--- /dev/null
+++ b/src/esieequest/view/text/FileReader.java
@@ -0,0 +1,45 @@
1package esieequest.view.text;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.util.Scanner;
6
7/**
8 * The textual file reader view.
9 *
10 * @author Pacien TRAN-GIRARD
11 */
12public class FileReader extends TextInterface {
13
14 private File file;
15
16 /**
17 * The FileReader initialiser.
18 *
19 * @param filePath
20 * the path of the file to read
21 */
22 public FileReader(final String filePath) {
23 super();
24 this.file = new File(filePath);
25 }
26
27 /**
28 * Runs the text file input scanner.
29 */
30 @Override
31 protected void run() {
32 try {
33 final Scanner scanner = new Scanner(file);
34 while (this.running && scanner.hasNextLine()) {
35 String line = scanner.nextLine();
36 System.out.println("> " + line);
37 this.gameEngine.interpret(line);
38 }
39 scanner.close();
40 } catch (FileNotFoundException exception) {
41 System.out.println("Error: file not found.");
42 }
43 }
44
45}
diff --git a/src/esieequest/view/console/Console.java b/src/esieequest/view/text/TextInterface.java
index 6faaced..e2de893 100644
--- a/src/esieequest/view/console/Console.java
+++ b/src/esieequest/view/text/TextInterface.java
@@ -1,4 +1,4 @@
1package esieequest.view.console; 1package esieequest.view.text;
2 2
3import java.util.Scanner; 3import java.util.Scanner;
4 4
@@ -7,35 +7,25 @@ import esieequest.model.Game;
7import esieequest.view.View; 7import esieequest.view.View;
8 8
9/** 9/**
10 * The textual console view. 10 * The textual view.
11 * 11 *
12 * @author Pacien TRAN-GIRARD 12 * @author Pacien TRAN-GIRARD
13 */ 13 */
14public class Console implements View { 14abstract class TextInterface implements View {
15 15
16 private Game game; 16 protected Game game;
17 private GameEngine gameEngine; 17 protected GameEngine gameEngine;
18 18
19 private boolean running; 19 protected boolean running;
20 20
21 /** 21 /**
22 * The default constructor. 22 * The default constructor.
23 */ 23 */
24 public Console() { 24 public TextInterface() {
25 this.running = false; 25 this.running = false;
26 } 26 }
27 27
28 /** 28 protected abstract void run();
29 * Runs the input scanner.
30 */
31 private void run() {
32 final Scanner scanner = new Scanner(System.in);
33 while (this.running) {
34 System.out.print("> ");
35 this.gameEngine.interpret(scanner.nextLine());
36 }
37 scanner.close();
38 }
39 29
40 @Override 30 @Override
41 public void setModel(final Game game) { 31 public void setModel(final Game game) {
diff --git a/src/esieequest/view/console/package-info.java b/src/esieequest/view/text/package-info.java
index 24089ab..614ff9f 100644
--- a/src/esieequest/view/console/package-info.java
+++ b/src/esieequest/view/text/package-info.java
@@ -1,5 +1,5 @@
1/** 1/**
2 * The console view package. Contains the textual user interface and its associated input stream processor. 2 * The console view package. Contains the textual user interface and its associated input stream processor.
3 */ 3 */
4package esieequest.view.console; 4package esieequest.view.text;
5 5
diff --git a/test/commands.txt b/test/commands.txt
new file mode 100644
index 0000000..73d07ea
--- /dev/null
+++ b/test/commands.txt
@@ -0,0 +1,2 @@
1help
2quit \ No newline at end of file