Adding Client vs Client.
This commit is contained in:
parent
f5ba309a6e
commit
732ff2a709
2 changed files with 124 additions and 35 deletions
78
Client.java
78
Client.java
|
@ -34,6 +34,7 @@ public class Client {
|
|||
public static String boardoutput = "";
|
||||
public String oldBoard = "";
|
||||
public static String undoString = "";
|
||||
public static char myTurn = 'O';
|
||||
|
||||
public Client() {
|
||||
initializeGui();
|
||||
|
@ -163,15 +164,15 @@ public class Client {
|
|||
public String TrimBoard(String board) {
|
||||
String output = "";
|
||||
for(int i = 0; i < board.length(); ++i) {
|
||||
if(board.charAt(i) == 'X') {
|
||||
if((output.length() != 64) && board.charAt(i) == 'X') {
|
||||
output += board.charAt(i);
|
||||
}
|
||||
|
||||
else if(board.charAt(i) == '_') {
|
||||
else if((output.length() != 64) && board.charAt(i) == '_') {
|
||||
output += board.charAt(i);
|
||||
}
|
||||
|
||||
else if(board.charAt(i) == 'O') {
|
||||
else if((output.length() != 64) && board.charAt(i) == 'O') {
|
||||
output += board.charAt(i);
|
||||
}
|
||||
}
|
||||
|
@ -197,15 +198,6 @@ public class Client {
|
|||
count++;
|
||||
}
|
||||
}
|
||||
if(board.charAt(count) == 'X') {
|
||||
boardSquares[7][7].setIcon(new ImageIcon(pieceImages[0]));
|
||||
}
|
||||
else if(board.charAt(count) == 'O') {
|
||||
boardSquares[7][7].setIcon(new ImageIcon(pieceImages[1]));
|
||||
}
|
||||
else {
|
||||
boardSquares[7][7].setIcon(icon);
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
else {
|
||||
|
@ -253,7 +245,19 @@ public class Client {
|
|||
int tempc = move.charAt(2) - '0';
|
||||
int tempd = move.charAt(3) - '0';
|
||||
|
||||
if(tempa == (tempc + 1)) {
|
||||
if((myTurn == 'O') && (tempa == (tempc + 1))) {
|
||||
//cout << "\nTest\n";
|
||||
if(tempb == tempd) {
|
||||
output = ((char)('A' + tempb)) + "" + (8 - tempa) + " FWD";
|
||||
}
|
||||
else if(tempb == (tempd + 1)) {
|
||||
output = ((char)('A' + tempb)) + "" + (8 - tempa) + " LEFT";
|
||||
}
|
||||
else if(tempb == (tempd - 1)) {
|
||||
output = ((char)('A' + tempb)) + "" + (8 - tempa) + " RIGHT";
|
||||
}
|
||||
}
|
||||
else if((myTurn == 'X') && (tempa == (tempc - 1))){
|
||||
//cout << "\nTest\n";
|
||||
if(tempb == tempd) {
|
||||
output = ((char)('A' + tempb)) + "" + (8 - tempa) + " FWD";
|
||||
|
@ -310,28 +314,36 @@ public class Client {
|
|||
frame.setVisible(true);
|
||||
//userInput = stdIn.readLine();
|
||||
int gameMode = 0;
|
||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
||||
gameMode = Integer.parseInt(userInput);
|
||||
while(!userInput.equals("1") && !userInput.equals("2")) {
|
||||
if(!temp.equals("skip")) {
|
||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
||||
gameMode = Integer.parseInt(userInput);
|
||||
while(!userInput.equals("1") && !userInput.equals("2") && !userInput.equals("3")) {
|
||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
||||
output.println(userInput);
|
||||
}
|
||||
gameMode = Integer.parseInt(userInput);
|
||||
output.println(userInput);
|
||||
|
||||
String diff = "?";
|
||||
in.read(b, 0, 256);
|
||||
temp = String.valueOf(b).trim();
|
||||
if(gameMode == 3) {
|
||||
while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
||||
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
|
||||
diff = diff.toUpperCase();
|
||||
if(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
||||
JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
output.println(userInput);
|
||||
}
|
||||
gameMode = Integer.parseInt(userInput);
|
||||
output.println(userInput);
|
||||
|
||||
String diff = "?";
|
||||
in.read(b, 0, 256);
|
||||
temp = String.valueOf(b).trim();
|
||||
while(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
||||
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
|
||||
diff = diff.toUpperCase();
|
||||
if(!diff.equals("EASY") && !diff.equals("MEDIUM") && !diff.equals("HARD")) {
|
||||
JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp);
|
||||
}
|
||||
else {
|
||||
gameMode = 3;
|
||||
myTurn = 'X';
|
||||
}
|
||||
output.println(userInput);
|
||||
String out = "";
|
||||
if(gameMode == 1)
|
||||
if(gameMode == 1 || gameMode == 3)
|
||||
{
|
||||
while(!end) {
|
||||
char[] buffer = new char[256];
|
||||
|
@ -346,7 +358,12 @@ public class Client {
|
|||
System.out.println(String.valueOf(buffer).trim());
|
||||
window.updateBoard(String.valueOf(buffer).trim());
|
||||
while(!end && boardoutput.length() < 4) {
|
||||
String tempstring;
|
||||
Thread.sleep(100);
|
||||
if((tempstring = in.readLine())!=null) {
|
||||
System.out.println(String.valueOf(buffer).trim());
|
||||
window.updateBoard(String.valueOf(buffer).trim());
|
||||
}
|
||||
if(!undoString.equals("")) {
|
||||
System.out.println(undoString);
|
||||
output.println(undoString);
|
||||
|
@ -362,6 +379,7 @@ public class Client {
|
|||
}
|
||||
}
|
||||
}
|
||||
System.out.println(String.valueOf(buffer).trim());
|
||||
System.out.println(out);
|
||||
output.println(out);
|
||||
output.flush();
|
||||
|
|
81
Server.cpp
81
Server.cpp
|
@ -22,10 +22,10 @@ void error(const char *msg)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sockfd, newsockfd, portno;
|
||||
socklen_t clilen;
|
||||
int sockfd, newsockfd, newsockfd2, portno;
|
||||
socklen_t clilen, clilenen;
|
||||
|
||||
struct sockaddr_in serv_addr, cli_addr;
|
||||
struct sockaddr_in serv_addr, cli_addr, cli2_addr;
|
||||
|
||||
int n;
|
||||
|
||||
|
@ -96,7 +96,75 @@ int main(int argc, char *argv[])
|
|||
|
||||
while(true) {
|
||||
|
||||
if(choice == 1)
|
||||
if(choice == 3) {
|
||||
|
||||
listen(sockfd,5);
|
||||
|
||||
clilenen = sizeof(cli_addr);
|
||||
newsockfd2 = accept(sockfd, (struct sockaddr *) &cli2_addr, &clilenen);
|
||||
write(newsockfd2, "skip", 4);//Display the board to the client (line by line)
|
||||
sleep(1);
|
||||
string tempboard = e.getBoard()->boardToString();
|
||||
write(newsockfd2, tempboard.c_str(), tempboard.length());//Display the board to the client (line by line)
|
||||
|
||||
if (newsockfd2 < 0)
|
||||
error("ERROR on accept");
|
||||
|
||||
while(gameOver != true)
|
||||
{
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
|
||||
while(e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid())
|
||||
{
|
||||
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)
|
||||
cout<<"\nWaiting for client: ";
|
||||
n = read(newsockfd,buffer,255);
|
||||
move = buffer;
|
||||
bzero(buffer,256);
|
||||
//cout << move << "\n\n";
|
||||
//move = tempParse(move);
|
||||
parse(move,(*e.getBoard()));
|
||||
|
||||
if(e.getBoard()->isValid()) {
|
||||
e.getBoard()->changeTurns();
|
||||
e.getBoard()->setValidFalse();
|
||||
}
|
||||
}
|
||||
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
e.getBoard()->snapshot(record, *e.getBoard());
|
||||
|
||||
while(e.getBoard()->getTurn() == 'X' && !e.getBoard()->isValid())
|
||||
{
|
||||
e.getBoard()->displayBoard();//Display the board on the server
|
||||
string boardState = e.getBoard()->boardToString();
|
||||
final_move = e.getBoard()->boardToString();
|
||||
write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
cout<<"\nWaiting for client: ";
|
||||
n = read(newsockfd2,buffer,255);
|
||||
move = buffer;
|
||||
bzero(buffer,256);
|
||||
//cout << move << "\n\n";
|
||||
//move = tempParse(move);
|
||||
parse(move,(*e.getBoard()));
|
||||
|
||||
if(e.getBoard()->isValid()) {
|
||||
e.getBoard()->changeTurns();
|
||||
e.getBoard()->setValidFalse();
|
||||
}
|
||||
}
|
||||
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
e.getBoard()->snapshot(record, *e.getBoard());
|
||||
}
|
||||
}
|
||||
|
||||
else if(choice == 1)
|
||||
{
|
||||
while(gameOver != true)
|
||||
{
|
||||
|
@ -122,8 +190,11 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
//Debugging
|
||||
/*
|
||||
if(e.getBoard()->isValid())
|
||||
cout << e.getBoard()->getTurn();
|
||||
*/
|
||||
|
||||
while(e.getBoard()->getTurn() == 'X' )
|
||||
{
|
||||
|
@ -141,7 +212,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
if(choice == 2)
|
||||
else if(choice == 2)
|
||||
{
|
||||
while (gameOver != true) {
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
|
|
Reference in a new issue