-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinPickingGame.java
More file actions
90 lines (72 loc) · 2.96 KB
/
Copy pathCoinPickingGame.java
File metadata and controls
90 lines (72 loc) · 2.96 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
package edu.gwu.csci6212;
public class CoinPickingGame {
private MemoizationTableCell[][] memoizationTable;
private int[] coins;
private class MemoizationTableCell {
public int playerOneScore;
public int playerTwoScore;
MemoizationTableCell(int playerOneScore, int playerTwoScore) {
this.playerOneScore = playerOneScore;
this.playerTwoScore = playerTwoScore;
}
}
public void setup(int coins[]) {
if (hasOddCount(coins) || isEmpty(coins))
throw new IllegalCapacity();
this.coins = coins;
this.memoizationTable = new MemoizationTableCell[coins.length][coins.length];
}
private boolean hasOddCount(int[] coins) {
return coins.length % 2 == 1;
}
private boolean isEmpty(int[] coins) {
return coins.length == 0;
}
private void calculateMemoizationTable() {
calculateMainDiagonal();
calculateCells();
}
private void calculateMainDiagonal() {
for (int coinIndex = 0; coinIndex < this.coins.length; coinIndex++) {
this.memoizationTable[coinIndex][coinIndex] = new MemoizationTableCell(coins[coinIndex], 0);
}
}
private void calculateCells() {
for (int startBounds = 1; startBounds < this.coins.length; startBounds++) {
for (int x = startBounds; x < this.coins.length; x++) {
int y = x - startBounds;
calculateCell(x, y);
}
}
}
private void calculateCell(int x, int y) {
this.memoizationTable[x][y] = calculatePlayerOneScoreFromOneCellDown(x,
y) >= calculatePlayerOneScoreFromOneCellLeft(x, y)
? new MemoizationTableCell(calculatePlayerOneScoreFromOneCellDown(x, y),
calculatePlayerTwoScoreFromOneCellDown(x, y))
: new MemoizationTableCell(calculatePlayerOneScoreFromOneCellLeft(x, y),
calculatePlayerTwoScoreFromOneCellLeft(x, y));
}
private int calculatePlayerOneScoreFromOneCellDown(int x, int y) {
return this.memoizationTable[x][y + 1].playerTwoScore + coins[y];
}
private int calculatePlayerTwoScoreFromOneCellDown(int x, int y) {
return this.memoizationTable[x][y + 1].playerOneScore;
}
private int calculatePlayerOneScoreFromOneCellLeft(int x, int y) {
return this.memoizationTable[x - 1][y].playerTwoScore + coins[x];
}
private int calculatePlayerTwoScoreFromOneCellLeft(int x, int y) {
return this.memoizationTable[x - 1][y].playerOneScore;
}
public int play() {
this.calculateMemoizationTable();
return this.calculateTotalValue();
}
private int calculateTotalValue() {
return this.memoizationTable[this.coins.length - 1][0].playerOneScore;
}
public static class IllegalCapacity extends RuntimeException {
private static final long serialVersionUID = 1L;
}
}