diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/Assignment 4/src/ca/cosc3p91/a4/Main.java b/Assignment 4/src/ca/cosc3p91/a4/Main.java index e06aa2f..cdde87e 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/Main.java +++ b/Assignment 4/src/ca/cosc3p91/a4/Main.java @@ -1,11 +1,15 @@ package ca.cosc3p91.a4; import ca.cosc3p91.a4.game.GameEngine; +import ca.cosc3p91.a4.util.Client; + +import java.io.*; +import java.net.*; public class Main { - public static void main(String[] args) { - new GameEngine().run(); + public static void main(String[] args) throws IOException { + Client gameClient = new Client(42069); } } diff --git a/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java b/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java index fba0bc3..48dc875 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java +++ b/Assignment 4/src/ca/cosc3p91/a4/game/GameEngine.java @@ -9,6 +9,7 @@ import ca.cosc3p91.a4.util.ChallengeAdapter; import java.beans.XMLEncoder; import java.io.BufferedOutputStream; +import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Random; @@ -26,14 +27,16 @@ public class GameEngine implements Runnable { private int currentTime; + private final InputStream input; private final Random random = new Random(System.nanoTime()); public Map map; public GameDisplay view; - public GameEngine() { + public GameEngine(InputStream commandStream) { player = new Player(); map = generateInitialMap(); + input = commandStream; } public void attackVillage(Map map) { @@ -149,7 +152,7 @@ public class GameEngine implements Runnable { public void run() { String in; - view = new GameDisplay(); + view = new GameDisplay(input); view.printVillageState(this.map,"Current Village State"); view.printGameMenu(); @@ -165,6 +168,8 @@ public class GameEngine implements Runnable { if ((in = view.nextInput()) != null) { String[] args = in.split(" "); + if (in.charAt(0) == '0') continue; + view.printLastInput(); // reset the map if they aren't exploring if (in.charAt(0) != '4') diff --git a/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java b/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java index 19e06fd..5b6680b 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java +++ b/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java @@ -7,22 +7,21 @@ import ca.cosc3p91.a4.util.Print; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.util.Scanner; public class GameDisplay { private BufferedReader reader; - private Scanner scanner; private String input; - public GameDisplay() { - reader = new BufferedReader(new InputStreamReader(System.in)); - scanner = new Scanner(reader); + public GameDisplay(InputStream readFrom) { + reader = new BufferedReader(new InputStreamReader(readFrom)); } public String nextInput() throws IOException { if (reader.ready()) { - return (input = scanner.nextLine()); + return (input = reader.readLine().trim()); } else return null; } diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/Client.java b/Assignment 4/src/ca/cosc3p91/a4/util/Client.java new file mode 100644 index 0000000..97879e9 --- /dev/null +++ b/Assignment 4/src/ca/cosc3p91/a4/util/Client.java @@ -0,0 +1,30 @@ +package ca.cosc3p91.a4.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; + +public class Client { + public Client(int port) throws IOException { + BufferedReader inFromUser = + new BufferedReader(new InputStreamReader(System.in)); + DatagramSocket clientSocket = new DatagramSocket(); + InetAddress IPAddress = InetAddress.getByName("localhost"); + byte[] sendData = new byte[1024]; + byte[] receiveData = new byte[1024]; + String sentence = inFromUser.readLine(); + sendData = sentence.getBytes(); + DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); + clientSocket.send(sendPacket); + /* + DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); + clientSocket.receive(receivePacket); + String modifiedSentence = new String(receivePacket.getData()); + System.out.println("FROM SERVER:" + modifiedSentence); + */ + clientSocket.close(); + } +} diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/Server.java b/Assignment 4/src/ca/cosc3p91/a4/util/Server.java index cbe4286..4db9ada 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/util/Server.java +++ b/Assignment 4/src/ca/cosc3p91/a4/util/Server.java @@ -1,11 +1,38 @@ package ca.cosc3p91.a4.util; +import ca.cosc3p91.a4.game.GameEngine; + +import java.io.*; +import java.net.*; +import java.util.Arrays; + public class Server { public static final int SERVER_PORT = 42069; - public Server() { + public static ByteArrayInputStream stream_in; + + public static void main(String[] args) throws IOException { + DatagramSocket serverSocket = new DatagramSocket(SERVER_PORT); + byte[] receiveData = new byte[1284]; + receiveData[0] = (byte)'0'; + stream_in = new ByteArrayInputStream(receiveData); + byte[] sendData = new byte[1024]; + new Thread(new GameEngine(stream_in)).start(); + while(true) { + DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); + serverSocket.receive(receivePacket); + stream_in.reset(); + /* + InetAddress IPAddress = receivePacket.getAddress(); + int port = receivePacket.getPort(); + String capitalizedSentence = sentence.toUpperCase(); + sendData = capitalizedSentence.getBytes(); + DatagramPacket sendPacket = + new DatagramPacket(sendData, sendData.length, IPAddress, port); + serverSocket.send(sendPacket); */ + } } }