Skip to content

Commit

Permalink
Merge pull request #14 from TriForMine/multi-threading-and-better-int…
Browse files Browse the repository at this point in the history
…erface

Multi threading and better interface
  • Loading branch information
TriForMine authored Dec 25, 2022
2 parents 3ea012a + 58ca5f8 commit 26f8567
Show file tree
Hide file tree
Showing 22 changed files with 669 additions and 193 deletions.
7 changes: 0 additions & 7 deletions .idea/codeStyles/Project.xml

This file was deleted.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ if (DEBUG)
add_compile_definitions(DEBUG_MODE=1)
endif ()

add_executable(battleship src/main.c src/game/board.h src/utils/error.h src/game/tile.h src/game/ship.h src/utils/types.h src/game/ship.c src/game/ship.c src/game/game.h src/game/game.h src/utils/error.c src/game/tile.c src/game/board.c src/game/board.c src/utils/const.h src/main.h src/utils/helpers.h src/utils/helpers.c src/utils/stdprof.c src/utils/hmap.c src/utils/hmap.h src/core/parser.c src/core/parser.h src/game/game.c src/utils/random.c src/utils/random.h src/core/interactive.c src/core/interactive.h src/ai/random_ai.h src/ai/random_ai.c src/ai/common.c src/ai/common.h src/ai/common.h src/ai/hunt_target.c src/ai/hunt_target.h src/ai/hunt_target.h src/ai/hunt_target.h src/ai/hunt_target.h src/utils/colors.h src/ai/probability_targeting.c src/ai/probability_targeting.h)
add_executable(battleship src/main.c src/game/board.h src/utils/error.h src/game/tile.h src/game/ship.h src/utils/types.h src/game/ship.c src/game/ship.c src/game/game.h src/game/game.h src/utils/error.c src/game/tile.c src/game/board.c src/game/board.c src/utils/const.h src/main.h src/utils/helpers.h src/utils/helpers.c src/utils/stdprof.c src/utils/hmap.c src/utils/hmap.h src/core/parser.c src/core/parser.h src/game/game.c src/utils/random.c src/utils/random.h src/core/interactive.c src/core/interactive.h src/ai/random_ai.h src/ai/random_ai.c src/ai/common.c src/ai/common.h src/ai/common.h src/ai/hunt_target.c src/ai/hunt_target.h src/ai/hunt_target.h src/ai/hunt_target.h src/ai/hunt_target.h src/utils/colors.h src/ai/probability_targeting.c src/ai/probability_targeting.h src/core/benchmark.c src/core/benchmark.h)
20 changes: 12 additions & 8 deletions src/ai/hunt_target.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#include "hunt_target.h"

void playHuntTargetAI(Game* game) {
Coordinate coordinate;
Board* board = getOtherPlayerBoard(game);
do {
coordinate = getNextTarget(board);
} while (game->state == PLAYING && getTileState(board, coordinate) == MINE);
Coordinate coordinate = getHuntTargetCoordinate(game);
fire(game, coordinate);
}

Expand All @@ -30,9 +26,7 @@ void getTargetList(Board* board, Coordinate* result, int* size) {
}
}
}

printf("Found %d possible targets\n", k);


*size = k;
}

Expand Down Expand Up @@ -115,3 +109,13 @@ Coordinate getNextTarget(Board* board) {

return result;
}

Coordinate getHuntTargetCoordinate(Game* game) {
Coordinate coordinate;
Board* board = getOtherPlayerBoard(game);
do {
coordinate = getNextTarget(board);
} while (game->state == PLAYING && getTileState(board, coordinate) == MINE);

return coordinate;
}
1 change: 1 addition & 0 deletions src/ai/hunt_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ void getTargetList(Board* board, Coordinate* result, int* size);
void getPriorityTargetList(Board* board, Coordinate* targetList, int targetListSize, int* size);
Coordinate getRandomTarget(Board* board, Coordinate* targetList, int size);
Coordinate getNextTarget(Board* board);
Coordinate getHuntTargetCoordinate(Game* game);

#endif /* BATTLESHIP_HUNT_TARGET_H */
214 changes: 169 additions & 45 deletions src/ai/probability_targeting.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
#include "probability_targeting.h"

#ifdef _WIN32
#include <windows.h>
#else

#include <pthread.h>

#endif

/* Main */
void playProbabilityTargetingAI(Game* game) {
Board* board = getOtherPlayerBoard(game);
unsigned int* probability_density = calculateProbabilityDensity(getOtherPlayerBoard(game));
Coordinate coordinate = getTargetCoordinate(board, probability_density);
void playProbabilityTargetingAI(Game *game) {
Coordinate coordinate = getProbabilityTarget(game);

fire(game, coordinate);
}

/* Helpers */

/* Goes through the board and checks if there are any tiles that are hit but not sunk */
bool isInTargetingMode(Board *board) {
unsigned int x, y;

for (x = 0; x < board->WIDTH; x++) {
for (y = 0; y < board->HEIGHT; y++) {
Coordinate coordinate = createCoordinate(x, y);

#ifdef DEBUG_MODE
if (isTileHit(board, coordinate)) {
return true;
}
}
}
return false;
}

Coordinate getProbabilityTarget(Game *game) {
Board *board = getOtherPlayerBoard(game);
unsigned int *probability_density;
Coordinate coordinate;
#if defined DEBUG_MODE && defined DISABLE_MULTITHREADING
unsigned int x, y, i;
unsigned int MAX_DENSITY = 1;
#endif

probability_density = calculateProbabilityDensity(board);
coordinate = getHighestTargetCoordinate(board, probability_density);

#if defined DEBUG_MODE && defined DISABLE_MULTITHREADING
for (i = 0; i < board->HEIGHT * board->WIDTH; i++) {
if (probability_density[i] > MAX_DENSITY) {
MAX_DENSITY = probability_density[i];
Expand All @@ -27,38 +64,63 @@ void playProbabilityTargetingAI(Game* game) {
#endif

free_prof(probability_density);
fire(game, coordinate);

return coordinate;
}

/* Helpers */
/* Multi-threading */
typedef struct {
Board *board;
bool targeting_mode;
Ship_Type ship_type;
} ProbabilityCalcArgs;

/* Goes through the board and checks if there are any tiles that are hit but not sunk */
bool isInTargetingMode(Board* board) {
#ifdef _WIN32
DWORD WINAPI calcProbability(LPVOID lpParam) {
#else

void *calcProbability(void *args) {
#endif
ProbabilityCalcArgs *calc_args;
Board *board;
Ship_Type ship_type;
unsigned int x, y;
unsigned int *probability_density;
#ifdef _WIN32
calc_args = (ProbabilityCalcArgs *) lpParam;
#else
calc_args = (ProbabilityCalcArgs *) args;
#endif
board = calc_args->board;
ship_type = calc_args->ship_type;
probability_density = (unsigned int *) malloc(
(unsigned long) board->WIDTH * (unsigned long) board->HEIGHT * sizeof(unsigned int));
memset(probability_density, 0, (unsigned long) board->WIDTH * (unsigned long) board->HEIGHT * sizeof(unsigned int));

for (x = 0; x < board->WIDTH; x++) {
for (y = 0; y < board->HEIGHT; y++) {
for (y = 0; y < board->HEIGHT; ++y) {
for (x = 0; x < board->WIDTH; ++x) {
Coordinate coordinate = createCoordinate(x, y);

if (isTileHit(board, coordinate)) {
return true;
if (isTileUnknown(board, coordinate)) {
probability_density[y * board->WIDTH + x] +=
getShipProbability(calc_args->board, coordinate, ship_type, calc_args->targeting_mode);
}
}
}
return false;

return probability_density;
}

/* Probability Calculation */

/* Check if the placement is valid, and count hit if in targeting mode */
bool checkPlacement(Board* board, unsigned int ship_length, bool targetMode, long fixed_coordinate,
long variable_coordinate, char* hit_count, bool is_x_fixed) {
bool checkPlacement(Board *board, unsigned int ship_length, bool targetMode, long fixed_coordinate,
long variable_coordinate, char *hit_count, bool is_x_fixed) {
long i;
Coordinate current_coordinate;
bool is_valid = true;
(*hit_count) = 0;

for (i = 0; i < (long)ship_length; ++i) {
for (i = 0; i < (long) ship_length; ++i) {
if (is_x_fixed) {
current_coordinate = createCoordinate(fixed_coordinate, variable_coordinate + i);
} else {
Expand Down Expand Up @@ -91,35 +153,34 @@ bool checkPlacement(Board* board, unsigned int ship_length, bool targetMode, lon
}

/* Calculates the probability density for a given ship at a given coordinate */
unsigned int getShipProbability(Board* board, Coordinate coordinate, Ship_Type ship_type) {
unsigned int getShipProbability(Board *board, Coordinate coordinate, Ship_Type ship_type, bool isTargetingMode) {
unsigned int probability = 0;
long x, y;
unsigned int ship_length = getShipLength(ship_type);
bool targetMode = isInTargetingMode(board);

/* Check how many times it can be placed horizontally */
for (x = (long)coordinate.x - (long)ship_length; x <= coordinate.x; ++x) {
for (x = (long) coordinate.x - (long) ship_length; x <= coordinate.x; ++x) {
char hit_count = 0;
bool is_valid = checkPlacement(board, ship_length, targetMode, coordinate.y, x, &hit_count, false);
bool is_valid = checkPlacement(board, ship_length, isTargetingMode, coordinate.y, x, &hit_count, false);

if (is_valid) {
if (targetMode && hit_count > 0) {
if (isTargetingMode && hit_count > 0) {
probability += hit_count;
} else if (!targetMode && hit_count == 0) {
} else if (!isTargetingMode && hit_count == 0) {
probability++;
}
}
}

/* Check how many times it can be placed vertically */
for (y = (long)coordinate.y - (long)ship_length; y <= coordinate.y; ++y) {
for (y = (long) coordinate.y - (long) ship_length; y <= coordinate.y; ++y) {
char hit_count = 0;
bool is_valid = checkPlacement(board, ship_length, targetMode, coordinate.x, y, &hit_count, true);
bool is_valid = checkPlacement(board, ship_length, isTargetingMode, coordinate.x, y, &hit_count, true);

if (is_valid) {
if (targetMode && hit_count > 0) {
if (isTargetingMode && hit_count > 0) {
probability += hit_count;
} else if (!targetMode && hit_count == 0) {
} else if (!isTargetingMode && hit_count == 0) {
probability++;
}
}
Expand All @@ -129,42 +190,105 @@ unsigned int getShipProbability(Board* board, Coordinate coordinate, Ship_Type s
}

/* Calculates the probability density for all ships */
unsigned int* calculateProbabilityDensity(Board* board) {
unsigned int *calculateProbabilityDensity(Board *board) {
#ifndef DISABLE_MULTITHREADING
int i;
unsigned int j;
#ifdef _WIN32
HANDLE threads[4];
#else
pthread_t threads[4];
#endif
ProbabilityCalcArgs args[4];
#else
unsigned int x, y;
unsigned int* probability_density =
calloc_prof((unsigned long)board->WIDTH * (unsigned long)board->HEIGHT, sizeof(unsigned int));
#endif
bool ships_alive[4];
bool isTargetingMode = isInTargetingMode(board);
unsigned int *probability_density = (unsigned int *) malloc(
(unsigned long) board->WIDTH * (unsigned long) board->HEIGHT * sizeof(unsigned int));
memset(probability_density, 0, (unsigned long) board->WIDTH * (unsigned long) board->HEIGHT * sizeof(unsigned int));

bool carrier_alive = isShipAlive(board, CARRIER);
bool battleship_alive = isShipAlive(board, BATTLESHIP);
bool cruiser_alive = isShipAlive(board, CRUISER);
bool destroyer_alive = isShipAlive(board, DESTROYER);
ships_alive[3] = isShipAlive(board, CARRIER);
ships_alive[2] = isShipAlive(board, BATTLESHIP);
ships_alive[1] = isShipAlive(board, CRUISER);
ships_alive[0] = isShipAlive(board, DESTROYER);

#ifndef DISABLE_MULTITHREADING
/* Set up arguments for each thread */
for (i = 0; i < 4; i++) {
if (ships_alive[i] == true) {
args[i].board = board;
args[i].ship_type = (Ship_Type) (i + 2);
args[i].targeting_mode = isTargetingMode;
}
}

/* Create threads for each ship type */
for (i = 0; i < 4; i++) {
if (ships_alive[i]) {
#ifdef _WIN32
threads[i] = CreateThread(NULL, 0, calcProbability, &args[i], 0, NULL);
#else
pthread_create(&threads[i], NULL, calcProbability, &args[i]);
#endif
}
}

/* Wait for all threads to finish and sum the results */
for (i = 0; i < 4; i++) {
if (ships_alive[i]) {
void *thread_result;
unsigned int *thread_probability_density;
#ifdef _WIN32
WaitForSingleObject(threads[i], INFINITE);
GetExitCodeThread(threads[i], (LPDWORD)&thread_result);
#else
pthread_join(threads[i], &thread_result);
#endif

thread_probability_density = (unsigned int *) thread_result;
for (j = 0; j < board->WIDTH * board->HEIGHT; j++) {
probability_density[j] += thread_probability_density[j];
}

free(thread_probability_density);
}
}

#else
for (x = 0; x < board->WIDTH; ++x) {
for (y = 0; y < board->HEIGHT; ++y) {
Coordinate coordinate = createCoordinate(x, y);
if (isTileUnknown(board, coordinate)) {
if (carrier_alive) {
probability_density[y * board->WIDTH + x] += getShipProbability(board, coordinate, CARRIER);
if (ships_alive[3]) {
probability_density[y * board->WIDTH + x] +=
getShipProbability(board, coordinate, CARRIER, isTargetingMode);
}
if (battleship_alive) {
probability_density[y * board->WIDTH + x] += getShipProbability(board, coordinate, BATTLESHIP);
if (ships_alive[2]) {
probability_density[y * board->WIDTH + x] +=
getShipProbability(board, coordinate, BATTLESHIP, isTargetingMode);
}
if (cruiser_alive) {
probability_density[y * board->WIDTH + x] += getShipProbability(board, coordinate, CRUISER);
if (ships_alive[1]) {
probability_density[y * board->WIDTH + x] +=
getShipProbability(board, coordinate, CRUISER, isTargetingMode);
}
if (destroyer_alive) {
probability_density[y * board->WIDTH + x] += getShipProbability(board, coordinate, DESTROYER);
if (ships_alive[0]) {
probability_density[y * board->WIDTH + x] +=
getShipProbability(board, coordinate, DESTROYER, isTargetingMode);
}
}
}
}
#endif

return probability_density;
}

/* Targeting */

/* Gets the coordinate with the highest probability density */
Coordinate getTargetCoordinate(Board* board, const unsigned int* probability_density) {
Coordinate getHighestTargetCoordinate(Board *board, const unsigned int *probability_density) {
unsigned int x, y;
unsigned int max = 0;
Coordinate coordinate = createCoordinate(0, 0);
Expand All @@ -178,4 +302,4 @@ Coordinate getTargetCoordinate(Board* board, const unsigned int* probability_den
}
}
return coordinate;
}
}
Loading

0 comments on commit 26f8567

Please sign in to comment.