From f0bbfee11a0418064a142101a952c3043dde7351 Mon Sep 17 00:00:00 2001 From: Nathan Vance Date: Sat, 28 Feb 2015 23:21:31 -0500 Subject: Started work on ai --- ai.c | 25 ++++++++++ ai_weights.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ ai_weights_special.c | 40 ++++++++++++++++ data.c | 15 +++++- science.c | 14 ++++-- 5 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 ai.c create mode 100644 ai_weights.c create mode 100644 ai_weights_special.c diff --git a/ai.c b/ai.c new file mode 100644 index 0000000..74e79f5 --- /dev/null +++ b/ai.c @@ -0,0 +1,25 @@ +#include "7w.h" + +int data_canafford(int p, int *cost); +int* data_gethand(int p); +int data_numcards(int p); +int data_getera(); +int weight_buildcard(int era, int card, int player); +int weight_buildwonder(int player); +int* cards_getproduction(int era, int card); + +void ai_turn(int player) +{ + int i, temp; + int *hand = data_gethand(player); + int numcards = data_numpcards(player); + int max = 0; + int card = 0; + for(i = 0; i < numcards; i++) { + temp = weight_buildcard(data_getera(), hand[i], player); + if(temp > max ) { + max = temp; + card = hand[i]; + } + } +} diff --git a/ai_weights.c b/ai_weights.c new file mode 100644 index 0000000..e76e9bf --- /dev/null +++ b/ai_weights.c @@ -0,0 +1,126 @@ +#include "7w.h" + +int data_getdir(int dir, int p); //0 is east, 1 is west, 2 returns player +int* cards_getproduction(int era, int card); +int* cards_getcost(int era, int card); +int data_canafford(int p, int *cost); +int* data_getdefinites(int p); +int** data_getindefinites(int p); +int* data_gettradables(int p); +int data_productiontype(int e, int card); +int data_getwonderstages(int p); +int* get_special(int era, int card, int player); +int get_trade(int player, int type, int direction); +int cards_gettype(int era, int card); +int* get_science(int player); +int* get_special(int era, int card, int player); +int data_potentialvps(int p, int era, int card); +int military_might(int player); + +int weight_science(int era, int card, int player) +{ + int weight = 0; + int *science = get_science(player); + int min = 100; + int max = 0; + int i, j; + for(i = 0; i < 3; i++) { + if(science[i] < min) + min = science[i]; + if(science[i] > max) + max = science[i]; + } + if(era > 0 && max == 0) return 0; + int *prod = cards_getproduction(era, card); + for(i = COMPASS; prod[i] == 0; i++); + i -= COMPASS; + if(science[i] == min) weight = 5; + int tots[3]; + for(i = 0; i < 3; i++) tots[i] = 0; + for(i = 0; i < 3; i++) { + science = get_science(data_getdir(i, player)); + for(j = 0; j < 3; j++) { + tots[i] != science[j]; + } + } + if(tots[2] < tots[0]-1 || tots[2] < tots[1]-1) weight -= 3; + return weight; +} + +int weight_military(int era, int card, int player) +{ + int weight = 0; + int military = cards_getproduction(era, card)[SHIELD]; + int strength[3]; + int i; + for(i = 0; i < 3; i++) + strength[i] = military_might(data_getdir(i, player)); + for(i = 0; i < 2; i++) { + if(strength[i] >= strength[2] && strength[i] < strength[2] + military) + weight += (era+2)*2; + if(strength[i] == strength[2] + military) + weight += (era+1)*2; + if(strength[i] < strength[2]) + weight += 5 / (strength[2]-strength[i]); + } + return weight; +} + +int weight_resource(int era, int card, int player) +{ + int weight = 0; + int *prod = cards_getproduction(era, card); + int *trade = data_gettradables(player); + int i, j; + for(i = 0; i < GOLD; i++) { + if(prod[i] && !trade[i]) weight += 5; + if(prod[i] && (i == STONE || i == ORE) && trade[i] < 2) weight += 2; + } + for(j = 0; j < 2; j++) { + trade = data_gettradables(data_getdir(j, player)); + for(i = 0; i < GOLD; i++) { + if(prod[i] && !trade[i]) weight++; + if(prod[i] && trade[i] && get_trade(player, i, j) == 1) weight--; + } + } + return weight; +} + +int weight_tradingpost(int dir, int player); +int weight_marketplace(int player); +int weight_gold(int gold, int player); + +int weight_commercial(int era, int card, int player) +{ + if(era == 0) { + if(card == 17) return weight_gold(4, player); + if(card == 18) return weight_tradingpost(0, player); + if(card == 19) return weight_tradingpost(1, player); + if(card == 20) return weight_marketplace(player); + } + if(era == 1) { + if(card == 11 || card == 12) return weight_resource(era, card, player); + if(card == 13 || card == 14) return weight_gold(get_special(era, card, player)[1], player); + } + return 0; +} + +int weight_buildcard(int era, int card, int player) +{ + int weight = data_potentialvps(player, era, card); + weight -= data_gettotvps(player); + if(era == 2) return weight; + if(cards_gettype(era, card) == SCIENTIFIC) + weight += weight_science(era, card, player); + if(cards_gettype(era, card) == MILITARY) + weight += weight_military(era, card, player); + if(cards_gettype(era, card) == RESOURCE || cards_gettype(era, card) == INDUSTRY) + weight += weight_resource(era, card, player); + if(cards_gettype(era, card) == COMMERCIAL) + weight += weight_commercial(era, card, player); +} + +int weight_buildwonder(int player) +{ + return (data_getwonderstages(player)+2) * 2; +} diff --git a/ai_weights_special.c b/ai_weights_special.c new file mode 100644 index 0000000..32c13dd --- /dev/null +++ b/ai_weights_special.c @@ -0,0 +1,40 @@ +#include "7w.h" + +int* data_gettradables(int p); +int data_getdir(int dir, int p); +int data_getgold(int p); + +//dir 0 is east, 1 is west +int weight_tradingpost(int dir, int player) +{ + int weight = 0; + int *trade = data_gettradables(data_getdir(dir, player)); + int *prod = data_gettradables(player); + int i; + for(i = 0; i < CLOTH; i++) { + if(trade[i] && !prod[i]) weight++; + } + return weight; +} + +int weight_marketplace(int player) +{ + int weight = 0; + int i, j; + int *prod = data_gettradables(player); + int *trade; + for(i = 0; i < 2; i++) { + trade = data_gettradables(data_getdir(i, player)); + for(j = CLOTH; j <= PAPER; j++) { + if(trade[j] && !prod[j]) weight++; + } + } + return weight; +} + +int weight_gold(int gold, int player) +{ + return gold - data_getgold(player); +} + + diff --git a/data.c b/data.c index 75df70b..2763cb8 100644 --- a/data.c +++ b/data.c @@ -128,7 +128,8 @@ int data_getwest(int p) int data_getdir(int dir, int p) { if(dir == 0) return data_geteast(p); - return data_getwest(p); + if(dir == 1) return data_getwest(p); + return p; } int* data_gethand(int p) @@ -411,3 +412,15 @@ int data_gettotvps(int p) tot += science(p); return tot; } + +int data_potentialvps(int p, int era, int card) +{ + int i; + for(i = 0; player[p][era][i] != -1; i++); //get free spot in array + player[p][era][i] = card; //build card + player[i][3][3] += cards_getproduction(era, card)[GOLD]; + int vps = data_gettotvps(p); + player[p][era][i] = -1; + player[i][3][3] -= cards_getproduction(era, card)[GOLD]; + return vps; +} diff --git a/science.c b/science.c index 89af807..2d0f323 100644 --- a/science.c +++ b/science.c @@ -4,13 +4,13 @@ int* data_getbuilt(int p); int* cards_getproduction(int era, int card); int cards_gettype(int era, int card); int data_hasbuilt(int p, int era, int card); +int* get_intarray(int size); -int calc_science(int player, int guildval) +int* get_science(int player) { - int science[3]; + int *science = get_intarray(3); int i, j; for(i = 0; i < 3; i++) science[i] = 0; - if(guildval != -1) science[guildval]++; int *built = data_getbuilt(player); int *prod; for(i = 0; built[i] != -1; i += 2) { @@ -21,6 +21,14 @@ int calc_science(int player, int guildval) } } } + return science; +} + +int calc_science(int player, int guildval) +{ + int i; + int *science = get_science(player); + if(guildval != -1) science[guildval]++; int min = science[0]; for(i = 0; i < 3; i++) if(science[i] < min) min = science[i]; -- cgit