-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainGame.java
65 lines (45 loc) · 2.58 KB
/
MainGame.java
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
61
62
63
64
65
import java.util.*;
import java.util.stream.Collectors;
public class MainGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("\nWelcome to the Yahtzee Game!\n\n" +
"Please enter your name: ");
String name = sc.nextLine();
System.out.println();
List<Combination> combinations = new ArrayList<>(Combination.createCombinations());
Player user = new Player(name, combinations);
Player systemPlayer = new Player("System", combinations);
List<Player> players = List.of(user, systemPlayer);
Random random = new Random();
List<Integer> dices;
int turn = 0;
while(true) {
dices = new Random().ints(5, 1, 7).boxed().collect(Collectors.toList());
System.out.print("\n" + players.get(turn).getName() + " turn: ");
for(int dice : dices)
System.out.print(dice + " ");
System.out.println("\n");
List<Combination> availableCombinations = players.get(turn).getAvailableCombinations();
System.out.println("-------FORMED COMBINATIONS-------\n");
for(Combination combination : availableCombinations) {
if(combination.isFormed(dices))
System.out.format("\t%s\t\tPoints: %d\n", combination.getCombinationName(), combination.getPoints(dices));
}
System.out.print("\nChoose a formed combination or the dice(1-6) you want to roll again: ");
List<String> input = Arrays.stream(sc.nextLine().split(" ")).toList();
if (combinations.stream().map(c -> c.getCombinationName()).anyMatch(c -> c.equals(input.get(0)))) {
Combination combinationFormed = Combination.findCombinationByName(input.get(0));
players.get(turn).addPoints(combinationFormed.getPoints(dices));
players.get(turn).removeAvailableCombination(combinationFormed);
if (combinationFormed instanceof UpperSectionCombination)
players.get(turn).checkForBonus(combinationFormed.getPoints(dices));
}
else { //Roll some dice again
}
System.out.println("\nSCORE: " + players.get(0).getName() + " " + Integer.toString(players.get(0).getScore()) + " - "
+ Integer.toString(players.get(1).getScore()) + " " + players.get(1).getName());
turn = (turn + 1) % 2;
}
}
}