-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNimClient.java
More file actions
170 lines (159 loc) · 6.9 KB
/
NimClient.java
File metadata and controls
170 lines (159 loc) · 6.9 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* NimClient.java
*
* This program will connect to the NimServer so that the user can play a multiplayer Mim game
* It will not be a multithreaded program because Nim is a turn-based game so no two action needs to simultaneously run
*/
import java.net.Socket;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Scanner;
public class NimClient{
public static void main(String[] args){
Scanner keyboard;
String userInput, serverReply="", myUsername, opUsername, serverAddress;
boolean isFirst;
int port;
Socket connectionSock;
BufferedWriter serverOutput;
BufferedReader serverInput;
keyboard = new Scanner(System.in);
System.out.print("Please enter the address of the Nim server (press enter if it is a localhost): ");
serverAddress= keyboard.nextLine();
if(serverAddress.equals(""))
serverAddress="localhost";
port=7654;
System.out.println("Connecting to server on port " + port);
try{
while(true){
connectionSock=new Socket(serverAddress, port);
serverOutput= new BufferedWriter(new OutputStreamWriter(connectionSock.getOutputStream()));
serverInput= new BufferedReader(new InputStreamReader(connectionSock.getInputStream()));
System.out.println("Connection made.");
System.out.println("Welcome to Nim Game!!");
System.out.println("You will be matched with another player to play a Nim game. Here are the rules:");
System.out.println(" - There are three heaps of three, four, and five coins respectively.");
System.out.println(" - If it is your turn, you can remove ANY amount of coins from a SINGLE heap.");
System.out.println(" - The person that removes the last coin wins.");
System.out.println(" - The frist player to connect to the server will get the first turn.");
System.out.println("--------------------------------------------------------------------------------------");
System.out.print("Please enter a name you wished to be addressed as: ");
myUsername= keyboard.nextLine();
userInput="HELLO "+myUsername+"\n";
serverOutput.write(userInput);
serverOutput.flush();
serverReply=serverInput.readLine();
isFirst=false;
if(serverReply.equals("100")){
isFirst=true;
System.out.println("Waiting for opponent...");
serverReply=serverInput.readLine();
}
opUsername=serverReply.substring(4);
System.out.println("You have been matched with "+opUsername+".");
serverReply=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
if(!isFirst){
System.out.println("Waiting for "+opUsername+"\'s move...");
serverReply=serverInput.readLine();//listening for status code
serverReply=serverInput.readLine();//getting the board
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
}
while(true){
System.out.print("Please enter the row number you wished to change: ");
userInput= keyboard.nextLine();
serverOutput.write(userInput+"\n");
serverOutput.flush();
System.out.print("Please enter the number of coins to remove: ");
userInput= keyboard.nextLine();
serverOutput.write(userInput+"\n");
serverOutput.flush();
serverReply=serverInput.readLine();//listening for status code
if(serverReply.equals("0")){
serverReply=serverInput.readLine();//getting the board
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
}
else if(serverReply.equals("1")){
serverReply=serverInput.readLine();//getting the board
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
System.out.println("Congratulation! You won the game.");
break;
}
else{
serverReply=serverInput.readLine();//getting the board
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
System.out.println("The move you made is invalid.");
continue;
}
System.out.println("Waiting for "+opUsername+"\'s move...");
serverReply=serverInput.readLine();//listening for status code
if(serverReply.equals("0")){
serverReply=serverInput.readLine();//getting the board
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
}
else{
serverReply=serverInput.readLine();//getting the board
serverReply+="\n";
serverReply+=serverInput.readLine();
serverReply+="\n";
serverReply+=serverInput.readLine();
System.out.println("----------------------------");
System.out.println(serverReply);
System.out.println("----------------------------");
System.out.println("Sorry, you lost the game.");
break;
}
}
connectionSock.close();
serverInput.close();
serverOutput.close();
System.out.print("Would you like to play again? (y/n): ");
userInput= keyboard.nextLine();
if(userInput.equals("n"))
break;
}
}
catch (IOException e){
System.out.println(e.getMessage());
}
}
}