Skip to content

Commit 763401a

Browse files
committed
implement polymorphy
1 parent 144c016 commit 763401a

File tree

5 files changed

+89
-27
lines changed

5 files changed

+89
-27
lines changed

Dice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class Dice {
44

55
private int id;
6-
private int value;
6+
protected int value;
77

88
public Dice(int id) {
99
this.id = id;

DiceGame.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,56 @@ public void start() {
4343
}
4444

4545
private void move(Player player) {
46-
dices.add(new Dice(1));
47-
dices.add(new Dice(2));
48-
dices.add(new Dice(3));
49-
System.out.println(player.getName() + " hat aktuell " + player.getScore() + " Punkte");
50-
System.out.print(player.getName() + ", moechtest Du wuerfeln (true, false)?: ");
51-
if (scanner.nextBoolean()) {
52-
diceCup.rollTheDices(dices);
53-
int score = 0;
54-
for (Dice dice : dices) {
55-
score += dice.getValue();
56-
}
57-
System.out.println(player.getName() + " hat " + score + " Punkte");
58-
player.setScore(player.getScore() + score);
59-
System.out.println(player.getName() + " hat insgesamt " + player.getScore() + " Punkte");
60-
if (player.getScore() > 50) {
61-
System.out.println(player.getName() + " hat verloren");
62-
player.setScore(0);
63-
player.setActive(false);
64-
numberOfActivePlayers--;
65-
}
66-
} else {
67-
player.setActive(false);
68-
numberOfActivePlayers--;
69-
}
70-
System.out.println();
71-
dices.clear();
46+
int input;
47+
System.out.println(player.getName() + " hat aktuell " + player.getScore() + " Punkte");
48+
if (player.getSpecialAvailable()) {
49+
System.out.print(player.getName() + ", moechtest Du einmalig Spezialwuerfel verwenden (1=ja, 2=nein)?: ");
50+
input = scanner.nextInt();
51+
if (input == 1) {
52+
System.out.print(player.getName()
53+
+ ", welche Spezialwuerfel moechtest Du verwenden (1=4-5-6-Wuerfel, 2=1-2-3-Wuerfel)?: ");
54+
input = scanner.nextInt();
55+
if (input == 1) {
56+
dices.add(new HighValueDice(4));
57+
dices.add(new HighValueDice(5));
58+
dices.add(new HighValueDice(6));
59+
} else {
60+
dices.add(new LowValueDice(7));
61+
dices.add(new LowValueDice(8));
62+
dices.add(new LowValueDice(9));
63+
}
64+
player.setSpecialAvailable(false);
65+
} else {
66+
dices.add(new Dice(1));
67+
dices.add(new Dice(2));
68+
dices.add(new Dice(3));
69+
}
70+
} else {
71+
dices.add(new Dice(1));
72+
dices.add(new Dice(2));
73+
dices.add(new Dice(3));
74+
}
75+
System.out.print(player.getName() + ", moechtest Du wuerfeln (true, false)?: ");
76+
if (scanner.nextBoolean()) {
77+
diceCup.rollTheDices(dices);
78+
int score = 0;
79+
for (Dice dice : dices) {
80+
score += dice.getValue();
81+
}
82+
System.out.println(player.getName() + " hat " + score + " Punkte");
83+
player.setScore(player.getScore() + score);
84+
System.out.println(player.getName() + " hat insgesamt " + player.getScore() + " Punkte");
85+
if (player.getScore() > 50) {
86+
System.out.println(player.getName() + " hat verloren");
87+
player.setScore(0);
88+
player.setActive(false);
89+
numberOfActivePlayers--;
90+
}
91+
} else {
92+
player.setActive(false);
93+
numberOfActivePlayers--;
94+
}
95+
System.out.println();
96+
dices.clear();
7297
}
7398
}

HighValueDice.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Random;
2+
3+
public class HighValueDice extends Dice {
4+
5+
public HighValueDice(int id) {
6+
super(id);
7+
}
8+
9+
public void rollTheDice() {
10+
Random random = new Random();
11+
value = random.nextInt(3) + 4;
12+
}
13+
}

LowValueDice.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Random;
2+
3+
public class LowValueDice extends Dice {
4+
5+
public LowValueDice(int id) {
6+
super(id);
7+
}
8+
9+
public void rollTheDice() {
10+
Random random = new Random();
11+
value = random.nextInt(3) + 1;
12+
}
13+
}

Player.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ public class Player {
33
private String name;
44
private int score;
55
private boolean isActive;
6+
private boolean specialAvailable;
67

78
public Player(String name) {
89
this.name = name;
10+
isActive = true;
11+
specialAvailable = true;
912
}
1013

1114
public String getName() {
@@ -27,4 +30,12 @@ public boolean isActive() {
2730
public void setActive(boolean isActive) {
2831
this.isActive = isActive;
2932
}
33+
34+
public boolean getSpecialAvailable() {
35+
return specialAvailable;
36+
}
37+
38+
public void setSpecialAvailable(boolean specialAvailable) {
39+
this.specialAvailable = specialAvailable;
40+
}
3041
}

0 commit comments

Comments
 (0)