Completed Client vs Client AI vs AI implementation.
This commit is contained in:
parent
1dae33800e
commit
7f97b007f5
2 changed files with 210 additions and 69 deletions
119
Client.java
119
Client.java
|
@ -307,9 +307,10 @@ public class Client {
|
|||
frame.setVisible(true);
|
||||
//userInput = stdIn.readLine();
|
||||
int gameMode = 0;
|
||||
String diff = "?";
|
||||
if(!temp.equals("skip")) {
|
||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
||||
gameMode = Integer.parseInt(userInput);
|
||||
//gameMode = Integer.parseInt(userInput);
|
||||
while(!userInput.equals("1") && !userInput.equals("2") && !userInput.equals("3")) {
|
||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
||||
output.println(userInput);
|
||||
|
@ -317,7 +318,6 @@ public class Client {
|
|||
gameMode = Integer.parseInt(userInput);
|
||||
output.println(userInput);
|
||||
|
||||
String diff = "?";
|
||||
if(gameMode != 3) {
|
||||
in.read(b, 0, 256);
|
||||
temp = String.valueOf(b).trim();
|
||||
|
@ -347,19 +347,91 @@ public class Client {
|
|||
String out = "";
|
||||
if(gameMode == 3)
|
||||
{
|
||||
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;
|
||||
in.read(b, 0, 256);
|
||||
temp = String.valueOf(b).trim();
|
||||
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);
|
||||
|
||||
if(gameMode == 2) {
|
||||
in.read(b, 0, 256);
|
||||
temp = String.valueOf(b).trim();
|
||||
userInput = JOptionPane.showInputDialog(frame, temp);
|
||||
diff = userInput;
|
||||
while(!diff.equals("1") && !diff.equals("2") && !diff.equals("3")) {
|
||||
diff = JOptionPane.showInputDialog(frame, temp);//diff = stdIn.readLine();
|
||||
//diff = diff.toUpperCase();
|
||||
if(!diff.equals("1") && !diff.equals("2") && !diff.equals("3")) {
|
||||
JOptionPane.showMessageDialog(null, diff + "\nInvalid difficulty.\n" + temp);
|
||||
}
|
||||
}
|
||||
System.out.println(String.valueOf(buffer).trim());
|
||||
window.updateBoard(String.valueOf(buffer).trim());
|
||||
while(!end && boardoutput.length() < 4) {
|
||||
if(diff.equals("1")) {
|
||||
userInput = "EASY";
|
||||
}
|
||||
else if(diff.equals("2")) {
|
||||
userInput = "MEDIUM";
|
||||
}
|
||||
else if(diff.equals("3")) {
|
||||
userInput = "HARD";
|
||||
}
|
||||
output.println(userInput);
|
||||
}
|
||||
|
||||
while(!end) {
|
||||
char[] buffer = new char[256];
|
||||
if(gameMode == 1) {
|
||||
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) {
|
||||
if(in.ready()) {
|
||||
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());
|
||||
}
|
||||
String tempstring;
|
||||
Thread.sleep(1);
|
||||
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(String.valueOf(buffer).trim());
|
||||
System.out.println(out);
|
||||
output.println(out);
|
||||
output.flush();
|
||||
boardoutput = "";
|
||||
}
|
||||
else {
|
||||
if(in.ready()) {
|
||||
in.read(buffer, 0, 256);
|
||||
for(int i = 0; i < buffer.length; ++i) {
|
||||
|
@ -372,28 +444,7 @@ public class Client {
|
|||
System.out.println(String.valueOf(buffer).trim());
|
||||
window.updateBoard(String.valueOf(buffer).trim());
|
||||
}
|
||||
String tempstring;
|
||||
Thread.sleep(1);
|
||||
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(String.valueOf(buffer).trim());
|
||||
System.out.println(out);
|
||||
output.println(out);
|
||||
output.flush();
|
||||
boardoutput = "";
|
||||
}
|
||||
output.close();
|
||||
in.close();
|
||||
|
|
160
Server.cpp
160
Server.cpp
|
@ -86,8 +86,8 @@ int main(int argc, char *argv[])
|
|||
bzero(info,256); //Resets info back to normal
|
||||
|
||||
string choice_difficulty = "EASY";
|
||||
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n3. Hard\nEnter choice: \n";
|
||||
if(choice != 3) {
|
||||
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n3. Hard\nEnter choice: \n";
|
||||
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
|
||||
n = read(newsockfd,info,255);
|
||||
istringstream con(info);
|
||||
|
@ -99,12 +99,58 @@ int main(int argc, char *argv[])
|
|||
while(true) {
|
||||
|
||||
if(choice == 3) {
|
||||
int choice1;
|
||||
int choice2;
|
||||
string choice1_difficulty;
|
||||
string choice2_difficulty;
|
||||
//Ask client about game type
|
||||
string choice3 = "1. Human\n2. AI\nEnter choice: \n";
|
||||
write(newsockfd, choice3.c_str(), choice3.length());
|
||||
n = read(newsockfd,info,255); //Reads choice as a string
|
||||
istringstream convert(info); //Converts the read string to an integer
|
||||
convert >> choice1; //Sets value equal to the converted value
|
||||
//Later in the project, we need to check for AI and Human modes
|
||||
cout << "OK" << endl;
|
||||
bzero(info,256); //Resets info back to normal
|
||||
|
||||
if(choice1 == 2) {
|
||||
//Ask client about game type
|
||||
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
|
||||
n = read(newsockfd,info,255); //Reads choice as a string
|
||||
istringstream convert1(info); //Converts the read string to an integer
|
||||
convert1 >> choice1_difficulty; //Sets value equal to the converted value
|
||||
//Later in the project, we need to check for AI and Human modes
|
||||
cout << "OK" << endl;
|
||||
bzero(info,256); //Resets info back to normal
|
||||
}
|
||||
|
||||
listen(sockfd,5);
|
||||
|
||||
clilenen = sizeof(cli_addr);
|
||||
newsockfd2 = accept(sockfd, (struct sockaddr *) &cli2_addr, &clilenen);
|
||||
write(newsockfd2, "skip", 4);
|
||||
|
||||
sleep(1);
|
||||
//Ask client about game type
|
||||
write(newsockfd2, choice3.c_str(), choice3.length());
|
||||
n = read(newsockfd2,info,255); //Reads choice as a string
|
||||
istringstream convert2(info); //Converts the read string to an integer
|
||||
convert2 >> choice2; //Sets value equal to the converted value
|
||||
//Later in the project, we need to check for AI and Human modes
|
||||
cout << "OK" << endl;
|
||||
bzero(info,256); //Resets info back to normal
|
||||
|
||||
if(choice2 == 2) {
|
||||
//Ask client about game type
|
||||
write(newsockfd2, difficulty_select.c_str(), difficulty_select.length());
|
||||
n = read(newsockfd2,info,255); //Reads choice as a string
|
||||
istringstream convert(info); //Converts the read string to an integer
|
||||
convert >> choice2_difficulty; //Sets value equal to the converted value
|
||||
//Later in the project, we need to check for AI and Human modes
|
||||
cout << "OK" << endl;
|
||||
bzero(info,256); //Resets info back to normal
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
string tempboard = e.getBoard()->boardToString();
|
||||
write(newsockfd2, tempboard.c_str(), tempboard.length());//Display the board to the client (line by line)
|
||||
|
@ -117,60 +163,104 @@ int main(int argc, char *argv[])
|
|||
while(gameOver != true)
|
||||
{
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
|
||||
while(!gameOver && e.getBoard()->getTurn() == 'O' && !e.getBoard()->isValid())
|
||||
{
|
||||
cout << "Turn 1\n\n";
|
||||
e.getBoard()->displayBoard();//Display the board on the server
|
||||
string boardState = e.getBoard()->boardToString();
|
||||
final_move = e.getBoard()->boardToString();
|
||||
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()) {
|
||||
boardState = e.getBoard()->boardToString();
|
||||
cout << boardState << "testing\n\n";
|
||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
//write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
e.getBoard()->changeTurns();
|
||||
e.getBoard()->setValidFalse();
|
||||
if(choice1 == 1) {
|
||||
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()) {
|
||||
boardState = e.getBoard()->boardToString();
|
||||
cout << boardState << "testing\n\n";
|
||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
//write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
e.getBoard()->changeTurns();
|
||||
e.getBoard()->setValidFalse();
|
||||
}
|
||||
}
|
||||
else {
|
||||
//write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
if(choice1_difficulty == "EASY")
|
||||
e.AI(17);
|
||||
else if(choice1_difficulty == "MEDIUM")
|
||||
e.AI(47);
|
||||
else
|
||||
e.AI(77);
|
||||
|
||||
//e.getBoard()->changeTurns();
|
||||
}
|
||||
}
|
||||
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
e.getBoard()->snapshot(record, *e.getBoard());
|
||||
if(choice1 == 1) {
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
e.getBoard()->snapshot(record, *e.getBoard());
|
||||
}
|
||||
else {
|
||||
e.getBoard()->displayBoard();
|
||||
e.getBoard()->setValidFalse();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
while(!gameOver && e.getBoard()->getTurn() == 'X' && !e.getBoard()->isValid())
|
||||
{
|
||||
cout << "Turn 2\n\n";
|
||||
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()) {
|
||||
boardState = e.getBoard()->boardToString();
|
||||
if(choice2 == 1) {
|
||||
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()) {
|
||||
boardState = e.getBoard()->boardToString();
|
||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
e.getBoard()->changeTurns();
|
||||
e.getBoard()->setValidFalse();
|
||||
}
|
||||
}
|
||||
else {
|
||||
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
e.getBoard()->changeTurns();
|
||||
e.getBoard()->setValidFalse();
|
||||
//write(newsockfd2, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
|
||||
if(choice2_difficulty == "EASY")
|
||||
e.AI(17);
|
||||
else if(choice2_difficulty == "MEDIUM")
|
||||
e.AI(47);
|
||||
else
|
||||
e.AI(77);
|
||||
|
||||
//e.getBoard()->changeTurns();
|
||||
}
|
||||
}
|
||||
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
e.getBoard()->snapshot(record, *e.getBoard());
|
||||
if(choice2 == 1) {
|
||||
gameOver = e.getBoard()->isGameOver();
|
||||
e.getBoard()->setValidFalse();
|
||||
e.getBoard()->snapshot(record, *e.getBoard());
|
||||
}
|
||||
else {
|
||||
e.getBoard()->displayBoard();
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue