Results 1 to 5 of 5
Thread: Variable not updatet
- 12-21-2011, 03:11 PM #1
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
Variable not updatet
I am trying to write a blackjack program. In my program i use a method to return a value. It is the bepaalScore method and I return a value which I name totaal2.
Then in the paint section i want to perform another method (stapel2.kieskaart) as long as totaal2 is less then 17. But my program doesnt revalue totaal2 and it keeps on performing the method which is not what i want. I hope somebody can tell me why totaal2 is not updated and what is a good solution to this problem.
This is the code:
Java Code:import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class blackjack4 extends Applet implements ActionListener { private Deck stapel1; private Deck stapel2; private Button start; private Button neemKaart; private Button passen; public Boolean kaartGenomen=false; public Boolean gepast=false; public Boolean gestart=false; public int scoreComputer = 0; public void init() { stapel1 = new Deck(); stapel2 = new Deck(); start = new Button("Start potje blackjack"); add(start); start.addActionListener(this); passen = new Button("Ik pas"); passen.addActionListener(this); neemKaart = new Button("Neem een kaart"); neemKaart.addActionListener(this); } public void actionPerformed (ActionEvent gebeurtenis) { if (gebeurtenis.getSource() == passen) gepast=true; repaint(); if (gebeurtenis.getSource() == neemKaart) kaartGenomen = true; repaint(); if (gebeurtenis.getSource() == start) gestart = true ; add(neemKaart); add(passen); validate(); } public void paint (Graphics g) { // g.drawString("de waarde van gepast is: " + gepast, 8,222); // g.drawString("de waarde van kaartGenomen is: " + kaartGenomen, 8,232); if (gestart == true){ stapel1.kiesKaart(g); stapel1.kiesKaart(g); gestart = false; repaint(); } if (kaartGenomen == true) { stapel1.kiesKaart(g); kaartGenomen = false; repaint(); } int totaal = stapel1.bepaalScore(g, 160, 160); if (totaal > 21) g.drawString("Je bent dood", 10, 130); else g.drawString("De score is " + totaal, 1, 130); if (gepast==true) { g.drawString("je hebt gepast",2,55); int totaal2 = stapel2.bepaalScore(g, 18, 170); g.drawString("Computer heeft " + totaal2, 1, 140); while (totaal2 < 17) { stapel2.kiesKaart(g); } } } } class Kaart { public int nummer; public String soort; } class Deck { public Kaart[] stapel = new Kaart[52]; public Kaart[] totaalIk = new Kaart[20]; public Kaart[] totaalComputer = new Kaart[20]; public String[] soort = {"harten", "schoppen", "klaver", "ruiten"}; private int x=0; private int y=0; private int z=0; private int nummer = 1; int kaartNummer = 0; int kaartNummerIk = 0; int kaartNummerComputer=0; public int welkeKaart; public int dezePlek = 0; public Deck() { int soortNummer = 0; String soortNaam; soortNaam = soort[soortNummer]; for (soortNummer=0; soortNummer< 4; soortNummer++) for (nummer = 1; nummer < 14;nummer++){ stapel[kaartNummer]=new Kaart(); stapel[kaartNummer].nummer=nummer; stapel[kaartNummer].soort= soort[soortNummer]; kaartNummer++;} } public void kiesKaart (Graphics g) { welkeKaart = (int) (Math.random()*52) + 1; totaalIk[dezePlek]= new Kaart(); totaalIk[dezePlek].soort=stapel[welkeKaart].soort; totaalIk[dezePlek].nummer=stapel[welkeKaart].nummer; dezePlek = dezePlek +1; } public int bepaalScore (Graphics g, int l, int r) { int score = 0; for (kaartNummerIk = 0, z=0;kaartNummerIk<dezePlek;kaartNummerIk++){ switch(totaalIk[kaartNummerIk].nummer){ case 1: g.drawString(totaalIk[kaartNummerIk].soort + " 2",l,r + z); score = score + 2;break; case 2: g.drawString(totaalIk[kaartNummerIk].soort + " 3",l,r + z); score = score + 3;break; case 3: g.drawString(totaalIk[kaartNummerIk].soort + " 4",l,r + z); score = score + 4;break; case 4: g.drawString(totaalIk[kaartNummerIk].soort + " 5",l,r + z); score = score + 5;break; case 5: g.drawString(totaalIk[kaartNummerIk].soort + " 6",l,r + z); score = score + 6;break; case 6: g.drawString(totaalIk[kaartNummerIk].soort + " 7",l,r + z); score = score + 7;break; case 7: g.drawString(totaalIk[kaartNummerIk].soort + " 8",l,r + z); score = score + 8;break; case 8: g.drawString(totaalIk[kaartNummerIk].soort + " 9",l,r + z); score = score + 9;break; case 9: g.drawString(totaalIk[kaartNummerIk].soort + " 10",l,r + z); score = score + 10;break; case 10: g.drawString(totaalIk[kaartNummerIk].soort + " Boer",l,r + z); score = score + 10;break; case 11: g.drawString(totaalIk[kaartNummerIk].soort + " Vrouw",l,r + z); score = score + 10;break; case 12: g.drawString(totaalIk[kaartNummerIk].soort + " Heer",l,r + z); score = score + 10;break; case 13: g.drawString(totaalIk[kaartNummerIk].soort + " Aas",l,r + z); score = score + 11;break; } z=z+10; } return score; } }Last edited by KevinWorkman; 12-21-2011 at 03:24 PM. Reason: added code tags
- 12-21-2011, 03:27 PM #2
Re: Variable not updatet
When posting code, make sure you use the code tags and proper formatting. I've added the code tags for you this time, but please be more mindful in the future.
Why would it update your variable? You set it to whatever is returned from that method first, and then you enter the loop. You don't update the variable inside the loop, so it's never going to change.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-21-2011, 04:07 PM #3
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
Re: Variable not updatet
Hi,
Thanks for your reply. I'm sorry I didn's use the code-tags, I'll do that from now.
I understand your answer, but I dont know how to solve my problem yet.
This is the part where the variable is updated:
If I put this in the loop like:Java Code:int totaal2 = stapel2.bepaalScore(g, 18, 170);
it does not work. Is there a way to update totaal2 while performing the loop or am i on the wrong track?Java Code:while (totaal2 < 17) { stapel2.kiesKaart(g); int totaal2 = stapel2.bepaalScore(g, 18, 170); }
- 12-21-2011, 04:39 PM #4
Re: Variable not updatet
What exactly do you mean by "it does not work"? What you have there is not simply updating the value of the variable, it's trying to redeclare a variable with the same name.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-21-2011, 07:34 PM #5
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
Re: Variable not updatet
The variable totaal2 has the value that returns from the method stapel2.bepaalScore. If I use the method stapel2.kiesKaart, the value that is returned by stapel2.bepaalScore will change. This is because the method stapel2.kiesKaart inserts a value in an array which is read by stapel2.bepaalScore. I want the computer to perform the action stapel2.kiesKaart as many times as neccesary to get totaal2 to the value of 17 or higher. But I don't know how to implement this in my program.
Last edited by Maarten; 12-21-2011 at 07:40 PM.
Similar Threads
-
Dynamic variable name based on other variable
By nadissen in forum EclipseReplies: 4Last Post: 05-06-2011, 06:22 PM -
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
Variable name determined by another variable's value
By Lumpkabob in forum New To JavaReplies: 5Last Post: 04-14-2009, 08:00 AM -
getting the value of variable
By Lehane_9 in forum New To JavaReplies: 2Last Post: 03-05-2008, 01:42 AM -
Getting variable value from a variable name
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks