title: Java programming - Chess moves (basic, no AI)
body: <p>I need help in designing a Chess game. I've already started but haven't got far as I'm pretty new to Java, new to programming at all actually.</p>
  
  <p>Anyway, I have my abstract class Piece and the various pieces as subclasses.
  I have a method movePiece, in my abstract class, which I want to define for all subclasses.</p>
  
  <p>All it currently does is move the piece from one square to another. I have a Square class which can hold a Piece object, the board consists of a 64x1 Square array.</p>
  
  <p>I know how pieces move, but how do I actually do the programming?
  I want to try to apply the MVC pattern but this is really the first time I will be using patterns.</p>
  
  <p>Basically I was thinking on using Graphics2D to create a box for each Square. Then when a player clicks a piece, the squares that are available as destination after the move will be outlined in some colour. After the player clicks one of these squares, the code that I already have in my movePiece method will run.</p>
  
  <p>What I want to do is override my movePiece method in each subclass of Piece. The question is, how could the code look in one of these methods? take the Pawn subclass for example.</p>
  
  <p>I'm not asking for code to copy/paste, just some pointers on how to do this, eventually some sample code.</p>
  
  <p>Thanks!</p>
  
  <pre><code>public class Game {
  
  
  @SuppressWarnings("unused")
  public static void main(String[] args){
      Board board = new Board();
  } }
  
  public class Board {
  
  Square[] grid;
  
  public Board(){
      grid = new Square[64];
  }   
  public Square getSquare(int i){
      return grid[i];
  }   
  public void setDefault(){
  
  }   
  public Boolean isMoveValid(){
      return null;    
  } }
  
  public class Square {
  
  private Piece piece;
  
  public void addPiece(Piece pieceType, String pieceColour, String pieceOwner) 
          throws ClassNotFoundException, InstantiationException, IllegalAccessException{
  
      PieceFactory factory = new PieceFactory();
      Piece piece = factory.createPiece(pieceType);
  
      piece.setColour(pieceColour);
      piece.setOwner(pieceOwner);
  
      this.piece = piece; 
  }
  public void addPiece(Piece pieceType){ 
      this.piece = pieceType; 
  }
  public void removePiece(){  
      piece = null;
  }
  public Piece getPiece(){
      return piece;       
  }
  
  class PieceFactory {     
       @SuppressWarnings("rawtypes")
       public Piece createPiece(Piece pieceType) 
              throws ClassNotFoundException, InstantiationException, IllegalAccessException{
           Class pieceClass = Class.forName(pieceType.toString());
           Piece piece = (Piece) pieceClass.newInstance();
  
           return piece;       
       } }
  
  public void setColour(String colour){
  
  } }
  
  public abstract class Piece {
  
  Board board;
  
  public void setColour(String pieceColour) {
  }
  
  public void setOwner(String pieceOwner) {
  }
  
  public String getColour() {
      return "";
  }
  
  public String getOwner() {
      return "";      
  }
  public void movePiece(int oldIndex, int newIndex){
      board.getSquare(oldIndex).removePiece();
      board.getSquare(newIndex).addPiece(this);
  }
  public String toString(){
      return this.getClass().getSimpleName();
  } }
  </code></pre>
  
  <p>You wanted to see the code, very basic I know. And I will change the [64] to [8][8].
  I'm trying to not make it harder then it has to be. I can probably combine Colour and Owner as an attribute and make it an enum (either BLACK or WHITE).</p>
  
  <p>Sorry if the formatting isn't good.</p>