added basic server-client functionality

main
mike 2023-04-11 19:10:53 -04:00
parent 02683303dd
commit 9208fdcde6
6 changed files with 85 additions and 10 deletions

View File

@ -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>

View File

@ -1,11 +1,15 @@
package ca.cosc3p91.a4; package ca.cosc3p91.a4;
import ca.cosc3p91.a4.game.GameEngine; import ca.cosc3p91.a4.game.GameEngine;
import ca.cosc3p91.a4.util.Client;
import java.io.*;
import java.net.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
new GameEngine().run(); Client gameClient = new Client(42069);
} }
} }

View File

@ -9,6 +9,7 @@ import ca.cosc3p91.a4.util.ChallengeAdapter;
import java.beans.XMLEncoder; import java.beans.XMLEncoder;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Random; import java.util.Random;
@ -26,14 +27,16 @@ public class GameEngine implements Runnable {
private int currentTime; private int currentTime;
private final InputStream input;
private final Random random = new Random(System.nanoTime()); private final Random random = new Random(System.nanoTime());
public Map map; public Map map;
public GameDisplay view; public GameDisplay view;
public GameEngine() { public GameEngine(InputStream commandStream) {
player = new Player(); player = new Player();
map = generateInitialMap(); map = generateInitialMap();
input = commandStream;
} }
public void attackVillage(Map map) { public void attackVillage(Map map) {
@ -149,7 +152,7 @@ public class GameEngine implements Runnable {
public void run() { public void run() {
String in; String in;
view = new GameDisplay(); view = new GameDisplay(input);
view.printVillageState(this.map,"Current Village State"); view.printVillageState(this.map,"Current Village State");
view.printGameMenu(); view.printGameMenu();
@ -165,6 +168,8 @@ public class GameEngine implements Runnable {
if ((in = view.nextInput()) != null) { if ((in = view.nextInput()) != null) {
String[] args = in.split(" "); String[] args = in.split(" ");
if (in.charAt(0) == '0') continue;
view.printLastInput(); view.printLastInput();
// reset the map if they aren't exploring // reset the map if they aren't exploring
if (in.charAt(0) != '4') if (in.charAt(0) != '4')

View File

@ -7,22 +7,21 @@ import ca.cosc3p91.a4.util.Print;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Scanner; import java.util.Scanner;
public class GameDisplay { public class GameDisplay {
private BufferedReader reader; private BufferedReader reader;
private Scanner scanner;
private String input; private String input;
public GameDisplay() { public GameDisplay(InputStream readFrom) {
reader = new BufferedReader(new InputStreamReader(System.in)); reader = new BufferedReader(new InputStreamReader(readFrom));
scanner = new Scanner(reader);
} }
public String nextInput() throws IOException { public String nextInput() throws IOException {
if (reader.ready()) { if (reader.ready()) {
return (input = scanner.nextLine()); return (input = reader.readLine().trim());
} else return null; } else return null;
} }

View File

@ -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();
}
}

View File

@ -1,11 +1,38 @@
package ca.cosc3p91.a4.util; 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 class Server {
public static final int SERVER_PORT = 42069; 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); */
}
} }
} }