html - java applet program errors -
i finished making tic-tac-toe game in java, though received lots of errors, main errors 'else' without 'if' , ';' expected didn't need be. code:
import java.awt.*; import java.awt.event.*; import java.applet.applet; public class tictactoe extends applet implements actionlistener button squares[]; button newgamebutton; label score; int emptysquaresleft = 9; public void init (){ this.setlayout(new borderlayout()); this.setbackground(color.cyan); font appletfont = new font("monospased", font.bold,20); this.setfont(appletfont); newgamebutton = new button ("new game"); newgamebutton.addactionlistener(this); panel toppanel = new panel(); toppanel.add(newgamebutton); this.add(toppanel, "north"); panel centerpanel = new panel(); centerpanel.setlayout(new gridlayout (3,3)); this.add(centerpanel,"center"); score = new label ("your turn!"); this.add(score,"south"); squares = new button[9]; for(int i=0;i<9;i++){ squares[i] = new button(); squares[i].addactionlistener(this); squares[i].setbackground(color.orange); } } public void actionperformed(actionevent e){ button thebutton = (button) e.getsource(); if (thebutton == newgamebutton){ (int i=0; i<9;i++){ squares [i].setenabled (true); squares [i].setlabel(""); squares[i].setbackground(color.orange); } emptysquaresleft = 9; score.settext("your turn!"); newgamebutton.setenabled(false); return; } string winner = ""; (int i=0; i<9; i++){ if (thebutton == squares[i]){ squares[i].setlabel("x"); winner = lookforwinner(); if(!"".equals(winner)){ endthegame (); } else { computermove(); winner = lookforwinner(); if(!"".equals (winner)){ endthegame(); } } break; } } if (winner.equals("x")){ score.settext("you won!"); } else if (winner.equals("o")){ score.settext("you lost!"); } else if (winner.equals ("t")){ score.settext ("it's tie!"); } } string lookforwinner() { string thewinner = ""; emptysquaresleft--; if (emptysquaresleft == 0){ homecoming "t"; } if (!squares [0].getlabel().equals("") && squares[0].getlabel().equals(squares[1].getlabel()) && squares[0].getlabel().equals(squares[2].getlabel())){ thewinner = squares[0].getlabel(); highlightwinner(0,1,2); } else if (!squares [3].getlabel().equals("") && squares[3].getlabel().equals(squares[4].getlabel()) && squares[3].getlabel().equals(squares[5].getlabel())){ thewinner = squares[3].getlabel(); highlightwinner(3,4,5); } else if (!squares [6].getlabel().equals("") && squares[6].getlabel().equals(squares[7].getlabel()) && squares[6].getlabel().equals(squares[8].getlabel())){ thewinner = squares[6].getlabel(); highlightwinner(6,7,8); } else if (!squares [0].getlabel().equals("") && squares[0].getlabel().equals(squares[3].getlabel()) && squares[0].getlabel().equals(squares[6].getlabel())){ thewinner = squares[0].getlabel(); highlightwinner(0,3,6); } else if (!squares [1].getlabel().equals("") && squares[1].getlabel().equals(squares[4].getlabel()) && squares[1].getlabel().equals(squares[7].getlabel())){ thewinner = squares[2].getlabel(); highlightwinner(1,4,7); } else if (!squares [2].getlabel().equals("") && squares[2].getlabel().equals(squares[5].getlabel()) && squares[2].getlabel().equals(squares[8].getlabel())){ thewinner = squares[2].getlabel(); highlightwinner(2,5,8); } else if (!squares [0].getlabel().equals("") && squares[0].getlabel().equals(squares[4].getlabel()) && squares[0].getlabel().equals(squares[8].getlabel())){ thewinner = squares[2].getlabel(); highlightwinner(0,4,8); } else if (!squares [2].getlabel().equals("") && squares[2].getlabel().equals(squares[4].getlabel()) && squares[2].getlabel().equals(squares[6].getlabel())){ thewinner = squares[2].getlabel(); highlightwinner(2,4,6); } homecoming thewinner; } void computermove() { int selectedsquare; selectedsquare = findemptysquare("o"); if (selectedsquare == -1){ selectedsquare = findemptysquare("x"); } if ((selectedsquare == -1) && (squares[4].getlabel().equals("")) ) { selectedsquare = 4; } if (selectedsquare == -1){ selectedsquare = getrandomsquare(); } squares [selectedsquare].setlabel("o"); } int findemptysquare(string player) { int weigh[] = new int[9]; (int i=0; i<9; i++){ if(squares [i].getlabel().equals("o")) weight[i] = -1; else if (squares[i].getlabel().equals("x")) weight[i] = 1; else weight[i] = 0; } int twoweights = player.equals("o") ? -2:2; if (weight [0] + weight[1] + weight[2] == twoweights){ if(weight[0]==0) homecoming 0; else if (weight [1] == 0) homecoming 1; else homecoming 2; } if (weight [3] + weight[4] + weight[5] == twoweights){ if(weight[3]==0) homecoming 3; else if (weight [4] == 0) homecoming 4; else homecoming 5; } if (weight [6] + weight[7] + weight[8] == twoweights){ if(weight[6]==0) homecoming 6; else if (weight [7] == 0) homecoming 7; else homecoming 8; } if (weight [0] + weight[3] + weight[6] == twoweights){ if(weight[0]==0) homecoming 0; else if (weight [3] == 0) homecoming 3; else homecoming 6; } if (weight [1] + weight[4] + weight[7] == twoweights){ if(weight[1]==0){ homecoming 1; else if (weight [4] == 0) homecoming 4; else homecoming 7; } if (weight [2] + weight[5] + weight[8] == twoweights){ if(weight[2]==0) homecoming 2; else if (weight [5] == 0) homecoming 5; else homecoming 8; } if (weight [0] + weight[4] + weight[8] == twoweights){ if(weight[0]==0) homecoming 0; else if (weight [4] == 0) homecoming 4; else homecoming 8; } if (weight [2] + weight[4] + weight[6] == twoweights){ if(weight[2]==0) homecoming 2; else if (weight [4] == 0) homecoming 4; else homecoming 6; } homecoming -1; } int getrandomsquare(){ boolean gotemptysquare = false; int selectedsquare = -1; { selectedsquare = (int) (math.random() * 9); if (squares[selectedsquare].getlabel().equals("")){ gotemptysquare = true; } } while (!gotemptysquare) homecoming selectedsquare; } void highlightwinner(int win1; int win2; int win3) { squares [win1].setbackground(color.cyan); squares [win2].setbackground(color.cyan); squares [win3].setbackground(color.cyan); } void endthegame (){ newgamebutton.setenabled(true); for(int i=0;i<9;i++){ squares[i].setenabled(false); } } } }
in adition @mika's suggestion,
also in line 213 forgot close bracket:
if(weight[1]==0){ homecoming 1; else if (weight [4] == 0) homecoming 4;
should be
if(weight[1]==0){ homecoming 1; } else if (weight [4] == 0) homecoming 4;
in declaration of
void highlightwinner(int win1; int win2; int win3) {
you have ;
instead of ,
(don't know language comes from) need:
void highlightwinner(int win1, int win2, int win3) {
in getrandomsquare()
forgot semi-colon after do
while
:
} while (!gotemptysquare)
should
} while (!gotemptysquare);
typo in findemptysquare()
(forgot t
)
int weigh[] = new int[9];
should
int weight[] = new int[9];
extra }
@ end of file
squares[i].setenabled(false); } } } }
should
squares[i].setenabled(false); } } }
after these corrections code compiles
dont know if want, question compilation errors, not errors in logic.
i recomment start using ide
netbeans (it free), help debug programme self.
also improve compile programme , test after every couple of lines add, don't end screen total of errors when first compile.
good luck!
java html applet void
No comments:
Post a Comment