5.11 Project
import java.util.*;
public class Arithmetic {
static final int NUMBER_OF_ROUNDS = 7;
static final int NUMBER_OF_DICE = 5;
static final int NUM_REROLLS = 2;
static int num_chances = 1;
public static void main(String[] args) {
int score = 0;
int[] dice = new int[NUMBER_OF_DICE];
Scanner keyboard = new Scanner(System.in);
Scanner keyboard2 = new Scanner(System.in);
for (int x = 0; x < NUMBER_OF_ROUNDS; x++) {// round
dice = rollDice();
System.out.println(diceToString(dice));
for (int y = 0; y < NUM_REROLLS; y++) {// re-roll
System.out.print("Do you want to re-roll (Y/N): ");
char c = keyboard.next().toLowerCase().charAt(0);
if (c == 'y') {
System.out.print("Which dice do you want to reroll: ");
String s = keyboard2.nextLine();
rollDice(convert(s), dice);// re-roll selected dice
System.out.println(diceToString(dice));// print dice
} else
break;
}
score = score + scoreDice(dice);
}
System.out.println("You scored " + score + " points this game.");
}
public static int scoreDice(int[] dice) {
int score = 0;
score = ScoreYahtzee(dice);
if (score > 0) {
System.out
.println("Congratulations, you got Yahtzee. That counts for "
+ score + " points!");
return score;
}
score = scoreFourOfAKind(dice);
if (score > 0) {
System.out
.println("Congratulations, you got Four of a Kind. That counts for "
+ score + " points!");
return score;
}
score = scoreLargeStraight(dice);
if (score > 0) {
System.out
.println("Congratulations, you got a Large Straight. That counts for "
+ score + " points!");
return score;
}
score = scoreFullHouse(dice);
if (score > 0) {
System.out
.println("Congratulations, you got a Full House. That counts for "
+ score + " points!");
return score;
}
score = scoreThreeOfAKind(dice);
if (score > 0) {
System.out
.println("Congratulations, you got Three of a Kind. That counts for "
+ score + " points!");
return score;
}
score = scoreSmallStraight(dice);
if (score > 0) {
System.out
.println("Congratulations, you got a Small Straight. That counts for "
+ score + " points!");
return score;
}
score = scoreChance(dice);
if (score > 0) {
System.out
.println("Congratulations, you got a chance. That counts for "
+ score + " points!");
return score;
}
System.out.println("I'm sorry, you did not get any points this round.");
return score;
}
public static String diceToString(int[] dice) {
String result = "Your dice are: ";
for (int el : dice) {
result += el + " ";
}
return result;
}
public static int ScoreYahtzee(int[] dice) {
if (isYahtzee(dice)) {
return 50;
} else
return 0;
}
public static boolean isYahtzee(int[] dice) {
for (int el : dice) {
if (el != dice[0]) {
return false;
}
}
return true;
}
public static int scoreFourOfAKind(int[] dice) {
for (int i = 0; i < dice.length; i++) {
int x = 0;
for (int j = 0; j < dice.length; j++) {
if (dice[i] == dice[j])
x++;
}
if (x == 4)
return sumAllDice(dice);
else
x = 0;
}
return 0;
}
public static int[] bubbleSort(int[] dice) {
for (int n = 0; n < dice.length - 1; n++) {
for (int i = 0; i < dice.length - 1; i++) {
if (dice[i] > dice[i + 1]) {
int temp = dice[i];
dice[i] = dice[i + 1];
dice[i + 1] = temp;
}
}
}
return dice;
}
public static int scoreLargeStraight(int[] dice) {
dice = bubbleSort(dice);
for (int i = 0; i < dice.length - 1; i++) {
if (dice[i] != dice[i + 1] - 1)
return 0;
}
return 40;
}
public static int scoreFullHouse(int[] dice) {
dice = bubbleSort(dice);
if ((dice[0] == dice[1]) && (dice[2] == dice[3] && dice[3] == dice[4]))
return 25;
if ((dice[3] == dice[4]) && (dice[0] == dice[1] && dice[1] == dice[2]))
return 25;
return 0;
}
public static int scoreThreeOfAKind(int[] dice) {
dice = bubbleSort(dice);
for (int i = 0; i < 3; i++) {
if (dice[i] == dice[i + 1] && dice[i + 1] == dice[i + 2])
return sumAllDice(dice);
}
return 0;
}
public static int scoreSmallStraight(int[] dice) {
dice = bubbleSort(dice);
if ((dice[0] == dice[1] - 1) && (dice[1] == dice[2] - 1)
&& (dice[2] == dice[3] - 1))
return 30;
if ((dice[3] == dice[4] - 1) && (dice[1] == dice[2] - 1)
&& (dice[2] == dice[3] - 1))
return 30;
return 0;
}
public static int scoreChance(int[] dice) {
if (num_chances == 0)
return 0;
num_chances--;
return sumAllDice(dice);
}
public static int sumAllDice(int[] dice) {
int sum = 0;
for (int i = 0; i < dice.length; i++) {
sum = sum + dice[i];
}
return sum;
}
public static int[] rollDice() {
int[] d = new int[NUMBER_OF_DICE];
for (int i = 0; i < NUMBER_OF_DICE; i++) {
d[i] = getRandomDieValue();
}
return d;
}
public static void rollDice(int[] diceToChange, int[] dice) {
for (int i : diceToChange) {
dice[i - 1] = getRandomDieValue();
}
}
public static int getRandomDieValue() {
return (int) (Math.random() * 6 + 1);
}
public static int[] convert(String s) {
StringTokenizer st = new StringTokenizer(s);
int[] a = new int[st.countTokens()];
int i = 0;
while (st.hasMoreTokens()) {
a[i++] = Integer.parseInt(st.nextToken());
}
return a;
}
}
Thursday, November 13, 2014
Saturday, November 8, 2014
Bubble sort
5.10 Lab Write a method that sorts an array of integers using bubble sort. The method can use an
auxiliary method that swaps two entries in the array. Use a nested for loop to implement
the bubble sort. The method should modify the input array and have void return type.
Test your method with different inputs.
public class Arithmetic {
static int[] array = {10,7,6,3,2,1};
public static void main(String[] args) {
print();
bubbleSort();
}
public static void bubbleSort(){
for (int n = 0; n<array.length-1; n++)
{
for (int i = 0; i<array.length-1; i++)
{
if (array[i]>array[i+1])
{
swap(i);
}
}
}
}
public static void swap(int x){
int temp = array[x];
array[x] = array[x+1];
array[x+1] = temp;
print();
}
public static void print(){
String output = "";
for (int i = 0; i<array.length; i++)
{
output = output + array[i] +" ";
}
System.out.println(output);
}
}
auxiliary method that swaps two entries in the array. Use a nested for loop to implement
the bubble sort. The method should modify the input array and have void return type.
Test your method with different inputs.
public class Arithmetic {
static int[] array = {10,7,6,3,2,1};
public static void main(String[] args) {
print();
bubbleSort();
}
public static void bubbleSort(){
for (int n = 0; n<array.length-1; n++)
{
for (int i = 0; i<array.length-1; i++)
{
if (array[i]>array[i+1])
{
swap(i);
}
}
}
}
public static void swap(int x){
int temp = array[x];
array[x] = array[x+1];
array[x+1] = temp;
print();
}
public static void print(){
String output = "";
for (int i = 0; i<array.length; i++)
{
output = output + array[i] +" ";
}
System.out.println(output);
}
Subscribe to:
Comments (Atom)
