Passed
Push — main ( 6040c1...3e8a32 )
by Jenny
04:53
created

DiceGame::rollMixedHand()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
rs 10
1
<?php
2
3
namespace App\Dice;
4
5
use App\Dice\Dice;
6
use App\Dice\DiceGraphic;
7
use App\Dice\DiceHand;
8
9
/**
10
 * Methods for playing pig game.
11
 */
12
class DiceGame
13
{
14
    /**
15
     * Create hand and rolls all dices, returns hand with mixed images/integers.
16
     * @return diceHand
17
     */
18 2
    public function rollMixedHand(int $num): diceHand
19
    {
20 2
        $hand = new DiceHand();
21 2
        for ($i = 1; $i <= $num; $i++) {
22 2
            if ($i % 2 === 1) {
23 2
                $hand->add(new DiceGraphic());
24 2
                continue;
25
            }
26 2
            $hand->add(new Dice());
27
        }
28
29 2
        $hand->roll();
30
31 2
        return $hand;
32
    }
33
34
    /**
35
     * Create hand and rolls all dices, returns hand.
36
     * @return diceHand
37
     */
38 2
    public function rollHand(int $numDice): diceHand
39
    {
40
        // Skapar tärningshand
41 2
        $hand = new DiceHand();
42 2
        for ($i = 1; $i <= $numDice; $i++) {
43
            //Lägger antal tärningar i handen
44 2
            $hand->add(new DiceGraphic());
45
        }
46
        // Rullar tärningen
47 2
        $hand->roll();
48
49 2
        return $hand;
50
    }
51
52
    /**
53
     * Rolls number of dices used as parameter.
54
     * @return string[]
55
     */
56 2
    public function rollSeveral(int $num): array
57
    {
58 2
        $diceRoll = [];
59 2
        for ($i = 1; $i <= $num; $i++) {
60 2
            $die = new DiceGraphic();
61 2
            $die->roll();
62 2
            $diceRoll[] = $die->getAsString();
63
        }
64
65 2
        return $diceRoll;
66
    }
67
68
    /**
69
     * Calculates the value of game round.
70
     * @return int
71
     */
72 2
    public function gameRound(DiceHand $hand): int
73
    {
74 2
        $round = 0;
75 2
        $values = $hand->getValues();
76 2
        foreach ($values as $value) {
77 1
            if ($value === 1) {
78
                $round = 0;
79
                break;
80
            }
81 1
            $round += $value;
82
        }
83
84 2
        return $round;
85
    }
86
}
87