Client updates board periodically and Server sends updates correctly to each client.
This commit is contained in:
parent
b09e378c12
commit
ac5d1645e8
2 changed files with 28 additions and 18 deletions
24
Client.java
24
Client.java
|
@ -21,6 +21,7 @@ import java.net.URL;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.*;
|
import java.awt.image.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
//import javax.swing.text.html.parser.ParserDelegator;
|
//import javax.swing.text.html.parser.ParserDelegator;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
@ -319,14 +320,23 @@ public class Client {
|
||||||
String diff = "?";
|
String diff = "?";
|
||||||
in.read(b, 0, 256);
|
in.read(b, 0, 256);
|
||||||
temp = String.valueOf(b).trim();
|
temp = String.valueOf(b).trim();
|
||||||
if(gameMode == 3) {
|
if(gameMode != 3) {
|
||||||
while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
while(!diff.equals("1") && !diff.equals("2") && !diff.equals("3")) {
|
||||||
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
|
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
|
||||||
diff = diff.toUpperCase();
|
//diff = diff.toUpperCase();
|
||||||
if(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
if(!diff.equals("1") && !diff.equals("2") && !diff.equals("3")) {
|
||||||
JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp);
|
JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(diff.equals("1")) {
|
||||||
|
userInput = "EASY";
|
||||||
|
}
|
||||||
|
else if(diff.equals("2")) {
|
||||||
|
userInput = "MEDIUM";
|
||||||
|
}
|
||||||
|
else if(diff.equals("3")) {
|
||||||
|
userInput = "HARD";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
output.println(userInput);
|
output.println(userInput);
|
||||||
}
|
}
|
||||||
|
@ -339,9 +349,7 @@ public class Client {
|
||||||
{
|
{
|
||||||
while(!end) {
|
while(!end) {
|
||||||
char[] buffer = new char[256];
|
char[] buffer = new char[256];
|
||||||
if(in.available()) {
|
|
||||||
in.read(buffer, 0, 256);
|
in.read(buffer, 0, 256);
|
||||||
}
|
|
||||||
for(int i = 0; i < buffer.length; ++i) {
|
for(int i = 0; i < buffer.length; ++i) {
|
||||||
if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) {
|
if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) {
|
||||||
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
|
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
|
||||||
|
@ -352,13 +360,13 @@ public class Client {
|
||||||
System.out.println(String.valueOf(buffer).trim());
|
System.out.println(String.valueOf(buffer).trim());
|
||||||
window.updateBoard(String.valueOf(buffer).trim());
|
window.updateBoard(String.valueOf(buffer).trim());
|
||||||
while(!end && boardoutput.length() < 4) {
|
while(!end && boardoutput.length() < 4) {
|
||||||
if(in.available()) {
|
if(in.ready()) {
|
||||||
in.read(buffer, 0, 256);
|
in.read(buffer, 0, 256);
|
||||||
System.out.println(String.valueOf(buffer).trim());
|
System.out.println(String.valueOf(buffer).trim());
|
||||||
window.updateBoard(String.valueOf(buffer).trim());
|
window.updateBoard(String.valueOf(buffer).trim());
|
||||||
}
|
}
|
||||||
String tempstring;
|
String tempstring;
|
||||||
Thread.sleep(100);
|
Thread.sleep(1);
|
||||||
if(!undoString.equals("")) {
|
if(!undoString.equals("")) {
|
||||||
System.out.println(undoString);
|
System.out.println(undoString);
|
||||||
output.println(undoString);
|
output.println(undoString);
|
||||||
|
|
|
@ -76,7 +76,7 @@ int main(int argc, char *argv[])
|
||||||
string final_move;
|
string final_move;
|
||||||
|
|
||||||
//Ask client about game type
|
//Ask client about game type
|
||||||
string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\nEnter choice: \n";
|
string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?3. Play Client vs. Client?\nEnter choice: \n";
|
||||||
write(newsockfd, introduction.c_str(), introduction.length());
|
write(newsockfd, introduction.c_str(), introduction.length());
|
||||||
n = read(newsockfd,info,255); //Reads choice as a string
|
n = read(newsockfd,info,255); //Reads choice as a string
|
||||||
istringstream convert(info); //Converts the read string to an integer
|
istringstream convert(info); //Converts the read string to an integer
|
||||||
|
@ -85,14 +85,16 @@ int main(int argc, char *argv[])
|
||||||
cout << "OK" << endl;
|
cout << "OK" << endl;
|
||||||
bzero(info,256); //Resets info back to normal
|
bzero(info,256); //Resets info back to normal
|
||||||
|
|
||||||
|
string choice_difficulty = "EASY";
|
||||||
|
if(choice != 3) {
|
||||||
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n3. Hard\nEnter choice: \n";
|
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n3. Hard\nEnter choice: \n";
|
||||||
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
|
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
|
||||||
n = read(newsockfd,info,255);
|
n = read(newsockfd,info,255);
|
||||||
istringstream con(info);
|
istringstream con(info);
|
||||||
string choice_difficulty = "EASY";
|
|
||||||
con >> choice_difficulty;
|
con >> choice_difficulty;
|
||||||
cout << "OK" << endl;
|
cout << "OK" << endl;
|
||||||
bzero(info,256);
|
bzero(info,256);
|
||||||
|
}
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
|
|
||||||
|
@ -133,7 +135,7 @@ int main(int argc, char *argv[])
|
||||||
boardState = e.getBoard()->boardToString();
|
boardState = e.getBoard()->boardToString();
|
||||||
cout << boardState << "testing\n\n";
|
cout << boardState << "testing\n\n";
|
||||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||||
write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
//write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||||
e.getBoard()->changeTurns();
|
e.getBoard()->changeTurns();
|
||||||
e.getBoard()->setValidFalse();
|
e.getBoard()->setValidFalse();
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue