Below is my TicTacToe program it will not compile please look:
// TicTacToe.java
import java.awt.*; import java.applet.*;
public class TicTacToe extends Applet {
// 3X3 playing board and objects private Button [] [] board = new Button [3] [3]; private Panel p; private Label l;
// Players private Computer p1; private Human p2;
private Player next = p2; private Player winner = null; private int turns = 0;
public void init () { // this section will create the applet layout setLayout (new BorderLayout() );
// this will create the panel for he buttons p = new Panel (); p.setLayout (new GridLayout (3, 3)); for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) { board [r] [c] = new Button (""); p.add (board [r] [c]);
} add (p, BorderLayout.CENTER);
// creates the label at the buttom of the screen l = new Label (); add (l, BorderLayout.SOUTH);
// creates the players // this will initialize the computer as X p1 = new Computer ("X", this);
// this will initailize the chaleneger as 0 p2 = new Human ("0", this);
nextplayer(); }
public boolean gameOver () { int r, c; return (findWinner (p1) || findWinner(p2) || turns >= 9);
}
private boolean findWinner (Player p) { String name = p.getName(); for (int r = 0; r < 3; r++) if ((board [r] [0] .getLabel () == name) && (board[r] [1] .getLabel () == name) && (board [r] [2] .getLabel () == name) ) {winner = p; return true; }
for (int c = 0; c < 3; c++) if ((board [0] [c] .getLabel () == name) && (board [1] [c] .getLabel () == name) && (board [2] [c] .getLabel () == name) ) {winner = p; return true; }
public void showWinner () { if (winner == p1) l.setText ("X won"); else if (winner == p2) l.setText ("0 won"); else l.setText ("It was a draw"); }
public void nextPlayer() { if (gameOver ()) showWinner (); else { if (next == p1) next = p2; else next = p1; l.setText("Player: " + next.getName () ); turns++; next.takeTurn(); } }
public void errorFound (String s) { l.setText ("Fatal error, game terminated: " + s); }
public Button position (int r, int c) { return board [r] [c]; } } // ends class TicTacToe
// Player.java
public abstract class Player { protected String name = ""; protected TicTacToe ap;
public Player (String na, TicTacToe a) { name = na; ap = a; } public abstract void takeTurn(); public String getName () { return name; } }// ends class player
// Computer.java
import java.awt.*;
public class Computer extends Player { public Computer (String name, TicTacToe ap) { super (name, ap); }
public void takeTurn () { Button b = findMove (); if (b != null) { b.setLabel (name); b.setEnabled (false); ap.nextPlayer(); } else ap.errorFound ("computer player can not make a move!"); } private Button findMove () { for (int r=0; r < 3; r++) for (int c=0; c < 3; c++) { Button b = ap.position (r, c); if (b.isEnabled () ) return b; } return null; } }// ends class computer
// Human.java
import java.awt.*; import java.awt.event.*;
public class Human extends Player implements ActionListener { public Human (String name, TicTacToe ap) { super (name, ap); }
public void takeTurn () { for (int r=0; r < 3; r++) for (int c=0; c < 3; c++) { Button b = ap.position (r, c); if (b.isEnabled () ) b.addActionListener (this); } }
public void actionPerformed(ActionEvent e ) { Button b = (Button)e.getSource(); b.setLabel (name); b.setEnabled (false); for (int r=0; r < 3; r++) for (int c=0; c < 3; c++) { button bt = ap.position (r,c); if (bt.isEnabled () ) bt.removeActionListener (this); } ap.nextPlayer(); } }//ends class Human