aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Vance <nathav63@gmail.com>2015-03-11 20:51:33 -0400
committerNathan Vance <nathav63@gmail.com>2015-03-11 20:51:33 -0400
commit0d0206dd0a9a1bb53913a76b0ae6179bb627074e (patch)
tree35948bee50afb58efe124a34d3f609f721773f50
parentStarted work on ai (diff)
download7w-0d0206dd0a9a1bb53913a76b0ae6179bb627074e.tar.gz
7w-0d0206dd0a9a1bb53913a76b0ae6179bb627074e.tar.bz2
7w-0d0206dd0a9a1bb53913a76b0ae6179bb627074e.zip
Implemented basic ai
-rw-r--r--ai.c43
-rw-r--r--ai_weights.c5
-rw-r--r--data.c14
-rw-r--r--main.c46
-rw-r--r--player_turn.c8
-rw-r--r--wonder.c2
6 files changed, 73 insertions, 45 deletions
diff --git a/ai.c b/ai.c
index 74e79f5..5c0901a 100644
--- a/ai.c
+++ b/ai.c
@@ -4,22 +4,55 @@ int data_canafford(int p, int *cost);
int* data_gethand(int p);
int data_numcards(int p);
int data_getera();
+int data_getnext(int p); //next recipient of the hand
+int data_getwonderstages(int p);
+void data_discard(int p, int card);
+void data_build(int p, int card);
+void data_buildwonder(int p, int card);
+void data_addgold(int amnt, int p);
+int wonder_numstages(int player);
int weight_buildcard(int era, int card, int player);
int weight_buildwonder(int player);
int* cards_getproduction(int era, int card);
+int* cards_getcost(int era, int card);
-void ai_turn(int player)
+int* ai_bestcard(int *hand, int player) //return card
{
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++) {
+ for(i = 0; hand[i] != -1 && i < 7; i++) {
temp = weight_buildcard(data_getera(), hand[i], player);
- if(temp > max ) {
+ if(! data_canafford(player, cards_getcost(data_getera(), hand[i])))
+ temp = 0;
+ if(temp > max) {
max = temp;
card = hand[i];
}
}
+ static int ret[2];
+ ret[0] = max;
+ ret[1] = card;
+ return ret;
+}
+
+void ai_turn(int player)
+{
+ int *bestcard;
+ int *hand = data_gethand(player);
+ int wonder = 0;
+ if(data_getwonderstages(player) < wonder_numstages(player))
+ wonder = weight_buildwonder(player);
+ bestcard = ai_bestcard(hand, player);
+ if(bestcard[0] > wonder && bestcard[0] > 0) {
+ data_build(player, bestcard[1]);
+ return;
+ }
+ bestcard = ai_bestcard(hand, data_getnext(player));
+ if(wonder > 0) {
+ data_buildwonder(player, bestcard[1]);
+ return;
+ }
+ data_discard(player, bestcard[1]);
+ data_addgold(3, player);
}
diff --git a/ai_weights.c b/ai_weights.c
index e76e9bf..b56fe70 100644
--- a/ai_weights.c
+++ b/ai_weights.c
@@ -109,15 +109,16 @@ int weight_buildcard(int era, int card, int player)
{
int weight = data_potentialvps(player, era, card);
weight -= data_gettotvps(player);
+ if(cards_gettype(era, card) == MILITARY)
+ weight += weight_military(era, card, 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);
+ return weight;
}
int weight_buildwonder(int player)
diff --git a/data.c b/data.c
index 2763cb8..67e9f48 100644
--- a/data.c
+++ b/data.c
@@ -132,9 +132,19 @@ int data_getdir(int dir, int p)
return p;
}
+int data_getnext(int p)
+{
+ if(era == 0 || era == 2) return data_getwest(p);
+ return data_geteast(p);
+}
+
int* data_gethand(int p)
{
- return hands[(p+turn)%numplayers];
+ int *ret = get_intarray(7);
+ int i;
+ for(i = 0; i < 7; i++)
+ ret[i] = hands[(p+turn)%numplayers][i];
+ return ret;
}
int data_numcards(int p)
@@ -208,7 +218,7 @@ int data_numplayers()
void data_discard(int p, int card)
{
int i;
- int *hand = data_gethand(p);
+ int *hand = hands[(p+turn)%numplayers];
for(i = 0; hand[i] != card; i++);
for(; i < 6; i++) hand[i] = hand[i+1];
hand[6] = -1;
diff --git a/main.c b/main.c
index 86a26fb..ceff85c 100644
--- a/main.c
+++ b/main.c
@@ -8,9 +8,11 @@ void io_clearscreen();
int* getdeck(int era, int numplayers);
void data_init(int num_players);
void player_turn(int player);
+void ai_turn(int player);
void wonder_selected(int player);
void view_refresh(int focus, int cursor, int player);
int data_numplayers();
+void data_endturn();
void halt()
{
@@ -18,38 +20,26 @@ void halt()
exit(0);
}
+static int ais[7] = {0, 0, 1, 0, 0, 0, 0};
+
+main_routine()
+{
+ int player = 0;
+ while(1) {
+ if(ais[player]) ai_turn(player);
+ else player_turn(player);
+ if(++player == data_numplayers()) {
+ data_endturn();
+ player = 0;
+ }
+ }
+}
+
main()
{
io_init();
cards_init();
data_init(3);
-// tester();
-// wonder_selected(0);
- player_turn(0);
+ main_routine();
halt();
}
-
-void tester()
-{
- int focus, cursor, player;
- focus = cursor = player = 0;
- int numplayers = data_numplayers();
- while(1) {
- view_refresh(focus, cursor, player);
- switch(io_getkey()) {
- case DOWN: cursor++;
- break;
- case UP: if(cursor > 0) cursor--;
- break;
- case LEFT: if(focus > 0) focus--;
- break;
- case RIGHT: if(focus < numplayers) focus++;
- break;
- case ENTER: player = (player+1)%numplayers;
- break;
- case 'q': halt();
- break;
- default: break;
- }
- }
-}
diff --git a/player_turn.c b/player_turn.c
index 1759c5f..bd9e805 100644
--- a/player_turn.c
+++ b/player_turn.c
@@ -67,10 +67,8 @@ void player_turn(int player)
case ENTER:
if(player_build(focus, cursor, player)) {
trade_commit(player);
- player++;
- cursor = 0;
- focus = data_numplayers();
clearmessage();
+ return;
}
break;
case '\t': focus = (focus+1)%(data_numplayers()+1);
@@ -79,10 +77,6 @@ void player_turn(int player)
break;
default: break;
}
- if(player >= data_numplayers()) {
- data_endturn();
- player = 0;
- }
if(focus < 0) focus = data_numplayers();
focus = focus%(data_numplayers()+1);
if(focus == data_numplayers()) {
diff --git a/wonder.c b/wonder.c
index e945ea9..252472d 100644
--- a/wonder.c
+++ b/wonder.c
@@ -112,7 +112,7 @@ int print_wondersmall(int x, int y, int player, int selected, int dir)
if(dir == 1) io_printplain(29, y, "East");
if(dir == 2) io_printplain(29, y, "West");
y = io_printtext(x, y, 28, cards_getname(data_getwonder(player), 0));
- sprintf(s, "army: %-2d vp's: %-2d", military_might(player), data_gettotvps(player));
+ sprintf(s, "army: %-2d vps: %-2d gold: %-2d", military_might(player), data_gettotvps(player), data_getgold(player));
y = io_printtext(x, y, 28, s);
io_printborder(x, y, 28);
return y;
bgstack15