-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy_battle_game.c
More file actions
269 lines (229 loc) · 7.97 KB
/
Copy pathstrategy_battle_game.c
File metadata and controls
269 lines (229 loc) · 7.97 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mystructs.h"
#include "myfunctions.h"
int main(){
Player* player1 = initializePlayer("player 1");
Player* player2 = initializePlayer("player 2");
Player* winner = 0;
Unit* army_of_player1 = (Unit *)calloc(UNIQUEUNITS, sizeof( Unit));
player1->army = army_of_player1;
Unit* army_of_player2 = (Unit *)calloc(UNIQUEUNITS, sizeof( Unit));
player2->army = army_of_player2;
FILE *fp;
fp = fopen("units.txt", "r");
if (fp == NULL) {
printf("Could not open units.txt\n");
exit(0);
}
initializePlayersArmiesFromFile(player1, player2, fp);
printf("The game is about to start!!! Get Ready:\n");
printRules();
while(1){
printf("******************* New Round **********************\n");
playRound(player1);
printf("################## Next player #####################\n");
playRound(player2);
winner = checkForWinner(player1, player2);
if(winner != NULL){
printf("Congatulations %s, you won here are your stats\n",winner->player_name);
printPlayerStats(winner);
break;
}
printf("******************* End of Round **********************\n");
}
printf("Game is over, please play again whenever you wish\n");
fclose(fp);
exit(0);
}
//initialize player struct
Player* initializePlayer(char playerName[20]) {
Player* player = (Player *)calloc(1, sizeof(Player));
strcpy(player->player_name, playerName);
player-> army_size = 0;
player-> money_balance = STARTBALANCE;
return player;
}
//initialize all players armies while readinf from file unit's stats
void initializePlayersArmiesFromFile(Player *player1, Player *player2, FILE *fp){
int code=0;
char line[LINELENGTH];
char name[20];
int att, cost, sup;
Unit *unit;
while (fgets(line,LINELENGTH, fp) != NULL) {
//read a line from file
sscanf(line, "%s%d%d%d", name, &att, &cost, &sup);
//initialize units stats using the data from the line
//do the same for both players units
initializeUnitWithValues(player1->army + code, code, name, att, cost, sup);
unit = &player2->army[code];
initializeUnitWithValues(player2-> army + code, code, name, att, cost, sup);
code++;
}
}
//initialize unit with stats
void initializeUnitWithValues(Unit *unit, int code, char name[20], int att, int cost, int sup){
strcpy( unit->name , name);
unit->att = att;
unit->cost = cost;
unit->sup = sup;
unit->num = 0;
unit->code = code;
}
void printOptionsMenu(){
printf("What would you like to do next? \n");
printf("1) Buy some new units \n");
printf("2) Show Game Rules \n");
printf("3) Show my army stats \n");
printf("4) Do nothing \n");
}
//inteface main menu
void printRules(){
printf("Simple rules:\n");
printf("1) The first one whose army power is greater than others by 100,at the end of the round is declared as a winner\n");
printf("2) The first one whose money balance reachs 200 euros will be the winner\n");
printf("3) You all start with %d euros", STARTBALANCE);
printf("4) You can only buy one type of unit during your turn\n");
printf("5) If your money balance get to be negative it's over mannn.. Take care what you choose to buy\n");
}
void printPurchaseOptions(Unit *army){
int i =0;
printf("Choose what kind of unit would you like to buy ? \n");
for(i=0; i<UNIQUEUNITS; i++){
printf("%d)Buy some %ss?", i, (army+i)->name);
printf("(cost per unit: %d , attack dmg per unit: %d, maintanance cost per round for each unit: %d)\n",(army+i)-> cost, (army+i)->att, (army+i)-> sup);
}
}
//show unit info
void printUnitInfo(Unit *unit){
printf("Unit Name: %s\n", unit->name);
printf("Unit purchae cost: %d\n", unit->cost);
printf("Unit support cost: %d\n", unit->sup);
printf("Unit code: %d\n", unit->code);
printf("Players unit size: %d\n", unit->num);
}
void printPlayerStats(Player *player){
printf("Your finance balance is: %d \n", player->money_balance);
printf("Your army cost per round is: %d \n", player-> cost_per_round);
printf("Your army size is: %d units total\n" , player->army_size);
printf("Your army attack damage is: %d \n", player-> attack_damage);
}
void printPlayersArmyStats(Player *player){
Unit *unit;
for(unit = player->army; unit< player-> army + UNIQUEUNITS; unit++){
printf(" You have %d %ss\n",unit->num, unit->name);
}
}
//central function for our logic
//simulation of one round for a player
void playRound(Player *player){
int choice=-1;
int purchaceChoice = -1;
printf("It's your turn %s\n", player->player_name);
printPlayerStats(player);
do{
//get what to do next from players input
printOptionsMenu();
scanf("%d", &choice);
while ( getchar() != '\n' );
switch (choice){
case(1):
//if he wants to buy something, show him his options
printPurchaseOptions(player-> army);
purchaceChoice = choosePurchaseOption();
//purchase what he asked for
purchaseUnitWithCode( purchaceChoice, player);
break;
case(2):
printRules();
break;
case(3):
printPlayersArmyStats(player);
break;
case(4):
choice = 1;
break;
default:
printf("Not a valid option \n");
break;
}
}while(choice != 1);
calculateRoundCostFor(player);
printf("Your turn ended, your new stats are: \n");
printPlayerStats(player);
}
int choosePurchaseOption(){
int choice = -1;
while(choice <0 || choice >= UNIQUEUNITS){
printf("What is your choice? You must enter a value from 0 to %d \n", UNIQUEUNITS-1);
scanf("%d", &choice);
while ( getchar() != '\n' );
}
return choice;
}
void purchaseUnitWithCode(int choice, Player *player){
int size = -1;
int cost = 0;
while (1){
while(size == -1){
printf("How many units would you like to buy? Choose wise ...The total cost of the units cannot exceed your money balance\n");
printf("Also remeber that after the round ends you cannot owe money\n");
scanf("%d", &size);
while ( getchar() != '\n' );
}
cost = size * player->army[choice].cost;
if (player-> money_balance >= cost){
break;
}
else{
printf("Your cannot afford so many units , choose again..\n");
}
}
player-> money_balance -= cost;
printf("New balance after purchase is %d\n", player-> money_balance);
player-> army_size += size;
player-> army[choice].num += size;
player-> attack_damage += size * player-> army[choice].att;
}
//well having an army costs a lot
//depending on the total cost ( or gain ) of units
//we calculate the new balance
//and we also calculate the total cost per round .
void calculateRoundCostFor(Player *player){
int i;
player-> cost_per_round = 0;
for(i =0; i<UNIQUEUNITS; i++){
player-> cost_per_round += player->army[i].sup * player->army[i].num;
player-> money_balance -= player->army[i].sup * player->army[i].num;
}
}
// We should have a winner
// this functions tries to find if we have a winner in this round
Player* checkForWinner(Player *player1, Player *player2){
Player *winner = NULL;
//check if someone bankrupted
if(player1->money_balance < 0){
winner = player2;
return winner;
}
if(player2-> money_balance <0){
winner = player1;
return winner;
}
//check if someones balance is much greater than others
if(player1-> money_balance != player2-> money_balance){
if(player1-> money_balance >= RICH)
winner = player1;
if(player2-> money_balance >= RICH)
winner = player2;
}
//army power is more important than just being rich
if((player1-> attack_damage - player2-> attack_damage) == GREATARMYPOWER){
winner = player1;
}else if((player2-> attack_damage -player1-> attack_damage) == GREATARMYPOWER){
winner = player2;
}
return winner;
}