Final finishing touches on Client vs. Client.

This commit is contained in:
Alexander Huddleston 2015-11-03 23:24:42 -06:00
parent d38a38c598
commit 1dae33800e
3 changed files with 20 additions and 10 deletions

View file

@ -318,9 +318,9 @@ public class Client {
output.println(userInput); output.println(userInput);
String diff = "?"; String diff = "?";
if(gameMode != 3) {
in.read(b, 0, 256); in.read(b, 0, 256);
temp = String.valueOf(b).trim(); temp = String.valueOf(b).trim();
if(gameMode != 3) {
while(!diff.equals("1") && !diff.equals("2") && !diff.equals("3")) { 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();
@ -337,9 +337,9 @@ public class Client {
else if(diff.equals("3")) { else if(diff.equals("3")) {
userInput = "HARD"; userInput = "HARD";
} }
}
output.println(userInput); output.println(userInput);
} }
}
else { else {
gameMode = 3; gameMode = 3;
myTurn = 'X'; myTurn = 'X';
@ -362,6 +362,13 @@ public class Client {
while(!end && boardoutput.length() < 4) { while(!end && boardoutput.length() < 4) {
if(in.ready()) { if(in.ready()) {
in.read(buffer, 0, 256); in.read(buffer, 0, 256);
for(int i = 0; i < buffer.length; ++i) {
if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
JOptionPane.showMessageDialog(null, "\nGAME OVER\n");
end = true;
}
}
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());
} }

View file

@ -34,13 +34,14 @@ void parseAndMove(vector<string> tokens, Board& board) {
if(tokens[0].size() == 2) { if(tokens[0].size() == 2) {
if(tokens[1].size() >= 3 && tokens[1].size() <= 5) { if(tokens[1].size() >= 3 && tokens[1].size() <= 5) {
if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 7) { cout << "Inside parser " << tokens[0] << "\n\n";
if(tokens[0][0] - 'A' < 0 || tokens[0][0] - 'A' > 8) {
cout << "Error. Invalid move location. (1st coord.)\n\n"; cout << "Error. Invalid move location. (1st coord.)\n\n";
cout << tokens[0][0] << " " << (tokens[0][0] - 'A') << "\n\n"; cout << tokens[0][0] << " " << (tokens[0][0] - 'A') << "\n\n";
return; return;
} }
else if(tokens[0][1] - '0' < 0 || tokens[0][1] - '0' > 7) { else if(tokens[0][1] - '0' < 0 || tokens[0][1] - '0' > 8) {
cout << "Error. Invalid move location. (1st coord.)\n\n"; cout << "Error. Invalid move location. (2nd coord.)\n\n";
return; return;
} }
else { else {

View file

@ -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?3. Play Client vs. Client?\nEnter choice: \n"; string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\n3. 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
@ -118,7 +118,7 @@ int main(int argc, char *argv[])
{ {
gameOver = e.getBoard()->isGameOver(); gameOver = e.getBoard()->isGameOver();
while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid()) while(!gameOver && e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid())
{ {
e.getBoard()->displayBoard();//Display the board on the server e.getBoard()->displayBoard();//Display the board on the server
string boardState = e.getBoard()->boardToString(); string boardState = e.getBoard()->boardToString();
@ -145,7 +145,7 @@ int main(int argc, char *argv[])
e.getBoard()->setValidFalse(); e.getBoard()->setValidFalse();
e.getBoard()->snapshot(record, *e.getBoard()); e.getBoard()->snapshot(record, *e.getBoard());
while(e.getBoard()->getTurn() == 'X' && !e.getBoard()->isValid()) while(!gameOver && e.getBoard()->getTurn() == 'X' && !e.getBoard()->isValid())
{ {
e.getBoard()->displayBoard();//Display the board on the server e.getBoard()->displayBoard();//Display the board on the server
string boardState = e.getBoard()->boardToString(); string boardState = e.getBoard()->boardToString();
@ -254,6 +254,8 @@ int main(int argc, char *argv[])
string game_over = "\n\nGAME OVER!!!\n"; string game_over = "\n\nGAME OVER!!!\n";
write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line) write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line)
write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over
write(newsockfd2, final_move.c_str(), final_move.length()); //Display the board to the client (line by line)
write(newsockfd2, game_over.c_str(), game_over.length()); //Tell the client the game is over
cout << game_over; cout << game_over;
usleep(1); usleep(1);
close(newsockfd); close(newsockfd);