Files
oop/labs/lab3/Shooting/Program.cs
2025-09-30 08:21:09 +03:00

82 lines
2.3 KiB
C#

using System;
class Program
{
static int VariantFromStudentNumber(int n) => (n % 2 != 0) ? 1 : 2;
static int ScoreVariant1(double x, double y)
{
var r = Math.Sqrt(x * x + y * y);
if (r <= 1) return 10;
if (r <= 2) return 5;
if (r <= 3) return 2;
return 0;
}
static int Sector(double x, double y)
{
var a = Math.Atan2(y, x);
if (a > Math.PI / 4 && a <= 3 * Math.PI / 4) return 1;
if (a > -Math.PI / 4 && a <= Math.PI / 4) return 5;
if (a > -3 * Math.PI / 4 && a <= -Math.PI / 4) return 2;
return 3;
}
static int ScoreVariant2(double x, double y)
{
var r = Math.Sqrt(x * x + y * y);
if (r <= 1) return 10;
if (r <= 3) return Sector(x, y);
return 0;
}
static void Main()
{
Console.Write("Номер по списку: ");
int student = int.Parse(Console.ReadLine());
int variant = VariantFromStudentNumber(student);
Console.Write("Сколько выстрелов: ");
int n = int.Parse(Console.ReadLine());
Console.Write("Случайный центр? (y/n): ");
bool rndCenter = Console.ReadLine().Trim().ToLower() == "y";
Console.Write("Случайная помеха? (y/n): ");
bool noise = Console.ReadLine().Trim().ToLower() == "y";
var rnd = new Random();
double cx = 0, cy = 0;
if (rndCenter)
{
cx = rnd.NextDouble() * 2 - 1;
cy = rnd.NextDouble() * 2 - 1;
}
int sum = 0;
for (int i = 0; i < n; i++)
{
Console.Write($"Выстрел {i + 1} (x y): ");
var parts = Console.ReadLine().Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
double x = double.Parse(parts[0]);
double y = double.Parse(parts[1]);
if (noise)
{
x += (rnd.NextDouble() * 0.2 - 0.1);
y += (rnd.NextDouble() * 0.2 - 0.1);
}
x -= cx;
y -= cy;
int s = variant == 1 ? ScoreVariant1(x, y) : ScoreVariant2(x, y);
sum += s;
Console.WriteLine($"Очки: {s}, сумма: {sum}");
}
Console.WriteLine($"Итоговая сумма: {sum}");
}
}