This repository was archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.h
159 lines (140 loc) · 4.41 KB
/
player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef PLAYER_H
#define PLAYER_H
#include <stdlib.h>
#include "card.h"
struct card fish[3]; //card array to hold the cards player has before transfer, this allows the cards to be printed to the terminal
struct card fishSrc[3]; //card array to hold the cards that the player giving the cards away has, allows cards to be printed to the terminal
/*
* Structure: player
* -----------------
* Each player holds some number of cards in their hand
* Each player can have at most 7 "books" before winning
*/
struct player {
struct hand* card_list;
char book[7][2]; //changed book array to allow for 10 to be stored more easily
size_t hand_size; //created hand_size and book_size to help functions
size_t book_size;
int player_number; //created player_number to print out correct player numbers to terminal
};
/*
* Instance Variables: user, computer
* ----------------------------------
*
* We only support 2 users: a human and a computer
*/
struct player user;
struct player computer;
/*
* Function: add_card
* -------------------
* Add a new card to the player's hand.
*
* target: the target player
* new_card: pointer to the new card to add
*
* returns: return 0 if no error, non-zero otherwise
*/
int add_card(struct player* target, struct card* new_card);
/*
* Function: remove_card
* ---------------------
* Remove a card from the player's hand.
*
* target: the target player
* old_card: pointer to the old card to remove
*
* returns: return 0 if no error, non-zero otherwise
*/
int remove_card(struct player* target, struct card* old_card);
/*
* Function: check_add_book
* ------------------------
* Check if a player has all 4 cards of the same rank.
* If so, remove those cards from the hand, and add the rank to the book.
* Returns after finding one matching set of 4, so should be called after adding each a new card.
*
* target: pointer to the player to check
*
* Return: a char that indicates the book that was added; return 0 if no book added.
*/
char* check_add_book(struct player* target);
/*
* Function: search
* ----------------
* Search a player's hand for a requested rank.
*
* rank: the rank to search for
* target: the player (and their hand) to search
*
* Return: If the player has a card of that rank, return 1, else return 0
*/
int search(struct player* target, char rank[2]);
/*
* Function: transfer_cards
* ------------------------
* Transfer cards of a given rank from the source player's
* hand to the destination player's hand. Remove transferred
* cards from the source player's hand. Add transferred cards
* to the destination player's hand.
*
* src: a pointer to the source player
* dest: a pointer to the destination player
* rank: the rank to transfer
*
* Return: 0 if no cards found/transferred, <0 if error, otherwise
* return value indicates number of cards transferred
*/
int transfer_cards(struct player* src, struct player* dest, char* rank);
/*
* Function: game_over
* -------------------
* Boolean function to check if a player has 7 books yet
* and the game is over
*
* target: the player to check
*
* Return: 1 if game is over, 0 if game is not over
*/
int game_over(struct player* target);
/*
* Function: reset_player
* ----------------------
*
* Reset player by free'ing any memory of cards remaining in hand,
* and re-initializes the book. Used when playing a new game.
*
* target: player to reset
*
* Return: 0 if no error, and non-zero on error.
*/
int reset_player(struct player* target);
/*
* Function: computer_play
* -----------------------
*
* Select a rank randomly to play this turn. The player must have at least
* one card of the selected rank in their hand.
*
* target: the player's hand to select from
*
* Rank: return a valid selected rank
*/
char* computer_play(struct player* target);
/*
* Function: user_play
* -------------------
*
* Read standard input to get rank user wishes to play. Must perform error
* checking to make sure at least one card in the player's hand is of the
* requested rank. If not, print out "Error - must have at least one card from rank to play"
* and then re-prompt the user.
*
* target: the player's hand to check
*
* returns: return a valid selected rank
*/
char* user_play(struct player* target);
void print_card_list(struct player* target);
void print_book(struct player* target);
#endif