-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.c
209 lines (156 loc) · 5.98 KB
/
Game.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*-----------------------------------------------------------------------------+
File Name: Game.c
+------------------------------------------------------------------------------+
+------------------------------------------------------------------------------+
Programming III COP4338
Author: Daniel Gonzalez P#4926400
assignment 3: Deck of Cards
Date: 10/20/2016
program description
Input: Accept input via the command-line arguments. Validate command-line
input. Input will be the number of cards/hand and the number of
hands(players), in that order.
Output: Display of a deck of cards, cards of each hands after dealing
and a winner hand..
+-------------------------------------------------------------------------+
| I Daniel Gonzalez #4926400 hereby certify that this collective work is |
| my own and none of it is the work of any other person or entity. |
+-------------------------------------------------------------------------+
how to compile and execute:
1.Open the terminal
Go to the program folder
Run the following command "make"
2.Open the terminal
Go to the program folder
Run the following command
"gcc -Wall -w -lm Main.c Game.c Deck.c Card.c Player.c -o poker"
Program execution:
From the terminal enter “./poker [#Cards][#Players]"
both arguments are required
+-----------------------------------------------------------------------------*/
#include "Main.h"
/* Include structures, prototypes and libraries for the program */
/*-----------------------------------------------------------------------------+
* dealPlayers function assigns to each player the specified amount of
* cards from the deck
* @param deck deck to get the cards from
* @param players players to deal the cards
* @param gamePlayers amount of players
* @param cardsToDeal amount of cards to deal to the players
*/
void dealPlayers( Deck *deck,
Player players[],
int gamePlayers,
int cardsToDeal){
int index;
int cardsIndex;
for(index = 0; index < gamePlayers; index++){
createPlayer(&players[index], cardsToDeal, deck->cards);
deck->cards += cardsToDeal;
deck->cardsAmt -= cardsToDeal;
}
}
/*-----------------------------------------------------------------------------+
* [finds a winner or more and displays them]
* @param players [list of players]
* @param gamePlayers [amount of payers]
*/
void findWinner(Player players[], int gamePlayers){
int index;
int winner = 0;
for (index = 1; index < gamePlayers; index++){
winner = (players[index].rank > players[winner].rank)? index : winner;
}
printf(
"\n\n%s\n|%40s%d%s%40s|\n%s\n",
"+--------------------------------------------"
"--------------------------------------------+",
"Player ",
(winner + OFFSET),
" WINNER",
"",
"+--------------------------------------------"
"--------------------------------------------+"
);
displayPlayer(&players[winner]);
for (index = 0; index < gamePlayers; index++){
if (index != winner){
if (players[index].rank == players[winner].rank ){
printf(
"\n\n%s\n|%40s%d%s%40s|\n%s\n",
"+--------------------------------------------"
"--------------------------------------------+",
"Player ",
(winner + OFFSET),
" WINNER",
"",
"+--------------------------------------------"
"--------------------------------------------+"
);
displayPlayer(&players[winner]);
}
}
}
}
/*-----------------------------------------------------------------------------+
* [gives a rank to each player]
* @param players [players to be ranked]
* @param gamePlayers [amount of players]
*/
void rankPlayers( Player players[], int gamePlayers ){
int index;
for ( index = 0; index < gamePlayers; ++index ){
rankPlayer( &players[index] );
}
}
/*-----------------------------------------------------------------------------+
* validateGame ensures that all required parameters
* are present and valid for the program to run
* @param argc argument counter
* @param argv program arguments
* @param cardsToDeal amount of cards to be assign for dealing
* @param gamePlayers amount of players to be assign for the game
* @return TRUE if it is a valid game and FALSE if not valid
*/
Boolean validateGame(
int argc, char **argv,
int *cardsToDeal, int *gamePlayers){
int total_cards = 0;
if ( argc == GAME_ARG) {
*cardsToDeal = atoi(argv[ARG_1]);
*gamePlayers = atoi(argv[ARG_2]);
total_cards = (*cardsToDeal) * (*gamePlayers);
if (*gamePlayers <= MAX_PLAYERS &&
*cardsToDeal <= MAX_CARDS &&
total_cards <= DECK_CARDS){
return TRUE;
}else{
printf(
"%s\n\n%s\n%s%d%s%d%s\n%s\n\n%s\n\n",
"+--------------------------------------------"
"--------------------------------------------+",
"The amount of players or card per players are not valid",
"This game requires a max of ",
MAX_PLAYERS,
" players, ",
MAX_CARDS,
" cards per player,",
"and the sum of all the cards per players can't be more than 52",
"+--------------------------------------------"
"--------------------------------------------+"
);
return FALSE;
}
}else{
printf("%s\n\n%s\n%s\n\n%s\n\n%s\n\n",
"+--------------------------------------------"
"--------------------------------------------+",
"This program requires two arguments for the amount of,",
"cards per player and the amount of players. ",
"./poker [#Cards][#Players]",
"+--------------------------------------------"
"--------------------------------------------+"
);
return FALSE;
}
}