Ok, so I've got to make a game of blackjack with Java. The codes for the way the game works are already done. they're set in individual classes. my job is to make the event-handlers of the main java applet thing work together with the other classes so the game works as a whole.
I have no idea how though D:. I spent my whole day yesterday making event handlers that do what turned out to already have been done in the classes -.-
so anyway. I'm supposed to call upon those methods. And I have no idea how it works.
I just need someone to post a clear example for me to observe.
please, I've wasted so much time on this. even worse, when I started screwing with the event handlers this morning, I cut the stuff I made before and it autosaved. then I got a bsod before I could paste it back >_<.
Re: Help with Java please!
Posted: Sun May 23, 2010 4:21 pm
by Squirt
Use banana phone
lol I slacked on java. All I did was a tic tax toe game. Sort of
Re: Help with Java please!
Posted: Sun May 23, 2010 5:33 pm
by SM-Count
Um... multi-threaded examples? MVC or just random classes? A lot of things to consider and they all have different examples... (Or just post the code, er... upload the files somewhere :/)
Re: Help with Java please!
Posted: Sun May 23, 2010 5:43 pm
by poehalcho
SM-Count wrote:Um... multi-threaded examples? MVC or just random classes? A lot of things to consider and they all have different examples... (Or just post the code, er... upload the files somewhere :/)
private static Image kaarten; public int kaart; public int kleur;
// constructor
public Kaart( int kleur, int kaart) { this.kleur = kleur; this.kaart = kaart; }
// methoden
public static void laadKaarten(Applet a) { if (kaarten == null) { MediaTracker tracker = new MediaTracker(a); kaarten = a.getImage(a.getDocumentBase(),"kaarten.gif"); tracker.addImage(kaarten,0); try { tracker.waitForAll(); } catch(InterruptedException e){} } }
public void tekenKaart(Graphics g, int x, int y) { g.setClip(x, y, KAARTBREEDTE, KAARTHOOGTE); g.drawImage(kaarten,x-kaart*KAARTBREEDTE,y-kleur*KAARTHOOGTE, null); }
public abstract interface KaartDef { public static final int KLAVEREN = 0; public static final int SCHOPPEN = 1; public static final int RUITEN = 2; public static final int HARTEN = 3;
public static final int AAS = 0; public static final int TWEE = 1; public static final int DRIE = 2; public static final int VIER = 3; public static final int VIJF = 4; public static final int ZES = 5; public static final int ZEVEN = 6; public static final int ACHT = 7; public static final int NEGEN = 8; public static final int TIEN = 9; public static final int BOER = 10; public static final int VROUW = 11; public static final int HEER = 12;
public static final int KAARTBREEDTE = 73; public static final int KAARTHOOGTE = 97; public static final int AANTALHANDEN = 4; public static final int AANTALKAARTEN = 52; public static final int AANTALKRTHAND = AANTALKAARTEN / AANTALHANDEN; }
private Kaart[] kaart = new Kaart[AANTALKAARTEN]; private Kaart[] spelerhand = new Kaart[10]; private Kaart[] bankhand = new Kaart[10]; private boolean gedeeld = false; private boolean pas = false; private int kaartnr; private int kleurnr; private int tSpeler; private int tBank;
//constructor
public KaartSpel() { int i = 0; for (int klr = KLAVEREN; klr <= HARTEN; klr++) { for (int krt = TWEE; krt <= HEER; krt++) { kaart[i] = new Kaart(klr,krt); i++; } kaart[i] = new Kaart (klr, AAS); i++; } }
//methoden
public void deelKaarten() { tSpeler =1; tBank = 1; for (int i=0; i<=tSpeler; i++) { spelerhand[i] = new Kaart(pakKaart().getKleur(),pakKaart().getKaart()); } for (int i=0; i<=tBank; i++) { bankhand[i] = new Kaart(pakKaart().getKleur(),pakKaart().getKaart()); } gedeeld = true;
}
public void spelerSpeelt() { if ((tSpeler < 6) && (geefSpelerscore() < 21) && gedeeld) { tSpeler++; spelerhand[tSpeler] = new Kaart(pakKaart().getKleur(),pakKaart().getKaart()); } }
public int geefSpelerscore() { int spelerscore = 0; for (int i=0; i<=tSpeler; i++) { spelerscore = spelerscore+geefKaartWaarde(spelerhand[i],true); } if (spelerscore > 21) { spelerscore = 0; for (int i=0; i<=tSpeler; i++) { spelerscore = spelerscore+geefKaartWaarde(spelerhand[i],false); } } return spelerscore; }
public void bankSpeelt() { pas = true; if (geefSpelerscore() < 21) while (geefBankscore() < 17 && geefBankscore() < geefSpelerscore() && tBank < 6 && gedeeld) { tBank++; bankhand[tBank] = new Kaart(pakKaart().getKleur(),pakKaart().getKaart()); } }
public int geefBankscore() { int bankscore = 0; for (int i=0; i<=tBank; i++) { bankscore = bankscore+geefKaartWaarde(bankhand[i],true); } if (bankscore > 21) { bankscore = 0; for (int i=0; i<=tBank; i++) { bankscore = bankscore+geefKaartWaarde(bankhand[i],false); } } return bankscore; }
public String bepaalUitslag() { if (geefSpelerscore() > 21) return "Bank heeft gewonnen."; else if (geefBankscore() > 21) return "Jij hebt gewonnen."; else if (geefSpelerscore() == geefBankscore()) return "Geen winnaar."; else if (geefSpelerscore() > geefBankscore()) return "Jij hebt gewonnen."; else return "Bank heeft gewonnen."; }
private Kaart pakKaart() { int i = (int)(52*Math.random()); return kaart[i]; }
public void teken (Graphics g) { if(gedeeld) { int x = 120; for (int i=0; i<=tSpeler; i++) { spelerhand[i].tekenKaart(g, x, 30); x += KAARTBREEDTE; } } if(pas) { int x = 120; for (int i=0; i<=tBank; i++) { if (i > 1) try { Thread.sleep(300); } catch (InterruptedException e) {} bankhand[i].tekenKaart(g, x, 210); x += KAARTBREEDTE; } gedeeld = false; pas = false; } }
private int geefKaartWaarde(Kaart k, boolean a) { if (k.kaart == AAS && a) return 11; else if (k.kaart == AAS && !a) return 1; else if (k.kaart > TIEN) return 10; else return k.kaart+1; } }
public void paint(Graphics g) { blackjack.teken(g); }
is experimental.
Re: Help with Java please!
Posted: Sun May 23, 2010 7:02 pm
by SM-Count
Yeah.... reading code in a different language isn't gonna work out too well for me. For a game like this, you just have to fire property change events and then call the methods that corresponds with the event. (you come up with the event names obvoiusly) That or use propertychangesupport which is a pretty easy way to do it, just google that and there should be examples.
As to the specific questions, say you don't have all that swing stuff and this was your main method.
//gonna pretend this is the button that deals a new hand public void nieuwspelKnopActionPerformed(ActionEvent evt) { k.deelKaarten(); //alternatively k = new kaartSpel(); k.deelKaarten(); I dunno if there's a reshuffle method or it reshuffles itself or something cause I can't read it but if there is use that instead of just instantiating a new one but you need to have all the cards back before you deal again } }
and that's it... now if you press the thing I pretended was the "deal new hand" button your kaartSpel will have a new hand dealt and store it inside the player/bank list. Of course you'll have to write swing code (/pat) to make it show up and stuff but just stuff all your important things in those action performed methods. The event name (evt) may matter but again I didn't read through the entire code, just skimmed it. For example if you have an ace you can name the evt name for draw card as the card number from your interface and then do a conditional if evt == 0 (assume 0 stands for ace) then pop up a dialog for player to choose whether they want a 1 or 11 and else just have the card number be stored in the player hand list.
Btw, why are there 10 things for the hand list? doesn't black jack only have 2 cards? Maybe it's just the language barrier and you're storing more stuff in there than simple card numbers :/
Re: Help with Java please!
Posted: Sun May 23, 2010 7:11 pm
by poehalcho
SM-Count wrote:Btw, why are there 10 things for the hand list? doesn't black jack only have 2 cards? Maybe it's just the language barrier and you're storing more stuff in there than simple card numbers :/
erhh might not've been the exact same thing as blackjack. my card game knowledge is as limited as my java -.-
the idea of the game was to get as close as possible to 21. if you go over you lose. if the computer gets closer to 21 than you, you also lose. if the computer goes over 21 you win. you can skip a turn if you think you're chances of getting too high are large.
Also thank you for the explanation. This is much clearer than any of my online instructions D:. I also just received a completed version of the whole thing from a classmate. I'll use your info and his project to make something of my own.
As my thanks, a laugh: here's an example of how they explain an instance in my instructions:
A new chair that is made under the class Chair is called an instance of Chair. For instance, a kitchen chair is an instance of the class Chair, but a kitchen table is not.
Yes, now they expect me to know everything about instances.
Re: Help with Java please!
Posted: Sun May 23, 2010 7:16 pm
by SM-Count
edit: didn't see your edit and I'm sorry for how terribly they're teaching you, makes me cringe