|
| 1 | +package java_craps; |
| 2 | +import java.util.*; |
| 3 | +public class methodOverloadExample { |
| 4 | + |
| 5 | + public static void main(String[] args) { |
| 6 | + // first turn starts here |
| 7 | + int players; |
| 8 | + Scanner input = new Scanner(System.in); |
| 9 | + |
| 10 | + // resume next round loop |
| 11 | + |
| 12 | + |
| 13 | + String nextRound = "yes"; |
| 14 | + do |
| 15 | + { |
| 16 | + for(int roundpos = 0; roundpos >= 0; roundpos++) |
| 17 | + { |
| 18 | + //nextRound = ""; |
| 19 | + if(roundpos == 0) |
| 20 | + { |
| 21 | + System.out.println("Would you like to play this round? Enter \"yes\" to play, and \"no\" to quit."); |
| 22 | + nextRound = input.nextLine(); |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + System.out.println("Would you like to play again? Enter \"yes\" to play again, and \"no\" to quit."); |
| 27 | + nextRound = input.nextLine(); |
| 28 | + } |
| 29 | + if (nextRound.equalsIgnoreCase("yes")) |
| 30 | + { |
| 31 | + |
| 32 | + do |
| 33 | + { |
| 34 | + System.out.print("Welcome to the craps table! How many players will be playing? (maximum of 6) "); |
| 35 | + |
| 36 | + players = input.nextInt(); |
| 37 | + } |
| 38 | + while (players > 6); |
| 39 | + |
| 40 | + |
| 41 | + double[] firstBet = new double[players]; |
| 42 | + for(int pos = 0; pos < firstBet.length; pos ++) |
| 43 | + { |
| 44 | + System.out.print("Player " + (pos +1) + ", how much would you like to bet? "); |
| 45 | + firstBet[pos] = input.nextDouble(); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + System.out.print("Which player will be rolling the dice? "); |
| 51 | + int playerTurn = input.nextInt(); |
| 52 | + System.out.print("Player " + playerTurn + ", please enter \"roll\" to roll the dice. "); |
| 53 | + String discard = input.next(); |
| 54 | + System.out.println(); |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + Random rand = new Random(); |
| 59 | + int die1 = rand.nextInt(6) + 1; |
| 60 | + int die2 = rand.nextInt(6) + 1; |
| 61 | + int sumPassRound = die1 + die2; |
| 62 | + System.out.println("You rolled: " + die1 + " + " + die2 + " = " + sumPassRound); |
| 63 | + |
| 64 | + // sumPassRound = 8; |
| 65 | + System.out.println("You rolled " + sumPassRound); |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + if(sumPassRound == 7 || sumPassRound == 11) |
| 70 | + { |
| 71 | + System.out.println("Congratulations! You won."); |
| 72 | + for(int pos = 0; pos < firstBet.length; pos++) |
| 73 | + { |
| 74 | + System.out.println("Player " + (pos+1) + ", you won $" + firstBet[pos] + "!"); |
| 75 | + } |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + if(sumPassRound == 2 || sumPassRound == 3 || sumPassRound == 12) |
| 80 | + { |
| 81 | + System.out.println("Sorry, you have lost."); |
| 82 | + for(int pos = 0; pos < firstBet.length; pos++) |
| 83 | + { |
| 84 | + System.out.println("Player " + (pos+1) + ", you have lost $" + firstBet[pos] + "."); |
| 85 | + } |
| 86 | + |
| 87 | + System.exit(0); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + System.out.println("You rolled " + sumPassRound + ". This is your point value as we proceed to the point round."); |
| 92 | + System.out.println(); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + double[] secondBet = new double[players]; |
| 98 | + for(int pos = 0; pos < secondBet.length; pos++) |
| 99 | + { |
| 100 | + secondBet[pos] = 0; |
| 101 | + } |
| 102 | + char[] forOrAgainst = new char[players]; |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + for(int pos = 0; pos < secondBet.length; pos ++) |
| 107 | + { |
| 108 | + System.out.print("Player " + (pos +1) + ", how much would you like to bet in the point round? (enter 0 if not) "); |
| 109 | + secondBet[pos] = secondBet[pos] + input.nextDouble(); |
| 110 | + System.out.print("Player " + (pos +1) + ", would you like to bet for or against the point value? Enter 'f' for for and 'a' for against. "); |
| 111 | + forOrAgainst[pos] = input.next().charAt(0); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + System.out.println(); |
| 117 | + int sumPointRound = 0; |
| 118 | + int die3; |
| 119 | + int die4; |
| 120 | + double[] odds = new double[11]; //{2,1.5,1.2,1.2,1.5,2} |
| 121 | + odds[0] = 1; //odds on a 2 |
| 122 | + odds[1] = 1; //odds on a 3 |
| 123 | + odds[2] = 2; //odds on a 4 |
| 124 | + odds[3] = 1.5; //odds on a 5 |
| 125 | + odds[4] = 1.2; //odds on a 6 |
| 126 | + odds[5] = 1; //odds on a 7 |
| 127 | + odds[6] = 1.2; //odds on an 8 |
| 128 | + odds[7] = 1.5; //odds on a 9 |
| 129 | + odds[8] = 2; //odds on a 10 |
| 130 | + odds[9] = 1; //odds on an 11 |
| 131 | + odds[10] = 1; //odds on a 12 |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + while (sumPointRound != 7 && sumPointRound != sumPassRound) |
| 136 | + { |
| 137 | + die3 = rand.nextInt(5) + 1; |
| 138 | + die4 = rand.nextInt(5) + 1; |
| 139 | + sumPointRound = die3 + die4; |
| 140 | + System.out.println("You rolled: " + die3 + " + " + die4 + " = " + sumPointRound); |
| 141 | + } |
| 142 | + //sumPointRound = 8; |
| 143 | + if(sumPointRound == 7) |
| 144 | + { |
| 145 | + System.out.println("If you bet for the point, you have lost. If you bet against the point, you have won!"); |
| 146 | + for(int pos = 0; pos < secondBet.length; pos++) |
| 147 | + { |
| 148 | + if(forOrAgainst[pos] == 'f') |
| 149 | + { |
| 150 | + System.out.println("Player " + (pos+1) + ", you have lost $" + (firstBet[pos] + secondBet[pos]) + "."); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + System.out.println("Player " + (pos+1) + ", you have won $" + (firstBet[pos] + (secondBet[pos] * |
| 155 | + (1/(odds[sumPassRound - 2])))) + "! This is less than the amount " |
| 156 | + + "you bet because of the odds of rolling each different number."); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + if(sumPointRound == sumPassRound) |
| 161 | + { |
| 162 | + System.out.println("If you bet for the point, you have won! If you bet against the point, you have lost."); |
| 163 | + for(int pos = 0; pos < secondBet.length; pos++) |
| 164 | + { |
| 165 | + if(forOrAgainst[pos] == 'a') |
| 166 | + { |
| 167 | + System.out.println("Player " + (pos+1) + ", you have lost $" + (firstBet[pos] + secondBet[pos]) + "."); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + System.out.println("Player " + (pos+1) + ", you have won $" + (firstBet[pos] + |
| 172 | + (secondBet[pos] * odds[sumPointRound-2])) + "! This is more than the amount you bet because " |
| 173 | + + "of the odds of rolling each different number."); |
| 174 | + |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } while(!nextRound.equalsIgnoreCase("no")); |
| 182 | + } |
| 183 | + } |
0 commit comments