Complementary task for topic: 5
M Nemeth 2023-08-29 15:21:04.622218'
Structs: create logic
Structs: create logic
Do some simple logic to the game. There are two opposing teams, let us say the winner team will be that has more points according to the following:
The best students attacks each other, the attack will decrease magical defense by the following rule:
Defence_new=Defence_old-Opponent_attack*10/|Own_defese|
When one teams sum of student's defence is negative, the winner is the other team.
For simplicity, we can get rid of age cleverness and House fields, for more complex game, you can keep it. Let's see how much earier to delete the fields, as functions are used.
let us see who is the winner!
Hint: There ary many ways to do it. Try for yourself to solve. Here there are many pitfalls, but you can learn from it a lot.
Solution
#include
typedef struct Student {
char name[100];//Max. 99 chars for a name!
float magic_attack;
float magic_defense;
}Student;
typedef struct{
Student Team[100]; //we will not allow more than 100 students!
int size; //we keep the size here (that should be size_t!)
}Stud_Arr;
Stud_Arr fill_up(Stud_Arr Students){
Students.size=0; //set to zero
char c='y'; //This variable will responsible to terminate the read
int i=0;
char name[100];
while(c=='y'||c=='Y'){
printf("please give the name of the student:\n");
scanf("%99[^\n]", Students.Team[i].name);
printf("please give magic attack of the student:\n");
scanf("%f", &Students.Team[i].magic_attack);
printf("please give magic defense of the student:\n");
scanf("%f", &Students.Team[i].magic_defense);
//This is a portable way to flush the input! Newline sticks!
int r;
while ((r = getchar()) != '\n' && r != EOF) { }
printf("Do you want to add one more? y/n\n");
scanf("%c",&c);
//This is a portable way to flush the input! Newline sticks!
while ((r = getchar()) != '\n' && r != EOF) { }
Students.size++;
i++;
}
return Students;
}
float force(Student stud){
return stud.magic_defense+stud.magic_attack;
}
int best_student(Student Stud[], size_t size){//can be int size as well
int max_index=0;
int max_value=force(Stud[0]);//force must be declared earier!
for(int i=1;imax_value){
max_index=i;
max_value=force(Stud[i]);}
}
return max_index;
}
int abs(int a){
if(a<0)
return -a;
return a;
}
int play(Stud_Arr studs1,Stud_Arr studs2){
Student Player1;
Student Player2;
while(1){
//Naive idea!! Will be wrong:
//
/*Player1=studs1.Team[best_student(studs1.Team,studs1.size)];
Player2=studs2.Team[best_student(studs2.Team,studs2.size)];
Player1.magic_defense-=Player2.magic_attack*10/Player1.magic_defense;
Player2.magic_defense-=Player1.magic_attack*10/Player2.magic_defense;
*/
//Think over what happens, maybe you can try it!
//Solution: Player1 and Player2 fields are overwritten, not the member in the team IT IS JUST A COPY!
int P1=best_student(studs1.Team,studs1.size);
int P2=best_student(studs2.Team,studs2.size);
studs1.Team[P1].magic_defense-=studs2.Team[P2].magic_attack*10/abs(studs1.Team[P1].magic_defense);
studs2.Team[P2].magic_defense-=studs1.Team[P1].magic_attack*10/abs(studs2.Team[P2].magic_defense);
//check for negativ sum of defence
int sum=0;
for(int i=0;i