Skip to content

Commit

Permalink
The AI decideAction function has been written but it is currently unt…
Browse files Browse the repository at this point in the history
…ested, as runGame has not beed modified to make use of the new program. To make this function work the listLength prototype has been moved into Game.h to be used by the AI, a getDeck function was added to give the playerHand list to the AI function and the README has been changed to include the ai.c file in the compilation line.
  • Loading branch information
AdamD2 committed Jun 23, 2016
1 parent 01329bb commit beeafb1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Game.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ int getPairs (Game g) {
return g->playerArray[g->whoseTurn - 1].pairs;
}

list getDeck (Game g) {
return g->playerArray[g->whoseTurn - 1].playerHand;
}

int checkOpponent (Game g, action a) {
link curr = g->playerArray[a.player-1].playerHand->head;
int result = FALSE;
Expand Down
4 changes: 4 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void printHand (Game g);
int getWhoseTurn (Game g);
int getRoundNumber (Game g);
int getPairs (Game g);
list getDeck (Game g);

// Search through the current player's deck and search for pairs,
// removing them and incrementing the pair count
Expand All @@ -97,3 +98,6 @@ void takeFromPlayer (Game g, action a);

// Calculate and print the winner of a game
void calculateWinner (Game g);

// Return the length of a given list
int listLength (list l);
4 changes: 2 additions & 2 deletions README
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
An implementation of the go fish card game in C.

Compile using:
gcc -Wall -Werror -O -o goFish Game.c runGame.c
It is recommended to compile this program using the following line:
gcc -Wall -Werror -O -o goFish Game.c runGame.c ai.c

Created by Adam Douglas and James Wardman.
15 changes: 14 additions & 1 deletion ai.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ action decideAction (Game g);
action decideAction (Game g) {
action nextAction;
int me = getWhoseTurn (g);
list playerHand = getDeck (g);
int len = listLength (playerHand);


// Choose a random card from the player's deck to ask for
int cardNumber = rand () % len;
link curr = playerHand->head;
for (int i = 0; i < cardNumber; i++) {
curr = curr->next;
}
nextAction.card = curr->value;

// Choose a random player that isn't the current player to ask
do {
nextAction.player = rand() % NUM_PLAYERS + 1;
} while (nextAction.player != me);

return nextAction;
}
Binary file modified goFish
Binary file not shown.

0 comments on commit beeafb1

Please sign in to comment.