package service;
import org.springframework.stereotype.Service;
import java.util.Scanner;
public class BowlingGame {
private int score = 10;
public BowlingGame() {
}
public BowlingGame(int score) {
this.score = score;
}
public int getTotalScore(String input) {
int totalScore = 0;
if (judgeLength(input)) {
try {
totalScore = calculateScore(input);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("数组索引越界");
} catch (Exception e) {
e.printStackTrace();
System.out.println("error");
}
return totalScore;
}
return -1;
}
private int calculateScore(String input) {
//索引
int i = 0;
//循环次数(轮次)
int j = 0;
int k = 0;
char strike = 'X';
char spare = '/';
char out = '-';
char[] chars=input.toCharArray();
int score = 0;
while (j < 10) {
//该位置是strike的情况,要向后面看两位,有三种情况---XX N/ NN(-)
if (strike == chars[i]) {
chars = input.toCharArray();
score += this.score;
//X
if (strike == chars[i + 1])
score += this.score;
//N
else
score += Integer.parseInt(chars[i + 1] + "");
//XX
if (strike == chars[i + 2])
score += this.score;
//N/
else if (spare == chars[i + 2])
score += this.score - Integer.parseInt(chars[i + 1] + "");
//NN(-)
else
score += Integer.parseInt(chars[i + 2] + "");
//这种情况,本轮次只有一个索引位置
++i;
++j;
//该位置是spare的情况,要向后面看一位,有两种情况----X / N(-)
} else if (spare == chars[i]) {
//加上(10-前一位)的数值
score += this.score - Integer.parseInt(chars[i - 1] + "");
//X
if (strike == chars[i + 1])
score += this.score;
//N(-)
else
score += Integer.parseInt(chars[i + 1] + "");
++i;
++j;
k = 0;
//该位置是out的情况,不向后看,score不用变
} else if (out == chars[i]) {
//还不确定这次索引位置上的是否是本轮次的第二次,所以使用k进行计数,k达到2的时候则进行下一轮次,i++,开始下一个索引位置的判断;
score += 0;
++i;
++k;
if (k == 2) {
k = 0;
++j;
}
//该位置是N的情况,不向后看,score+N
} else {
//还不确定这次索引位置上的是否是本轮次的第二次,所以使用k进行计数,k达到2的时候则进行下一轮次;
score += Integer.parseInt(chars[i] + "");
++i;
++k;
if (k == 2) {
k = 0;
++j;
}
}
}
return score;
}
private boolean judgeLength(String input) {
char[] chars = input.toCharArray();
int length = chars.length;
if (10 <= length && chars.length <= 21 && chars[0]!='/') {
return true;
}
return false;
}
public void strike(String input){
}
}
package com.ljw.yeb.bowling;
import java.util.Objects;
public class BowlingGame {
private int score = 10;
//索引
private int i = 0;
//循环次数(轮次)
private int j = 0;
private int k = 0;
private char strike = 'X';
private char spare = '/';
private char out = '-';
private int totalScore = 0;
public BowlingGame() {
}
public BowlingGame(int score) {
this.score = score;
}
public int getTotalScore(String input) {
int totalScore = 0;
if (judgeLength(input)) {
try {
totalScore = calculateScore(input);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("数组索引越界");
} catch (Exception e) {
e.printStackTrace();
System.out.println("error");
}
return totalScore;
}
return -1;
}
public int calculateScore(String input) {
//X72X9/7-9/XX9/9-
char[] chars = input.toCharArray();
while (j < 10) {
//该位置是strike的情况,要向后面看两位,有三种情况---XX N/ NN(-)
if (Objects.equals(strike,chars[i])) {
firstJudge(chars);
//该位置是spare的情况,要向后面看一位,有两种情况----X / N(-)
} else if (Objects.equals(spare,chars[i])){
secondJudge(chars);
//该位置是out的情况,不向后看,score不用变
} else if (Objects.equals(out,chars[i])) {
thirdJudge(chars);
}
//该位置是N的情况,不向后看,score+N
else {
forthJudge(chars);
}
++i;
}
return totalScore;
}
private boolean judgeLength(String input) {
char[] chars = input.toCharArray();
int length = chars.length;
if (10 <= length && chars.length <= 21 && chars[0] != '/') {
return true;
}
return false;
}
private int firstJudge(char[] chars) {
totalScore += this.score;
//X
if (strike == chars[i + 1])
totalScore += this.score;
//N
else
totalScore += Integer.parseInt(chars[i + 1] + "");
//XX
if (strike == chars[i + 2])
totalScore += this.score;
//N/
else if (spare == chars[i + 2])
totalScore += this.score - Integer.parseInt(chars[i + 1] + "");
//NN(-)
else
totalScore += Integer.parseInt(chars[i + 2] + "");
//这种情况,本轮次只有一个索引位置
++j;
return totalScore;
}
private int secondJudge(char[] chars) {
//加上(10-前一位)的数值
totalScore += this.score - Integer.parseInt(chars[i - 1] + "");
//X
if (strike == chars[i + 1])
totalScore += this.score;
//N(-)
else
totalScore += Integer.parseInt(chars[i + 1] + "");
++j;
k = 0;
return totalScore;
}
private int thirdJudge(char[] chars) {
//还不确定这次索引位置上的是否是本轮次的第二次,所以使用k进行计数,k达到2的时候则进行下一轮次,i++,开始下一个索引位置的判断;
totalScore += 0;
++k;
if (k == 2) {
k = 0;
++j;
}
return totalScore;
}
private int forthJudge(char[] chars) {
//还不确定这次索引位置上的是否是本轮次的第二次,所以使用k进行计数,k达到2的时候则进行下一轮次;
totalScore += Integer.parseInt(chars[i] + "");
++k;
if (k == 2) {
k = 0;
++j;
}
return totalScore;
}
}