Merge branch 'master' into alex
This commit is contained in:
commit
a2f5023881
5 changed files with 802 additions and 0 deletions
28
Example socketProgramming
Normal file
28
Example socketProgramming
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
I included an example of how to do socket programming; this example uses the server.C file and the client.C file attached:
|
||||||
|
|
||||||
|
How to use these files to see example:
|
||||||
|
|
||||||
|
1) open two different putty windows, one of these windows will be used for the client, and the other one for the server
|
||||||
|
2) compile each file on separatedly and make two object files using g++ command as follows (on either window):
|
||||||
|
|
||||||
|
g++ server.C -o server
|
||||||
|
|
||||||
|
g++ client.C -o client
|
||||||
|
|
||||||
|
3) start the server first, run the server object file with a port number (some number between 2000 and 65535) in the server window (just pick one of the two). For example: ./server 51717
|
||||||
|
|
||||||
|
once the server runs, it will wait for the client to connect.
|
||||||
|
|
||||||
|
4) Start the client in the other putty window with two arguments: the host name (such as linux.cse.tamu.edu) and the port number used to start the server (int this case: 51717). For example:
|
||||||
|
|
||||||
|
./client linux.cse.tamu.edu 51717
|
||||||
|
|
||||||
|
Once you start the client, the client can send a message to the server. Type a message, enter it, and the server will receive it. Once the server receives the message, it will display it on the server window and terminate. These files can be modified so that it won't terminate after sending only one message.
|
||||||
|
|
||||||
|
*Note:*
|
||||||
|
If you run the server immediately for the second time using the same port number, the server won't accept this number because it says that it is being used. This is because it takes a couple of minutes for the server to release this port number. So in this case you can either run the server using a different name, or wait a couple of minutes before running the server again with the same port number.
|
||||||
|
|
||||||
|
|
||||||
|
I hope this helps. Let me know if you have any questions.
|
||||||
|
|
||||||
|
-William
|
10
README.md
10
README.md
|
@ -1,2 +1,12 @@
|
||||||
# Breakthrough
|
# Breakthrough
|
||||||
Reposity for the second CSCE 315 project
|
Reposity for the second CSCE 315 project
|
||||||
|
|
||||||
|
The main.cpp file contains all the structure necessary to play breakthrough in a local machine; it has all the classes incorporated to it, and it seems to be bug-free and bulletproof. So far, it only has a simple AI that picks random movements.
|
||||||
|
|
||||||
|
To test it:
|
||||||
|
|
||||||
|
- compile using c++11. For example: g++ -std=c++11 main.cpp
|
||||||
|
- An example of valid command is: B2_FWD, where B is the column letter, 2 is the row number (see board print on screen when running the program), underscore, and either "FWD" for forward, "LEFT" for moving a piece forward (relative to the piece) and left (relative to the board, not the piece), or "RIGHT" (similar to left, but moving right relative to the board).
|
||||||
|
|
||||||
|
As of now, the program will automatically terminate once either player wins.
|
||||||
|
|
||||||
|
|
57
client.C
Normal file
57
client.C
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
void error(const char *msg)
|
||||||
|
{
|
||||||
|
perror(msg);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int sockfd, portno, n;
|
||||||
|
struct sockaddr_in serv_addr;
|
||||||
|
struct hostent *server;
|
||||||
|
|
||||||
|
char buffer[256];
|
||||||
|
if (argc < 3) {
|
||||||
|
fprintf(stderr,"usage %s hostname port\n", argv[0]);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
portno = atoi(argv[2]);
|
||||||
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sockfd < 0)
|
||||||
|
error("ERROR opening socket");
|
||||||
|
server = gethostbyname(argv[1]);
|
||||||
|
if (server == NULL) {
|
||||||
|
fprintf(stderr,"ERROR, no such host\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
bzero((char *) &serv_addr, sizeof(serv_addr));
|
||||||
|
serv_addr.sin_family = AF_INET;
|
||||||
|
bcopy((char *)server->h_addr,
|
||||||
|
(char *)&serv_addr.sin_addr.s_addr,
|
||||||
|
server->h_length);
|
||||||
|
serv_addr.sin_port = htons(portno);
|
||||||
|
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
|
||||||
|
error("ERROR connecting");
|
||||||
|
printf("Please enter the message: ");
|
||||||
|
bzero(buffer,256);
|
||||||
|
fgets(buffer,255,stdin);
|
||||||
|
n = write(sockfd,buffer,strlen(buffer));
|
||||||
|
if (n < 0)
|
||||||
|
error("ERROR writing to socket");
|
||||||
|
bzero(buffer,256);
|
||||||
|
n = read(sockfd,buffer,255);
|
||||||
|
if (n < 0)
|
||||||
|
error("ERROR reading from socket");
|
||||||
|
printf("%s\n",buffer);
|
||||||
|
close(sockfd);
|
||||||
|
return 0;
|
||||||
|
}
|
624
main.cpp
Normal file
624
main.cpp
Normal file
|
@ -0,0 +1,624 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// class piece
|
||||||
|
// {
|
||||||
|
// char color; //w for white, b for white, e for empty
|
||||||
|
// int xpos;
|
||||||
|
// int ypos;
|
||||||
|
|
||||||
|
// public:
|
||||||
|
|
||||||
|
// piece(int x, int y)
|
||||||
|
// {
|
||||||
|
// xpos = x;
|
||||||
|
// ypos = y;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void setColor(char kolor)
|
||||||
|
// {
|
||||||
|
// color = kolor;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// char getColor()
|
||||||
|
// {
|
||||||
|
// return color;
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
|
||||||
|
string myToUpper(string input)
|
||||||
|
{
|
||||||
|
string output;
|
||||||
|
|
||||||
|
for (int i = 0 ; i < input.size(); ++i)
|
||||||
|
{
|
||||||
|
int numeric;
|
||||||
|
|
||||||
|
if ((input[i] - 0 >= 97) && (input[i] - 0 <= 122))
|
||||||
|
{
|
||||||
|
numeric = input[i] - 32;
|
||||||
|
output.push_back((char)numeric);// = 'Q';//(char) numeric;
|
||||||
|
}
|
||||||
|
else output.push_back(input[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < output.size(); ++i)
|
||||||
|
{
|
||||||
|
cout<<output[i]<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct moves
|
||||||
|
{
|
||||||
|
int row;
|
||||||
|
char column;
|
||||||
|
string moveType;
|
||||||
|
|
||||||
|
moves(int linea, int columna, string m)
|
||||||
|
{
|
||||||
|
row = linea;
|
||||||
|
column = columna;
|
||||||
|
moveType = m;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Board
|
||||||
|
{
|
||||||
|
char boardArray [8][8];
|
||||||
|
char turn = 'O';
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Board()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 2; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
boardArray[i][j] = 'X';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 2; i < 6; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
boardArray[i][j] = '_';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 6; i <= 7; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
boardArray[i][j] = 'O';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
moves parse(string input)
|
||||||
|
{
|
||||||
|
|
||||||
|
input = myToUpper(input);
|
||||||
|
|
||||||
|
cout<<input<<endl;
|
||||||
|
|
||||||
|
int temp1;
|
||||||
|
char temp2;
|
||||||
|
string temp3;
|
||||||
|
|
||||||
|
temp2 = input[0];
|
||||||
|
temp1 = input[1] - '0';
|
||||||
|
|
||||||
|
if (input[3] == 'L')
|
||||||
|
{
|
||||||
|
temp3 = "LEFT";
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (input[3] == 'R')
|
||||||
|
{
|
||||||
|
temp3 = "RIGHT";
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp3 = "FWD";
|
||||||
|
}
|
||||||
|
|
||||||
|
moves output(temp1,temp2,temp3);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
char getTurn()
|
||||||
|
{
|
||||||
|
return turn;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isGameOver()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (boardArray[0][i] == 'O')
|
||||||
|
{
|
||||||
|
cout<<"\n\n\nplayer O wins!\n\n\n"<<endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (boardArray[7][i] == 'X')
|
||||||
|
{
|
||||||
|
cout<<"\n\n\nplayer X wins!\n\n\n"<<endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// else return false; this line was making this function return false no matter what, apparently...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void changeTurns()
|
||||||
|
{
|
||||||
|
if (turn == 'O') turn = 'X';
|
||||||
|
else turn = 'O';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void displayBoard()
|
||||||
|
{
|
||||||
|
cout<<"\n\n";
|
||||||
|
cout<<" A B C D E F G H"<<endl;
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
{
|
||||||
|
int mango = 8 - i;
|
||||||
|
cout<<mango<<" ";
|
||||||
|
for (int j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
cout<<boardArray[i][j]<<" ";
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
}
|
||||||
|
cout<<'\n'<<endl;
|
||||||
|
cout<<"turn : "<<turn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int charToIntColumn(char input) //converts column number to int
|
||||||
|
{
|
||||||
|
int kolumn;
|
||||||
|
|
||||||
|
switch (input)
|
||||||
|
{
|
||||||
|
case 'A': kolumn = 0; break;
|
||||||
|
case 'B': kolumn = 1; break;
|
||||||
|
case 'C': kolumn = 2; break;
|
||||||
|
case 'D': kolumn = 3; break;
|
||||||
|
case 'E': kolumn = 4; break;
|
||||||
|
case 'F': kolumn = 5; break;
|
||||||
|
case 'G': kolumn = 6; break;
|
||||||
|
case 'H': kolumn = 7; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return kolumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char intToCharColumn(int input) //converts column number to int
|
||||||
|
{
|
||||||
|
char kolumn;
|
||||||
|
|
||||||
|
switch (input)
|
||||||
|
{
|
||||||
|
case 1: kolumn = 'A'; break;
|
||||||
|
case 2: kolumn = 'B'; break;
|
||||||
|
case 3: kolumn = 'C'; break;
|
||||||
|
case 4: kolumn = 'D'; break;
|
||||||
|
case 5: kolumn = 'E'; break;
|
||||||
|
case 6: kolumn = 'F'; break;
|
||||||
|
case 7: kolumn = 'G'; break;
|
||||||
|
case 8: kolumn = 'H'; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return kolumn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void move(string inputMove)
|
||||||
|
{
|
||||||
|
|
||||||
|
moves jugada = parse(inputMove);
|
||||||
|
|
||||||
|
int row = 8 - (jugada.row);
|
||||||
|
int kolumn = charToIntColumn(jugada.column);
|
||||||
|
int temp = boardArray[row][kolumn];
|
||||||
|
int reflector = 1;
|
||||||
|
|
||||||
|
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
||||||
|
{
|
||||||
|
cout<<"ERROR: index out of bound!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (temp != turn)
|
||||||
|
{
|
||||||
|
cout<<"you can't move that piece at this turn!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (temp == '_')
|
||||||
|
{
|
||||||
|
cout<<"there's no piece in that spot!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (temp == 'X' || temp == 'O')
|
||||||
|
{
|
||||||
|
|
||||||
|
if (temp == 'O')
|
||||||
|
{
|
||||||
|
reflector *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (jugada.moveType == "FWD")
|
||||||
|
{
|
||||||
|
|
||||||
|
if(boardArray[row+reflector][kolumn] != '_')
|
||||||
|
{
|
||||||
|
cout<<"you can't move that piece forward"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boardArray[row][kolumn] = '_';
|
||||||
|
boardArray[row+reflector][kolumn] = temp;
|
||||||
|
|
||||||
|
changeTurns();
|
||||||
|
displayBoard();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (jugada.moveType == "LEFT")
|
||||||
|
{
|
||||||
|
if (kolumn == 0)
|
||||||
|
{
|
||||||
|
cout<<"Destination Spot out of range!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (boardArray[row+reflector][kolumn-1] == temp)
|
||||||
|
{
|
||||||
|
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boardArray[row][kolumn] = '_';
|
||||||
|
boardArray[row+reflector][kolumn-1] = temp;
|
||||||
|
|
||||||
|
changeTurns();
|
||||||
|
displayBoard();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (jugada.moveType == "RIGHT")
|
||||||
|
{
|
||||||
|
if (kolumn == 7)
|
||||||
|
{
|
||||||
|
cout<<"Destination Spot out of range!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (boardArray[row+reflector][kolumn+1] == temp)
|
||||||
|
{
|
||||||
|
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boardArray[row][kolumn] = '_';
|
||||||
|
boardArray[row+reflector][kolumn+1] = temp;
|
||||||
|
|
||||||
|
changeTurns();
|
||||||
|
displayBoard();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout<<"Unrecognized movetype!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout<<"Invalid piece!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void move(moves jugada)
|
||||||
|
{
|
||||||
|
|
||||||
|
int row = 8 - (jugada.row);
|
||||||
|
int kolumn = charToIntColumn(jugada.column);
|
||||||
|
|
||||||
|
if (row > 8 || row < 0 || kolumn > 8 || kolumn < 0)
|
||||||
|
{
|
||||||
|
cout<<"ERROR: index out of bound!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int temp = boardArray[row][kolumn];
|
||||||
|
if (temp != turn)
|
||||||
|
{
|
||||||
|
cout<<"you can't move that piece at this turn!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (temp == '_')
|
||||||
|
{
|
||||||
|
cout<<"there's no piece in that spot!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int reflector = 1;
|
||||||
|
|
||||||
|
if (temp == 'X' || temp == 'O')
|
||||||
|
{
|
||||||
|
|
||||||
|
if (temp == 'O')
|
||||||
|
{
|
||||||
|
reflector *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (jugada.moveType == "FWD")
|
||||||
|
{
|
||||||
|
|
||||||
|
if(boardArray[row+reflector][kolumn] != '_')
|
||||||
|
{
|
||||||
|
cout<<"you can't move that piece forward"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boardArray[row][kolumn] = '_';
|
||||||
|
boardArray[row+reflector][kolumn] = temp;
|
||||||
|
|
||||||
|
changeTurns();
|
||||||
|
displayBoard();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (jugada.moveType == "LEFT")
|
||||||
|
{
|
||||||
|
if (kolumn == 0)
|
||||||
|
{
|
||||||
|
cout<<"Destination Spot out of range!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (boardArray[row+reflector][kolumn-1] == temp)
|
||||||
|
{
|
||||||
|
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boardArray[row][kolumn] = '_';
|
||||||
|
boardArray[row+reflector][kolumn-1] = temp;
|
||||||
|
|
||||||
|
changeTurns();
|
||||||
|
displayBoard();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (jugada.moveType == "RIGHT")
|
||||||
|
{
|
||||||
|
if (kolumn == 7)
|
||||||
|
{
|
||||||
|
cout<<"Destination Spot out of range!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (boardArray[row+reflector][kolumn+1] == temp)
|
||||||
|
{
|
||||||
|
cout<<"you hate your own team or something? you can't do that!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boardArray[row][kolumn] = '_';
|
||||||
|
boardArray[row+reflector][kolumn+1] = temp;
|
||||||
|
|
||||||
|
changeTurns();
|
||||||
|
displayBoard();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout<<"Unrecognized movetype!"<<endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout<<"Invalid piece!"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool isThisMovePossible(int r, int c, string moveType)
|
||||||
|
{
|
||||||
|
char pieceToMove = boardArray[r][c];
|
||||||
|
|
||||||
|
if (pieceToMove != turn) //trying to move invalid piece
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int reflector = 1;
|
||||||
|
|
||||||
|
if (pieceToMove == 'O')
|
||||||
|
{
|
||||||
|
reflector *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (moveType == "FWD")
|
||||||
|
{
|
||||||
|
|
||||||
|
if (boardArray[r+reflector][c] == '_') return true;
|
||||||
|
else return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (moveType == "RIGHT")
|
||||||
|
{
|
||||||
|
if (boardArray[r+reflector][c+1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 <= 7) ) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (moveType == "LEFT")
|
||||||
|
{
|
||||||
|
if (boardArray[r+reflector][c-1] != pieceToMove && (r+reflector >= 0) && (r+reflector <= 7) && (c+1 >= 0) ) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
vector<moves> viewPossibleMoves()
|
||||||
|
{
|
||||||
|
vector<moves> output;
|
||||||
|
|
||||||
|
for (int r = 0; r < 8; ++r)
|
||||||
|
{
|
||||||
|
for (int c = 0; c < 8; ++c)
|
||||||
|
{
|
||||||
|
if (boardArray[r][c] == turn)
|
||||||
|
{
|
||||||
|
if (isThisMovePossible(r,c,"FWD"))
|
||||||
|
{
|
||||||
|
moves temp(8-r,intToCharColumn(c+1),"FWD");
|
||||||
|
output.push_back(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isThisMovePossible(r,c,"LEFT"))
|
||||||
|
{
|
||||||
|
moves temp(8-r,intToCharColumn(c+1),"LEFT");
|
||||||
|
output.push_back(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isThisMovePossible(r,c,"RIGHT"))
|
||||||
|
{
|
||||||
|
moves temp(8-r,intToCharColumn(c+1),"RIGHT");
|
||||||
|
output.push_back(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void easyAI()
|
||||||
|
{
|
||||||
|
|
||||||
|
//1) see all possible movements
|
||||||
|
|
||||||
|
vector<moves> listOfMoves = viewPossibleMoves();
|
||||||
|
|
||||||
|
//2) pick a movement
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
int randomChoice = rand() % (listOfMoves.size()-1) - 0; // choose a move betwen listOfMoves[0] to last element
|
||||||
|
|
||||||
|
//3) execute movement
|
||||||
|
|
||||||
|
int temp = randomChoice;
|
||||||
|
|
||||||
|
move(listOfMoves[randomChoice]);
|
||||||
|
|
||||||
|
//cout<<"\n\nMove executed by AI: "<<listOfMoves[temp].column<<" "<<listOfMoves[temp].row<<" "<<listOfMoves[temp].moveType<<endl; uncomment for debugging purposes
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void displayPossibleMoves(vector<moves> input)
|
||||||
|
{
|
||||||
|
cout<<"\n\nList of possible Moves:"<<endl;
|
||||||
|
for (int i = 0; i < input.size(); ++i)
|
||||||
|
{
|
||||||
|
cout<<"possible move: "<<input[i].row<<" "<<input[i].column<<" "<<input[i].moveType<<endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
cout<<"Welcome to Breakthrough\n"<<endl;
|
||||||
|
cout<<"playing with AI..."<<endl;
|
||||||
|
|
||||||
|
Board b;
|
||||||
|
|
||||||
|
string move;
|
||||||
|
|
||||||
|
bool gameOver = false;
|
||||||
|
|
||||||
|
while (gameOver != true)
|
||||||
|
{
|
||||||
|
gameOver = b.isGameOver();
|
||||||
|
|
||||||
|
while(b.getTurn() == 'O')
|
||||||
|
{
|
||||||
|
b.displayBoard();
|
||||||
|
cout<<"\nEnter move: ";
|
||||||
|
cin>>move;
|
||||||
|
b.move(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
vector<moves> possibleMoves = b.viewPossibleMoves();
|
||||||
|
//displayPossibleMoves(possibleMoves); for debugging purposes - AI
|
||||||
|
|
||||||
|
|
||||||
|
b.easyAI();
|
||||||
|
|
||||||
|
gameOver = b.isGameOver();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
83
server.C
Normal file
83
server.C
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
/* 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 <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
void error(const char *msg)
|
||||||
|
{
|
||||||
|
perror(msg);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int sockfd, newsockfd, portno;
|
||||||
|
socklen_t clilen;
|
||||||
|
char buffer[256];
|
||||||
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
bzero(buffer,256);
|
||||||
|
|
||||||
|
n = read(newsockfd,buffer,255);
|
||||||
|
|
||||||
|
|
||||||
|
if (n < 0) error("ERROR reading from socket");
|
||||||
|
|
||||||
|
|
||||||
|
printf("Here is the message: %s\n",buffer);
|
||||||
|
|
||||||
|
n = write(newsockfd,"I got your message",18);
|
||||||
|
|
||||||
|
if (n < 0) error("ERROR writing to socket");
|
||||||
|
|
||||||
|
close(newsockfd);
|
||||||
|
|
||||||
|
close(sockfd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in a new issue