Reputation: 69
Being new to Javafx and java, I'm having trouble picturing the design for a chess game. So I have the start method like following in my JavaFX Application-extended class:
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setTitle("ChessGame");
primaryStage.getIcons().add(new Image("file:images/icon.png"));
primaryStage.setScene(scene);
primaryStage.show();
}
protected Parent createContent(){
Pane root = new Pane();
root.setPrefSize(WIDTH*TILE_SIZE, HEIGHT*TILE_SIZE);
boardInitialize("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -");
root.getChildren().add(tileGroup);
root.getChildren().add(pieceGroup);
return root;
}
The problem is, I don't know where to put the game's logic inside my application. The game logic will handle player's turn, check for checks and checkmates, generate possible moves, etc (and later a very crude AI if possible). I have tried to jam it into the start method, but it doesn't work because the start method only runs once. Pygame with their gameloop makes much more sense than this and I can see how I would go with it. So my question is: where do I put the game logic in my application?
Upvotes: 2
Views: 1948
Reputation: 3563
I would stand upon the shoulders of others and use a library that already has some structure in it.
With that you can follow the model that build upon the experience of others and use their best practices and avoid the pitfalls they have experienced.
For JavaFX I am aware of the FXGL library, which comes with a nice list of examples of how to build a game in JavaFX.
On YouTube you can find several examples and tutorials for this library.
Upvotes: 1
Reputation: 6836
You may use the Model-View-Controller (+ Network if needed) architecture to design your system. JavaFX is really useful when working with the MVC.
Rules of Thumb:
1- Do not put your game logic inside the Model, Network and View(FXML) classes.
2- Use FXML as part of the design (it will bootstrap your work).
3- Try to achieve " Low coupling & High Coherence "
4- Some example from a fully-working MVC JavaFX project.
> /* WelcomeScreenController class (interacts with the FXML file)*/
> @FXML
> void doSignup(ActionEvent event) {
>
> user = username.getText();
> pass = password.getText();
>
> if(user != null && !user.isEmpty() && pass != null && !pass.isEmpty())
> if (checkBox.isSelected())
> GameEngine.game().getAccessManager().callSignupService(user,pass);
> else
> showNotification("You need to accept terms.");
> }
5- Try to handle button logic from the Controller class of the particular FXML file. http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
6- Create a controller class named "GameEngine" to do all your computations about the game logic. Call GameEngine's methods from the button's handle action event method.
7- Try to use Object-Oriented Programming methodologies. (Polymorphism, Inheritance, Design Patterns, etc.)
Upvotes: 1
Reputation: 76444
First of all, I would like to let you know that I have never written a Javafx application and I could be wrong.
The start method, as you correctly pointed out is the main entry point of a Javafx application, as you can see from the anatomy of a Javafx application described here. You can initialize your game there, but naturally, the moves will occur later than running the start. You will need to use event handling to handle the case when a move is being attempted by a player.
Once you are able to handle the move events, you will also be able to check whether a move is correct or not, or, to check the possible valid move options of a given player. I would like to suggest that you should have your own classes for the business logic and to avoid mixing business logic with event handling, even though the event handler will invoke business logic methods.
Upvotes: 0