Skip to content

Commit

Permalink
Fix assigning speed to balls
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubRamisz committed Apr 27, 2022
1 parent 3e21715 commit 8c3212f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
12 changes: 11 additions & 1 deletion CP/Logic/BallManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class BallManager : LogicApi
private readonly int _width;
private readonly int _height;
private readonly int _radius;
private readonly int _maxSpeed = 2;
private readonly List<Ball> _balls = new List<Ball>();
private readonly List<IObserver> _observers = new List<IObserver>();

Expand All @@ -23,14 +24,23 @@ public BallManager(int width, int height, int radius)
public override int Height { get => _height; }
public override int Radius { get => _radius; }
public override List<Ball> Balls { get => _balls; }
public override int MaxSpeed { get => _maxSpeed; }

public override void CreateBalls(int amount)
{
Random random = new Random();

for (int i = 0; i < amount; i++)
{
Point motionDirection = new Point(random.Next(-2, 3), random.Next(-2, 3));
int speedHorizontal = 0;
int speedVertical = 0;
while (speedHorizontal == 0 && speedVertical == 0)
{
speedHorizontal = random.Next(-_maxSpeed, _maxSpeed + 1);
speedVertical = random.Next(-_maxSpeed, _maxSpeed + 1);

}
Point motionDirection = new Point(speedHorizontal, speedVertical);
int x = random.Next(_radius, _width - _radius);
int y = random.Next(_radius, _height - _radius);

Expand Down
1 change: 1 addition & 0 deletions CP/Logic/LogicApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static LogicApi CreateApi(int width, int height, int radius)
public abstract int Width { get; }
public abstract int Height { get; }
public abstract int Radius { get; }
public abstract int MaxSpeed { get; }

public abstract void CreateBalls(int amount);

Expand Down
29 changes: 11 additions & 18 deletions CP/LogicTests/LogicApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,17 @@ public void TestCreateBalls()

Assert.AreEqual(testBallManager.Balls.Count, testBallCount);

Assert.IsTrue(testBallManager.Balls[0].Center.X - testRadius >= 0);
Assert.IsTrue(testBallManager.Balls[0].Center.X + testRadius <= testWidth);
Assert.IsTrue(testBallManager.Balls[0].Center.Y - testRadius >= 0);
Assert.IsTrue(testBallManager.Balls[0].Center.Y + testRadius <= testHeight);
Assert.IsTrue(testBallManager.Balls[0].MotionDirection.X <= 1);
Assert.IsTrue(testBallManager.Balls[0].MotionDirection.X >= -1);
Assert.IsTrue(testBallManager.Balls[0].MotionDirection.Y <= 1);
Assert.IsTrue(testBallManager.Balls[0].MotionDirection.Y >= -1);


Assert.IsTrue(testBallManager.Balls[1].Center.X - testRadius >= 0);
Assert.IsTrue(testBallManager.Balls[1].Center.X + testRadius <= testWidth);
Assert.IsTrue(testBallManager.Balls[1].Center.Y - testRadius >= 0);
Assert.IsTrue(testBallManager.Balls[1].Center.Y + testRadius <= testHeight);
Assert.IsTrue(testBallManager.Balls[1].MotionDirection.X <= 1);
Assert.IsTrue(testBallManager.Balls[1].MotionDirection.X >= -1);
Assert.IsTrue(testBallManager.Balls[1].MotionDirection.Y <= 1);
Assert.IsTrue(testBallManager.Balls[1].MotionDirection.Y >= -1);
foreach (Ball ball in testBallManager.Balls)
{
Assert.IsTrue(ball.Center.X - testRadius >= 0);
Assert.IsTrue(ball.Center.X + testRadius <= testWidth);
Assert.IsTrue(ball.Center.Y - testRadius >= 0);
Assert.IsTrue(ball.Center.Y + testRadius <= testHeight);
Assert.IsTrue(ball.MotionDirection.X <= testBallManager.MaxSpeed);
Assert.IsTrue(ball.MotionDirection.X >= -testBallManager.MaxSpeed);
Assert.IsTrue(ball.MotionDirection.Y <= testBallManager.MaxSpeed);
Assert.IsTrue(ball.MotionDirection.Y >= -testBallManager.MaxSpeed);
}
}

[TestMethod]
Expand Down

0 comments on commit 8c3212f

Please # to comment.