Register here: http://gg.gg/w5u8a
*Blackjack Game In Java
*Programming Blackjack In Java Programming
*Java Program Blackjack CodeGreenhorn
Java Classes Used: Logic for Drawing Cards: 1) Random class of Java: Cards in the game are drawn at random. For that I used the random class of Java. 2) Binding cards with numbers: This random Class of Java works on numbers so we would need to bind the images of cards with numbers. Download source files - 839 Kb; Download demo files - 953 Kb; Download Blackjack 2005 - 1,083.0 KB; Introduction. When I first posted this application back in 2002, I challenged everyone to improve it and provided a list of enhancements I thought were needed.posted 4 years agoI am trying to code a simple blackjack game with no suits or anything. Just for number values 1-10 between the dealer and the player. I have it mostly coded out yet, for some reason, when I prompt the system to ask if you want to play again via (y/n).. I type in ’y’ to play again and it will say I won - just by typing in y to play again! Its frustrating and if anyone could point me in the right direction it would be greatly appreciated.
import java.util.Scanner; import java.util.*; public class Blackjack { public static void main(String[] args) { String anotherCard, playAgain = ’y’, ctn = null; int nextCard, card1, card2, dCard1, dCard2; int cardTotal = 0, dTotal = 0; Scanner keyboard = new Scanner(System.in); Random random = new Random(); // Begin dealing the players first two cards while (’y’.equals(playAgain)) { //dealers first two random cards dCard1 = random.nextInt(10) + 1; dCard2 = random.nextInt(10) + 1; //players first two random cards and card total card1 = random.nextInt(10) + 1; card2 = random.nextInt(10) + 1; cardTotal = card1 + card2; //dealers two card total and display only one dealer card dTotal = dCard1 + dCard2; System.out.println(’Dealer shows: ’ + dCard1); //display players first two cards & card total System.out.println(’First Cards: ’ + card1 + ’, ’ + card2); System.out.println(’Total: ’+ cardTotal); System.out.println(’Another Card (y/n)?: ’); anotherCard = keyboard.nextLine(); while (’y’.equals(anotherCard)) { nextCard = random.nextInt(10) + 1; cardTotal += nextCard; System.out.println(’Card: ’ + nextCard); System.out.println(’Total: ’ + cardTotal); if (cardTotal > 21) { System.out.println(’You busted, Dealer wins’); System.out.println(’Do you want to play again? (y/n):’); playAgain = keyboard.nextLine(); } if (cardTotal < 21) System.out.println(’Another Card (y/n)?: ’); anotherCard = keyboard.nextLine(); if (’n’.equals(anotherCard)) System.out.println(’Dealer had: ’ + dTotal); System.out.println(’You had: ’ + cardTotal); while (’n’.equals(anotherCard)) { if (dTotal < cardTotal && cardTotal < 21) { System.out.println(’Yay you win!’); System.out.println(’Play again? (y/n): ’); playAgain = keyboard.nextLine(); if (playAgain.equalsIgnoreCase(’y’)) playAgain = ’y’; if (dTotal > cardTotal && dTotal < 21) System.out.println(’You lose!’); playAgain = keyboard.nextLine(); } } } } } } Sheriffposted 4 years ago
* 1Hi Mike,
Welcome to the Ranch!
When posting code, please UseCodeTags (←click that link to learn how). They make code so much easier to read and reference in replies because they provide line numbers to the listing. As a courtesy to a newcomer such as yourself, we will add those for you this time.
The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeSheriffposted 4 years ago
* 1A few things that led you to the place where you find yourself now: painted into a corner and wondering how the heck you’re going to get out of it.
1. You put everything in your main() method. We have a wiki page that explains why Main Is A Pain. You might want to read that if you want to avoid feeling that pain over and over again.
2. Because you have put all your code into the same method, you have amassed a huge amount of it. Lots of code in one method is BAD. Lots of code that does lots of things in one method is WORSE. Lots of code that does lots of things in loops that have been nested lots of times is the WORST. Wait for it..
3. Code that is not aligned properly to clearly show the different levels of logical structures of your code is the WORST of the WORST.
Ok, so there’s a lot of bad stuff going on in that code and I’ve even barely scratched the surface.
So you don’t feel too bad, let me just say that it’s a pleasant surprise to see someone use something like ’n’.equals(anotherCard). There are a couple things right about that little gem in the muck around it but just look at it for a little bit if you want to feel better.
Next reply, we’ll talk about what you can do to get out of that corner.

The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeSheriffposted 4 years ago
* 1So, there are at least two ways you can try to get yourself out of this sticky situation you’re in:
1. Hack your way out.
2. Clean your way out.
One way may get you out eventually but it’s likely to leave your code in a bigger mess than it’s in now.
The other way, of course, will get you out with your code in much better shape than what it’s in now.
You choose. The right way is not always the easiest way though.
The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeGreenhornposted 4 years agoThank you for the response, this is stressing me out to say the least. I’m gonna try to tidy it up to make it easier to understand and maybe that’ll help with trying to figure out what exactly is going on with my code. I will definitely take a read at the wiki page that you linked as to avoid this pain over and over again . Sheriffposted 4 years ago
* 1Well, the first thing you want to do is reformat your code so that it’s properly aligned. At least that will bump your code down from being the WORST of the WORST to just being the WORST.
In Eclipse or NetBeans, the keyboard command to autoformat your code is CTRL+SHIFT+F. That will give you something like the listing below; I added some blank lines to get separation between a few logical parts of the code (another favor.. you owe us TWO now):
import java.util.Scanner; import java.util.*; public class Blackjack { public static void main(String[] args) { String anotherCard, playAgain = ’y’, ctn = null; int nextCard, card1, card2, dCard1, dCard2; int cardTotal = 0, dTotal = 0; Scanner keyboard = new Scanner(System.in); Random random = new Random(); // Begin dealing the players first two cards while (’y’.equals(playAgain)) { // dealers first two random cards dCard1 = random.nextInt(10) + 1; dCard2 = random.nextInt(10) + 1; // players first two random cards and card total card1 = random.nextInt(10) + 1; card2 = random.nextInt(10) + 1; cardTotal = card1 + card2; // dealers two card total and display only one dealer card dTotal = dCard1 + dCard2; System.out.println(’Dealer shows: ’ + dCard1); // display players first two cards & card total System.out.println(’First Cards: ’ + card1 + ’, ’ + card2); System.out.println(’Total: ’ + cardTotal); System.out.println(’Another Card (y/n)?: ’); anotherCard = keyboard.nextLine(); while (’y’.equals(anotherCard)) { nextCard = random.nextInt(10) + 1; cardTotal += nextCard; System.out.println(’Card: ’ + nextCard); System.out.println(’Total: ’ + cardTotal); if (cardTotal > 21) { System.out.println(’You busted, Dealer wins’); System.out.println(’Do you want to play again? (y/n):’); playAgain = keyboard.nextLine(); } if (cardTotal < 21) System.out.println(’Another Card (y/n)?: ’); anotherCard = keyboard.nextLine(); if (’n’.equals(anotherCard)) System.out.println(’Dealer had: ’ + dTotal); System.out.println(’You had: ’ + cardTotal); while (’n’.equals(anotherCard)) { if (dTotal < cardTotal && cardTotal < 21) { System.out.println(’Yay you win!’); System.out.println(’Play again? (y/n): ’); playAgain = keyboard.nextLine(); if (playAgain.equalsIgnoreCase(’y’)) playAgain = ’y’; if (dTotal > cardTotal && dTotal < 21) System.out.println(’You lose!’); playAgain = keyboard.nextLine(); } } } } } }
The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeGreenhornposted 4 years agoSuch a simple command to format my entire code, gee that would’ve been nice to know.. professor. Ya think she would’ve taught us such a simple trick!Sheriffposted 4 years ago
mike laww wrote:Such a simple command to format my entire code, gee that would’ve been nice to know.. professor. Ya think she would’ve taught us such a simple trick!Blackjack Game In Java
Yeah, you’d think that would be one of the first things they teach but no. It’s one of the first things I taught my son when he enrolled in his first programming course in college last year.
Have you made your choice on which way you want to go? Hack your way out or Clean your way out? Choose wisely.. there is only one path down which I will be willing to go with you. Choose poorly and you’re on your own.

The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeGreenhornposted 4 years agoI believe the best decision would be to clean my way out, as I for sure don’t want to leave my code in a bigger mess than when it started. Sheriffposted 4 years ago
* 1Good deal. One more little detail to get out of the way. Have you been taught about using methods yet or is your teacher the one who’s been telling you to jam everything and the kitchen sink into the main() method? Because you’re going to have to create a bunch of smaller methods to separate, segregate, and compartmentalize all the different things that your program is doing right in that one main() method. That’s the goal. Instead of having to cook, eat, shower, attend to bodily functions, sleep, study, exercise, and entertain in the same room of your house, i.e. your main() method, you’re going to create a bunch of little ’rooms’ (called ’methods’) so that in each room, you can do one thing and only that one thing. So, however many ’things’ it is that you want to do, that’s how many ’rooms’ you’re going to create and that’s where you’re going to move all the clutter you have in main() out to. Does that make sense?
The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeGreenhornposted 4 years agoThis is my first programming class for my major so sadly she hasn’t really touched on methods. As much as you probably hate to hear it, she has just been having us cram everything into the main method so far, which after your description doesn’t seem like the logical way to do it. I really like the idea of having separate methods as it would be so much easier to organize thoughts and not get lost in your own code. Your example made it very easy to understand the concept. So, would it be organized by different data types? Like a method for loops, method for if statements, method for setting and getting variables, etc?lowercase babaposted 4 years ago
* 1your methods should NOT be for loops or if statements.
methods should be broken out by actions as described in English (or whatever language you speak). Imagine you were telling a story of what happens:
Deal a card to the player
Deal a card to the dealer
Deal a card to the player
Deal a card to the dealer
ask the player what they want to do
get their answer
etc.
you do this, then look for ’actions’. So, ’deal a card’ is something you DO. Note that it doesn’t matter who you are dealing to, the process is the same - select a card from what’s left in the deck. once you know what it is, you add it to the correct player’s hand..
Methods for getting and setting variables are OK - they’re sometimes called ’getters’ and ’setters’, or ’accessors’ and ’mutators’ (I think), but they are part of a class (like a Player class). I’m not sure if you’re going to start defining your own classes yet..

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errorsSheriffposted 4 years ago
* 1
mike laww wrote:So, would it be organized by different data types? Like a method for loops, method for if statements, method for setting and getting variables, etc?
As Fred said, your program is broken out by actions. I like to call them ’behaviors’ because I find that helps shift your mindset to a place where it’s more natural to think of the programs as living, sentient ’things’, i.e. Objects. You heard that Java is an object-oriented programming language, right?
Let’s not get ahead of ourselves yet though. Just think of it for now as something called ’functional decomposition’ - breaking down your huge problem into smaller, more manageable problems that focus on one small part of the larger whole.
The best ideas are the crazy ones. Program poker 3d. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeSheriffposted 4 years ago
* 1Also, think about it as a kind of layering and drilling down from high-level instructions to low-level detailed, step-by-step instructions. It’s kind of like how your mom might ask you to cook Thanksgiving dinner. That’s ’high-level’ - you don’t have a lot of details, just one command to do something. When you get home, you’re like ’Ok, I’m here, what do I do now?’ and Mom will say, ’Well, we want to prepare mashed potatoes, turkey, and bean casserole. Everybody else is bringing a different dish for dinner.’ So now you have a little more detail.
This gradual breaking down of the problem from high-level to more details, then to even more details as you drill down and finally arrive at the smallest task, like ’Mom, how do I peel the potatoes?’ and Mom will say, ’Go to the drawer next to the sink, pull it out, dig around for the peeler, make sure you wash the potatoes first, then get a trash bag ready, walk over to the island, set the potatoes down, then start peeling those taters!’ That level of detail would not have been appropriate during that first time that she asked you to prepare Thanksgiving dinner, right?
Mom held off giving you those details until just the right moment when you were ready to follow that many detailed instructions to a Tee. Giving you all of those detailed instructions up front would have been like the mess that you find right now in your main() method, right? All jumbled up and confusing as heck. How would you ever get dinner ready that way?!
That’s kind of like how you want to layer your program instructions. Start with the large one-liner command in the main() method. Ideally, it would look something like this:
play();
That’s it! And then, the play method would look something like this:
private static void play() { showInstructions(); do { initPlayer(); initGame(); playUntilSomebodyWins(); } while (playersWantToKeepGoing()); sayGoodbye(); }
And then you just keep drilling down into more details of each of the initPlayer(), initGame(), playUntilSomebodyWins(), and other methods you see there.
It’s not that complicated. It just takes a little bit of organization and logical thinking.
The best ideas are the crazy ones. If you have a crazy idea and it works, it’s really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your CodeProgramming Blackjack In Java Programming Metachecker.net|TrafficChecker.net|RankChecker.netLearn Java Script Today|Over 5000 Free Fonts|Tutorials|Javascript Forum|Other Javascript Resources|Cheat SheetJavaScript BasicsInserting JavascriptComplete TutorialAdvance DHTML TutorialScript archiveAdvanced TutorialsAlert ScriptsAnimationAudioBackground EffectsBanner AdsBrowser WindowButtonsButton FormsCalculcatorsCalendarsClocks & DatesCookiesCursor EffectsDHMTL GamesDHTML MiscellaneousEquivalentsFormsGamesIE4+ ScriptsIE5+ ScriptsImage EffectsImage MiscellaneousLinks & ButtonsMath RelatedMessages MiscellaneousMiscellaneousMouse TricksNavigationPage DetailsPassword ProtectionPulldown MenusRandom StuffScrollingStatus BarText AnimationUser DetailUser InfoWindow Control
Basic Javascript games code

Here are games made with javascript. If you are looking for simple games to code, or if you are looking for spesifc game like simon game in javascript this is the place where you can find all javascript game codes. JavaScript game Five in a rowCompatibility: IE, Firefox, Chrome, SafariDescription:
This is a simple javascript game to code. Your goal in Five-in-a-row is to get five X’s in a row while preventing your opponent from getting five O’s in a row. In this free JavaScript basic game, it is played on a 15x15 board. (If you’d like a bigger or smaller board, you can set it to any size from 10x10 through 20x20.) You play X’s, and the computer plays O’s. First move is yours. Enjoy!Ouths and Crosses (Version of Tic-Tac-Toe)Compatibility: IE, Firefox, Chrome, SafariDescription:
If you are learning javascript game coding this is the game to see. This javascript game with source code is called Ouths and Crosses. It is a very simple version or tic-tac-toe made in javascript. Try to beat computer…Javascript game Mine SweeperCompatibility: IE, Firefox, Chrome, SafariDescription:
A well-known windows game realized on java-script. An incredible similarity with an original! Customize the area you are ready to clear of mine and go!Javascript guessing game Who? What? Where?Compatibility: IE, Firefox, Chrome, SafariDescription:
This game made with javascript is a perfect example of how to make a simple game in javascript. This is a famous javascript guessing game. The computer guessess your thoughts which inevidably are: denmark, gray elephants and orange kangaroos.Simple javascript code Where Born?Compatibility: IE, Firefox, Chrome, SafariDescription:
This javascript game is another guessing game. If yo uare learning to make games in javascript this is a very simple game with example code. Enter the first 3 digits of your SSN and the script will tell you where you were born. Go ahead - see if JavaScript can tell you where you were born.Javascript Typing TestCompatibility: IE, Firefox, Chrome, SafariDescription:
Javascript typing game source code. I really think this is neat - JavaScript will actually give you a typing test and then tell you the results in words per minute! Very neat!Tower of Hanoi javascript gameCompatibility: IE, Firefox, Chrome, SafariDescription:
Its one thing to play tower of hanoi

https://diarynote-jp.indered.space

コメント

最新の日記 一覧

<<  2025年7月  >>
293012345
6789101112
13141516171819
20212223242526
272829303112

お気に入り日記の更新

テーマ別日記一覧

まだテーマがありません

この日記について

日記内を検索