473,769 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a few problems I am having with a card game I made...

13 New Member
Hello all, awesome site! I guess I am technically not a beginner in JAVA, but from my code you would not realize it! I don’t expect anyone to help me with this, but I figure I might as well as try and ask. Any help is really appreciated; this should be a piece of cake…driving me crazy.

I am writing a simple program (from a book for fun) that creates a deck of cards, shuffles them, gives the user a card, and asks if the next card will be higher or lower. Then, I average all the wins or correct guesses made with the number of games played at the end. This only utilizes one class Cards1, in order to make a card object with suit and value properties. Ignore the horrible structure of the program; I am just trying to figure out why I am getting the errors I am getting following the program’s logic…

Problems occuring:
1. Every time I compile I always get the one of Spades as the first card, even though I shuffle the deck.
2. The next card is always either a two of spades or one of spades
3. when I guess wrong and prompted to play again, if I select no then the while loop I use is broken and it tells me how many games won lost or whatever, excellent, but when I say yes to play again…nothing happens, it does not loop back and start the game over, lol, I really don’t know why…so frustrating…
Expand|Select|Wrap|Line Numbers
  1. public class Text1{
  2.  
  3.     public static void main(String[] args){
  4.         play();
  5.  
  6.     }
  7.  
  8.     static void play(){
  9.  
  10.         int deckCount = 0;
  11.         int average = 0;
  12.         int wins = 0;
  13.         int gamesPlayed = 0;
  14.         boolean continuePlay = true;
  15.         boolean continueGame = true;
  16.         boolean playAgainB = true;
  17.  
  18.  
  19.         while (continuePlay){
  20.  
  21.             //make deck 
  22.             Card1[] deck; 
  23.             deck = new Card1[52];
  24.             int tempcounter = 0;
  25.             //suits and values
  26.             for(int suit = 0;suit < 3;suit++){
  27.  
  28.                 for(int value = 1;value < 13;value++){
  29.  
  30.                 deck[tempcounter] = new Card1(suit, value); 
  31.                 tempcounter++;
  32.  
  33.                 }//deck made
  34.             }
  35.  
  36.             Card1 aCard, bCard;
  37.  
  38.             //now you need to shuffle
  39.             for(int x = 51; x > 0; x--){
  40.  
  41.                 int random = (int)(Math.random() * (x+1));
  42.                 deck[x] = deck[random];
  43.                 deck[random] = deck[x];
  44.  
  45.             }
  46.  
  47.  
  48.             while (continueGame){
  49.  
  50.                 aCard = deck[deckCount];
  51.                 bCard = deck[deckCount + 1];
  52.  
  53.                 TextIO.putln("Your card is:");
  54.                 TextIO.putln("a " + aCard.switchValue() + " of " + aCard.switchSuit());
  55.                 int previousCard = aCard.getValue();
  56.                 int nextCard = bCard.getValue();
  57.  
  58.  
  59.             while(true){
  60.                 TextIO.putln("Will the next card be higher or lower?!?!? Type H or L for higher or lower");
  61.                 char answer = TextIO.getlnChar();
  62.  
  63.                 char answerToUp = Character.toUpperCase(answer);
  64.  
  65.                 if (answerToUp == 'H'){
  66.  
  67.                     if (previousCard < nextCard){
  68.  
  69.                         TextIO.putln("Your right! The next card is a " + bCard.switchValue() + " of " + bCard.switchSuit());
  70.                         wins++;
  71.                         continueGame = true;
  72.                         break;
  73.                     }
  74.  
  75.                     else if (previousCard > nextCard){
  76.  
  77.                         TextIO.putln("Your wrong! The next card is a " + bCard.switchValue() + " of " + bCard.switchSuit());
  78.                         continueGame = false;
  79.                         break;
  80.                     }
  81.  
  82.                     else {
  83.                         TextIO.putln("Your wrong! A tie! The next card is a " + bCard.switchValue() + " of " + bCard.switchSuit());
  84.                         continueGame = false;
  85.                         break;
  86.                     }
  87.  
  88.  
  89.                 }
  90.  
  91.                 else if (answerToUp == 'L'){
  92.  
  93.                     if (previousCard > nextCard){
  94.  
  95.                         TextIO.putln("Your right! The next card is a " + bCard.switchValue() + " of " + bCard.switchSuit());
  96.                         wins++;
  97.                         continueGame = true;
  98.                         break;
  99.                     }
  100.  
  101.                     else if (previousCard < nextCard){
  102.  
  103.                         TextIO.putln("Your wrong! The next card is a " + bCard.switchValue() + " of " + bCard.switchSuit());
  104.                         continueGame = false;
  105.                         break;
  106.                     }
  107.  
  108.                     else {
  109.                         TextIO.putln("Your wrong! A tie! The next card is a " + bCard.switchValue() + " of " + bCard.switchSuit());
  110.                         continueGame = false;
  111.                         break;            
  112.                     }
  113.  
  114.  
  115.                 }
  116.  
  117.                 else
  118.                     TextIO.putln("your gonna have to type in like a H or a L...");
  119.  
  120.             }//end high/low question while
  121.  
  122.             deckCount++;
  123.  
  124.             }//end continueGame while
  125.  
  126.             gamesPlayed++;
  127.  
  128.             while (playAgainB){    
  129.                 TextIO.putln("awwwww too bad...wanna play again? Type y or n for yes or no!");
  130.                 char playAgain = TextIO.getlnChar();
  131.                 char playAgainUp = Character.toUpperCase(playAgain);
  132.  
  133.                 if (playAgainUp == 'Y')
  134.                     playAgainB = false;
  135.  
  136.                 else if (playAgainUp == 'N'){
  137.                     continuePlay = false;    
  138.                     playAgainB = false;        
  139.                 }
  140.  
  141.                 else
  142.                     TextIO.putln("your gonna have to type in like a y or a n...");
  143.             }//end playagain while
  144.  
  145.         }//end continueplay while
  146.  
  147.         if (wins > 0){
  148.             average = wins/gamesPlayed;
  149.             TextIO.putln("you played " + gamesPlayed + " games and got like " + wins + " wins, so your average is like:" + average);
  150.         }
  151.         else 
  152.             TextIO.putln("you played " + gamesPlayed + " games and got like " + wins + " wins");
  153.     }//end play method
  154. }//end class Text1
  155.  
  156.  
  157. public class Card1{
  158.  
  159.  
  160.     int Suit;
  161.     int Value;
  162.  
  163.     public Card1(int suit, int value){
  164.  
  165.     Suit = suit;
  166.  
  167.     Value = value;
  168.  
  169.     }
  170.  
  171.     //method switch suit    
  172.     public String switchSuit(){
  173.  
  174.         switch (Suit) {
  175.  
  176.         case 0: return "Spades";
  177.         case 1: return "Clubs";
  178.         case 2: return "Diamonds";
  179.         case 3: return "Hearts";
  180.         default: return "??";
  181.  
  182.         }
  183.  
  184.     }//end method switch suit
  185.  
  186.     //method switch value
  187.     public String switchValue(){
  188.  
  189.         switch (Value) {
  190.  
  191.         case 1: return "1";
  192.         case 2: return "2";
  193.         case 3: return "3";
  194.         case 4: return "4";
  195.         case 5: return "5";
  196.         case 6: return "6";
  197.         case 7: return "7";
  198.         case 8: return "8";
  199.         case 9: return "9";
  200.         case 10: return "10";
  201.         case 11: return "jack";
  202.         case 12: return "queen";
  203.         case 13: return "king";
  204.         default: return "??";
  205.  
  206.         }    
  207.  
  208.     }//end method switch value
  209.  
  210.     public int getValue(){
  211.  
  212.         return Value;
  213.  
  214.     }
  215.  
  216.  
  217. }//end public class card
  218.  
Mar 11 '07 #1
6 1890
horace1
1,510 Recognized Expert Top Contributor
think in your shuffle you need to swop the cards, e.g.
Expand|Select|Wrap|Line Numbers
  1.                      //now you need to shuffle
  2.                         for(int x = 51; x >= 0; x--){
  3.                                 int random = (int)(Math.random() * (x+1));
  4.                                  Card1 temp=deck[x];
  5.                                 deck[x] = deck[random];
  6.                                 deck[random] = temp;
  7.                         }
  8.  
Mar 11 '07 #2
JNeko
13 New Member
think in your shuffle you need to swop the cards, e.g.
Expand|Select|Wrap|Line Numbers
  1.                      //now you need to shuffle
  2.                         for(int x = 51; x >= 0; x--){
  3.                                 int random = (int)(Math.random() * (x+1));
  4.                                  Card1 temp=deck[x];
  5.                                 deck[x] = deck[random];
  6.                                 deck[random] = temp;
  7.                         }
  8.  
excellent! thanks for the above code and the quick reply! I actually knew of that code but thought it performed the same functions as my original code, looking at it again though, uggghh, anyway, of course a new problem has come up and it's just as confusing to me. Now when i run the program with the new code, I sometimes* get a nullpoint at
TextIO.putln("a " + aCard.switchVal ue() + " of " + aCard.switchSui t());

which means I guess that the program is not finding a Card1 object at the deck[deckCount] (deckCount initially at 0), which is quite interesting because I could have sworn I just a made an array full of cards a few lines above this...

It really is weird, because sometimes it will run fine and display the first card! Then, like the other problem stated in my original post, once I get to the do you want to play again part and I select Y, it just sits there and does nothing...but first thing is first...

anyone know why this thing is giving me a nullpoint exception error? Thanks again!
Mar 11 '07 #3
horace1
1,510 Recognized Expert Top Contributor
think you are only initialising 3 suits and 12 cards. Should the initialisation be?
Expand|Select|Wrap|Line Numbers
  1.                         for(int suit = 0;suit < 4;suit++){
  2.                                 for(int value = 1;value < 14;value++){
  3.                                 deck[tempcounter] = new Card1(suit, value); 
  4.                                 tempcounter++;
  5.                                 }//deck made
  6.  
Mar 11 '07 #4
JNeko
13 New Member
think you are only initialising 3 suits and 12 cards. Should the initialisation be?
Expand|Select|Wrap|Line Numbers
  1.                         for(int suit = 0;suit < 4;suit++){
  2.                                 for(int value = 1;value < 14;value++){
  3.                                 deck[tempcounter] = new Card1(suit, value); 
  4.                                 tempcounter++;
  5.                                 }//deck made
  6.  
o....lord.....t hat would explain the nullpoint error, it's kewl to know that I wasted a few good hours because I forgot to add a freaking = sign after the <....nothing out of the ordinary for a JAVA programmer I guess...thanks so much horace1!

********JUST GOT IT**********
I just typed a really long message, asking the next question: why the heck is the game not looping back through once the player asks to play again. And after going through the program about the 80th time, realised I forgot to change my playAgainB and freaking continueGame booleans back to true at the beginning of the first while loop!! Why am I so freaking retarded?!?!?

But it works now..thanks again horace1...now to move on to reviewing superclasses... .this site is awesome!
Mar 11 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Glad to see you got the help.

As far as the simple mistakes, don't sweat it! In fact, treasure the easily explained errors! I've been recently getting a few errors in my C++ code that even my teacher and I double-teamed cannot figure out.
Mar 11 '07 #6
JNeko
13 New Member
Glad to see you got the help.

As far as the simple mistakes, don't sweat it! In fact, treasure the easily explained errors! I've been recently getting a few errors in my C++ code that even my teacher and I double-teamed cannot figure out.
>< ugghh, i know i know, dont tell me that, lol, im sure i will run into that later, scary stuff
Mar 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2629
by: Sam | last post by:
Hey all, I want to create a computerised version of this game, though I'm not really sure how to go about it. For those who don't know how the game works, here's my attempt at a brief description: It is very similar to the card game Uno, though played with a standard deck of cards. An initial card is played off the top of the deck, and then each player has to play a card of the same suit, or the same face value in another suit....
1
2690
by: Gary Camblin | last post by:
Has anyone got a script to run a higher or lower card game on a web page. Like the game show play you cards right.
7
3097
by: Ken Smith | last post by:
I have a little video poker game I created in Javascript. It uses Tables and inner html stuff - for example: (psudo-code) imagePicked=random card image (2h.gif); getElementById(cellNum).innerHTML="<img src='imagePicked'>"; I use small 1k.gif images for the cards. Sometimes on the net the 1k.gifs take a while to load & kills bandwith traffic.
3
1478
by: Brian | last post by:
Hello all, Hope you all had a enjoyable Christmas and New Year! Making some good progress on my Air Hockey game, which you've all heard so much about! :) Apologies for the vagueness of the following: Having some little problems not... got my hit-testing working but am
6
1440
by: developer24 | last post by:
Hello, I have written a 3D game engine on a win98SE platform, I get about 30 frames per second. But on an winXP station I only get 3-1 frames perscond, it is very choppy. I like to use OpenGL. Is there a solution to this problem?
0
2256
by: bohuge | last post by:
Hey! At the time being I'm working on a backup solution for a Qtek9090 pocketpc, which should be able to find and backup outlook data to a server, local files, messages and contact from the sim card (and backup them to a server as well) all written in C#. So I'm making a wrapper for the sim manager api. In order to use it's functions in my device application, I've used platform invoke method, to acces the simmanager functions from...
1
4577
by: Olmar | last post by:
Hi, I am trying to write a version of the Hears card game. I can't figue out what is the best way to detect clicks on the cards. Using the cards.dll interface as described in tutorials like http://www.publicjoe.f9.co.uk/csharp/card00.html . Can anyone give me suggestions how to detect which card is clicked and how to redraw it at the center of the sceen. For now all i know is that I have to detect the X and Y coordinates of the mouse....
9
2415
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links. Communications on the Windows PCs use a class derived from CAsyncSocket(). I have written client and server versions of my software for use on the appropriate PCs. I am also using communicating with a legacy DOS based system, with the software...
3
2307
by: Fiona1200 | last post by:
Please help me..i have to write a code in java for a card game as follows. there are 4 players each with 4 decks of cards. each deck has four zeros - four seven as follows (0000,1111,2222,3333,4444,5555,6666,7777). each pack is then shuffled player 1 deals player 2 four of his cards, player 2 deals player 3, 4 of his cards and so on. when each player has four cards, player 1 will pull a card from his deck (the object is to get four of the same...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.