Hey I'm going through your tutorial now. But for some reason when it displays the board the top row and left most column are solid white. Any idea what I could be off on? I have my x and y for the for loops starting at 0. Note, when the pieces are put on the board they're distributed normally and not off.
Thank you for this wonderful tutorial! Your voice is pleasing lol. I am new to programming and new to JavaFX. Question: why did you create the method, makePiece and the logic for setOnMouseReleased, inside of the CheckersApp class instead of inside the constructor for Piece? And therefore seperated the OnMouseReleased (which is inside makePiece) from the MouseClicked, MouseDragged events that are declared inside the Piece constructor. I guess its a two part question. 1) Why are you using makePiece instead of using the Piece constructor. 2) Why did you seperate the MouseReleased from the MouseClicked, MouseDragged. Thank you, Trevor
Hi, it's been a while so I don't quite remember why. From a quick skim through, it seems I needed access to some methods, like tryMove(). Most of the JavaFX tutorials I've done are goal-oriented and may not follow good programming principles, so tread carefully. For proper software design with JavaFX, here's a great resource: docs.oracle.com/javase/8/javase-clienttechnologies.htm
I'm a beginner, just found this and it looks awesome. I implemented your code and now trying to add stuff to make more like an actual game. I recently added a method to turn regular pieces into kings. Although I'm still puzzled at how to make the kings move freely. I kind of get the basic idea of how it works, I just don't know how to implement it into the code. If anyone here can kindly clear this up for me, it'll be a huge help :)
think of it like 2 dementional array, if you sum x and y %2, in each position where the sum of them is even then a white tile will be placed if not the green one will be placed and so on
I think the algorithm in ChekcerApp's toBoard() method is a bit off. It returns two different coordinates per square depending on where you click on the chess piece. If you change the algorithm to something like "return (int) ((pixel + SQUARE_SIZE) / SQUARE_SIZE) - 1;" instead, you get correct x and y coordinates no matter where on the chess piece you click.
Yes, you are correct. The existing code only works because the pieces are snapped to corners. If we allowed free movement, the current code would break. Nice work spotting that! The code is here - github.com/AlmasB/FXTutorials/blob/master/src/com/almasb/checkers/CheckersApp.java Can you pull a request with changes and I'll merge that.
I've run the updated code and the movement felt awkward. I realized that TILE_SIZE / 2 was deliberately allowing the user to 'drop' the piece near the tile and it would stick. Hence, adding a smoother experience. Having said that, your approach revealed another bug the code previously had. If you grab the piece, wander off the screen and drop it there, the board will still try to get coordinates for that. This has now been fixed, so thank you for that. I should, of course, point out that your approach is correct for tile-based games, it's just here we use the layout X and Y of the piece, as well as considering the center point of the piece and thus it's an edge case.
So I've written the code and run the program, and everything works except that all the pieces go into the top left corner of the board? Any idea why this is happing?
So this code just sets up the board? Would you recommend another class or 2 for the actual game logic? In the real game, when a checker gets to the end it can change direction, would I have to adjust the pieceTypes class which contains: red(1) white(-1) as surely they need to be able to go more than one way
Hi, having game logic separate is a good idea. Since this is just a basic demo, you will find that some of the code may not scale well to an actual game. I haven't actually thought about the software design of the full game, so take the following recommendation with caution. Have a boolean flag to check if a piece has reached the end. Then, in tryMove() have another condition to check that flag, in which case you would need to compute both directions.
great tutorial ;) but it gives me this error whenever i,m trying to use *setOnMouesPressed* or *setOnMouseDragged* "lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)" can you plz help me with that?
i dont understand this method, setFill(light ? Color.valueOf("#feb") : Color.valueOf("#582")); why do you have a question mark inside the parameter and why not this way setFill(Color.valueOf("#feb");
+Moha Ibra Hi, ? and : together form a shorthand for value = if (boolean expression) then value1 else value2, so this becomes now value = boolean expression ? value1 : value2
Yes if the ai component can be develop with undo redo, other advance features can be added this will make the game highly intelligent and with difficulty level implemented using min-max and alpha plunning and other algorithm such as neural network I believe it will help so much Great work ...Thanks
Hi, i have some questions, how can i re draw the board? and most important if i save the position of a tile(oldX and oldY) in an array, if in the future i want to change something of that tile(value), can i do it without clicking in the tile?
Hi Almas, Need to add the rules for when a piece becomes a king so it can move anywhere and to draw a king symbol on a piece when it becomes a king. How we write it?
Hi, checking when a piece should ascend to a king is simple: use the 2D grid bounds, e.g. 0 and size - 1. If a piece reaches the opposite side then it should be upgraded. The movement is a bit tricky: when you move a piece you should also check if it's a king type, if it is, then allow to move accordingly. As for the drawing, that should be easy. A Piece is a StackPane, when the "king" status is obtained, just update the children of a Piece by replacing the existing ones with a new image / sprite.
hey almas one question how we could call the class CheckersApp from a menu that we make with javafx, we got a main java and we are trying to use your checkersApp for testing it, and we cant. how we can use checkersApp like an object and call it from our menu .Thank you for help us
Hi, JavaFX cannot run two applications under a single runtime. As such, what you are describing cannot be readily done. If you want to add a menu to Checkers, you will need to create an intermediate Scene or Parent. From within the menu you should be able to create a basic event handler that will allow you to switch between root nodes, i.e. menu root and game root.
Could you do a tutorial on how to implement the kings in checkers as well as Minimax/Alpha Beta with this? If you have source code already, could you please link it?
Hi, generally if starting from no programming background, I'd expect 2nd, maybe 3rd year computer science students to be able to produce something similar. Having said that, if one focuses on just a single technology, e.g. first learn programming basics in Java, then move straight to JavaFX, it is potentially possible to acquire skills within a year. Nevertheless, each person is different and has a different background, hence the times I've given are just rough estimates.
+Hans Baum Hi, yes, you will, however, need to adjust the game size to the screen size, so that mouse events are mapped correctly. Everything else should work just fine.
Hello, your video is awesome! I am currently trying to add some commands to this game. a reset button, a player vs player etc. Just some GUI to it. What I actually want to do is add this code to another Pane so it doesn't take all the space of the scene. Could you help me doing this thank you.!
now i have a different problem i get Graphics Device initialization failed for : d3d, sw Error initializing QuantumRenderer: no suitable pipeline found java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
СПбПУ!!!!!! Если у вас задание шашки, то аккуратнее! Преподы знают об этом видео
спасибо, добрый человек. дико крутые туториалы)
Need to add the rules for when a piece becomes a king so it can move anywhere and to draw a king symbol on a piece when it becomes a king.
Hey I'm going through your tutorial now. But for some reason when it displays the board the top row and left most column are solid white. Any idea what I could be off on? I have my x and y for the for loops starting at 0.
Note, when the pieces are put on the board they're distributed normally and not off.
Thank you for this wonderful tutorial! Your voice is pleasing lol. I am new to programming and new to JavaFX.
Question: why did you create the method, makePiece and the logic for setOnMouseReleased, inside of the CheckersApp class instead of inside the constructor for Piece? And therefore seperated the OnMouseReleased (which is inside makePiece) from the MouseClicked, MouseDragged events that are declared inside the Piece constructor.
I guess its a two part question. 1) Why are you using makePiece instead of using the Piece constructor.
2) Why did you seperate the MouseReleased from the MouseClicked, MouseDragged.
Thank you,
Trevor
Hi, it's been a while so I don't quite remember why. From a quick skim through, it seems I needed access to some methods, like tryMove(). Most of the JavaFX tutorials I've done are goal-oriented and may not follow good programming principles, so tread carefully. For proper software design with JavaFX, here's a great resource: docs.oracle.com/javase/8/javase-clienttechnologies.htm
I'm a beginner, just found this and it looks awesome. I implemented your code and now trying to add stuff to make more like an actual game. I recently added a method to turn regular pieces into kings. Although I'm still puzzled at how to make the kings move freely. I kind of get the basic idea of how it works, I just don't know how to implement it into the code. If anyone here can kindly clear this up for me, it'll be a huge help :)
Hello Almas,
good tutorial. Do you could explain me, wich is the idea for this?
Tile tile = new Tile ((x + y) % 2 == 0, x , y);
think of it like 2 dementional array, if you sum x and y %2, in each position where the sum of them is even then a white tile will be placed if not the green one will be placed and so on
I think the algorithm in ChekcerApp's toBoard() method is a bit off. It returns two different coordinates per square depending on where you click on the chess piece. If you change the algorithm to something like "return (int) ((pixel + SQUARE_SIZE) / SQUARE_SIZE) - 1;" instead, you get correct x and y coordinates no matter where on the chess piece you click.
Yes, you are correct. The existing code only works because the pieces are snapped to corners. If we allowed free movement, the current code would break. Nice work spotting that! The code is here - github.com/AlmasB/FXTutorials/blob/master/src/com/almasb/checkers/CheckersApp.java Can you pull a request with changes and I'll merge that.
Ok, cool thanks - I'll do that
I've run the updated code and the movement felt awkward. I realized that TILE_SIZE / 2 was deliberately allowing the user to 'drop' the piece near the tile and it would stick. Hence, adding a smoother experience. Having said that, your approach revealed another bug the code previously had. If you grab the piece, wander off the screen and drop it there, the board will still try to get coordinates for that. This has now been fixed, so thank you for that. I should, of course, point out that your approach is correct for tile-based games, it's just here we use the layout X and Y of the piece, as well as considering the center point of the piece and thus it's an edge case.
Great tutorial. Many thanks.
So I've written the code and run the program, and everything works except that all the pieces go into the top left corner of the board? Any idea why this is happing?
So this code just sets up the board? Would you recommend another class or 2 for the actual game logic? In the real game, when a checker gets to the end it can change direction, would I have to adjust the pieceTypes class which contains: red(1) white(-1) as surely they need to be able to go more than one way
Hi, having game logic separate is a good idea. Since this is just a basic demo, you will find that some of the code may not scale well to an actual game. I haven't actually thought about the software design of the full game, so take the following recommendation with caution. Have a boolean flag to check if a piece has reached the end. Then, in tryMove() have another condition to check that flag, in which case you would need to compute both directions.
Hello this tutorial is awesome, can you please make a video on how to check who wins and how to tell the player its their turn to play.
great tutorial ;) but it gives me this error whenever i,m trying to use *setOnMouesPressed* or *setOnMouseDragged*
"lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)"
can you plz help me with that?
Hi, the error says you are using java 7. Lambda expressions were introduced in java 8, so to fix, ensure your IDE uses java 8.
i dont understand this method, setFill(light ? Color.valueOf("#feb") : Color.valueOf("#582")); why do you have a question mark inside the parameter and why not this way setFill(Color.valueOf("#feb");
+Moha Ibra Hi, ? and : together form a shorthand for
value = if (boolean expression) then value1 else value2, so this becomes now
value = boolean expression ? value1 : value2
i get it thanks,
is there a way to move backwards??
Yes if the ai component can be develop with undo redo, other advance features can be added this will make the game highly intelligent and with difficulty level implemented using min-max and alpha plunning
and other algorithm such as neural network I believe it will help so much Great work ...Thanks
Hi, i have some questions, how can i re draw the board? and most important if i save the position of a tile(oldX and oldY) in an array, if in the future i want to change something of that tile(value), can i do it without clicking in the tile?
Nice. Have a question. How i can add possible to kill backwards.
Ok i got it. Just remove && newY - y0 == piece.getType().moveDir * 2 (commented)
if (Math.abs(newX - x0) == 1 && newY - y0 == piece.getType().moveDir) {
System.out.println("q");
return new MoveResult(MoveType.NORMAL);
/* THESE ELSE IF */
} else if (Math.abs(newX - x0) == 2){
int x1 = x0 + (newX - x0) / 2;
int y1 = y0 + (newY - y0) / 2;
if (board[x1][y1].hasPiece() && board[x1][y1].getPiece().getType() != piece.getType()) {
System.out.println("w");
return new MoveResult(MoveType.KILL, board[x1][y1].getPiece());
}
}
Hi Almas, Need to add the rules for when a piece becomes a king so it can move anywhere and to draw a king symbol on a piece when it becomes a king. How we write it?
Hi, checking when a piece should ascend to a king is simple: use the 2D grid bounds, e.g. 0 and size - 1. If a piece reaches the opposite side then it should be upgraded. The movement is a bit tricky: when you move a piece you should also check if it's a king type, if it is, then allow to move accordingly. As for the drawing, that should be easy. A Piece is a StackPane, when the "king" status is obtained, just update the children of a Piece by replacing the existing ones with a new image / sprite.
hey almas one question how we could call the class CheckersApp from a menu that we make with javafx, we got a main java and we are trying to use your checkersApp for testing it, and we cant. how we can use checkersApp like an object and call it from our menu .Thank you for help us
Hi, JavaFX cannot run two applications under a single runtime. As such, what you are describing cannot be readily done. If you want to add a menu to Checkers, you will need to create an intermediate Scene or Parent. From within the menu you should be able to create a basic event handler that will allow you to switch between root nodes, i.e. menu root and game root.
Could you do a tutorial on how to implement the kings in checkers as well as Minimax/Alpha Beta with this? If you have source code already, could you please link it?
Almas ? how many years of practice are needed to be master like this ?
Hi, generally if starting from no programming background, I'd expect 2nd, maybe 3rd year computer science students to be able to produce something similar. Having said that, if one focuses on just a single technology, e.g. first learn programming basics in Java, then move straight to JavaFX, it is potentially possible to acquire skills within a year. Nevertheless, each person is different and has a different background, hence the times I've given are just rough estimates.
hi. Can you use the same code to run it with gluon on android and ios devices?
+Hans Baum Hi, yes, you will, however, need to adjust the game size to the screen size, so that mouse events are mapped correctly. Everything else should work just fine.
Thanks a lot
Hello, your video is awesome!
I am currently trying to add some commands to this game. a reset button, a player vs player etc. Just some GUI to it.
What I actually want to do is add this code to another Pane so it doesn't take all the space of the scene. Could you help me doing this thank you.!
What is the name of program where you creating that?
It's called IntelliJ IDEA. Here's the link: www.jetbrains.com/idea/
thanks!!!
import static com.almasb.checkers.CheckersApp.TILE_SIZE;
Why import, why not class variable?
what am i supposed to import?
+Felipe Figueiredo Hi, the link to full source code is in the video description.
how do you install javafx on android studio i cant imortjavafx
now i have a different problem i get Graphics Device initialization failed for : d3d, sw
Error initializing QuantumRenderer: no suitable pipeline found
java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
Can you please make Ludo King Game in javaFX.
code is not available anymore
Thanks, updated the link: github.com/AlmasB/FXTutorials/tree/master/src/main/java/com/almasb/checkers