Card Game
berikut ini merupakan game card sederhana dengan java:
Class Game:
Class Play:
Class Game:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | import java.util.Scanner; import java.util.Random; import java.util.concurrent.TimeUnit; /** * Gameplay or game flow. * * */ public class Game { private final String[] sym = new String[] { "♤", "♥", "♢", "♧" }; private final String[] val = new String[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; private int ansl, ansr, nlft, nrgt, ans, gss; private int score; public Game() { score = 0; ansl = 0; ansr = 0; nlft = 0; nrgt = 0; gss = 0; ans = 0; System.out.println("Shuffling Cards... Please Wait..."); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException ie) { System.out.println("\n"); } System.out.println("Here's your mystery card:"); nlft = getCardNum(); nrgt = getCardNum(); String numleft = val[nlft]; String numright = val[nrgt]; String symleft = sym[getCardSym()]; String symright = sym[getCardSym()]; printCard("L", "R", " ", " ", 0, 0); play(); System.out.println("Your cards were:"); printCard(numleft, numright, symleft, symright, nlft+1, nrgt+1); } public int check() { if (gss == 1) { int a = ansl%2; int b = (nlft + 1)%2; if (a == 0 && b == 0) { score = score + 3; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 3 points."); } else if ( a != 0 && b !=0 ) { score = score + 3; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 3 points."); } else { score = score - 1; System.out.println("Ow! You guessed wrongly."); System.out.println("You got -1 points."); System.out.println("\nBut don't worry, You can play again."); } } else if (gss == 2) { int a = ansr%2; int b = (nrgt + 1)%2; if (a == 0 && b == 0) { score = score + 3; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 3 points."); } else if ( a != 0 && b !=0 ) { score = score + 3; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 3 points."); } else { score = score - 1; System.out.println("Ow! You guessed wrongly."); System.out.println("You got -1 points."); System.out.println("\nBut don't worry, You can play again."); } } else if (gss == 3) { int a = ansl%2; int b = (nlft + 1)%2; int c = ansr%2; int d = (nrgt + 1)%2; if (((a == 0 && b == 0) || (a != 0 && b != 0)) && ((c == 0 && d == 0) || (c != 0 && d != 0))) { score = score + 8; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 8 points."); } else { score = score - 4; System.out.println("Ow! You guessed wrongly."); System.out.println("You got -4 points."); System.out.println("\nBut don't worry, You can play again."); } } else if (gss == 4) { int a = (ansl + ansr)%2; int b = (nlft + nrgt + 2)%2; if (a == 0 && b == 0) { score = score + 6; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 6 points."); } else if ( a != 0 && b !=0 ) { score = score + 6; System.out.println("Congratulation! You guessed it right."); System.out.println("You got 6 points."); } else { score = score - 3; System.out.println("Ow! You guessed wrongly."); System.out.println("You got -3 points."); System.out.println("\nBut don't worry, You can play again."); } } else { score = score; } return score; } public void play() { System.out.println("Wanna Guess?"); System.out.println("1. Left card only."); System.out.println("2. Right card only."); System.out.println("3. Left and right card."); System.out.println("4. Addition of both cards."); System.out.println("Or insert number other than 1 - 4 to cancel the game."); Scanner sc = new Scanner(System.in); gss = sc.nextInt(); switch(gss) { case 1: System.out.println("Left Card is: "); System.out.println("(insert 1 = Odd or 2 = Even)"); ansl = sc.nextInt(); break; case 2: System.out.println("Right Card is: "); System.out.println("(insert 1 = Odd or 2 = Even)"); ansr = sc.nextInt(); break; case 3: System.out.println("Left Card is: "); System.out.println("(insert 1 = Odd or 2 = Even)"); ansl = sc.nextInt(); System.out.println("Right Card is: "); System.out.println("(insert 1 = Odd or 2 = Even)"); ansr = sc.nextInt(); break; case 4: System.out.println("Left + Right Card Number is: "); System.out.println("(insert 1 = Odd or 2 = Even)"); ansl = sc.nextInt(); ans = ansr + ansl; break; default: System.out.println("You have choosen to cancel the game."); break; } } public void printCard(String numleft, String numright, String symleft, String symright, int num1, int num2) { if (num1 != 10 && num2 != 10) { System.out.println("_____________ _____________"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| "+numleft+" | | "+numright+" |"); System.out.println("| "+symleft+" | | "+symright+" |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("|___________| |___________|\n"); } else if (num1 == 10 && num2 != 10) { System.out.println("______________ _____________"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| "+numleft+" | | "+numright+" |"); System.out.println("| "+symleft+" | | "+symright+" |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("|____________| |___________|\n"); } else if (num1 != 10 && num2 == 10) { System.out.println("_____________ ______________"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| "+numleft+" | | "+numright+" |"); System.out.println("| "+symleft+" | | "+symright+" |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("|___________| |____________|\n"); } else { System.out.println("______________ ______________"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("| "+numleft+" | | "+numright+" |"); System.out.println("| "+symleft+" | | "+symright+" |"); System.out.println("| | | |"); System.out.println("| | | |"); System.out.println("|____________| |____________|\n"); } } public int getCardNum() { Random rnd = new Random(); int num = rnd.nextInt(12); return num; } public int getCardSym() { Random rnd = new Random(); int sym = rnd.nextInt(3); return sym; } } |
Class Play:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.util.Scanner; /** * main program or interface. * * */ public class Play { public static void main(String[] args) { int totalScore = 0;; System.out.println("Welcome to The Game of Odds or Evens"); System.out.println("Instruction:"); System.out.println("1. You will be given 2 random cards,"); System.out.println(" named LEFT card and RIGHT card."); System.out.println("2. The cards are set upside down."); System.out.println("3. You should guess whether those two"); System.out.println(" cards or their sum result are odd or"); System.out.println(" even number."); System.out.println("4. Note that Ace is equal to 1, Jack"); System.out.println(" is equal to 11, Queen is equal to 12,"); System.out.println(" and King is equal to 13."); System.out.println("5. Guessing score details: "); System.out.println(" Left card only, true = 3 pts, false = -1 pts."); System.out.println(" Right card only, true = 3 pts, false = -1 pts."); System.out.println(" Left and right card, true = 8 pts, false = -4 pts."); System.out.println(" Addition of both cards, true = 6 pts, false = -3 pts.\n"); System.out.println("Want to start the game?"); System.out.println("Insert y (yes) or n (no)"); int t = 0; while( t == 0) { Scanner sc = new Scanner(System.in); char yn = sc.next().charAt(0); if (yn == 'y') { t = 1; Game g = new Game(); totalScore= totalScore + (g.check()); int t2 = 0; while(t2 == 0) { System.out.println("\nContiune Playing?"); System.out.println("Insert y (yes) or n (no)"); char yn2 = sc.next().charAt(0); if (yn2 == 'y') { Game gg = new Game(); totalScore= totalScore + (gg.check()); } else if (yn2 == 'n') { t2 = 1; System.out.println("Your score accumulation: " +totalScore); System.out.println("Thanks for Playing"); } else { System.out.println("\nPlease insert y or n only."); } } } else if (yn == 'n') { t = 1; System.out.println("~See You~"); } else { System.out.println("\nPlease insert y or n only."); } } } } |
Komentar
Posting Komentar