国际象棋 - GUI控制台

时间:2013-12-28 04:59:42

标签: java user-interface console chess

我正在尝试将基于控制台的国际象棋游戏移动到GUI。我目前正在引用this chess tutorial来帮助我入门。我目前在电路板上只设置了一个白车用于测试目的。在我的控制台国际象棋程序中,我有一个Rook Class来确定Rook如何移动,它是什么颜色,等等。

在教程代码中,他们像这样在板上添加一块:

    // Add a few pieces to the board    
    JLabel piece = new JLabel("rl.png");
    JPanel panel = (JPanel) chessBoard.getComponent(0);
    panel.add(piece);

//  my Attempt ---------------------------------------------------//

//      String rook = "Rook";
//      ImageIcon image4 = new ImageIcon("rl.png");
//      Rook firstRook = new Rook(rook, image4);
//      
//      piece = new JLabelfirstRook);
//      panel = (JPanel) chessBoard.getComponent(0);
//      panel.add(piece);

然后可以将车辆全部移动,因为它没有适用于它的任何移动规则。我想把我的车级挂钩到JLabel的一块,这样车就受到了移动规则的约束。因此,我的问题是:

如何将我的控制台Rook Class连接到主板上的Rook?还有更好的方法吗?任何帮助表示赞赏,我已经在互联网上寻求帮助,并没有成功。谢谢!

我的车课:

public class RookTest extends ChessPiece
{ 

String name;
ImageIcon visualRep; //= new ImageIcon("rl.png");

public RookTest(String name, ImageIcon visualRep)
{
    this.name = name;
    this.visualRep = visualRep; 
}   

public String setInBoard() {
    return "| " + name + " |";
}


public String getName()
{
    return name;
}

public String getColor()
{
    String pieceColor = null;
    if (name.equals("r"))
    {
        pieceColor = "d";
    }
    else if (name.equals("R"))
    {
        pieceColor = "l";
    }
    return pieceColor;  
}

public String toString() {
    //something to find the color in here
    return "Rook";
}

//legal moves
//-------------------------------------------------------------------------------------------------------//

public boolean legalMove(ChessPiece[][] chess, int startY, int startX, int endY, int endX) 
{
    boolean thisIsAValidMove = false;

    //MOVING NORTH-SOUTH //if its in the same column and not in the same row//
    if (startY == endY && startX != endX ) 
        thisIsAValidMove = true;

    //MOVING EAST-WEST // if its in the same row but not the same column//
    else if(startX == endX && startY != endY)
        thisIsAValidMove = true;
    else
        thisIsAValidMove = false;
    return thisIsAValidMove;
}
//-------------------------------------------------------------------------------------------------------//

0 个答案:

没有答案