-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClock.java
More file actions
115 lines (100 loc) · 2.54 KB
/
Clock.java
File metadata and controls
115 lines (100 loc) · 2.54 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
/**
* Simulation of one game of the solitaire Clock.
*/
class Clock {
private ClockDeck deck;
protected Card[][] piles;
protected static int nbrWins;
protected static int nbrLosses;
protected static int timesPlayed;
protected static int totalPilesReady;
/**
* Constructor, no arguments
*/
public Clock () {
deck = new ClockDeck();
piles = new Card[13][52];
timesPlayed ++;
run();
}
private void run() {
deck.shuffle();
int next = 0;
/* Loop throught the piles until loss or win. */
OuterLoop:
while (true) {
for (int k = 1; k <= 13; k++) {
/* No cards left => loss */
if (deck.isEmpty()) {
nbrLosses++;
break OuterLoop;
}
/* Check if the solitaire has succeded. */
if (success()) {
nbrWins++;
break OuterLoop;
}
/* Skip piles that are ready. */
if (!ready(k - 1)) {
/*
* Pick the top card of the deck.
* If its number matches the current pile,
* put the cards of the pile back in the deck.
*/
Card current = deck.drawCard();
if (k == current.getNbr()) {
deck.refill(piles, k - 1);
piles[k - 1][0] = current;
} else {
/* Put the current card in the current pile. */
piles[k - 1][next] = current;
}
}
}
next++;
}
/* Update for statistics. */
totalPilesReady += pilesReady();
}
/**
* Print statistics of all games played so far.
*/
public static void printStatics() {
System.out.println("Statistics for Clock:");
System.out.println("Nbr of wins: " + nbrWins);
System.out.println("Nbr of losses: " + nbrLosses);
System.out.println("Average result: " +
((double) totalPilesReady / timesPlayed) + " piles ready");
System.out.println("");
}
/*********************** PRIVATE HELP FUNCTIONS **************************/
/* Check if a pile is ready i.e. its top card is correct */
private boolean ready(int pile){
if (piles[pile][0] != null) {
if (piles[pile][0].getNbr() == pile + 1) {
return true;
}
}
return false;
}
/* Check if the solitaire has succeded. */
private boolean success(){
for (int nbr = 0; nbr < 13; nbr++) {
if (!ready(nbr)) {
return false;
}
}
return true;
}
/* The number of piles that are ready. */
private int pilesReady() {
int nbrReady = 0;
for (int nbr = 0; nbr < 13; nbr++) {
if (ready(nbr)) {
nbrReady++;
}
}
return nbrReady;
}
}