Fala pessoal, acabei de terminar o meu programa e para não deixar o tópico sem um fim vou postar aqui. Jaboot muito Obrigado pela ajuda cara, foi de grande valia.
Segue abaixo o código. Ps. os comentarios estão em inglês por que o tenho que entregar o projeto aqui na faculdade mesmo.
/**
* Simulate the calculation used to calculate a bowling game score
*
* @author Kaio Jonathas Alencar Gurgel
* @version April 2, 2013
*/
public class Bowling
{
private int[] rolls;
private int frames;
/**
* Constructor for objects of the class Bowling
*
* @param inRolls A array with the rolls of a bowling game
* @param inFrames A int number with the number of frames
*/
public Bowling(int[] inRolls, int inFrames)
{
rolls = inRolls;
frames = inFrames;
}
/**
* Make all the calculations needed to calculate the score of the bowling game
* using the array rolls and the number of frames. To print a well formatted frame
* of the game the method uses a private method called printFrame and in the case
* of the last frame with three rolls it is called the method printLastFrame
*/
public void play()
{
int numberFrame = 1; //The frame number starts in 1 and is incremented each time we print the frame
int scoreSoFar = 0; //Score at each frame (it is being changed periodically)
for(int i=0;i<rolls.length-1;i=i+2)
{
if(rolls[rolls.length-3] == 10 || rolls[rolls.length-3] + rolls[rolls.length-2] == 10) //Extra Roll at the end
{
if (i>=rolls.length-3)
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1] + rolls[i+2];
printLastFrame(numberFrame,rolls[i], rolls[i+1], rolls[i+2],scoreSoFar);
numberFrame++;
}
else if(rolls[i]==10) //if it is a strike
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1] + rolls[i+2];
printFrame(numberFrame,rolls[i], 0,scoreSoFar); //When we have a strike the second roll doesn't apply
numberFrame++;
i--;
}
else if(rolls[i]+rolls[i+1]==10 && rolls[i] != 10)
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1] + rolls[i+2];
printFrame(numberFrame,rolls[i], rolls[i+1],scoreSoFar);
numberFrame++;
}
else
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1];
printFrame(numberFrame,rolls[i], rolls[i+1],scoreSoFar);
numberFrame++;
}
}
else if(rolls[i]==10) //if it is a strike
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1] + rolls[i+2];
printFrame(numberFrame,rolls[i], 0,scoreSoFar); //When we have a strike the second roll doesn't apply
numberFrame++;
i--;
}
else if(rolls[i]+rolls[i+1]==10 && rolls[i] != 10)
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1] + rolls[i+2];
printFrame(numberFrame,rolls[i], rolls[i+1],scoreSoFar);
numberFrame++;
}
else
{
scoreSoFar = scoreSoFar + rolls[i] + rolls[i+1];
printFrame(numberFrame,rolls[i], rolls[i+1],scoreSoFar);
numberFrame++;
}
}
}
/**
* Print one well formatted frame of the bowling game
*
* @param numberFrame The number of frames of the game
* @param roll1 The first roll of the frame
* @param roll2 The second and last roll of the frame
* @param score The updated score of the game
*/
private void printFrame(int numberFrame,int roll1, int roll2, int score)
{
System.out.println("+---+---+ Frame"+ numberFrame);
if (roll1 + roll2 == 10 && roll1 != 10)
{
System.out.println("| "+roll1+" | / |");
}
else if (roll1==10)
{
System.out.println("| | X |");
}
else
{
System.out.println("| "+roll1+" | "+roll2+" |");
}
System.out.println("|---+---|");
if (score>=10 && score<=99)
{
System.out.println("| "+score+" |");
}
else if (score>=100)
{
System.out.println("| "+score+" |");
}
else
{
System.out.println("| "+score+" |");
}
System.out.println("+-------+");
System.out.println(" ");
}
/**
* Print one well formatted frame of the bowling game in the special case of the last frame with three rolls
*
* @param numberFrame The number of frames of the game
* @param roll1 The first roll of the frame
* @param roll2 The roll of the frame
* @param roll2 The third and last roll of the frame
* @param score The updated score of the game
*/
private void printLastFrame(int numberFrame,int roll1, int roll2, int roll3, int score)
{
System.out.println("+---+---+---+ Frame"+ numberFrame);
if(roll1 == 10 && roll2 != 10 && roll3 != 10 && roll2 + roll3 != 10)
{
System.out.println("| X | "+roll2+" | "+roll3+" |");
}
else if(roll1 == 10 && roll2 == 10 && roll3 != 10)
{
System.out.println("| X | X | "+roll3+" |");
}
else if(roll1 == 10 && roll2 == 10 && roll3 == 10)
{
System.out.println("| X | X | X |");
}
else if(roll1 + roll2 == 10 && roll1 != 10 && roll3 != 10)
{
System.out.println("| "+roll1+" | / | "+roll3+" |");
}
else if(roll2 + roll3 == 10 && roll1 == 10 && roll2 != 10)
{
System.out.println("| X | "+roll2+" | / |");
}
else if(roll1 + roll2 == 10 && roll1 != 10 && roll3 == 10)
{
System.out.println("| "+roll1+" | / | X |");
}
System.out.println("|---+---+---|");
if(score>=10 && score<=99)
{
System.out.println("| "+score+" |");
}
else if(score<10)
{
System.out.println("| "+score+" |");
}
else if(score>100)
{
System.out.println("| "+score+" |");
}
System.out.println("+-----------+");
System.out.println(" ");
}
}
Vou colocar a classe Driver que estou usando.
/**
* The driver for CSCI 111 Program 5; Bowling
*
* @author John Paxton
* @version March 9, 2013
*/
public class Driver
{
public static void main (String [] args)
{
Bowling game;
int [] rolls;
rolls = new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 4, 6, 3, 2, 7, 1, 8, 0, 9, 0, 0};
oneGame(rolls, 10); // bowl a game with 10 frames
rolls = new int[] {10, 10, 10, 10, 10, 10, 10};
oneGame(rolls, 5); // bowl a game with 5 frames
rolls = new int[] {6, 4, 5};
oneGame(rolls, 1); // bowl a game with 1 frame
rolls = new int[] {10, 2, 8};
oneGame(rolls, 1);
rolls = new int[] {2, 8, 10};
oneGame(rolls, 1);
rolls = new int[] {10, 2, 7};
oneGame(rolls, 1);
rolls = new int[] {10, 10, 2};
oneGame(rolls, 1);
}
private static void oneGame(int [] rolls, int frames)
{
Bowling game = new Bowling(rolls, frames);
System.out.println(">>>>> New Bowling Game <<<<<\n");
game.play();
}
}
Então assim fecho o tópico, todo o material necessário para simular a pontuação de um jogo de boliche esta aqui nesse topico, espero que seja de grande valia para alguém no futuro por que eu sofri um pouco para codificar. Até mais gente.