java - Why is Action Listener malfunctioning? -
i developing basic tic tac toe game using java. supposed have 9 buttons in grid, , when click one, x appears. once you've clicked on button, kind of switch variable i
should change value, next time click o appears. there no syntax errors, there problem. when click on button, x appear, when click on button, instead of o appearing, x appears again.
logical code.
public void actionperformed (actionevent evt) { int = 1; boolean b1xon=false; boolean b2xon=false; boolean b3xon=false; boolean b4xon=false; boolean b5xon=false; boolean b6xon=false; boolean b7xon=false; boolean b8xon=false; boolean b9xon=false; boolean b1oon=false; boolean b2oon=false; boolean b3oon=false; boolean b4oon=false; boolean b5oon=false; boolean b6oon=false; boolean b7oon=false; boolean b8oon=false; boolean b9oon=false; boolean win=false; if ((evt.getsource() == b1) && (i == 1)) { b1.settext("x"); i++; b1xon=true; } else if (evt.getsource() == b1 && == 2) { b1.settext("o"); = 1; b1oon=true; } else if (evt.getsource() == b2 && == 1) { b2.settext("x"); i++; b2xon=true; } else if (evt.getsource() == b2 && == 2) { b2.settext("o"); = 1; b2oon=true; } else if (evt.getsource() == b3 && == 1) { b3.settext("x"); i++; b3xon=true; } else if (evt.getsource() == b3 && == 2) { b3.settext("o"); = 1; b3oon=true; } else if (evt.getsource() == b4 && == 1) { b4.settext("x"); i++; b4xon=true; } else if (evt.getsource() == b4 && == 2) { b4.settext("o"); = 1; b4oon=true; } else if (evt.getsource() == b5 && == 1) { b5.settext("x"); i++; b5xon=true; } else if (evt.getsource() == b5 && == 2) { b5.settext("o"); = 1; b5oon=true; } else if (evt.getsource() == b6 && == 1) { b6.settext("x"); i++; b6xon=true; } else if (evt.getsource() == b6 && == 2) { b6.settext("o"); = 1; b6oon=true; } else if (evt.getsource() == b7 && == 1) { b7.settext("x"); i++; b7xon=true; } else if (evt.getsource() == b7 && == 2) { b7.settext("o"); = 1; b7oon=true; } else if (evt.getsource() == b8 && == 1) { b8.settext("x"); i++; b8xon=true; } else if (evt.getsource() == b8 && == 2) { b8.settext("o"); = 1; b8oon=true; } else if (evt.getsource() == b9 && == 1) { b9.settext("x"); i++; b9xon=true; } else if(evt.getsource() == b9 && i==1){ b9.settext("o"); = 1; b9oon=true; } if((b1xon&&b2xon&&b3xon)||(b4xon&&b5xon&&b6xon)||(b7xon&&b8xon&&b9xon)||(b1xon&&b4xon&&b7xon)||(b2xon&&b5xon&&b8xon)||(b3xon&&b6xon&&b9xon)||(b1xon&&b5xon&&b9xon)||(b3xon&&b5xon&&b7xon)){ win=true; } else if((b1oon&&b2oon&&b3oon)||(b4oon&&b5oon&&b6oon)||(b7oon&&b8oon&&b9oon)||(b1oon&&b4oon&&b7oon)||(b2oon&&b5oon&&b8oon)||(b3oon&&b6oon&&b9oon)||(b1oon&&b5oon&&b9oon)||(b3oon&&b5oon&&b7oon)){ win=true; }
Comments
Post a Comment