/* * Created on Jul 19, 2005 * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ /* The Piece class has two uses: * 1 - Keep track of how to mark the players. This is the static data. * 2 - Return the value of an individual piece, includes location. */ import java.awt.Point; class Piece { /* First, the static members and methods to keep track of which player * has which symbol. */ static char Player1 = 'X'; static char Player2 = 'O'; static char Empty = ' '; public void swapPieces() { char temp = Player1; Player1 = Player2; Player2 = temp; } public boolean setPlayer1(char p1) { /* Returns true on error */ if(p1 == Player2 || p1 == Empty) return true; else { Player1 = p1; return false; } } public boolean setPlayer2(char p2) { /* Returns true on error */ if(p2 == Player1 || p2 == Empty) return true; else { Player2 = p2; return false; } } public boolean setEmpty(char e) { /* Returns true on error */ if(e == Player1 || e == Player2) return true; else { Empty = e; return false; } } /* Now for the iplementation for instantiated objects */ private int player; private Point location; public Piece() { /* Default value of (0,0) */ this(Empty, 0, 0); } public Piece(char p) { this(p, 0, 0); } public Piece(char p, int x, int y) { location = new Point(); set(p); set(x, y); } public Piece(Piece p) { this(p.getPiece(), p.location); } public Piece(char p, Point location) { this(p, location.x, location.y); } public Piece(int x, int y) { this(Empty, x, y); } public boolean equals(Piece p) { return (player==player && location.equals(p.location)); } /* p - char indicating marker of piece. should be Piece.Player1 or Piece.Player2 * what is really stored is the player, not the character * x - int of the x location (0,2) * y - int of the y location (0,2) * returns true on error */ public boolean set(char p, int x, int y) { return set(p) || set(x, y); } public boolean set(char p) { if(p==Player1) { player = 1; return false; } else if(p==Player2) { player = 2; return false; } else if(p==Empty) { player = 0; return false; } else return true; } public boolean set(Piece p) { return set(p.getPiece()) || set(p.location.x, p.location.y); } public boolean set(int x, int y) { /* Returns true on error */ if(x>=0 && x<3 && y>=0 && y<3) { location.setLocation(x, y); return false; } else return true; } public int getX() { return location.x; } public int getY() { return location.y; } public char getPiece() { if(player==1) return Player1; else if(player==2) return Player2; else return Empty; } }