הערה (קובץ קוד)
קובץ זה הוא קובץ קוד מסוג C.
#include <stdbool.h>
#include "stdio.h"
#define SHIP_PARTS 8
#define SHIP_MAX_HEALTH 100
#define INPUT_DAMAGE_MIN 0
#define INPUT_DAMAGE_MAX 50
int main(void)
{
bool isError = false;
// damage for parts
int shipPartsImpotence[SHIP_PARTS] = {0};
printf("Please enter ship's parts value:\n");
for (int i = 0; i < SHIP_PARTS; i++)
{
scanf("%d", &shipPartsImpotence[i]);
}
// checking the inputted numbers for correct input
for (int i = 0; i < SHIP_PARTS; i++)
{
if (shipPartsImpotence[i] == -1)
{
shipPartsImpotence[i] = 100;
}
else if (shipPartsImpotence[i] < INPUT_DAMAGE_MIN)
{
isError = true;
}
else if (shipPartsImpotence[i] > INPUT_DAMAGE_MAX)
{
isError = true;
}
}
if (!isError)
{
// getting the ships damage
bool ship1HitPoints[SHIP_PARTS];
bool ship2HitPoints[SHIP_PARTS];
printf("Please enter first ship's hit marks:\n");
for (int i = 0; i < SHIP_PARTS; i++)
{
int temp;
scanf("%d", &temp);
ship1HitPoints[i] = temp;
}
printf("Please enter second ship's hit marks:\n");
for (int i = 0; i < SHIP_PARTS; i++)
{
int temp;
scanf("%d", &temp);
ship2HitPoints[i] = temp;
}
// calculating damage
int ship1Damage = SHIP_MAX_HEALTH, ship2Damage = SHIP_MAX_HEALTH;
for (int i = 0; i < SHIP_PARTS; i++)
{
ship1Damage -= (ship1HitPoints[i]) ? shipPartsImpotence[i] : 0;
ship2Damage -= (ship2HitPoints[i]) ? shipPartsImpotence[i] : 0;
}
if (ship1Damage > 0 && ship2Damage > 0)
{
printf("Both ships survived the battle!\n");
}
else if (ship1Damage > 0)
{
printf("Ship #1 has won the battle!\n");
}
else if (ship2Damage > 0)
{
printf("Ship #2 has won the battle!\n");
}
else
{
printf("Both ships have lost the battle!\n");
}
}
else
{
printf("There was an error in input\n");
}
return 0;
}