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 static String boardoutput = "";
|
||||||
public String oldBoard = "";
|
public String oldBoard = "";
|
||||||
public static String undoString = "";
|
public static String undoString = "";
|
||||||
|
public static char myTurn = 'O';
|
||||||
|
|
||||||
public Client() {
|
public Client() {
|
||||||
initializeGui();
|
initializeGui();
|
||||||
|
@ -163,15 +164,15 @@ public class Client {
|
||||||
public String TrimBoard(String board) {
|
public String TrimBoard(String board) {
|
||||||
String output = "";
|
String output = "";
|
||||||
for(int i = 0; i < board.length(); ++i) {
|
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);
|
output += board.charAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(board.charAt(i) == '_') {
|
else if((output.length() != 64) && board.charAt(i) == '_') {
|
||||||
output += 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);
|
output += board.charAt(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,15 +198,6 @@ public class Client {
|
||||||
count++;
|
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;
|
count = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -253,7 +245,19 @@ public class Client {
|
||||||
int tempc = move.charAt(2) - '0';
|
int tempc = move.charAt(2) - '0';
|
||||||
int tempd = move.charAt(3) - '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";
|
//cout << "\nTest\n";
|
||||||
if(tempb == tempd) {
|
if(tempb == tempd) {
|
||||||
output = ((char)('A' + tempb)) + "" + (8 - tempa) + " FWD";
|
output = ((char)('A' + tempb)) + "" + (8 - tempa) + " FWD";
|
||||||
|
@ -310,28 +314,36 @@ public class Client {
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
//userInput = stdIn.readLine();
|
//userInput = stdIn.readLine();
|
||||||
int gameMode = 0;
|
int gameMode = 0;
|
||||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
if(!temp.equals("skip")) {
|
||||||
gameMode = Integer.parseInt(userInput);
|
|
||||||
while(!userInput.equals("1") && !userInput.equals("2")) {
|
|
||||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
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);
|
output.println(userInput);
|
||||||
}
|
}
|
||||||
gameMode = Integer.parseInt(userInput);
|
else {
|
||||||
output.println(userInput);
|
gameMode = 3;
|
||||||
|
myTurn = 'X';
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
output.println(userInput);
|
|
||||||
String out = "";
|
String out = "";
|
||||||
if(gameMode == 1)
|
if(gameMode == 1 || gameMode == 3)
|
||||||
{
|
{
|
||||||
while(!end) {
|
while(!end) {
|
||||||
char[] buffer = new char[256];
|
char[] buffer = new char[256];
|
||||||
|
@ -346,7 +358,12 @@ 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) {
|
||||||
|
String tempstring;
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
if((tempstring = in.readLine())!=null) {
|
||||||
|
System.out.println(String.valueOf(buffer).trim());
|
||||||
|
window.updateBoard(String.valueOf(buffer).trim());
|
||||||
|
}
|
||||||
if(!undoString.equals("")) {
|
if(!undoString.equals("")) {
|
||||||
System.out.println(undoString);
|
System.out.println(undoString);
|
||||||
output.println(undoString);
|
output.println(undoString);
|
||||||
|
@ -362,6 +379,7 @@ public class Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
System.out.println(String.valueOf(buffer).trim());
|
||||||
System.out.println(out);
|
System.out.println(out);
|
||||||
output.println(out);
|
output.println(out);
|
||||||
output.flush();
|
output.flush();
|
||||||
|
|
81
Server.cpp
81
Server.cpp
|
@ -22,10 +22,10 @@ void error(const char *msg)
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int sockfd, newsockfd, portno;
|
int sockfd, newsockfd, newsockfd2, portno;
|
||||||
socklen_t clilen;
|
socklen_t clilen, clilenen;
|
||||||
|
|
||||||
struct sockaddr_in serv_addr, cli_addr;
|
struct sockaddr_in serv_addr, cli_addr, cli2_addr;
|
||||||
|
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
|
@ -96,7 +96,75 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
while(true) {
|
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)
|
while(gameOver != true)
|
||||||
{
|
{
|
||||||
|
@ -122,8 +190,11 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Debugging
|
||||||
|
/*
|
||||||
if(e.getBoard()->isValid())
|
if(e.getBoard()->isValid())
|
||||||
cout << e.getBoard()->getTurn();
|
cout << e.getBoard()->getTurn();
|
||||||
|
*/
|
||||||
|
|
||||||
while(e.getBoard()->getTurn() == 'X' )
|
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) {
|
while (gameOver != true) {
|
||||||
gameOver = e.getBoard()->isGameOver();
|
gameOver = e.getBoard()->isGameOver();
|
||||||
|
|
Reference in a new issue