Results 1 to 2 of 2

Thread: Chess program

  1. #1
    Chro is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Question Chess program

    Hey everyone, I'm trying to make a simple human vs. human chess program using BlueJ.

    I am very new to Java and just trying stuff out.

    My issue is that I don't know how I should structure the program. At the moment I have just a class called Board which has an array which says either null or bQ, bR, wP etc in each cell. So this says where the pieces on the board are. The class initializes the starting positions.

    This class has a function which displays the board and pieces in the terminal (as seen in the picture below). The second part of the picture shows the board with the array element numbers (another function).



    The users are going to be able to make moves by inputting the reference of the piece they want to move (e.g. C5) and the place they are moving it to. I don't have any problems with writing code to check if pieces can be moved where the user wants etc. but I don't know if I should have the pieces as individual objects from a separate class or something else.

    For example, if the user says they want to move a bishop to a certain place should I have the board class identify the type of piece and then use the bishopMoveCheck function in the Pieces class to see if it's a legal move and then have a function in the Board class make the move? Or should I have the bishop, as an object in the Pieces class, do this in another way? I just don't know what the best way is.

    As a newbie I probably don't fully understand the usefulness of classes and objects. I'd probably just write everything in one class and without objects if left to my own devices!

    Any guidance would be ace!
    Thanks, Alex

    PS, I'm studying comp sci at University but this is my own private project.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You would definately benefit from a more object oriented aproach. You could have a class Piece, and make subclasses Pawn, Bishop etc... This way, it would be much easier to define legal moves the pieces can make. If you just make a String array, you'd need a monstrosity like this to determine the moves:
    Java Code:
    if(piece.equals("bQ")) {
      if(targetPosition.isValidBlackQueenMove())
        //tra la la
    }
    else if(piece.equals("bP") {
      if(targetPosition.isValidBlackPawnMove())
        //bla bla bla
    }
    And so on for every piece. If your structure would be like this:
    Java Code:
    public abstract class Piece {
      private char color; //'b' and 'w'
      private int posX, posY;
      //constructor
      public abstract boolean isValidMove(int x, int y);
    }
    
    public class Bishop extends Piece {
      public Bishop(int x, int y, char color) {
        super(x,y,color);
      }
    
      public boolean isValidMove(int x, int y) {
        //determine the validity of the move
      }
    }
    This way, you can have an array of Piece objects, and each one knows how it can move via the overridden isValidMove() method.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. a good java chess program
    By rico16135 in forum New To Java
    Replies: 10
    Last Post: 11-06-2010, 12:25 PM
  2. Chess, Variable Loosing it.s Value
    By michail in forum New To Java
    Replies: 6
    Last Post: 01-24-2010, 01:54 PM
  3. chess game
    By michail in forum New To Java
    Replies: 13
    Last Post: 01-12-2010, 07:24 AM
  4. Chess game
    By michail in forum New To Java
    Replies: 4
    Last Post: 12-23-2009, 06:51 PM
  5. New Chess-Like game
    By jSarK0Y in forum Reviews / Advertising
    Replies: 3
    Last Post: 06-10-2009, 03:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •