473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Comparing array values for card game

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 4927
jkmyoung
2,057 Expert 2GB
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
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...
1
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
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...
0
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...
3
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...
1
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...
1
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...
3
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.