Skip to content

Commit

Permalink
re-implement exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Dec 20, 2024
1 parent 510b176 commit f5f0b79
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 54 deletions.
32 changes: 11 additions & 21 deletions exercises/practice/darts/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@

declare(strict_types=1);

class Darts
function calculateScore(float $xAxis, float $yAxis): int
{
public int $score = 0;
$location = $xAxis ** 2 + $yAxis ** 2;

public function __construct(float $xAxis, float $yAxis)
{
$this->score = $this->calculateScore($xAxis, $yAxis);
if ($location > 100) {
return 0;
}

private function calculateScore(float $xAxis, float $yAxis): int
{
$location = $xAxis ** 2 + $yAxis ** 2;

if ($location > 100) {
return 0;
}
if ($location > 25) {
return 1;
}
if ($location > 1) {
return 5;
}

return 10;
if ($location > 25) {
return 1;
}
if ($location > 1) {
return 5;
}

return 10;
}
9 changes: 2 additions & 7 deletions exercises/practice/darts/Darts.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@

declare(strict_types=1);

class Darts
function __construct(float $xAxis, float $yAxis)
{
public int $score = 0;

public function __construct(float $xAxis, float $yAxis)
{
throw new BadFunctionCallException("Please implement the Darts class!");
}
throw new BadFunctionCallException("Please implement the Darts class!");
}
39 changes: 13 additions & 26 deletions exercises/practice/darts/DartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public static function setUpBeforeClass(): void
*/
public function testMissedTarget(): void
{
$board = new Darts(-9, 9);
$this->assertEquals(0, $board->score);
$this->assertEquals(0, calculateScore(-9.0, 9.0));
}

/**
Expand All @@ -25,8 +24,7 @@ public function testMissedTarget(): void
*/
public function testInOuterCircle(): void
{
$board = new Darts(0, 10);
$this->assertEquals(1, $board->score);
$this->assertEquals(1, calculateScore(0.0, 10.0));
}

/**
Expand All @@ -35,8 +33,7 @@ public function testInOuterCircle(): void
*/
public function testInMiddleCircle(): void
{
$board = new Darts(-5, 0);
$this->assertEquals(5, $board->score);
$this->assertEquals(5, calculateScore(-5.0, 0.0));
}

/**
Expand All @@ -45,8 +42,7 @@ public function testInMiddleCircle(): void
*/
public function testInInnerCircle(): void
{
$board = new Darts(0, -1);
$this->assertEquals(10, $board->score);
$this->assertEquals(10, calculateScore(0.0, -1.0));
}

/**
Expand All @@ -55,8 +51,7 @@ public function testInInnerCircle(): void
*/
public function testInCenter(): void
{
$board = new Darts(0, 0);
$this->assertEquals(10, $board->score);
$this->assertEquals(10, calculateScore(0.0, 0.0));
}

/**
Expand All @@ -65,8 +60,7 @@ public function testInCenter(): void
*/
public function testNearCenter(): void
{
$board = new Darts(-0.1, -0.1);
$this->assertEquals(10, $board->score);
$this->assertEquals(10, calculateScore(-0.1, -0.1));
}

/**
Expand All @@ -75,8 +69,7 @@ public function testNearCenter(): void
*/
public function testJustInsideCenter(): void
{
$board = new Darts(0.7, 0.7);
$this->assertEquals(10, $board->score);
$this->assertEquals(10, calculateScore(0.7, 0.7));
}

/**
Expand All @@ -85,8 +78,7 @@ public function testJustInsideCenter(): void
*/
public function testJustOutsideCenter(): void
{
$board = new Darts(0.8, -0.8);
$this->assertEquals(5, $board->score);
$this->assertEquals(5, calculateScore(0.8, -0.8));
}

/**
Expand All @@ -95,8 +87,7 @@ public function testJustOutsideCenter(): void
*/
public function testJustWithinMiddleCircle(): void
{
$board = new Darts(-3.5, 3.5);
$this->assertEquals(5, $board->score);
$this->assertEquals(5, calculateScore(-3.5, 3.5));
}

/**
Expand All @@ -105,8 +96,7 @@ public function testJustWithinMiddleCircle(): void
*/
public function testJustOutsideMiddleCircle(): void
{
$board = new Darts(-3.6, -3.6);
$this->assertEquals(1, $board->score);
$this->assertEquals(1, calculateScore(-3.6, -3.6));
}

/**
Expand All @@ -115,8 +105,7 @@ public function testJustOutsideMiddleCircle(): void
*/
public function testJustInsideOuterCircle(): void
{
$board = new Darts(-7.0, 7.0);
$this->assertEquals(1, $board->score);
$this->assertEquals(1, calculateScore(-7.0, 7.0));
}

/**
Expand All @@ -125,8 +114,7 @@ public function testJustInsideOuterCircle(): void
*/
public function testJustOutsideOuterCircle(): void
{
$board = new Darts(7.1, -7.1);
$this->assertEquals(0, $board->score);
$this->assertEquals(0, calculateScore(7.1, -7.1));
}

/**
Expand All @@ -135,7 +123,6 @@ public function testJustOutsideOuterCircle(): void
*/
public function testAsymmetricPositionBetweenInnerAndOuterCircles(): void
{
$board = new Darts(0.5, -4);
$this->assertEquals(5, $board->score);
$this->assertEquals(5, calculateScore(0.5, -4));
}
}

0 comments on commit f5f0b79

Please sign in to comment.