This commit is contained in:
Mykola Fesenko
2025-09-30 14:53:58 +02:00
parent 5de88e7fae
commit 0d892f03bb
6 changed files with 368 additions and 65 deletions

135
Game.java
View File

@@ -2,6 +2,9 @@ import javafx.scene.shape.Shape;
import java.util.ArrayList;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.control.Label;
import javafx.scene.shape.Circle;
import java.util.AbstractList;
/**
*
* Beschreibung
@@ -22,31 +25,50 @@ public class Game {
public Pane game_board;
public Chip current_selection;
public void start(Pane gameBoard) {
public Label LTop;
public Circle[] white_player_chips = new Circle[9];
public Circle[] black_player_chips = new Circle[9];
public void start(Pane gameBoard, Label label) {
game_board = gameBoard;
LTop = label;
setLabelText("White Turn");
}
public void chip_button_clicked(MuehleButton button) {
if (game_state == 0) {
Chip newChip;
if (currentTurnPlayer == "White") {
addChip(button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.WHITE);
newChip = addChip(button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.WHITE);
white_chips--;
setChipCount(white_chips,"White");
}
else {
addChip(button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.BLACK);
newChip = addChip(button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.BLACK);
black_chips--;
setChipCount(white_chips,"Black");
}
if (checkCombo(newChip)) {
System.out.println("Comb!");
} // end of if(newChip);
game_board.getChildren().remove(button.getShape());
nextTurn();
} // end of if
}
if(game_state == 1) {
// second state of game
setLabelText("Second State");
}
}
public void addChip(int relX, int relY,double absX, double absY, Color color) {
Chip chip = new Chip(relX,relY,absX,absY,color,this);
public Chip addChip(int relX, int relY,double absX, double absY, Color color) {
Chip chip = new Chip(relX,relY,absX,absY,color,this);
chips.add(chip);
addShape(chip.getShape());
return chip;
}
public void addShape(Shape newShape) {
game_board.getChildren().add(newShape);
@@ -57,11 +79,108 @@ public class Game {
}
public void nextTurn() {
if (white_chips == 0 && black_chips == 0) {
game_state = 1;
}
if (currentTurnPlayer == "White") {
setLabelText("Black Turn");
currentTurnPlayer = "Black";
}
else {
currentTurnPlayer = "White";
setLabelText("White Turn");
} // end of if-else
}
public void setLabelText(String value) {
LTop.setText(value);
}
public boolean checkCombo(Chip chip) {
return checkCombo(chip.relativeX, chip.relativeY);
}
public boolean checkCombo(int relX, int relY) {
ArrayList<Chip> hChips = getHorizontalCloseChips(relX,relY);
ArrayList<Chip> vChips = getHorizontalCloseChips(relX,relY);
System.out.println(hChips.get(0).relativeX);
Chip start_chip = getChipByPos(relX,relY);
if (start_chip.color == hChips.get(0).color && start_chip.color == hChips.get(1).color) {
return true;
}
if (start_chip.color == vChips.get(0).color && start_chip.color == vChips.get(1).color) {
return true;
}
return false;
}
public ArrayList<Chip> getHorizontalCloseChips(int relX, int relY) {
ArrayList<Chip> result = new ArrayList<Chip>();
if (relX == 0) {
result.add(getChipByPos(1,relY));
result.add(getChipByPos(2,relY));
}
else if ( relX == 1) {
result.add(getChipByPos(0,relY));
result.add(getChipByPos(2,relY));
}
else {
result.add(getChipByPos(0,relY));
result.add(getChipByPos(1,relY));
} // end of if-else
return result;
}
public ArrayList<Chip> getVerticalCloseChips(int relX, int relY) {
ArrayList<Chip> result = new ArrayList<Chip>();
if (relY == 0) {
result.add(getChipByPos(relX,1));
result.add(getChipByPos(relX,2));
}
else if ( relY == 1) {
result.add(getChipByPos(relX,0));
result.add(getChipByPos(relX,2));
}
else {
result.add(getChipByPos(relX,0));
result.add(getChipByPos(relX,1));
} // end of if-else
return result;
}
public Chip getChipByPos(int relX, int relY) {
for (int i = 0; i < chips.size()-1; i++) {
if (chips.get(i).relativeX == relX && chips.get(i).relativeY == relY ) {
return chips.get(i);
} // end of if
}
return null;
}
public void setChipCount(int value, String player) {
System.out.println(white_chips-1);
if (player == "White") {
if (white_chips == 0) {
for (int i = 0; i < white_player_chips.length -1; i++) {
white_player_chips[i].setVisible(false);
}
}
for (int i = white_player_chips.length-1; i >= value; i--) {
white_player_chips[i].setVisible(false);
}
}
else {
{
if (white_chips == 0) {
for (int i = 0; i < black_player_chips.length -1; i++) {
black_player_chips[i].setVisible(false);
} // end of for
} // end of if
for (int i = black_player_chips.length-1; i >= value; i--) {
black_player_chips[i].setVisible(false);
}
}
} // end of if-else
}
}