The Artima Developer Community
Sponsored Link

Java Answers Forum
URGENT TicTacToe Application

2 replies on 1 page. Most recent reply: Apr 26, 2002 6:08 PM by Justin Belcher

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Kevin Abbatessa

Posts: 1
Nickname: kevin
Registered: Apr, 2002

URGENT TicTacToe Application Posted: Apr 25, 2002 8:15 PM
Reply to this message Reply
Advertisement
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; }

if ((board [0] [0] .getLabel () == name)
&& (board [1] [1] .getLabel () == name)
&& (board [2] [2] .getLabel () == name)
) {winner = p; return true;}

if ((board [0] [2] .getLabel () == name)
&& (board [1] [1] .getLabel () == name)
&& (board [2] [0] .getLabel () == name)
) {winner = p; return true;}
return false; // no winner
}

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


Chet Underwood, Esq.

Posts: 14
Nickname: chet
Registered: Mar, 2002

Re: URGENT TicTacToe Application Posted: Apr 26, 2002 11:17 AM
Reply to this message Reply
How could a tic-tac-toe problem possibly be urgent?

Justin Belcher

Posts: 1
Nickname: jrbelcher
Registered: Apr, 2002

Re: URGENT TicTacToe Application Posted: Apr 26, 2002 6:08 PM
Reply to this message Reply
Probably because he has it due in a class soon. :-)

Flat View: This topic has 2 replies on 1 page
Topic: Get the processid of already running application Previous Topic   Next Topic Topic: why my repaint() didn't call paint() function

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use