Skip to main content
fixed some typos & removed some fluff
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

Currently, I am working on my very first bigger project in Java. It is a simple Checkers gamescheckers game, which is based on GridPaneGridPane. For now, everything is more than fine, but I occurred some problemsencountered a problem and I have got no idea how to get rid ofsolve it.

I tried to copy the method responsible for move validation and upgrade it to check isif the beating move available. Without any result for now (Mymy pawn is unable to jump over the second one).

I will be grateful for typing where is the mistake of mine. I can not trace the problem and resolve it. Thank you and forgive me :D

Currently, I am working on my very first bigger project in Java. It is a simple Checkers games, which is based on GridPane. For now, everything is more than fine, but I occurred some problems and I have got no idea how to get rid of it.

I tried to copy the method responsible for move validation and upgrade it to check is the beating move available. Without any result for now (My pawn is unable to jump over the second one).

I will be grateful for typing where is the mistake of mine. I can not trace the problem and resolve it. Thank you and forgive me :D

Currently, I am working on my very first bigger project in Java. It is a simple checkers game, which is based on GridPane. For now, everything is more than fine, but I encountered a problem and I have got no idea how to solve it.

I tried to copy the method responsible for move validation and upgrade it to check if the beating move available. Without any result for now (my pawn is unable to jump over the second one).

I can not trace the problem and resolve it.

added 1 character in body
Source Link

I tried to copy the method responsible for move validation and upgrade it to check is the batingbeating move available. Without any result for now (My pawn is unable to jump over the second one).

I tried to copy the method responsible for move validation and upgrade it to check is the bating move available. Without any result for now (My pawn is unable to jump over the second one).

I tried to copy the method responsible for move validation and upgrade it to check is the beating move available. Without any result for now (My pawn is unable to jump over the second one).

Source Link

Creating method to jump over another pawn

Currently, I am working on my very first bigger project in Java. It is a simple Checkers games, which is based on GridPane. For now, everything is more than fine, but I occurred some problems and I have got no idea how to get rid of it.

I'm trying to create a method, which will be responsible for beating another pawn in my game. To do so, my pawn has to "jump over" the second one.

I tried to copy the method responsible for move validation and upgrade it to check is the bating move available. Without any result for now (My pawn is unable to jump over the second one).

package main.checkers.board; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.geometry.VPos; import javafx.scene.layout.GridPane; import main.checkers.board.pawns.Figure; import main.checkers.board.pawns.FigureColor; import main.checkers.board.pawns.None; import main.checkers.board.pawns.Pawn; import java.util.ArrayList; import java.util.List; public class Board { private List<BoardRow> rows = new ArrayList<>(); protected FigureColor lastColor = FigureColor.BLACK; public Board() { for (int i = 0; i < 8; i++) rows.add(new BoardRow()); } public Figure getFigure(int col, int row) { return rows.get(row).getCols().get(col); } public void setFigure(int col, int row, Figure figure) { rows.get(row).getCols().add(col, figure); rows.get(row).getCols().remove(col + 1); } public void initBoard() { //SetFigure for Red Pawns setFigure(0, 0, new Pawn(FigureColor.RED)); setFigure(0, 2, new Pawn(FigureColor.RED)); setFigure(1, 1, new Pawn(FigureColor.RED)); setFigure(2, 0, new Pawn(FigureColor.RED)); setFigure(2, 2, new Pawn(FigureColor.RED)); setFigure(3, 1, new Pawn(FigureColor.RED)); setFigure(4, 0, new Pawn(FigureColor.RED)); setFigure(4, 2, new Pawn(FigureColor.RED)); setFigure(5, 1, new Pawn(FigureColor.RED)); setFigure(6, 0, new Pawn(FigureColor.RED)); setFigure(6, 2, new Pawn(FigureColor.RED)); setFigure(7, 1, new Pawn(FigureColor.RED)); //SetFigure for black Pawns setFigure(0, 6, new Pawn(FigureColor.BLACK)); setFigure(1, 5, new Pawn(FigureColor.BLACK)); setFigure(1, 7, new Pawn(FigureColor.BLACK)); setFigure(2, 6, new Pawn(FigureColor.BLACK)); setFigure(3, 5, new Pawn(FigureColor.BLACK)); setFigure(3, 7, new Pawn(FigureColor.BLACK)); setFigure(4, 6, new Pawn(FigureColor.BLACK)); setFigure(5, 5, new Pawn(FigureColor.BLACK)); setFigure(5, 7, new Pawn(FigureColor.BLACK)); setFigure(6, 6, new Pawn(FigureColor.BLACK)); setFigure(7, 5, new Pawn(FigureColor.BLACK)); setFigure(7, 7, new Pawn(FigureColor.BLACK)); } public void displayOnGrid(GridPane gridPane) { gridPane.getChildren().clear(); for (int i = 0; i < 8; i++) { //iterate over rows for (int j = 0; j < 8; j++) { Pawn pawn = new Pawn(getFigure(j, i).getColor()); if (getFigure(j, i) instanceof Pawn) { if (getFigure(j, i).getColor().equals(FigureColor.RED)) { gridPane.add(pawn.getImage(getFigure(j, i).getColor()), j, i); gridPane.setPadding(new Insets(25, 0, 25, 10)); gridPane.setHgap(0); gridPane.setVgap(0); gridPane.setAlignment(Pos.CENTER); gridPane.setValignment(gridPane, VPos.CENTER); } else { gridPane.add(pawn.getImage(getFigure(j, i).getColor()), j, i); gridPane.setPadding(new Insets(25, 0, 25, 10)); gridPane.setHgap(0); gridPane.setVgap(0); gridPane.setAlignment(Pos.CENTER); gridPane.setValignment(gridPane, VPos.CENTER); } } } } gridPane.setHgap(0); gridPane.setVgap(0); gridPane.setAlignment(Pos.CENTER); } public void move(int x1, int y1, int x2, int y2) { FigureColor color = getFigure(x1, y1).getColor(); FigureColor isThereAFigureColor = getFigure(x2, y2).getColor(); if (isRedOrBlackChoosen(color) && (color != lastColor)) { if(isThereAnotherFigure(x1, y1, x2, y2, isThereAFigureColor)) { if (isMoveDiagonalOneField(x1, y1, x2, y2, color)) { doMove(x1, y1, x2, y2); lastColor = color; } else if(isMoveGoingToKill(x1, y1, x2, y2, color) && isThereAnotherFigure(x1, y1, x2, y2, isThereAFigureColor)){ FigureColor colorOfFigureToBeKilled = getFigure(x2, y2).getColor(); if (color != colorOfFigureToBeKilled) { doKill(x1, y1, x2, y2); lastColor = color; } } } } } private boolean isMoveDiagonalOneField(int x1, int y1, int x2, int y2, FigureColor color) { return (color == FigureColor.BLACK && y1 - y2 == 1 && Math.abs(x2 - x1) == 1) || (color == FigureColor.RED && y2 - y1 == 1 && Math.abs(x2 - x1) == 1); } private boolean isMoveGoingToKill(int x1, int y1, int x2, int y2, FigureColor color) { return (color == FigureColor.BLACK && (y1+2) - y2 == 2 && Math.abs((x2+2) - x1) == 2) || (color == FigureColor.RED && (y2 +2) - y1 == 2 && Math.abs((x2+2) - x1) == 2); } private boolean isRedOrBlackChoosen(FigureColor color) { return color == FigureColor.RED || color == FigureColor.BLACK; } private boolean isThereAnotherFigure(int x1, int y1, int x2, int y2, FigureColor isThereAFigureColor){ boolean check; isThereAFigureColor = getFigure(x2, y2).getColor(); if (isThereAFigureColor == FigureColor.NONE){ check = true; }else{ check = false; } return check; } private void doMove(int x1, int y1, int x2, int y2) { Figure figure = getFigure(x1, y1); setFigure(x2, y2, figure); setFigure(x1, y1, new None()); } private void doKill(int x1, int y1, int x2, int y2) { Figure figure = getFigure(x1, y1); setFigure(x2, y2, figure); setFigure(x2-1, y2-1, new None()); setFigure(x1, y1, new None()); } } 

Up here it is my Board Class. And below is my main class. package main.checkers;

import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.stage.Stage; import main.checkers.board.Board; import main.checkers.game.Game; import static javax.swing.SwingConstants.CENTER; public class BrazilianCheckers extends Application { private Image imageback = new Image("CheckersBoard2.jpg"); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { BackgroundSize backgroundSize = new BackgroundSize(800, 800, true, true, true, false); BackgroundImage backgroundImage = new BackgroundImage(imageback, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize); Background background = new Background(backgroundImage); GridPane grid = new GridPane(); grid.setPadding(new Insets(50, 50, 50, 50)); grid.setHgap(0); grid.setVgap(0); grid.setBackground(background); grid.setAlignment(Pos.CENTER); Game game = new Game(new Board(), grid); grid.setOnMouseClicked(e -> { int x = (int) (e.getX() / 100); int y = (int) (e.getY() / 100); System.out.println(x + " " + y); game.move(x, y); }); for (int i = 0; i < 8; i++) { grid.getColumnConstraints().add(new ColumnConstraints(100)); } for (int i = 0; i < 8; i++) { grid.getRowConstraints().add(new RowConstraints(100)); } Scene scene = new Scene(grid, 800, 800, Color.BLACK); grid.setPadding(new Insets(CENTER, CENTER, CENTER, CENTER)); grid.setHgap(0); grid.setVgap(0); grid.setAlignment(Pos.CENTER); game.playGame(); primaryStage.setTitle("Brazilian Checkers"); primaryStage.setScene(scene); primaryStage.show(); } } 

I will be grateful for typing where is the mistake of mine. I can not trace the problem and resolve it. Thank you and forgive me :D