Added Context menus to GUI.
This commit is contained in:
parent
c807d75e14
commit
1cdc7e4e3c
2 changed files with 158 additions and 114 deletions
71
Client.java
71
Client.java
|
@ -1,4 +1,4 @@
|
||||||
// Alex Huddleston
|
// Alex Huddleston and Brandon Jackson
|
||||||
// Breakthrough Client in Java
|
// Breakthrough Client in Java
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -289,10 +289,6 @@ public class Client {
|
||||||
in.read(b, 0, 256);
|
in.read(b, 0, 256);
|
||||||
|
|
||||||
String temp = String.valueOf(b).trim();
|
String temp = String.valueOf(b).trim();
|
||||||
System.out.print(temp);
|
|
||||||
//setBoard(temp);
|
|
||||||
//window.drawBoard(temp);
|
|
||||||
|
|
||||||
String g = "GAME OVER";
|
String g = "GAME OVER";
|
||||||
char[] go = new char[9];
|
char[] go = new char[9];
|
||||||
for(int x = 0; x < go.length; ++x) {
|
for(int x = 0; x < go.length; ++x) {
|
||||||
|
@ -312,27 +308,38 @@ public class Client {
|
||||||
frame.pack();
|
frame.pack();
|
||||||
frame.setMinimumSize(frame.getSize());
|
frame.setMinimumSize(frame.getSize());
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
userInput = stdIn.readLine();
|
//userInput = stdIn.readLine();
|
||||||
|
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);
|
||||||
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();
|
||||||
System.out.print(temp);
|
|
||||||
while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
||||||
diff = stdIn.readLine();
|
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
|
||||||
|
diff = diff.toUpperCase();
|
||||||
if(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
if(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
||||||
System.out.print(diff + "\nInvalid difficulty.\n" + temp);
|
JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
output.println(userInput);
|
output.println(userInput);
|
||||||
String out = "";
|
String out = "";
|
||||||
|
if(gameMode == 1)
|
||||||
|
{
|
||||||
while(!end) {
|
while(!end) {
|
||||||
char[] buffer = new char[256];
|
char[] buffer = new char[256];
|
||||||
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));
|
||||||
|
JOptionPane.showMessageDialog(null, "\nGAME OVER\n");
|
||||||
end = true;
|
end = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -365,6 +372,50 @@ public class Client {
|
||||||
stdIn.close();
|
stdIn.close();
|
||||||
echoSocket.close();
|
echoSocket.close();
|
||||||
}
|
}
|
||||||
|
if(gameMode == 2)
|
||||||
|
{
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
System.out.println(out);
|
||||||
|
//output.println(out);
|
||||||
|
//output.flush();
|
||||||
|
boardoutput = "";
|
||||||
|
}
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
|
|
69
Server.cpp
69
Server.cpp
|
@ -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,6 +96,8 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
|
|
||||||
|
if(choice == 1)
|
||||||
|
{
|
||||||
while(gameOver != true)
|
while(gameOver != true)
|
||||||
{
|
{
|
||||||
gameOver = e.getBoard()->isGameOver();
|
gameOver = e.getBoard()->isGameOver();
|
||||||
|
@ -175,6 +139,35 @@ int main(int argc, char *argv[])
|
||||||
e.getBoard()->setValidFalse();
|
e.getBoard()->setValidFalse();
|
||||||
e.getBoard()->snapshot(record, *e.getBoard());
|
e.getBoard()->snapshot(record, *e.getBoard());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(choice == 2)
|
||||||
|
{
|
||||||
|
while (gameOver != true) {
|
||||||
|
gameOver = e.getBoard()->isGameOver();
|
||||||
|
|
||||||
|
while(e.getBoard()->getTurn() == 'O') {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final_move = e.getBoard()->boardToString();
|
final_move = e.getBoard()->boardToString();
|
||||||
string game_over = "\n\nGAME OVER!!!\n";
|
string game_over = "\n\nGAME OVER!!!\n";
|
||||||
|
|
Reference in a new issue