Project: Creating a Chess Game with GUI

I'm a fresh programmer with limited experience looking to expand upon it. This is my first project I will be attempting outside of school so my resources are rather fluctuated due to me simply not knowing what it is I need to know. However, I have some small foundation regarding basic GUI using Java.

I want to create a chess game, and later implement an AI. At the moment however, I am just building the game itself. Now, I want a simple GUI framework, it doesn't have to look pretty or be complex. Just show the board, where the pieces are, click on a piece, it will highlight where you can go, and click on the square, and voila, move finished.

Here is my main question in the design of the GUI interface. Is it done separate of the logic part? In other words, if I create a Piece, do I need to have an .jpg or something for it, OR can I just have a separate class where, if there is a pawn in this square, display a pawn picture in these coordinates. What do I need to know to build a GUI? I am mostly using Google to look around but a starting point would be appreciated, along with knowing if it's okay to just continue working on the chessboard while progressing with the GUI part at a different pace.

Thanks ahead and I'm sorry if any questions I ask are readily found information. It's possible I have touched upon it but didn't recognize the significance of it to what I want to do.

Thanks again :)


I'll just update this for anyone interested. I have a better form of what I'm going to do and have this structure in my head and translating on paper. Right now, everything will just be in Java for simplicity's sake.

Anyways, sorry for long post. No one has to read this. :)

I will attempt to use the MVC format though it still might be slightly confusing to me.

Model- I will create a Piece board[8][8]. Piece will be an abstract class and consist of the coordinates and an abstract method I'm calling right now canMove(x,y). I originally also had move(x,y) but I realized that the pieces won't be doing the moving. Therefore, I'm sticking to a canMove method that lets the controller see if the Piece can indeed do that action.

Controller- Here I will create the board and set up all the pieces. I think this will be the biggest class as it will pretty much regulate everything.

View- This is the GUI basically.

This is how I think the controller/view coordination ought to work.

  • There will be a variable in the controller called hand. When I first click on a piece, it will check to see if it's black/white, a parameter in the piece itself. If it's the right color, it gets assigned to hand. then it will check every square in the double array. if canMove() is true, highlight that square in the view. Then the second click will check canMove(). If true, check if it's occupied by a piece of same color, then move there if not. If false, nothing. If you click on hand, hand becomes empty. For me, the problem will be how the view interacts with the controller. Will figure that later.

  • At the end of every move, check every piece's canMove() to see if the King is in it. If so, check. For checkmate, I guess I will have a whole method that marks the squares around the King and sees if they're all marked, including the one he's on.

  • So I have several things I also realize I'm not accounting for that I'll have to figure out. Pawn capturing and pawn movement will be very specific. Castling. For a canMove to apply, there must be no obstruction to it in case of some pieces.

    Building view is going to be a pain since none of my classes taught me anything about it. Will get to it last, I suppose. Idk how I would test without view though so maybe not last.


    Here is my main question in the design of the GUI interface. Is it done separate of the logic part?

    Absolutely, yes.

    It is very common for the chess engine and the user interface to actually be two completely different programs, running in separate processes.

    The chess engine is typically optimized for speed and often written in C or C++ (though you could use Java if you wish). The GUI is often written in Java. The engine and GUI will often be written by completely different teams of programmers with different skill sets.

    There are standard protocols for communication, for example the Universal Chess Interface, also known as UCI. If you adopt this standard you could write your own GUI and use it with existing engines. Then if you later write your own engine that implements the UCI protocol you can just change the settings in your GUI to use your engine.

    As mentioned in the comments implementing the UCI standard might be a bit much for your first project. However I hope that knowing how it is done in the real world may provide some inspiration for whatever design you choose.


    This is a vague "question", but here are a couple of initial pointers:

  • Read up on the Model-view-controller pattern (Yes, the business logic will be in the model, and the GUI will be the view.)

  • Try using a GridLayout .

  • Use JLabel s or JButton s in your cells

  • they can easily display images
  • you can set their backgrounds to black or white, and
  • you can easily listen for clicks on them.

  • In a word, yes. Business logic and presentation should be separate. Read up on MVC. Good luck.

    EDIT: Reading up on MVP may be a good idea, as well. The main point to take away though is that your application logic should be decoupled from the display. This sounds like an ambitious project for someone new to programming - hope it's a great learning experience for you.

    链接地址: http://www.djcxy.com/p/14810.html

    上一篇: 国际象棋编程制作和取消制作

    下一篇: 项目:使用GUI创建国际象棋游戏