# chinese chess by use OOP



## teresa91198 (Apr 24, 2007)

i want to do a chinese chess game by using Object Oriented Programming Techniques.
i finished the Advisor.java and Position.java and Piece.java
http://www.sendspace.com/file/wz4ccm

but i don't know how to do the ChessBoard.java and ChineseChessGame.java. 
i want to do the chess board likes the photo of attached thumbnails. 
can you teach me??THANKS~~~~


----------



## DoubleHelix (Dec 10, 2004)

Life is so uncertain except for 3 things.

Death
Taxes
Programmers begging for help on the 'Net mere weeks before finals.

Welcome back, teresa. We haven't seen you since _last_ semester.
http://forums.techguy.org/software-development/566007-use-joptionpane-do-blackjack.html
http://forums.techguy.org/software-development/572313-reversewords.html
http://forums.techguy.org/software-development/566880-blackjack-java.html
http://forums.techguy.org/software-development/578553-about-vb-5-0-black.html
http://forums.techguy.org/software-development/625644-about-rational-problem.html


----------



## Chicon (Jul 29, 2004)

Hi teresa91198,

Welcome back ! 

Here we go !

A Chinese chess has two main properties : it has a color(black or red) and a role(soldier, cannon, elephant, ...).
And according to its role, it has a collection of moves it is allowed to perform on the board.
Therefore, I generalize the chess in a form of an abstract class that provides three methods :

```
[SIZE=2]
import java.util.List;

abstract public class Chess {
    // Constants
    final public static boolean COLOR_RED = true;
    final public static boolean COLOR_BLACK = false;
    final public static int ROLE_SOLDIER = 0;
    final public static int ROLE_CANNON = 1;
    final public static int ROLE_ELEPHANT = 2;
    //[COLOR=BLUE]... and so on[/COLOR]
   
    protected int role;
    protected boolean color;
    
    public int getRole() {
        return role;
    }
    
    public boolean isRed() {
        return color;
    } 
   
    abstract public List<Move> getAllowedMoves(Board board, int fromX, int fromY);
}[/SIZE]
```
And I design the class of the move allowed by a chess like a classic POJO (plain old Java object) :

```
[SIZE=2]
public class Move {
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}[/SIZE]
```
As an example of how to use the Chess class, I've build a Soldier class. You've just to use the same pattern for the other types of chess.
For each other types, you'll only have to change the role attribute and to rewrite the getAllowedMoves method (polymorphism).

```
[SIZE=2]
import java.util.ArrayList;
import java.util.List;

public class [COLOR=BLUE]Soldier extends Chess[/COLOR] {
      
    public Soldier(boolean color) {
        [COLOR=BLUE]this.role = Chess.ROLE_SOLDIER;[/COLOR]
        this.color = color;
    }
    
    @Override
    public List<Move> getAllowedMoves(Board board, int fromX, int fromY) {
        List<Move> list = new ArrayList<Move>();
        //[COLOR=BLUE]the method that describes the allowed moves of the soldier[/COLOR]
        return list;
    }
}[/SIZE]
```
And finally the board itself :

```
[SIZE=2]
public class Board {
    private Chess[][] board;
    
    public Board() {
        initBoard();
    }

    private void initBoard() {
        board = new Chess[10][10];
        board[3][0] = new Soldier(Chess.COLOR_BLACK);
	board[2][1] = new Cannon(Chess.COLOR_BLACK);
        board[3][2] = new Soldier(Chess.COLOR_BLACK);
        // and so on for all types of chess
    }
    
    public Chess atLocation(int x, int y) {
        return board[x][y];
    }
    
    // and so on
}[/SIZE]
```


----------



## Chicon (Jul 29, 2004)

Sorry ! When I posted my reply, I didn't notice there was an archive file to download from the link you gave. 

I'm having a look to what you've already done.


----------



## Chicon (Jul 29, 2004)

I'm back ! I've had read what you've done.
You're on the good path but I have a remark about the _Piece_ class. It's too overcrowded with business for a 'pawn'.
For example, the position of a piece must be treated at the level of the board but not at the level of the piece itself. Also, when a piece is 'captured', it is just removed from the board. It is the role of the business to keep records of discarded pieces.

In OOP, it is better to keep the more logic or business outside the objects when it's possible.


----------

