-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoryClient.java
63 lines (57 loc) · 2.58 KB
/
StoryClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* This class represents a client for the Story Builder Game.
* It connects to the server and allows the player to contribute to the ongoing story.
* @author Brent Reynolds
* @date Fall 2023
*/
public class StoryClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int PORT = 8080;
/**
* The main method that starts the client.
* It connects to the server and handles the communication between the client and server.
*/
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_ADDRESS, PORT);
Scanner serverInput = new Scanner(socket.getInputStream());
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
Scanner userInput = new Scanner(System.in)) {
// Print the welcome message and initial story
System.out.println("Welcome to the Story Builder Game!");
// Allow the player to contribute to the story continuously
while (true) {
String message = serverInput.nextLine();
String turnIndicator = "";
// Print the message from the server (Your turn or the updated story)
if (message.equals("Your turn: ") || message.equals("Waiting for other players...")) {
turnIndicator = message;
if (turnIndicator.equals("Your turn: ")) {
System.out.print(turnIndicator);
}
if (turnIndicator.equals("Your turn: ")) {
// Player's turn, prompt for input and send to the server
System.out.print("Enter your sentence: ");
String userSentence = userInput.nextLine();
output.println(userSentence);
} else {
// Not player's turn, print the updated story
String updatedStory = serverInput.nextLine();
System.out.println(updatedStory);
}
} else {
if (!message.equals("Waiting for other players...")) {
String updatedStory = message;
System.out.println(updatedStory);
System.out.println("----------------------------------");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}