473,386 Members | 1,715 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.

I need a little help with my BlackJack program

I am a little confused with how to get this method to determine what the best choice is for the number of aces that should be counted as 1 or 11. I will post the method and the code so you can run it. Any help is greatly appreciated

Expand|Select|Wrap|Line Numbers
  1. public static int weHaveAces(int player[], int deal[])
  2. {
  3.     int plyttl = 0;
  4.  
  5.     for(int x = 0; x < player.length - 1;x++)
  6.  
  7.         {
  8.             plyttl += player[x];
  9.             System.out.println(plyttl);
  10.         }
  11.         int bestttl = plyttl;
  12.     for(int z = 0; z <= player[2];z++)
  13.         {
  14.             //System.out.println(( plyttl - (10*z)));
  15.             //System.out.println(21 - plyttl);
  16.             while((21 - ( plyttl - 10*z)) < 21 - plyttl)
  17.             {
  18.  
  19.                 bestttl = (plyttl - 10*z);
  20.                 //System.out.println(bestttl);
  21.                 //System.out.println("test"+ z);
  22.             }
  23.         }
  24.  
  25.  
  26. Whole Code:
  27.  
  28. /*Jordan Feldman
  29. Pitt BlackJack
  30. 4/5/2010*/
  31.  
  32. import javax.swing.JOptionPane;
  33.  
  34. class BlackJack
  35. {
  36. public static void main(String args[])
  37.     {
  38.         int playerCard1 = 0, playerCard2 = 0, dealerCard1 = 0, dealerCard2 = 0, playerAces = 0, dealerAces = 0;
  39.  
  40.         int[] playerCards =  new int [3];// Creates an int array with 3 positions
  41.  
  42.         makeDeck(playerCards);
  43.  
  44.         playerCard1 = playerCards[0];
  45.         playerCard2 = playerCards[1];
  46.         playerAces = playerCards[2];
  47.  
  48.         int[] dealerCards =  new int [3];// Creates an int array with 3 positions
  49.  
  50.         makeDeck(dealerCards);
  51.  
  52.         dealerCard1 = dealerCards[0];
  53.         dealerCard2 = dealerCards[1];
  54.         dealerAces = dealerCards[2];
  55.  
  56.  
  57.         printMethod(playerCards,dealerCards);
  58.         firstLogic(playerCards,dealerCards);
  59.  
  60.     }
  61.  
  62. public static void makeDeck(int cards[])
  63.     {
  64.         int y = 0;
  65.  
  66.         for(int x = 0;x < 2; x++)
  67.         {
  68.         cards[x] = 1;//(int)(Math.random()* 13 +1); //Generates a random value between 1-13
  69.         if (cards[x] == 11 || cards[x] == 12 || cards[x] == 13)
  70.             {
  71.                 cards[x] = 10;
  72.             }
  73.         else if (cards[x] == 1)
  74.         {
  75.             cards[x] = 11;
  76.             ++y;
  77.  
  78.         }
  79.     }
  80.         cards[2] = y;
  81.         //System.out.println(cards[2]);
  82.     }
  83. public static void printMethod(int playerCards[], int dealerCards[])
  84.     {
  85.         String name;
  86.  
  87.         int x = playerCards[0];
  88.         int y = playerCards[1];
  89.         int playerTotal = x + y;
  90.  
  91.         System.out.println("Welcome to the River's Casino!");
  92.         name = JOptionPane.showInputDialog("What is your name?");
  93.         System.out.println("Let's play BlackJack " + name + ".\n");
  94.  
  95.         System.out.println("The dealer:\n? + "+ dealerCards[1] + "\n");
  96.  
  97.         System.out.println("You:\n" + playerCards[0] + " + " + playerCards[1] + " = " + playerTotal);
  98.         int plyttl = weHaveAces(playerCards,dealerCards);
  99.         System.out.println("Your best choice is " + plyttl);
  100.  
  101.     }
  102. public static void firstLogic(int player[], int dealer[])
  103.     {
  104.         int acecnt;
  105.         if(dealer[2] >= 1 || player[2] >= 1)
  106.         {
  107.             // weHaveAces(player,dealer);
  108.         }
  109.         else
  110.         {
  111.             noAces(player,dealer);
  112.         }
  113.     }
  114. public static int weHaveAces(int player[], int deal[])
  115. {
  116.     int plyttl = 0;
  117.  
  118.     for(int x = 0; x < player.length - 1;x++)
  119.  
  120.         {
  121.             plyttl += player[x];
  122.             System.out.println(plyttl);
  123.         }
  124.         int bestttl = plyttl;
  125.     for(int z = 0; z <= player[2];z++)
  126.         {
  127.             //System.out.println(( plyttl - (10*z)));
  128.             //System.out.println(21 - plyttl);
  129.             while((21 - ( plyttl - 10*z)) < 21 - plyttl)
  130.             {
  131.  
  132.                 bestttl = (plyttl - 10*z);
  133.                 //System.out.println(bestttl);
  134.                 //System.out.println("test"+ z);
  135.             }
  136.         }
  137.  
  138.     return bestttl;
  139.  
  140. }
  141.  
  142.  
  143.  
  144. public static void noAces(int ply[], int deal[])
  145. {
  146.  
  147. }
  148.  
  149.  
  150. }
Apr 10 '10 #1
1 1905
jkmyoung
2,057 Expert 2GB
I would:
1. Have a flag that says whether you have an ace or not.
2. Count the aces as 1 to the total.
3. When you display the total,
- Check if you have an ace
- Check if the total is < 11.
If so, add 10 to the 'displayed' total.
Apr 12 '10 #2

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

Similar topics

3
by: Good Time Tribe | last post by:
I'm an aspiring game programmer. I'm more experienced with pascal, c++, and Java so I naturally love what c# offers and want to learn it. I also wanted to start with something simple, to kind of...
7
by: f pemberton | last post by:
I have a string (xdata) and theres a newline after every 17 characters of the string. I was wondering how I can replace multiple substrings multiple times within a string? To put it another way,...
1
by: mturner64 | last post by:
Trying to link my Samsung BlackJack phone to my laptop to achieve internet access. When I plug the phone into the laptop the computer recognizes the BlackJack but I do not know how to configure one...
45
by: Gaijinco | last post by:
Hi my name is Carlos Obregón and I'm currently a profesor of C/C++ programming at the CUMD in Bogotá Colombia. This last term I ask my students to develop an implementation of the minesweeper...
1
by: EXotiX | last post by:
hey im new to vb6 and I am making a blackjack game but can not randomize the array correctly. Could you help please Option Explicit Const NumItems As Integer = 53 Public Sub RandomizeCards()...
7
by: Martin Marcher | last post by:
Hello, having worked quite a bit with python in the last months (some Java before, and some C++ before that) I was very impressed by an idea the Java people had. Explanation: the JSRs define...
3
by: Marc 'BlackJack' Rintsch | last post by:
On Thu, 28 Aug 2008 09:13:00 -0700, SUBHABRATA wrote: a1, a2, a2, …, a20? You must be kidding. Please stop numbering names and use *meaningful* names instead! Could you describe them...
2
by: AEB | last post by:
Hello, my supervisor has requested that I write a program to display 2D waves in 3D using Python. Basically I have a time series file that has the heights of the wave over time steps, and I want...
30
by: imran akhtar | last post by:
i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong...
21
by: imran akhtar | last post by:
hi, i am making black jack code, but i am stuck, i have made start, but for reason gettin errors, which i dont seem be able to fix, below is my code wht i started. import random deck = *4...
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: 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
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...
0
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,...
0
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...

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.