-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscores.java
138 lines (126 loc) · 5.16 KB
/
scores.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.ArrayList;
import java.util.HashMap;
public class scores {
private static HashMap<player, Integer> scoreMap;
public static HashMap<Character, Integer> tileScore;
public scores(player player1, player player2) {
scoreMap = new HashMap<player, Integer>();
scoreMap.put(player1, 0);
scoreMap.put(player2, 0);
this.initTileScore();
}
//Initializes tile scores in another hashmap
// TODO: Find a way to read these values from the file
public void initTileScore() {
scores.tileScore = new HashMap<Character, Integer>();
if (scores.tileScore.isEmpty()) {
scores.tileScore.put('A', 1);
scores.tileScore.put('B', 3);
scores.tileScore.put('C', 3);
scores.tileScore.put('D', 2);
scores.tileScore.put('E', 1);
scores.tileScore.put('F', 2);
scores.tileScore.put('G', 2);
scores.tileScore.put('H', 4);
scores.tileScore.put('I', 1);
scores.tileScore.put('J', 8);
scores.tileScore.put('K', 5);
scores.tileScore.put('L', 1);
scores.tileScore.put('M', 3);
scores.tileScore.put('N', 1);
scores.tileScore.put('O', 1);
scores.tileScore.put('P', 3);
scores.tileScore.put('Q', 10);
scores.tileScore.put('R', 1);
scores.tileScore.put('S', 1);
scores.tileScore.put('T', 1);
scores.tileScore.put('U', 1);
scores.tileScore.put('V', 4);
scores.tileScore.put('W', 4);
scores.tileScore.put('X', 8);
scores.tileScore.put('Y', 4);
scores.tileScore.put('Z', 10);
}
}
/*
@param C tile to be scored
@return score for a tile
*/
private int getTileScore(char C) {
if (scores.tileScore.containsKey(C)) {
return scores.tileScore.get(C);
}
return -1;
}
/*
updates players score
@param player player in question
@param score new score
*/
public void updatePlayerScore(player player, int score) {
if (score < 0) { throw new IllegalArgumentException("negative score!"); }
if (player == null) { throw new IllegalArgumentException("player obj null"); }
int newScore = scoreMap.get(player) + score;
scoreMap.put(player, newScore);
}
/*
get players score
@param player player in question
@return players score
*/
public static int getPlayerScore(player player) {
if (player == null) { throw new IllegalArgumentException("player obj null"); }
if (scoreMap.containsKey(player)) {
return scoreMap.get(player);
} else return -1;
}
/*
returns the score of the move
@param player who played the move
@param move move being played
@return move score
*/
public static int computeWordScore(move move) {
ArrayList<String> list = move.secondaryWords;
String word = move.word;
int dir = move.direction;
int row = move.startRow;
int col = move.startCol;
int totalScore = 0;
int tempWordScore = 0;
int wordMultiplier = 1; //double or triple scores
int tileMultiplier = 1;
int secondaryScore = 0;
for (int i = 0; i < word.length(); i++) {
if (dir == move.RIGHT) {
String boardRef = String.valueOf(row) + String.valueOf(col + i);
if (Game.getBoardScoreForTile(boardRef).equals("2W")) { wordMultiplier = 2;}
if (Game.getBoardScoreForTile(boardRef).equals("3W")) { wordMultiplier = 3;}
if (Game.getBoardScoreForTile(boardRef).equals("2L")) { wordMultiplier = 2;}
if (Game.getBoardScoreForTile(boardRef).equals("3L")) { wordMultiplier = 3;}
tempWordScore += (scores.tileScore.get(Character.toUpperCase(word.charAt(i)))) * tileMultiplier;
} else if (dir == move.DOWN) {
String boardRef = String.valueOf(row + i) + String.valueOf(col);
if (Game.getBoardScoreForTile(boardRef).equals("2W")) { wordMultiplier = 2;}
if (Game.getBoardScoreForTile(boardRef).equals("3W")) { wordMultiplier = 3;}
if (Game.getBoardScoreForTile(boardRef).equals("2L")) { wordMultiplier = 2;}
if (Game.getBoardScoreForTile(boardRef).equals("3L")) { wordMultiplier = 3;}
tempWordScore += (scores.tileScore.get(Character.toUpperCase(word.charAt(i)))) * tileMultiplier;
}
}
totalScore += tempWordScore * wordMultiplier;
secondaryScore = scores.computeSecondaryWordScore(move);
return totalScore + secondaryScore;
}
//the score of the move assuming the move is valid
private static int computeSecondaryWordScore(move move) {
ArrayList<String> list = move.secondaryWords;
int tempScore = 0;
for (String str : list) {
for (int i = 0; i < str.length(); i++) {
tempScore += scores.tileScore.get(Character.toUpperCase(str.charAt(i)));
}
}
return tempScore;
}
}