Fix math bugs for scoreboard (#4)

master
Garrett Mills 4 years ago
parent 8a5c4eec79
commit fbe8ac8ce6
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -15,14 +15,14 @@ const template = `
<td class="scoreboard_data">progress</td> <td class="scoreboard_data">progress</td>
</tr> </tr>
<tr class="scoreboard_rows_score&progress"> <tr class="scoreboard_rows_score&progress">
<td class="scoreboard_player">player_1</td> <td class="scoreboard_player">Player 1</td>
<td class="scoreboard_data">{{player_one_score}}</td> <td class="scoreboard_data">{{player_one_score}}</td>
<td class="scoreboard_data">{{player_one_progress}}</td> <td class="scoreboard_data">{{player_one_progress * 100}}%</td>
</tr> </tr>
<tr class="scoreboard_lastRow"> <tr class="scoreboard_lastRow">
<td class="scoreboard_player">player_2</td> <td class="scoreboard_player">Player 2</td>
<td class="scoreboard_data">{{player_one_score}}</td> <td class="scoreboard_data">{{player_two_score}}</td>
<td class="scoreboard_data">{{player_two_progress}}</td> <td class="scoreboard_data">{{player_two_progress * 100}}%</td>
</tr> </tr>
</table> </table>
</div> </div>

@ -210,21 +210,16 @@ export class GameStateService {
* @private * @private
*/ */
get_player_score(player) { get_player_score(player) {
let i = 1; let score = 0
let j = 1; this.player_x_game_board[player].some(row => {
let score = 0; row.some(cell => {
for(i; i<=8; i++) if ( cell.render === GridCellState.Damaged || cell.render === GridCellState.Sunk ) {
{ score += 1
for(j; j<=8; j++)
{
let cell = this.player_x_game_board[this.get_other_player(player)][i][j];
if(cell.render === GridCellState.Damaged || cell.render === GridCellState.Sunk )
{
score++;
} }
} })
} })
return(score);
return score
} }
/** /**
@ -234,21 +229,16 @@ export class GameStateService {
* @private * @private
*/ */
get_boat_count(player){ get_boat_count(player){
let i = 1; let boat_count = 0
let j = 1; this.player_x_game_board[player].some(row => {
let boat_count = 0; row.some(cell => {
for(i; i<=8; i++) if ( isShipCell(cell.render) ) {
{ boat_count += 1
for(j; j<=8; j++)
{
let cell = this.player_x_game_board[this.get_other_player(player)][i][j];
if(cell.render === GridCellState.Damaged || cell.render === GridCellState.Sunk || cell.render === GridCellState.Ship )
{
boat_count++;
} }
} })
} })
return(boat_count);
return boat_count
} }
/** /**
@ -257,7 +247,12 @@ export class GameStateService {
* @private * @private
*/ */
get_progress(player){ get_progress(player){
return(this.get_player_score(player) / this.get_boat_count(player)) const boat_count = this.get_boat_count(player)
if ( boat_count !== 0 ) {
return (this.get_player_score(player) / boat_count).toFixed(2)
} else {
return 0
}
} }
/** /**

Loading…
Cancel
Save