Implement the following java class for scoring a bowling game, provided below, i
ID: 3762514 • Letter: I
Question
Implement the following java class for scoring a bowling game, provided below, in MIPS assembly language using the MARS MIPS simulator (http://courses.missouristate.edu/KenVollmar/MARS/). Include in your asm implementation the ability to populate the array through the MARS command prompt. The asm should return the correct score for the game provided. Comment every line of the code.
public class BowlingGame {
private int[] rolls = new int[21];
private int currentRoll = 0;
public void roll(int pins) {
rolls[currentRoll++] = pins;
}
public int getScore() {
int score = 0;
int rollIndex = 0;
for (int frameIndex = 0; frameIndex < 10; frameIndex++) {
if (isStrike(rollIndex)) {
score += getStrikeScore(rollIndex);
rollIndex++;
} else if (isSpare(rollIndex)) {
score += getSpareScore(rollIndex);
rollIndex += 2;
} else {
score += getStandardScore(rollIndex);
rollIndex += 2;
}
}
return score;
}
private boolean isStrike(int rollIndex) {
return rolls[rollIndex] == 10;
}
private boolean isSpare(int rollIndex) {
return rolls[rollIndex] + rolls[rollIndex + 1] == 10;
}
private int getStrikeScore(int rollIndex) {
return rolls[rollIndex] + rolls[rollIndex + 1] + rolls[rollIndex + 2];
}
private int getSpareScore(int rollIndex){
return rolls[rollIndex] + rolls[rollIndex + 1] + rolls[rollIndex + 2];
}
private int getStandardScore(int rollIndex){
return rolls[rollIndex] + rolls[rollIndex + 1];
}
}
Explanation / Answer
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Player {
private String _playerName;
private List<Integer> _firstBall = new ArrayList<Integer>();
private List<Integer> _secondBall = new ArrayList<Integer>();
private int _bonusBall;
private int _playerScore;
private int _bonusCounter;
// Sets player name
public Player(String name){
_playerName = name;
}
// Gets player name
public String getName(){
return _playerName;
}
// Sets the first ball of a frame
public void setFirstBall(int frame, int score) throws IOException{
_firstBall.add(score);
}
// Gets the first ball of a frame
public int checkFirstBall(int frame) {
return _firstBall.get(frame);
}
// Sets the second ball of a frame
public void setSecondball(int frame, int score) throws IOException{
_secondBall.add(score);
}
// Gets the second ball of a frame
public int checkSecondBall(int frame) {
return _secondBall.get(frame);
}
// Sets the bonus balls for the last frame
public void setBonusBall(int score) {
_bonusBall = score;
}
// Gets the bonus ball of the last frame
public int checkBonusBall() {
return _bonusBall;
}
// Updates the player's total score
public void setPlayerScore(int score) {
_playerScore = _playerScore + score;
}
// Gets a player's total score
public int checkPlayerScore() {
return _playerScore;
}
// Sets a player's bonus counter
public void setBonusCounter(int bonusCounter) {
_bonusCounter = bonusCounter;
}
// Gets a player's bonus counter
public int checkBonusCounter() {
return _bonusCounter;
}
}