AppletBoy


AppletBoy is a visual generic 8x8 boardgame player that shares a common design with JavaBoy. The visual version of FoxAndHounds is called "VisualFox." Here is appropriate HTML


<html>
<head>
<title> FoxAndHounds </title>
</head>
<body>
<applet code="AppletBoy.class" width=500 height=600>
<param name="WhichGame" value="VisualFox">
</applet>
</body>
</html>


Note that the design is the same as JavaBoy in that VisualFox is passed as a "param" (String) to AppletBoy. Also it is worth noting again that the Tag must begin immediately after the "<" that is "< param" works and "<  param" does not!


Next the code for AppletBoy.java


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.StringTokenizer;
import java.applet.*;
import java.lang.reflect.*;


public class AppletBoy extends java.applet.Applet implements java.awt.event.MouseListener{

Panel GamePanel = new Panel(); // This panel is used to show the game itself.
Panel MessagePanel = new Panel(); // This is the message Panel
VisualSquare[][] squarePanels= new VisualSquare[8][8];
TextField tell =new TextField();
String which_game;
Font fox_font;
int path_state=0;
Game generic_game;

public void init(){

which_game=getParameter("WhichGame");

// See note 1 below
try{
Object [] ob=new Object[1];
ob[0]=this;
Class[] cl=new Class[1];
cl[0]=Class.forName("AppletBoy");
Constructor con;
con=Class.forName(which_game).getConstructor(cl);
generic_game=(Game)con.newInstance(ob);
generic_game.player=generic_game.getFirst();
}
catch(ClassNotFoundException e)
  {System.out.println(which_game+ "could not be found");}
catch(InstantiationException e)
  {System.out.println(which_game+ "can not be instantiated");}
catch(IllegalAccessException e)
  {System.out.println(which_game+ "can not be accessed");}
catch(java.lang.reflect.InvocationTargetException e)
  {System.out.println(which_game+ " InvocationTargetException");}
catch(NoSuchMethodException e)
  {System.out.println(which_game+ " NoSuchMethodException");}

int i=0,j=0;
setFont(new Font("Serif",Font.BOLD,16));
setLayout(new BorderLayout());
setForeground(Color.red);
GamePanel.setLayout(new GridLayout(8,8));
for(i=0;i< 8;i++)
for(j=0;j< 8;j++){
  squarePanels[i][j]=new VisualSquare(generic_game.game_board[i][j]);
  squarePanels[i][j].addMouseListener(this);
if((i+j)%2==0)
    squarePanels[i][j].setBackground(Color.yellow);
else
    squarePanels[i][j].setBackground(Color.blue);
GamePanel.add(squarePanels[i][j]);
}

MessagePanel.add(tell);
add("South",tell);
add("Center",GamePanel);
generic_game.setup();
generic_game.Display();

}

//See Note 2 below;


public void mouseClicked(MouseEvent e){
  generic_game.player=generic_game.turn(generic_game.player,e);
  generic_game.Display();
}

public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mousePressed(MouseEvent e){}

}//AppletBoy


Note 1: New to Java 1.1 is the "Reflection" (java.lang.reflect), an API that allows classes to retrieve and use methods, fields, and constructors of other classes "by name." In this situation, VisualFox is a subclass of Game, however it uses AppletBoy for display. Hence, the class of AppletBoy must be passed as a parameter so that VisualFox can find the squares in which to Display() the moves. (Note JavaBoy uses System.out for display).

Note 2: We are implementing the MouseListener interface of java.awt.event.


VisualFox.java


import java.io.*;
import java.awt.event.*;
public class VisualFox extends Game{
AppletBoy f;
int path_state=0;

public String getFirst(){return "Fox";}
square TheFox;

public VisualFox(AppletBoy f) throws BadMoveException
{super(2);
this.f=f;
notify_player("The Fox goes first.");
}

public void setup(){

game_board[0][0].man=new Hound();
game_board[0][2].man=new Hound();
game_board[0][4].man=new Hound();
game_board[0][6].man=new Hound();
game_board[7][3].man=new Fox();
TheFox=game_board[7][3];
}
public String turn(String player){return player;}
public String turn(String player, MouseEvent e){
try{
if(path_state==0){
path[0]=((VisualSquare)e.getSource()).fox_or_hound;
if(path[0].man==null)throw new BadMoveException("No Piece on that square!");
if(!(player.equals(path[0].man.getPieceName())))throw new BadMoveException("Not your player");
path_state=1;
}
else{
path[1]=((VisualSquare)e.getSource()).fox_or_hound;
player=path[0].man.move(path);
if(player.equals("Win"))
notify_player("The Game is over");
else{
notify_player(player+" is next");
path_state=0;
}//player=Win
}//path_state=0
}//try

catch(BadMoveException e1){alert_player (e1);}

return player;
}

void alert_player(Exception e){notify_player(e.toString());}

public void Display(){
for(int i=0;i< 8;i++)
for(int j=0;j< 8;j++)
if(game_board[i][j].man!=null)
f.squarePanels[i][j].setText(game_board[i][j].man.getPieceName());
else
f.squarePanels[i][j].setText("");
}

void notify_player(String s){f.tell.setText(s);}


square capture_move(int pos) throws BadMoveException{
char[] b=new char[4];
int i=0,ihold,first,second;
if (pos==0)
notify_player("Input position of Piece to be moved");
else
notify_player("Input position to be moved to");
try{while((ihold=System.in.read())!='\n')
{ b[i++]=(char)ihold;
if(i==5)throw new BadMoveException("Retype Input");
}
}
catch(IOException e){}
first= new Integer(new String(b,0,1)).intValue();
second= new Integer(new String(b,2,1)).intValue();
{if((7< first)|(7< second))throw new BadMoveException("Error: Position is Out of Bounds");}
return game_board[first][second];
}

public class Fox extends Piece{
Fox(){super("Fox");}
String move(square [] sq)throws BadMoveException{
if(path[1].man!=null)throw new BadMoveException("Can not move there!");
if(!(((path[1].row-path[0].row)*(path[1].row-path[0].row)*(path[1].column-path[0].column)*(path[1].column-path[0].column))==1))
throw new BadMoveException("Can not move there!");
path[1].man=path[0].man;
path[0].man=null;
TheFox=path[1];
if(path[1].row==0)
return "Win";
else
return "Hound";}
}//Fox

public class Hound extends Piece{
Hound(){super("Hound");}
String move(square [] sq) throws BadMoveException{
if(path[1].man!=null)throw new BadMoveException("Can not move there!");
if(!(((path[1].row-path[0].row)==1)&&((path[1].column-path[0].column)*(path[1].column-path[0].column)==1)))
throw new BadMoveException("Can not move there!");
path[1].man=path[0].man;
path[0].man=null;
for(int i=0;i< 2;i++)
for(int j=0;j< 2;j++)
if(is_free(TheFox.row-1+2*i,TheFox.column-1+2*j))return "Fox";
return "Win";}
}//Hound

boolean is_free(int i,int j){
if((i< 0)||(i> 7)||(j< 0)||(j> 7))return false;
if(!(game_board[i][j].man==null))
if(game_board[i][j].man.getPieceName().equals("Hound"))
return false;
return true;
}

}//VisualFox


Game.java


import java.awt.event.*; //Needed to resolve argument in abstract method turn.
public abstract class Game {
public String player;
public square[][] game_board;
public square[] path;
public Game(int max_move_length) throws BadMoveException
{game_board= new square[8][8];
path= new square[max_move_length];
for(int i=0;i< 8;i++)
for(int j=0;j< 8;j++)
{game_board[i][j]=new square(i,j);
game_board[i][j].Name="row "+(new Integer(i).toString())+" column "+ (new Integer(j).toString());
}
}
public Game() throws BadMoveException{this(8);}

public void play(String FirstPlayer){
player=FirstPlayer;
setup();
Display();
while(!player.equals("Win")){
player=turn(player);
Display();
}
}
abstract public String getFirst();
abstract public void setup();
abstract public String turn(String player);
abstract public String turn(String player,MouseEvent e);
abstract public void Display();

}//Game


square.java, Piece.java, and BadMoveException.java


public class square{
public Piece man;
String Name;
int row, column;
square(int row, int column) throws BadMoveException{
{if((7 < row)|(7 < column))throw new BadMoveException("Error: Square to be Constructed is Out of Bounds");}
this.row=row;
this.column=column;
}
public int getRow(){return row;}
public int getColumn(){return column;}
public Piece getPiece(){return man;}
public String getName(){return Name;}
}//square

 

public abstract class Piece{
String name;
Piece(String name){
this.name=name;
}
String getPieceName(){return name;}
abstract String move(square[] sq) throws BadMoveException;
}//Piece

public class BadMoveException extends Exception{
BadMoveException(String s){
super(s);
}
}//BadMoveException


URL: https://www.umsl.edu/~siegelj/newcourse/part4/AppletBoy.html
Copyright: Jerrold Siegel for The University of Missouri -St. Louis
Last modified on 10/29/2000 14:31:40