swing - Java Craps Game Crashes -
i trying finish java craps game using gui, every time hit "play craps" , program freezes up, , doesn't compute. i'm assuming it's problem game logic, can out? thanks!
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class mcgovernjavaprogrammingassignment8bcrapsgui extends jframe { private static final int width=400; private static final int height=300; // declare jframe components: private jbutton jbutplaycraps, jbutclear, jbutexit; private playbuttonhandler playhandler; private clearbuttonhandler clearhandler; private exitbuttonhandler exithandler; private jscrollpane scrollingresult; private jtextarea jtaoutput; private jpanel jpnltop = new jpanel(); private jpanel jpnlcenter = new jpanel(); private jpanel jpnlbottom = new jpanel(); // constructor public mcgovernjavaprogrammingassignment8bcrapsgui() { //set title , size: settitle("matthew mcgovern - java craps game!"); setsize(width, height); //instantiate jtextarea jtaoutput = new jtextarea(10,1); scrollingresult = new jscrollpane(jtaoutput); //instantiate , register play button: jbutplaycraps = new jbutton("play craps!"); playhandler = new playbuttonhandler(); jbutplaycraps.addactionlistener(playhandler); //instantiate , register clear button: jbutclear = new jbutton("clear"); clearhandler = new clearbuttonhandler(); jbutclear.addactionlistener(clearhandler); //instantiate , register exit button: jbutexit = new jbutton("exit"); exithandler = new exitbuttonhandler(); jbutexit.addactionlistener(exithandler); //assemble jpanels: jpnltop.setlayout(new gridlayout(1, 2)); //this input box went....delete? jpnlcenter.setlayout(new gridlayout(1,1)); jpnlcenter.add(scrollingresult); jpnlbottom.setlayout(new gridlayout(1, 3)); jpnlbottom.add(jbutplaycraps); jpnlbottom.add(jbutclear); jpnlbottom.add(jbutexit); //start add components jframe container pane = getcontentpane(); pane.setlayout(new borderlayout()); pane.add(jpnltop, borderlayout.north); pane.add(jpnlcenter, borderlayout.center); pane.add(jpnlbottom, borderlayout.south); //show jframe , set "x" button close program: setvisible(true); setdefaultcloseoperation(exit_on_close); //set jframe center: setlocationrelativeto(null); } // end constructor. // play craps button event handler: private class playbuttonhandler implements actionlistener { public void actionperformed(actionevent e) { { //declare method variables. int idice1, idice2, iadddice, iadddiceroll2 = 0, igameresults; string sresults = ""; //craps game logic. //roll dice. idice1 = (int) (math.random() *6 + 1); idice2 = (int) (math.random() *6 + 1); //add dice together. iadddice = idice1 + idice2; //determine results. if (iadddice == 2 || iadddice == 3 || iadddice == 12) {igameresults = 1; sresults = "sorry, lost.";} else if (iadddice == 7 || iadddice == 11) {igameresults = 0; sresults = "congratulations, won!";} else {igameresults = 2; sresults = "point " + iadddice + "\n"; idice1 = (int) (math.random() *6+1); idice2 = (int) (math.random() *6+1); iadddiceroll2 = idice1 + idice2; sresults.concat("you rolled " + iadddiceroll2 + "\n"); while (iadddiceroll2 != 7) if (iadddice == iadddiceroll2) {sresults.concat("congratulations, win!"); break; }else{ sresults = "point " + iadddice + "\n"; } idice1 = (int) (math.random() *6+1); idice2 = (int) (math.random() *6+1); iadddiceroll2 = idice1 + idice2; sresults.concat("you rolled " + iadddiceroll2 + "\n"); } if (iadddiceroll2 == 7) {sresults = "sorry, lost";} jtaoutput.settext(sresults); } } } // clear button handler: private class clearbuttonhandler implements actionlistener { public void actionperformed(actionevent e) { jtaoutput.settext(""); } } // exit button handler: private class exitbuttonhandler implements actionlistener { public void actionperformed(actionevent e) { system.exit(0); } } public static void main(string args[]) { mcgovernjavaprogrammingassignment8bcrapsgui cs = new mcgovernjavaprogrammingassignment8bcrapsgui(); }
}
this because while
loop infinite. loop not modify iadddiceroll2
, never going equal 7
. forgot braces around body of loop?
i talking loop:
while (iadddiceroll2 != 7) if (iadddice == iadddiceroll2) {sresults.concat("congratulations, win!"); break; }else{ sresults = "point " + iadddice + "\n"; }
and think meant this:
while (iadddiceroll2 != 7) { if (iadddice == iadddiceroll2) {sresults.concat("congratulations, win!"); break; }else{ sresults = "point " + iadddice + "\n"; } idice1 = (int) (math.random() *6+1); idice2 = (int) (math.random() *6+1); iadddiceroll2 = idice1 + idice2; sresults.concat("you rolled " + iadddiceroll2 + "\n"); }
Comments
Post a Comment