Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
thanosDelatolas committed May 8, 2020
1 parent fe8c27e commit 6b5f98b
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 55 deletions.
128 changes: 92 additions & 36 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "mlist.h"

int max(int a, int b){
if (a > b)
return a;
return b;
}
int min(int a, int b){
if(a < b)
return a;
return b;
}

/**********************************************************/
Position gamePosition; // Position we are going to use
Expand All @@ -20,27 +32,7 @@ int mySocket; // our socket
char * agentName = "DEVOU_AGENT!";

char * ip = "127.0.0.1"; // default ip (local machine)
/**********************************************************/
/**
* PlayersInfo is an 1-D array with two elements, info's of black player and infos's of white player
* you can see the struct in client.h
*/
PlayerInfo** playersInfo;

/* Based on the infos of two players evaluate the state
* Black player wants positive
* White player wants negative
*/
int evaluate_function(){
int black_score = playersInfo[0] -> food + playersInfo[0] -> queens + playersInfo[0] -> soldiers + playersInfo[0] -> unprotected_soldiers +
playersInfo[0] -> in_danger_soldiers + playersInfo[0] -> threatening_soldiers;

int white_score = playersInfo[1] -> food + playersInfo[1] -> queens + playersInfo[1] -> soldiers + playersInfo[1] -> unprotected_soldiers +
playersInfo[1] -> in_danger_soldiers + playersInfo[1] -> threatening_soldiers;

return black_score - white_score;

}

int main( int argc, char ** argv ){
int c;
Expand Down Expand Up @@ -77,22 +69,6 @@ int main( int argc, char ** argv ){
char msg;


/*
* create two players infromations
* playersInfo[0] is black player
* playersInfo[1] is white player
*/
playersInfo=(PlayerInfo**)malloc(2*sizeof(PlayerInfo*));

for (int l = 0; l < 2; l++){
playersInfo[l] -> food = 0;
playersInfo[l] -> queens = 0;
playersInfo[l] -> soldiers = 12;
playersInfo[l] -> unprotected_soldiers = 0;
playersInfo[l] -> in_danger_soldiers = 0;
playersInfo[l] -> threatening_soldiers = 0;
}

while( 1 )
{

Expand Down Expand Up @@ -158,6 +134,86 @@ int main( int argc, char ** argv ){
return 0;
}

Move* make_move(Position* pos, int depth){


Move* agent_move = malloc(sizeof(Move));

Position* temp_pos = malloc(sizeof(Position));
memcpy(temp_pos, pos, sizeof(Position));

iterativeDeepening(temp_pos, agent_move);

free(temp_pos);

return agent_move;
}

/****white player wants to maximaze and black player wants to minimaze
*
* gain 100 points for each ant that you own
* gain points for the depth (in board ) of each ant
*
* lose 100 points for each ant that the opponent own
* lose points for the depth (in board ) of each opponent's ant
*
* pos -> score*90 because score has queens and food
*/
int evaluate_function(Position* pos){
int i,j, heuristic = 0;

for (i = 0; i < BOARD_ROWS; i++){
for ( j = 0; j < BOARD_COLUMNS; j++){
if (pos -> board[i][j] == myColor){
//for each cell that i own, gain 100 points!
heuristic += 100;
//add points for the depth of the cell
if(myColor == WHITE)
heuristic += i;
else
heuristic += (BOARD_ROWS-i-1);

}
else if (pos -> board[i][j] == getOtherSide(myColor)){
//for each cell that i own, gain 100 points!
heuristic -= 100;
//add points for the depth of the cell
if(myColor == BLACK)
heuristic -= i;
else
heuristic -= (BOARD_ROWS-i-1);
}

}
}
//scores of each player *90 beacause score is important!
return heuristic + pos -> score[myColor]*90 - pos -> score[getOtherSide(myColor)]*90;

}
/*
*It is an extension of the evaluation function to defer evaluation until the position is stable enough to be evaluated
* enough to be evaluated means no jump available
*/
int quiescence_search(Position* pos){
int i, j;
for( i = 0; i < BOARD_ROWS; i++ ){
for( j = 0; j < BOARD_COLUMNS; j++ ){
if( pos->board[ i ][ j ] == pos->turn ){
if( canJump( i, j, pos->turn, pos )){
return TRUE;
}
}
}
}
return FALSE;


}

int iterativeDeepening(Position* pos, Move* agent_move){
return -1;
}

/**********************************************************/
int i, j, k;
int jumpPossible;
Expand Down
49 changes: 31 additions & 18 deletions client.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
/** Implemented technics:
*
* minimax
* a-b pruning
* quiescence search
* Transposition table
* MTD(F)
* Iterative Deepening
*/

//player that does random moves!
void random_move();

//player based on AI theory
void ai_move();

/* used from evaluate function...*/
typedef struct{
int food;
int queens;
int soldiers;
int unprotected_soldiers;
/****white player wants to maximaze and black player wants to minimaze
*
* gain 100 points for each ant that you own
* gain points for the depth (in board ) of each ant
*
* lose 100 points for each ant that the opponent own
* lose points for the depth (in board ) of each opponent's ant
*
* pos -> score*90 because score has queens and food
*/
int evaluate_function(Position* pos);

//soldiers that are threatened...
int in_danger_soldiers;
/*
*It is an extension of the evaluation function to defer evaluation until the position is stable enough to be evaluated
* enough to be evaluated means no jump available
*/
int quiescence_search(Position* pos)

//how many soldiers this playes threatens
int threatening_soldiers;
} PlayerInfo;
/**
* Weigths on each parameter of PlayerInfo
/*** Main function of the agent
*
*/
//evaluates a state based on PlayerInfo
int evaluate_function();
Move* make_move(Position* pos, int depth);

int iterativeDeepening(Position* pos, Move* agent_move);
4 changes: 3 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ all: client server guiServer
guiServer: board comm gameServer guiServer.h global.h
gcc -o guiServer guiServer.c board.o comm.o gameServer.o -O3 `pkg-config --libs --cflags gtk+-2.0`

client: client.c board comm global.h
client: client.c board comm global.h client.h
gcc -o client client.c board.o comm.o -O3

server: server.c board comm gameServer global.h
Expand All @@ -18,5 +18,7 @@ board: board.c board.h move.h global.h
gameServer: gameServer.c gameServer.h board.h move.h global.h
gcc -c gameServer.c -O3

list: mlist.c mlist.h move.h
gcc -c mlist.c -O3
clean:
rm -f *.o client server
59 changes: 59 additions & 0 deletions mlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "mlist.h"
#include <stdio.h>
#include <stdlib.h>
/*
* This is a list with moves that is used in the agent (client.c)
*/

//creates an empty list
void initList(mlist* list_pointers){
list_pointers -> head = NULL;
list_pointers -> tail = NULL;
}

//push a Move to the list
void push(mlist* list_pointers, Move* data){
node* n =(node*) malloc(sizeof(node));
n->data = data;
n->next = list_pointers -> head;

if(list_pointers -> head == NULL)
list_pointers -> tail = n;
list_pointers -> head = n;
}

//get data of the first Moce
Move* peek(mlist* list_pointers){
if(list_pointers == NULL || list_pointers->head == NULL)
return NULL;
return list_pointers -> head -> data;
}

//Pop a move
Move* pop(mlist* list_pointers){
if(list_pointers == NULL || list_pointers -> head == NULL)
return NULL;
Move* move_data;

node* n = list_pointers -> head;
list_pointers -> head = list_pointers -> head -> next;
//empty list
if(list_pointers -> head == NULL)
list_pointers->tail = NULL;
move_data = n->data;

free(n);

return move_data;
}


void freeList(mlist* list_pointers){
while(list_pointers -> head != NULL)
free(pop(list_pointers));
free(list_pointers);
}
void emptyList(mlist* list_pointers){
while(list_pointers -> head != NULL)
free(pop(list_pointers));
}
26 changes: 26 additions & 0 deletions mlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "move.h"

/*
* This is a list with moves that is used in the agent (client.c)
*/

typedef struct node{
Move* data;
struct node *next;
} node;


typedef struct {
node *head;
node *tail;
} mlist;


void initList(mlist* list_pointers);
void push(mlist* list_pointers, Move* data);
Move* peek(mlist* list_pointers);
Move* pop(mlist* list_pointers);

void freeList(mlist*);

void emptyList(mlist*);

0 comments on commit 6b5f98b

Please sign in to comment.