java - Issue with if statements in JButton actions -
i'm pretty new java, , i've made tictactoe program. managed create ai, when attempted add additional option play human, ran issue. tried make if turncount (defined static int) number, check if pressed button's text equal nothing, , if set x. otherwise, should check see if pressed button's text equal nothing, , if set o. whatever reason, won't seem check else statements. following code pretty repetitive, need read first 12 or lines see mean. helping!
the action listener method:
public void actionperformed(actionevent e) { if (e.getsource() == topl) if (turncount % 2 == 0) { if (topl.gettext().equals("")) { topl.settext("x"); turncount += 1; winchecker(); } else if (topl.gettext().equals("")) { topl.settext("o"); turncount += 1; winchecker(); } } if (e.getsource() == midup) { if (turncount % 2 == 0) { if (midup.gettext().equals("")) { midup.settext("x"); turncount += 1; winchecker(); } else if (midup.gettext().equals("")) { midup.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == topr) { if (turncount % 2 == 0) { if (topr.gettext().equals("")) { topr.settext("x"); turncount += 1; winchecker(); } else if (topr.gettext().equals("")) { topr.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == midl) { if (turncount % 2 == 0) { if (midl.gettext().equals("")) { midl.settext("x"); turncount += 1; winchecker(); } else if (midl.gettext().equals("")) { midl.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == mid) { if (turncount % 2 == 0) { if (mid.gettext().equals("")) { mid.settext("x"); turncount += 1; winchecker(); } else if (mid.gettext().equals("")) { mid.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == midr) { if (turncount % 2 == 0) { if (midr.gettext().equals("")) { midr.settext("x"); turncount += 1; winchecker(); } else if (midr.gettext().equals("")) { midr.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == botl) { if (turncount % 2 == 0) { if (botl.gettext().equals("")) { botl.settext("x"); turncount += 1; winchecker(); } else if (botl.gettext().equals("")) { botl.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == midlow) { if (turncount % 2 == 0) { if (midlow.gettext().equals("")) { midlow.settext("x"); turncount += 1; winchecker(); } else if (midlow.gettext().equals("")) { midlow.settext("o"); turncount += 1; winchecker(); } } } if (e.getsource() == botr) { if (turncount % 2 == 0) { if (botr.gettext().equals("")) { botr.settext("x"); turncount += 1; winchecker(); } else if (botr.gettext().equals("")) { botr.settext("o"); turncount += 1; winchecker(); } } } }
if turncount (defined static int) number, check if pressed button's text equal nothing, , if set x. otherwise, should check see if pressed button's text equal nothing, , if set o.
if otherwise meant, if turncount
odd need set o
; need pair of braces {}
without them else
block match inner if
block not outer (and hence never executed match on same condition text ""
).
if (turncount % 2 == 0) { if (topl.gettext().equals("")) { topl.settext("x"); turncount += 1; winchecker(); } } // added else { // added if (topl.gettext().equals("")) { topl.settext("o"); turncount += 1; winchecker(); } }
Comments
Post a Comment