1. Player 1 will roll three dice and player 2 will roll two dice. 2. The highest number rolled for each player are compared. If the attacker number is higher, the defender loses one army. If the attacker number is lower than or equal to the defender number, the attacker loses one army. 3. The second highest number rolled for each player are compared. If the attacker number is higher, the defender loses one army. If the attacker number is lower than or equal to the defender number, the attacker loses one army.
Pls, can you help me with this code without using an array?
This is C language btw.
Program Code:
#include<stdio.h>
main()
{
int a1,a2,a3,d1,d2,highest_a,shighest_a;
int highest_d,shighest_d,flag_a,flag_d;
printf("The attacker roles:");
scanf("%d%d%d",&a1,&a2,&a3);
printf("\nThe defender roles:");
scanf("%d%d",&d1,&d2);
if(a1>a2 && a1>a3)
{
highest_a=a1;
if(a2>a3)
{
shighest_a=a2;
}
else{
shighest_a=a3;
}
}
else if(a2>a1 && a2>a3)
{
highest_a=a2;
if(a1>a3)
{
shighest_a=a1;
}
else{
shighest_a=a3;
}
}
else
{
highest_a=a3;
if(a1>a2)
{
shighest_a=a1;
}
else{
shighest_a=a2;
}
}
if(d1>d2)
{
highest_d=d1;
shighest_d=d2;
}
else{
highest_d=d2;
shighest_d=d1;
}
printf("\nAttacker: %d %d",highest_a,shighest_a);
printf("\nDefender: %d %d",highest_d,shighest_d);
if(highest_a>highest_d)
{
flag_a=1;
}
else{
flag_a=0;
}
if(shighest_a>shighest_d)
{
flag_d=1;
}
else{
flag_d=0;
}
if(flag_a==1 && flag_d==1)
{
printf("\nDefender loses two army");
}
else if(flag_a==0 && flag_d==0)
{
printf("\nAttacker loses two army");
}
else{
printf("\nBoth players lose one army");
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images