Files
muehle/Game.java
Mykola Fesenko db2b030bd4 13.10
2025-10-13 11:49:49 +02:00

192 lines
5.6 KiB
Java

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;
/**
*
* Beschreibung
*
* @version 1.0 vom 30.09.2025
* @author
*/
public class Game {
public String currentTurnPlayer = "White";
public int game_state = 0; // 0 == players placing their chips; 1 = players moving their chips; 2 = player can remove chip
public int white_chips = 9;
public int black_chips = 9;
public ArrayList<Shape> buttons = new ArrayList<Shape>();
public ArrayList<Chip> chips = new ArrayList<Chip>();
public Pane game_board;
public Chip current_selection;
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") {
newChip = addChip(button.ring,button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.WHITE);
white_chips--;
setChipCount(white_chips,"White");
}
else {
newChip = addChip(button.ring,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();
}
if(game_state == 1) {
// second state of game
setLabelText("Second State");
}
}
public Chip addChip(int ring,int relX, int relY,double absX, double absY, Color color) {
Chip chip = new Chip(ring,relX,relY,absX,absY,color,this);
chips.add(chip);
addShape(chip.getShape());
return chip;
}
public void addShape(Shape newShape) {
game_board.getChildren().add(newShape);
}
public void chip_clicked(Chip chip) {
System.out.println(chip.relativeX);
}
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, chip.ring);
}
public boolean checkCombo(int relX, int relY, int ring) {
ArrayList<Chip> hChips = getHorizontalCloseChips(relX,relY,ring);
ArrayList<Chip> vChips = getVerticalCloseChips(relX,relY);
Chip start_chip = getChipByPos(relX,relY);
System.out.println("Size" + hChips.toString());
if (hChips.get(0) != null && hChips.get(1) != null ) {
if (start_chip.color == hChips.get(0).color && start_chip.color == hChips.get(1).color) {
return true;
}
}
if(vChips.get(0) != null && vChips.get(1) != null) {
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, int ring) {
ArrayList<Chip> result = new ArrayList<Chip>();
System.out.println("Target: " + ring);
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
System.out.println("Result: " + result.toString());
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 ) {
System.out.println("X" + chips.get(i).relativeX);
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
}
}