added basic server-client functionality
parent
02683303dd
commit
9208fdcde6
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RunConfigurationProducerService">
|
||||
<option name="ignoredProducers">
|
||||
<set>
|
||||
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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); */
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue