Kleiner maken van mijn if/elseif constructie?
Inmiddels bezig met een PHP dart script en nu heb ik keuze menu waar je het aantal sets en legs kunt kiezen, nu is het zo dat wanneer je bijvoorbeeld een best of 1 leg kiest. Nu is dat niet zo moeilijk te maken. Nu is het probleem meer, moet ik nu een elseif aanmaken voor elke 'best of leg(s)'?
Op dit moment heb ik dit (voor speler 1):
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
if(isset($_SESSION['player1'], $_SESSION['scoreleft1']) && $_SESSION['scoreleft1'] == 0) {
$_SESSION['throws1'] = 0;
$_SESSION['throws2'] = 0;
unset($_SESSION['lastthrows1']);
unset($_SESSION['lastthrows2']);
if($_SESSION['bestofsets'] == 1) {
if($_SESSION['legs1'] == 0 && $_SESSION['bestoflegs'] == 1) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 1 && $_SESSION['bestoflegs'] == 3) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 1 && $_SESSION['bestoflegs'] == 3) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 2 && $_SESSION['bestoflegs'] == 5) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 2 && $_SESSION['bestoflegs'] == 5) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 3 && $_SESSION['bestoflegs'] == 7) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 3 && $_SESSION['bestoflegs'] == 7) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 4 && $_SESSION['bestoflegs'] == 9) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 4 && $_SESSION['bestoflegs'] == 9) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
}
}
?>
if(isset($_SESSION['player1'], $_SESSION['scoreleft1']) && $_SESSION['scoreleft1'] == 0) {
$_SESSION['throws1'] = 0;
$_SESSION['throws2'] = 0;
unset($_SESSION['lastthrows1']);
unset($_SESSION['lastthrows2']);
if($_SESSION['bestofsets'] == 1) {
if($_SESSION['legs1'] == 0 && $_SESSION['bestoflegs'] == 1) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 1 && $_SESSION['bestoflegs'] == 3) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 1 && $_SESSION['bestoflegs'] == 3) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 2 && $_SESSION['bestoflegs'] == 5) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 2 && $_SESSION['bestoflegs'] == 5) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 3 && $_SESSION['bestoflegs'] == 7) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 3 && $_SESSION['bestoflegs'] == 7) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 4 && $_SESSION['bestoflegs'] == 9) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] == 4 && $_SESSION['bestoflegs'] == 9) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
}
}
?>
Mijn vraag is dus, kan dit korter en zo ja, hoe?
zoals het nu lijkt, zou het verschil meer dan 2 "punten" moeten zijn, tenzij best gelijk is aan 9?
naast < en == bestaat er trouwens ook <= wat je in elk geval al de helft van je regels zou schelen.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
if($_SESSION['legs1'] == 0 && $_SESSION['bestoflegs'] == 1) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 1 && $_SESSION['bestoflegs'] == 3) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 2 && $_SESSION['bestoflegs'] == 5) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 3 && $_SESSION['bestoflegs'] == 7) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 4 && $_SESSION['bestoflegs'] == 9) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 1 && $_SESSION['bestoflegs'] == 3) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] < 2 && $_SESSION['bestoflegs'] == 5) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] < 3 && $_SESSION['bestoflegs'] == 7) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] < 4 && $_SESSION['bestoflegs'] == 9) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
?>
if($_SESSION['legs1'] == 0 && $_SESSION['bestoflegs'] == 1) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 1 && $_SESSION['bestoflegs'] == 3) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 2 && $_SESSION['bestoflegs'] == 5) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 3 && $_SESSION['bestoflegs'] == 7) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] == 4 && $_SESSION['bestoflegs'] == 9) {
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
}
elseif($_SESSION['legs1'] < 1 && $_SESSION['bestoflegs'] == 3) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] < 2 && $_SESSION['bestoflegs'] == 5) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] < 3 && $_SESSION['bestoflegs'] == 7) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
elseif($_SESSION['legs1'] < 4 && $_SESSION['bestoflegs'] == 9) {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
?>
En dan lijkt het mij een koud kunstje om het in twee opties te schrijven (maar test het nog wel, want dat heb ik niet gedaan):
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
if ( $_SESSION['legs1'] == floor( $_SESSION['bestoflegs'] / 2 ) ){
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
} else {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
?>
if ( $_SESSION['legs1'] == floor( $_SESSION['bestoflegs'] / 2 ) ){
$win = $_SESSION['player1'].' heeft gewonnen!';
$_SESSION['legs1'] = 0;
$_SESSION['legs2'] = 0;
$_SESSION['sets1']++;
} else {
$_SESSION['legs1']++;
$_SESSION['scoreleft1'] = $_SESSION['game'];
$_SESSION['scoreleft2'] = $_SESSION['game'];
}
?>
Gewijzigd op 11/03/2014 15:46:22 door Erwin H
Leuk gedaan trouwens Michael!