This commit is contained in:
Rebecca Schofield 2015-11-03 20:37:50 -06:00
commit 5cfb44a1dd
2 changed files with 160 additions and 96 deletions

View file

@ -309,16 +309,30 @@ public class Client {
frame.setMinimumSize(frame.getSize()); frame.setMinimumSize(frame.getSize());
frame.setVisible(true); frame.setVisible(true);
//userInput = stdIn.readLine(); //userInput = stdIn.readLine();
<<<<<<< HEAD
int gameMode = 0;
userInput = JOptionPane.showInputDialog(frame, temp);
gameMode = Integer.parseInt(userInput);
while(!userInput.equals("1") && !userInput.equals("2")) {
userInput = JOptionPane.showInputDialog(frame, temp);
output.println(userInput);
}
gameMode = Integer.parseInt(userInput);
=======
userInput = JOptionPane.showInputDialog(frame, temp); userInput = JOptionPane.showInputDialog(frame, temp);
//while(!userInput.contains("1") || !userInput.contains("2")) { //while(!userInput.contains("1") || !userInput.contains("2")) {
// userInput = JOptionPane.showInputDialog(frame, temp); // userInput = JOptionPane.showInputDialog(frame, temp);
//} //}
>>>>>>> b517b06bc614e18b2c6252c12fcfdc1eb71fd38f
output.println(userInput); output.println(userInput);
String diff = "?"; String diff = "?";
in.read(b, 0, 256); in.read(b, 0, 256);
temp = String.valueOf(b).trim(); temp = String.valueOf(b).trim();
<<<<<<< HEAD
=======
//System.out.print(temp); //System.out.print(temp);
>>>>>>> b517b06bc614e18b2c6252c12fcfdc1eb71fd38f
while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) { while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine(); diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
diff = diff.toUpperCase(); diff = diff.toUpperCase();
@ -328,6 +342,38 @@ public class Client {
} }
output.println(userInput); output.println(userInput);
String out = ""; String out = "";
<<<<<<< HEAD
if(gameMode == 1)
{
while(!end) {
char[] buffer = new char[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());
window.updateBoard(String.valueOf(buffer).trim());
while(!end && boardoutput.length() < 4) {
Thread.sleep(100);
if(!undoString.equals("")) {
System.out.println(undoString);
output.println(undoString);
output.flush();
undoString = "";
}
if(boardoutput.length() == 4) {
out = parseOutput(boardoutput);
if(out == "Invalid") {
out = "";
boardoutput = "";
System.out.println("Invalid move.");
}
}
=======
while(!end) { while(!end) {
char[] buffer = new char[256]; char[] buffer = new char[256];
@ -337,36 +383,61 @@ public class Client {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
JOptionPane.showMessageDialog(null, "\nGAME OVER\n"); JOptionPane.showMessageDialog(null, "\nGAME OVER\n");
end = true; end = true;
>>>>>>> b517b06bc614e18b2c6252c12fcfdc1eb71fd38f
} }
System.out.println(out);
output.println(out);
output.flush();
boardoutput = "";
} }
System.out.println(String.valueOf(buffer).trim()); output.close();
window.updateBoard(String.valueOf(buffer).trim()); in.close();
while(!end && boardoutput.length() < 4) { stdIn.close();
Thread.sleep(100); echoSocket.close();
if(!undoString.equals("")) { }
System.out.println(undoString); if(gameMode == 2)
output.println(undoString); {
output.flush(); while(!end) {
undoString = ""; char[] buffer = new char[256];
} in.read(buffer, 0, 256);
if(boardoutput.length() == 4) { for(int i = 0; i < buffer.length; ++i) {
out = parseOutput(boardoutput); if(Arrays.equals(Arrays.copyOfRange(buffer, i, i+9), go)) {
if(out == "Invalid") { frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
out = ""; JOptionPane.showMessageDialog(null, "\nGAME OVER\n");
boardoutput = ""; end = true;
System.out.println("Invalid move.");
} }
} }
System.out.println(String.valueOf(buffer).trim());
window.updateBoard(String.valueOf(buffer).trim());
/*
while(!end && boardoutput.length() < 4) {
Thread.sleep(100);
if(!undoString.equals("")) {
System.out.println(undoString);
//output.println(undoString);
//output.flush();
undoString = "";
}
if(boardoutput.length() == 4) {
out = parseOutput(boardoutput);
if(out == "Invalid") {
out = "";
boardoutput = "";
System.out.println("Invalid move.");
}
}
}
*/
System.out.println(out);
//output.println(out);
//output.flush();
boardoutput = "";
} }
System.out.println(out); output.close();
output.println(out); in.close();
output.flush(); stdIn.close();
boardoutput = ""; echoSocket.close();
} }
output.close();
in.close();
stdIn.close();
echoSocket.close();
} }
catch (IOException e){ catch (IOException e){
System.err.println("IOException: " + e.getMessage()); System.err.println("IOException: " + e.getMessage());

View file

@ -19,44 +19,6 @@ void error(const char *msg)
perror(msg); perror(msg);
exit(1); exit(1);
} }
/*
string tempParse(string move) {
string output = "";
int tempa = move[0] - '0';
int tempb = move[1] - '0';
int tempc = move[2] - '0';
int tempd = move[3] - '0';
//cout << "move: " << move << " " << tempa << " " << tempb << " " << tempc << " " << tempd << "\n";
for(int c = 0; c < 4; c++) {
if(!isdigit(move[c])) {
//cout << "\nfjdkjfjd\n";
if(move.substr(0, 4) == "UNDO") {
return "UNDO";
}
return move;
}
}
if(tempa == (tempc + 1)) {
//cout << "\nTest\n";
if(tempb == tempd) {
output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_f";
}
else if(tempb == (tempd + 1)) {
output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_l";
}
else if(tempb == (tempd - 1)) {
output = ((char)('A' + tempb)) + to_string(8 - tempa) + "_r";
}
}
else {
return move;
}
cout << "Debugging: " << output << "\n\n";
return output;
}
*/
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -134,47 +96,78 @@ int main(int argc, char *argv[])
while(true) { while(true) {
while(gameOver != true) if(choice == 1)
{
gameOver = e.getBoard()->isGameOver();
while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid())
{ {
e.getBoard()->displayBoard();//Display the board on the server while(gameOver != true)
string boardState = e.getBoard()->boardToString(); {
final_move = e.getBoard()->boardToString(); gameOver = e.getBoard()->isGameOver();
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
cout<<"\nWaiting for client: "; while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid())
n = read(newsockfd,buffer,255); {
move = buffer; e.getBoard()->displayBoard();//Display the board on the server
bzero(buffer,256); string boardState = e.getBoard()->boardToString();
//cout << move << "\n\n"; final_move = e.getBoard()->boardToString();
//move = tempParse(move); write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
parse(move,(*e.getBoard())); cout<<"\nWaiting for client: ";
n = read(newsockfd,buffer,255);
if(e.getBoard()->isValid()) { move = buffer;
e.getBoard()->changeTurns(); bzero(buffer,256);
//cout << move << "\n\n";
//move = tempParse(move);
parse(move,(*e.getBoard()));
if(e.getBoard()->isValid()) {
e.getBoard()->changeTurns();
e.getBoard()->setValidFalse();
}
}
if(e.getBoard()->isValid())
cout << e.getBoard()->getTurn();
while(e.getBoard()->getTurn() == 'X' )
{
if(choice_difficulty == "EASY")
e.AI(3);
else if(choice_difficulty == "MEDIUM")
e.AI(5);
else
e.AI(7);
}
gameOver = e.getBoard()->isGameOver();
e.getBoard()->setValidFalse(); e.getBoard()->setValidFalse();
e.getBoard()->snapshot(record, *e.getBoard());
} }
} }
if(e.getBoard()->isValid()) if(choice == 2)
cout << e.getBoard()->getTurn();
while(e.getBoard()->getTurn() == 'X' )
{ {
if(choice_difficulty == "EASY") while (gameOver != true) {
e.AI(3); gameOver = e.getBoard()->isGameOver();
else if(choice_difficulty == "MEDIUM")
e.AI(5); while(e.getBoard()->getTurn() == 'O') {
else e.getBoard()->displayBoard();//Display the board on the server
e.AI(7); string boardState = e.getBoard()->boardToString();
final_move = e.getBoard()->boardToString();
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
e.AI(7);
}
e.getBoard()->displayBoard();
sleep(1);
while(e.getBoard()->getTurn() == 'X' ) {
e.getBoard()->displayBoard();//Display the board on the server
string boardState = e.getBoard()->boardToString();
final_move = e.getBoard()->boardToString();
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
e.AI(7);
}
gameOver = e.getBoard()->isGameOver();
}
} }
gameOver = e.getBoard()->isGameOver();
e.getBoard()->setValidFalse();
e.getBoard()->snapshot(record, *e.getBoard());
}
final_move = e.getBoard()->boardToString(); final_move = e.getBoard()->boardToString();
string game_over = "\n\nGAME OVER!!!\n"; string game_over = "\n\nGAME OVER!!!\n";