This commit is contained in:
Alexander Huddleston 2015-10-28 15:08:07 -05:00
parent 14c9003932
commit b6ac66013e
4 changed files with 327 additions and 0 deletions

46
Client.java Executable file
View file

@ -0,0 +1,46 @@
// Alex Huddleston
// Breakthrough Client in Java
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.io.DataInputStream;
public class Client {
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
String hostname = args[0];
int portnum = Integer.parseInt(args[1]);
//keyboard.nextLine(); // used to buffer out extra space.
try {
Socket echoSocket = new Socket(hostname, portnum);
PrintWriter output = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
char[] b = new char[256];
in.read(b, 0, 256);
System.out.println("Response:\n" + b);
while((userInput = stdIn.readLine()) != null) {
output.println(userInput);
output.flush();
char[] buffer = new char[256];
in.read(buffer, 0, 256);
System.out.println(buffer);
System.out.println("testing.");
}
}
catch (IOException e){
System.err.println("IOException: " + e.getMessage());
}
}
}

152
Server.cpp Normal file
View file

@ -0,0 +1,152 @@
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <vector>
#include "Board.h"
#include "Engine.h"
using namespace std;
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
//After all the server setup crap is done, we start the board
// cout<<"WELCOME\n";
// cout<<"1. Play against AI?\n";
// cout<<"2. Play against a human?\n";
// cout<<"Enter choice: \n";
// cin >> choice;
// cout << "OK" << endl;
Engine e;
//Board* temp = e.getBoard();
//Board b = *temp;
string move;
//Brute force up in here!
bool gameOver = false;
vector<Board> record;
e.getBoard()->snapshot(record,(*e.getBoard()));
char buffer[256];
char info[256];
int choice;
int move_counter = 0;
string final_move;
write(newsockfd, "Select a choice:\n", 18);
//Waiting for client to select game type
n = read(newsockfd,info,255);
istringstream convert(info);
convert >> choice; //Sets choice equal to 1 or 2, based on clients input
//cout << choice << "\n\n";
bzero(info,256); //Resets info back to normal, "choice" now contains client's value
while(true) {
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 = b.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;
cout << "\ntest" << move << "\n\n";
e.getBoard()->interpret(move,(*e.getBoard()));
gameOver = e.getBoard()->isGameOver();
if(gameOver == true) {
string endGame = "Game_Over";
//write(newsockfd, endGame.c_str(), endGame.length()); //Display the board to the client (line by line)
break;
}
else {
string continueGame = "Continue_Game";
//write(newsockfd, continueGame.c_str(), continueGame.length()); //Display the board to the client (line by line)
e.getBoard()->changeTurns();
}
}
vector<moves> possibleMoves = e.getBoard()->viewPossibleMoves();
if(choice == 1) {
cout << "test\n\n";
e.easyAI();
}
}
/*
string final_move = b.boardToString();
write(newsockfd, final_move.c_str(), final_move.length());//Display the board to the client (line by line)
write(newsockfd, final_move.c_str(), final_move.length());
cout << "\n\nGAME OVER!!!";
usleep(1);
*/
close(newsockfd);
close(sockfd);
break;
}
return 0;
}

114
Serverbackup.txt Executable file
View file

@ -0,0 +1,114 @@
cout << "Launching server..." << endl;
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (port < 2) {
cout << "ERROR, no port provided\n";
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
cout << "ERROR opening socket";
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = port;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
cout << "ERROR on binding";
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
cout << "ERROR on accept";
string move;
bool gameOver = false;
vector<Board> record;
b->snapshot(record, *b);
char buffer[256];
char info[256];
int choice = -1;
int choice_difficulty = -1;
string final_move;
//Ask client about game type
string introduction = "WELCOME\n1. Play against AI?\n2. Watch AI vs. AI?\nEnter choice: \n";
write(newsockfd, introduction.c_str(), introduction.length());
n = read(newsockfd,info,255); //Reads choice as a string
istringstream convert(info); //Converts the read string to an integer
convert >> choice; //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
/*//Becca- once your AI code is working, use this code:
string difficulty_select = "Choose Difficulty\n1. Easy\n2. Medium\n4. Hard\nEnter choice: \n";
write(newsockfd, difficulty_select.c_str(), difficulty_select.length());
n = read(newsockfd,info,255);
istringstream convert(info);
convert >> choice_difficulty;
cout << "OK" << endl;
bzero(info,256);
*/
while (gameOver != true)
{
gameOver = b->isGameOver();
while(b->getTurn() == 'O' && !b->isValid())
{
b->displayBoard();
string boardState = b->boardToString();
final_move = b->boardToString();
write(newsockfd, boardState.c_str(), boardState.length());//Display the board to the client (line by line)
cout<<"\nStanding by for client... \n";
n = read(newsockfd,buffer,255);//Read the client's input
move = buffer;
bzero(buffer,256);
b->interpret(move, *b);
if(b->isValid()) {
b->changeTurns();
b->setValidFalse();
}
}
if(b->isValid()) cout << b->getTurn();
while(b->getTurn() == 'X' )
{
easyAI();
/*Becca- once you finish your AI, uncomment this out and remove the line directly above this
if(choice_difficulty == 1)
easyAI();
else if(choice_difficulty == 2)
mediumAI();
else
hardAI();
*/
}
gameOver = b->isGameOver();
b->setValidFalse();
b->snapshot(record, *b);
}
final_move = b->boardToString();
string game_over = "\n\nGAME OVER!!!\n";
write(newsockfd, final_move.c_str(), final_move.length()); //Display the board to the client (line by line)
write(newsockfd, game_over.c_str(), game_over.length()); //Tell the client the game is over
cout << game_over;
usleep(1);
close(newsockfd);
close(sockfd);

15
makefile Executable file
View file

@ -0,0 +1,15 @@
# makefile
all: test server
server: Server.o
g++ -std=c++11 -o server Server.o Engine.o Piece.o Board.o
Server.o: Server.cpp Engine.cpp Board.cpp Piece.cpp
g++ -std=c++11 -c -g Server.cpp Engine.cpp Board.cpp Piece.cpp
test: test.o
g++ -std=c++11 -o test test.o Engine.o Piece.o Board.o
test.o: test.cpp Engine.cpp Board.cpp Piece.cpp
g++ -std=c++11 -c -g test.cpp Engine.cpp Board.cpp Piece.cpp