395 lines
12 KiB
Java
395 lines
12 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;
|
|
import java.util.HashSet;
|
|
|
|
/**
|
|
*
|
|
* Beschreibung
|
|
*
|
|
* @version 1.0 vom 30.09.2025
|
|
* @author
|
|
*/
|
|
|
|
public class Game {
|
|
|
|
public int[][] win_comb = {{0,3,6},{9,4,12}
|
|
,{18,5,21},{1,10,19},{22,13,7},{20,15,23},{11,16,14},{2,17,8}
|
|
,{0,1,2},{9,10,11},{18,19,20},{3,4,5},{12,13,14},{6,7,8},{21,22,23},{15,16,17}
|
|
};
|
|
public int[][] close_chips = {{1,3},{0,2,10},{1,17},{0,4,6},{3,5,9,12},{4,18,21} // 0 1 2 3 4 5
|
|
,{3,7},{6,8},{7,17},{4,10},{9,11,19,1},{10,16},{4,13},{7,12,14,22},{13,16} // 6 7 8 9 10 11 12 13 14
|
|
,{16,20,23},{15,17,11,14},{2,16,8},{5,19},{18,20,10},{19,15},{5,22},{21,13,23} // 15 16 17 18 19 20 21 22 22
|
|
,{22,15}
|
|
};
|
|
public String currentTurnPlayer = "White";
|
|
public int game_state = 0; // 0 == players placing their chips; 1 = players moving their chips; 2,3 = player can remove chip
|
|
public int white_chips = 3;
|
|
public int black_chips = 3;
|
|
|
|
|
|
public int white_combos = 0;
|
|
public int black_combos = 0;
|
|
|
|
public ArrayList<MuehleButton> buttons = new ArrayList<MuehleButton>();
|
|
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) {
|
|
System.out.println("id" + button.id);
|
|
if (game_state == 0) {
|
|
Chip newChip;
|
|
if (currentTurnPlayer == "White") {
|
|
newChip = addChip(button.id,button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.WHITE);
|
|
white_chips--;
|
|
setChipCount(white_chips,"White");
|
|
|
|
}
|
|
else {
|
|
newChip = addChip(button.id,button.relativeX,button.relativeY,button.absoluteX,button.absoluteY,Color.BLACK);
|
|
black_chips--;
|
|
setChipCount(white_chips,"Black");
|
|
}
|
|
checkCombo(newChip);
|
|
removeChipTurn();
|
|
//game_board.getChildren().remove(button);
|
|
button.destroy();
|
|
button.setActive(false);
|
|
}
|
|
if(game_state == 1) {
|
|
// second state of game
|
|
if(current_selection != null) {
|
|
moveChip(current_selection, button);
|
|
|
|
|
|
}
|
|
//setLabelText("Second State");
|
|
}
|
|
|
|
}
|
|
public void moveChip(Chip chip,MuehleButton target) {
|
|
Chip newChip = new Chip(target.id,0,0,target.absoluteX,target.absoluteY,chip.color,chip.game);
|
|
//MuehleButton newButton = new MuehleButton(chip.id,0,0,chip.absoluteX, chip.absoluteY,chip.game);
|
|
|
|
//buttons.remove(target);
|
|
//removeMuehleButton(target);
|
|
MuehleButton newButton = getButtonById(chip.id);
|
|
newButton.create();
|
|
target.destroy();
|
|
chips.remove(chip);
|
|
|
|
//buttons.add(newButton);
|
|
|
|
|
|
chips.add(newChip);
|
|
|
|
|
|
//game_board.getChildren().remove(target.getShape());
|
|
game_board.getChildren().remove(chip.getShape());
|
|
|
|
//addShape(newButton.getShape());
|
|
addShape(newChip.getShape());
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("id to del: " + target.id);
|
|
for (int i = 0; i < buttons.size() ; i++ ) {
|
|
System.out.print("ID: " + buttons.get(i).id + " ");
|
|
} // end of for
|
|
checkCombo(newChip);
|
|
removeChipTurn();
|
|
}
|
|
public Chip addChip(int id,int relX, int relY,double absX, double absY, Color color) {
|
|
Chip chip = new Chip(id,relX,relY,absX,absY,color,this);
|
|
chips.add(chip);
|
|
addShape(chip.getShape());
|
|
return chip;
|
|
}
|
|
public void removeChip(Chip chip) {
|
|
//MuehleButton newButton = new MuehleButton(chip.id,0,0,chip.absoluteX,chip.absoluteY,chip.game);
|
|
//buttons.add(newButton);
|
|
//game_board.getChildren().add(newButton.getShape());
|
|
MuehleButton newButton = getButtonById(chip.id);
|
|
newButton.create();
|
|
chips.remove(chip);
|
|
game_board.getChildren().remove(chip.getShape());
|
|
if (game_state == 1) {
|
|
hideAllButtons();
|
|
} // end of if
|
|
}
|
|
|
|
public void removeMuehleButton(MuehleButton button) {
|
|
for (int i = 0; i < buttons.size(); i++) {
|
|
if (buttons.get(i).id == button.id) {
|
|
//buttons.remove(i);
|
|
button.destroy();
|
|
} // end of if
|
|
} // end of for
|
|
}
|
|
public void addShape(Shape newShape) {
|
|
game_board.getChildren().add(newShape);
|
|
|
|
}
|
|
public void chip_clicked(Chip chip) {
|
|
System.out.println("Chipd id: " + chip.id);
|
|
if(game_state == 2 && chip.color == Color.BLACK) {
|
|
removeChip(chip);
|
|
white_combos--;
|
|
removeChipTurn();
|
|
|
|
}
|
|
else if (game_state == 3 && chip.color == Color.WHITE) {
|
|
removeChip(chip);
|
|
black_combos--;
|
|
removeChipTurn();
|
|
}
|
|
|
|
if (game_state == 1) {
|
|
if(currentTurnPlayer == "White" && chip.color == Color.WHITE) {
|
|
if(current_selection == null) {
|
|
current_selection = chip;
|
|
drawAvaibleMoves(current_selection);
|
|
}
|
|
else {
|
|
current_selection = chip;
|
|
hideAllButtons();
|
|
drawAvaibleMoves(current_selection);
|
|
} // end of if-else
|
|
}
|
|
else if (currentTurnPlayer == "Black" && chip.color == Color.BLACK){
|
|
if(current_selection == null) {
|
|
current_selection = chip;
|
|
drawAvaibleMoves(current_selection);
|
|
}
|
|
else {
|
|
current_selection = chip;
|
|
hideAllButtons();
|
|
drawAvaibleMoves(current_selection);
|
|
}
|
|
} // end of if-else
|
|
|
|
}
|
|
|
|
}
|
|
public void hideAllButtons() {
|
|
for (int i = 0; i < buttons.size(); i++) {
|
|
if (!buttons.get(i).isDestroyed) {
|
|
buttons.get(i).setActive(false);
|
|
} // end of if
|
|
}
|
|
}
|
|
public void drawAvaibleMoves(Chip chip) {
|
|
if(getAmouuntOfColorChips(chip.color) > 3) {
|
|
drawAvaibleMoves(chip);
|
|
}
|
|
else {
|
|
drawAllAvaibleButtons(chip);
|
|
}
|
|
}
|
|
public void drawAvaibleButtons(Chip chip) {
|
|
ArrayList<MuehleButton> avaible_buttons = new ArrayList<MuehleButton>();
|
|
for (int i = 0; i < close_chips[chip.id].length ;i++ ) {
|
|
if (!isItTaken(close_chips[chip.id][i]) && getButtonById(close_chips[chip.id][i]).isDestroyed== false) {
|
|
avaible_buttons.add(getButtonById(close_chips[chip.id][i]));
|
|
} // end of if
|
|
|
|
} // end of for
|
|
if(avaible_buttons.size() > 0) {
|
|
for (int i = 0; i < avaible_buttons.size(); i++) {
|
|
avaible_buttons.get(i).setActive(true);
|
|
} // end of for
|
|
}
|
|
System.out.println();
|
|
for (int i = 0; i < buttons.size() ; i++ ) {
|
|
System.out.print("ID: " + buttons.get(i).id + " ");
|
|
}
|
|
for (int i = 0; i < avaible_buttons.size(); i++) {
|
|
System.out.print(" " + avaible_buttons.get(i).id);
|
|
} // end of for
|
|
|
|
}
|
|
|
|
public void drawAllAvaibleButtons(Chip chip) {
|
|
//ArrayList<MuehleButton> avaible_buttons = new ArrayList<MuehleButton>();
|
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
|
if(isItTaken(buttons.get(i).id) == false) {
|
|
//avaible_buttons.add(buttons.get(i));
|
|
buttons.get(i).setActive(true);
|
|
}
|
|
} // end of for
|
|
}
|
|
|
|
public void removeChipTurn() {
|
|
if (white_combos > 0 || black_combos > 0) {
|
|
if (white_combos > 0) {
|
|
setLabelText("White, You can remove "+ white_combos + " Black Pieces");
|
|
game_state = 2;
|
|
|
|
}
|
|
if (black_combos > 0) {
|
|
setLabelText("Black, You can remove "+ black_combos + " White Pieces");
|
|
game_state = 3;
|
|
}
|
|
|
|
}
|
|
else {
|
|
nextTurn();
|
|
} // end of if-else
|
|
}
|
|
public void nextTurn() {
|
|
// end of if
|
|
if (white_chips == 0 && black_chips == 0) {
|
|
game_state = 1;
|
|
hideAllButtons();
|
|
if (current_selection != null) {
|
|
current_selection = null;
|
|
}
|
|
|
|
if(getAmouuntOfColorChips(Color.BLACK) == 2) {
|
|
setLabelText("White won!!!");
|
|
game_state = 4;
|
|
}
|
|
if(getAmouuntOfColorChips(Color.WHITE) == 2) {
|
|
setLabelText("Black won!!!");
|
|
game_state = 4;
|
|
}
|
|
|
|
}
|
|
else {
|
|
game_state = 0;
|
|
}
|
|
|
|
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 void checkCombo(Chip chip) {
|
|
checkCombo(chip.id);
|
|
}
|
|
public void checkCombo(int id) {
|
|
for (int i = 0; i < win_comb.length ;i++ ) {
|
|
for (int j = 0; j < win_comb[i].length; j++) {
|
|
if (win_comb[i][j] == id) {
|
|
Chip[] chips_to_check = {getChipById(win_comb[i][0]),getChipById(win_comb[i][1]),getChipById(win_comb[i][2])};
|
|
//System.out.println("ID: " +id + " 0:"+win_comb[i][0]+" 1:"+win_comb[i][1]+" 2:"+win_comb[i][2]);
|
|
if (checkChipsColor(chips_to_check)) {
|
|
if (getChipById(id).color == Color.WHITE) {
|
|
white_combos++;
|
|
}
|
|
else {
|
|
black_combos++;
|
|
} // end of if-else
|
|
} // end of if
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
public boolean checkChipsColor(Chip[] chips) {
|
|
if (chips[0] != null && chips[1] != null && chips[2] != null) {
|
|
if (chips[0].color == chips[1].color && chips[0].color == chips[2].color) {
|
|
return true;
|
|
}
|
|
else {
|
|
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
return false;
|
|
} // end of if-else
|
|
}
|
|
|
|
public Chip getChipById(int id) {
|
|
for (int i = 0; i < chips.size(); i++) {
|
|
if (chips.get(i).id == id) {
|
|
return chips.get(i);
|
|
} // end of if
|
|
} // end of for
|
|
return null;
|
|
}
|
|
public MuehleButton getButtonById(int id) {
|
|
for (int i = 0; i < buttons.size(); i++) {
|
|
if (buttons.get(i).id == id) {
|
|
return buttons.get(i);
|
|
} // end of if
|
|
} // end of for
|
|
return null;
|
|
}
|
|
public boolean isItTaken(int id) {
|
|
for (int i = 0; i < chips.size(); i++) {
|
|
if (chips.get(i).id == id) {
|
|
return true;
|
|
} // end of if
|
|
} // end of for
|
|
return false;
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
public int getAmouuntOfColorChips(Color color) {
|
|
int result = 0;
|
|
for (int i = 0; i < chips.size(); i++) {
|
|
if (chips.get(i).color == color) {
|
|
result++;
|
|
} // end of if
|
|
}
|
|
return result;
|
|
}
|
|
} |