Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use enums for representing the state of a figure #44

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions BackEnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void playerMove() {
//if base not empty move a player out of base
else if (!isBaseEmpty(activePlayer) && randomNumber == 6) {
for (int i = activePlayer * 4; i < activePlayer * 4 + 4; i++){
if (figures[i].inBase){
if (figures[i].isInBase()){
figures[i].placeOption = true;
}
}
Expand All @@ -120,7 +120,7 @@ private void playerMoveOnField() {
//make user figure chooser for all figures on the gamefield or in the house
if (!beatsPossible){
for (int i = activePlayer * 4; i < activePlayer * 4 + 4; i++) {
if (!figures[i].finished && !figures[i].inBase) {
if (figures[i].isMovable()) {
figures[i].placeOption = true;
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ private void botMove(){
//if base not empty move a player out of base
else if (!isBaseEmpty(activePlayer) && randomNumber == 6) {
for (int i = activePlayer * 4; i < 16; i++){
if (figures[i].inBase){
if (figures[i].isInBase()){
moveFigure(i, randomNumber);
break;
}
Expand All @@ -202,7 +202,7 @@ else if (!isBaseEmpty(activePlayer) && randomNumber == 6) {
//make user figure chooser for all figures of the player
if (!beatsPossible){
for (int i = activePlayer * 4; i < activePlayer * 4 + 4; i++) {
if (!figures[i].finished && !figures[i].inBase) {
if (figures[i].isMovable()) {
moveFigure(i, randomNumber);
break;
}
Expand All @@ -228,7 +228,7 @@ public void moveFigure(int figureNumber, int stepLength) {
int figureColor = figures[figureNumber].color;

//check if the figure isn't finished and not in the base
if(!figures[figureNumber].finished && !figures[figureNumber].inBase){
if(figures[figureNumber].isMovable()) {
//store the old and new field-number in local variables
int numberOld = figures[figureNumber].field;
int cache = numberOld + stepLength;
Expand All @@ -238,7 +238,7 @@ public void moveFigure(int figureNumber, int stepLength) {
}

//check if the figure is on the gamefield
if (!figures[figureNumber].inHouse){
if (figures[figureNumber].isOnField()) {
//check if a figure would come over its own startfield
boolean goToHouse = false;
if (numberOld < figureColor * 10 && cache >= figureColor * 10){
Expand Down Expand Up @@ -278,7 +278,7 @@ public void moveFigure(int figureNumber, int stepLength) {
}
if (movePossible){
toMove--;
figures[figureNumber].inHouse = true;
figures[figureNumber].setInHouse();
figures[figureNumber].field = figureColor*4;
if (toMove > 0){
moveInHouse(figureNumber, toMove);
Expand Down Expand Up @@ -306,15 +306,15 @@ else if (figureOnField(numberNew) == 99){
}
}
//if figure is in the base and the step-length is 6 move figure out of base
else if (figures[figureNumber].inBase && stepLength == 6) {
else if (figures[figureNumber].isInHouse() && stepLength == 6) {
moveOutOfBase(figureNumber);
}
}

//move figure in the house by the given value
private void moveInHouse(int figureNumber, int steplength){
//if-loop preventing false moves by mistake
if (figures[figureNumber].inHouse && !figures[figureNumber].finished){
if (figures[figureNumber].isInHouse()){
//store the figure-field and the figure-color as local variable
int figureField = figures[figureNumber].field;
int figureColor = figures[figureNumber].color;
Expand All @@ -338,7 +338,7 @@ private void moveInHouse(int figureNumber, int steplength){

//check if a beat is possible
private boolean beatPossible(int figureNumber, int stepLength){
if (!figures[figureNumber].inBase && !figures[figureNumber].inHouse){
if (figures[figureNumber].isOnField()) {
//store some useful variables
int numberOld = figures[figureNumber].field;
int cache = numberOld + stepLength;
Expand Down Expand Up @@ -367,7 +367,7 @@ private boolean beatPossible(int figureNumber, int stepLength){

//move the given figure to the base
public void moveToBase(int figureNumber){
figures[figureNumber].inBase = true;
figures[figureNumber].setInBase();
figures[figureNumber].field = figureNumber;
}

Expand All @@ -387,7 +387,7 @@ private int giveColor(int figureNumber){
//check if a player has won (return true/false)
private boolean finished() {
for (int i = 0; i < 4; i++) {
if (figures[i].finished && figures[i + 1].finished && figures[i + 2].finished && figures[i + 3].finished) {
if (figures[i].isFinished() && figures[i + 1].isFinished() && figures[i + 2].isFinished() && figures[i + 3].isFinished()) {
return true;
}
}
Expand All @@ -396,11 +396,11 @@ private boolean finished() {

//check which player has won
private int whoFinished(){
if (figures[0].finished && figures[1].finished && figures[2].finished && figures[3].finished){
if (figures[0].isFinished() && figures[1].isFinished()&& figures[2].isFinished() && figures[3].isFinished()){
return 0;
} else if (figures[4].finished && figures[5].finished && figures[6].finished && figures[7].finished) {
} else if (figures[4].isFinished() && figures[5].isFinished() && figures[6].isFinished() && figures[7].isFinished()) {
return 1;
} else if (figures[8].finished && figures[9].finished && figures[10].finished && figures[11].finished) {
} else if (figures[8].isFinished() && figures[9].isFinished() && figures[10].isFinished() && figures[11].isFinished()) {
return 2;
} else {
return 3;
Expand All @@ -413,12 +413,12 @@ private void checkFiguresIfFinished() {
int cache = figureOnHouseField(i);
if (cache != 99) {
if (i == 15 || i == 11 || i == 7 || i == 3) {
figures[cache].finished = true;
figures[cache].setFinished();
} else {
int figureDeeper = figureOnHouseField(i + 1);
if (figureDeeper != 99) {
if (figures[figureDeeper].finished) {
figures[cache].finished = true;
if (figures[figureDeeper].isFinished()) {
figures[cache].setFinished();
}
}
}
Expand All @@ -429,7 +429,7 @@ private void checkFiguresIfFinished() {
//check which figure is on the normal field
public int figureOnField(int fieldNumber){
for (int i = 0; i < figures.length; i++){
if (figures[i].field == fieldNumber && !figures[i].inBase && !figures[i].inHouse){
if (figures[i].field == fieldNumber && figures[i].isOnField()) {
return i;
}
}
Expand All @@ -439,7 +439,7 @@ public int figureOnField(int fieldNumber){
//check which figure is on the house field
public int figureOnHouseField(int fieldNumber){
for (int i = 0; i < figures.length; i++){
if (figures[i].inHouse){
if (figures[i].isInHouse()){
if (figures[i].field == fieldNumber){
return i;
}
Expand All @@ -450,7 +450,7 @@ public int figureOnHouseField(int fieldNumber){

public int figureOnBaseField(int fieldNumber){
for (int i = 0; i < figures.length; i++){
if (figures[i].inBase) {
if (figures[i].isInBase()) {
if (figures[i].field == fieldNumber){
return i;
}
Expand All @@ -463,7 +463,7 @@ public int figureOnBaseField(int fieldNumber){
private boolean isBaseEmpty(int playerNumber){
boolean BaseStatus = true;
for(int i = playerNumber*4; i<playerNumber*4+4; i++){
if (figures[i].inBase) {
if (figures[i].isInBase()) {
BaseStatus = false;
break;
}
Expand All @@ -475,7 +475,7 @@ private boolean isBaseEmpty(int playerNumber){
private boolean isBaseFull(int playerNumber){
boolean BaseStatus = true;
for(int i = playerNumber*4; i<playerNumber*4+4; i++){
if (!figures[i].inBase) {
if (!figures[i].isInBase()) {
BaseStatus = false;
break;
}
Expand All @@ -492,13 +492,13 @@ private boolean threeTimesAllowed(int playerNumber){
int inBase = 0;
//count figures in base
for (int i = playerNumber * 4; i < playerNumber * 4 + 4; i++){
if (figures[i].inBase){
if (figures[i].isInBase()){
inBase++;
}
}
//count figures finished
for (int i = playerNumber * 4; i < playerNumber * 4 + 4; i++){
if (figures[i].finished){
if (figures[i].isFinished()){
finished++;
}
}
Expand All @@ -514,7 +514,7 @@ public void moveOutOfBase(int figureNumber){
moveToBase(figureOnFirstField);
}
figures[figureNumber].field = 10 * figureColor;
figures[figureNumber].inBase = false;
figures[figureNumber].setOnField();
}

//next player
Expand All @@ -535,4 +535,4 @@ private void nextPlayer(){
}
}
}
}
}
51 changes: 45 additions & 6 deletions Figure.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
public class Figure {
enum State {
IN_BASE,
ON_FIELD,
IN_HOUSE,
FINISHED,
}
private State state;

public int field;
public boolean inBase;
public boolean inHouse;
public int color;
public boolean finished;
public boolean placeOption;

Figure(int fieldNew, int colorNew){
inBase = true;
inHouse = false;
state = State.IN_BASE;
field = fieldNew;
color = colorNew;
finished = false;
placeOption = false;
}

public void setInBase() {
state = State.IN_BASE;
}

public void setOnField() {
state = State.ON_FIELD;
}

public void setInHouse() {
state = State.IN_HOUSE;
}

public void setFinished() {
state = State.FINISHED;
}

public boolean isInBase() {
return State.IN_BASE == state;
}

public boolean isOnField() {
return State.ON_FIELD == state;
}

public boolean isInHouse() {
return State.IN_HOUSE == state;
}

public boolean isFinished() {
return State.FINISHED == state;
}

public boolean isMovable() {
return State.ON_FIELD == state || State.IN_HOUSE == state;
}
}