473,795 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing array values for card game

1 New Member
I want to double the score if the cards have the same suit or value and if two cards have the same both suit and value, the score get tripled. And the 2 of clubs is a 20 points. i already wrote the basics of the program which it adds two random cards together and whoever get the higher score win.:

Expand|Select|Wrap|Line Numbers
  1. class Card{
  2.  GetCards gc = new GetCards();
  3. public static void main(String []args){
  4.   new Card(); 
  5. }  
  6. public Card(){
  7. int c1, c2, c3, c4=0;
  8. System.out.println("COMPUTER CARDS: ");
  9. c1 = getCardValue();
  10. c2 = getCardValue();
  11. System.out.println("Computer score: "+(c1+c2));
  12. System.out.println("YOUR CARDS: ");
  13. c3 = getCardValue();
  14. c4 = getCardValue();
  15. System.out.println("Your Score: "+(c3+c4));
  16.   if ( (c1+c2)>(c3+c4))
  17.  System.out.println("You Lose");
  18. else if ( (c1+c2)<(c3+c4))
  19. System.out.println("YOu win");
  20. else System.out.println(" Draw");
  21. }
  22. public int getCardValue(){
  23. String card = gc.finalCard();
  24. System.out.println(card+": ");
  25. int value = gc.i;
  26. if(value>9)value=10;
  27. else if(value==0)value=11;
  28. else value++;
  29. return value;   
  30. }
  31. }
  32. class GetCards{
  33. int i,j;
  34. String card;
  35. GetCards(){}
  36. public String finalCard(){
  37.        String []suit = {"Hearts", "Clubs", "Diamonds", "Spades"};
  38.       String []value = {"Ace","1","2","3","4","5","6","7","8","9","Ten","Jack","Queen","King"};
  39.  i= (int) (Math.random()*value.length);
  40. card=value[i];
  41. j= (int) (Math.random()*suit.length);
  42. card=card+"of "+suit[j];
  43. return card;
  44.  
  45. }  
  46. }


i have trouble define an array of a variable, i mean i don't know how to compare two arrays of two cards. please help me thanks
Mar 28 '10 #1
1 4983
jkmyoung
2,057 Recognized Expert Top Contributor
1. Note, you have 1 and Ace; you probably want to remove "1".
2. Your function names don't match up at all.
3. Ideally, you would seperate your card functions into the following:
A. Create a card. This includes setting suit and card, as well as the string.
B. Get the string of the card.

4. You could have a card comparison function, eg.
Expand|Select|Wrap|Line Numbers
  1. static boolean SameSuit(Card c1, Card c2){
  2.   return c1.j=c2.j
  3. }
5. Your class should be named Card. GetCard could be a function in the class.
6. Rename the members to something meaningful instead of i and j. eg value, suit.
7. If you want to have a multiplier, have a function which computes the total score of two cards, eg:
static int HandValue(Card c1, Card c2)
Mar 29 '10 #2

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

Similar topics

3
2630
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
2691
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.
0
3014
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can someone helps me. Thanks!! ----------------------------------------------------------------------- The code for the Stock class: ----------------------------------------------------------------------- public class Stock { private Deck deck = new...
0
3720
by: Paul | last post by:
Greetings: I have been trying to find source code for a Baccarat card game (C++ or Basic, but preferably the former). My reason for this is wanting to experiment with possible Baccarat card counting methodologies, which is a controversial subject but I'd like to try out some ideas. However, I know very little C++ and am having real trouble even just setting up an 8 deck card shoe. Also, I can't find source code for Baccarat anywhere...
3
8677
by: riauvision | last post by:
Hello, I have problem in writing ms Access vba code to compare two array taken from two different recordset. One is from table: Detail Orders (PK:,POID,Product.Code,Qty) and the other is from table (PK:InventoryID, POID, ProductID.Code, Qty). I retrive the and store its values in an array and also the in another array. My problem is How to compare the two arrays so that I can update the inventory? My code :
1
1649
by: Geoff | last post by:
Hi all, I've been making a card game in VB and am trying to get it work over a network. I think I've got all the card game pretty much planned out ready to make it, but need to find a way to get 2 computers to talk to each other over a network. I can't save files to anywhere both computers can access on the network though. I was thinking this would be solvable with IP's and stuff like that, I'd appreciate it if anyone can give me some code for...
1
4579
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....
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...
2
2744
by: TamaThps | last post by:
I have to write a program that takes the lines of code from a .cpp as a string into an array. Then I need to compute the ratio of total lines of code to the number of comment lines, and ratio of total number of non comment and non blank lines to the total number of code. This is the void function that I am using to get these ratio's. void CommentEvaluation(/* in */ string codeArray, int size, ofstream& dataOut) //This function computes the...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
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...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9043
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
7541
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
5437
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...
3
2920
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.