added win checker

This commit is contained in:
Dorian Zedler 2020-02-17 08:38:16 +01:00
parent c3f5f04dbd
commit bc73ed4829
3 changed files with 106 additions and 11 deletions

View file

@ -68,6 +68,9 @@ public class GameBoard extends JPanel {
if (!this.BoardContainers[column][i].containsPlate()) {
// if the container is empty -> add the plate
this.BoardContainers[column][i].insertPlate(plate);
this.checkContainerForWin(column, i);
return true;
}
}
@ -143,6 +146,102 @@ public class GameBoard extends JPanel {
super.setPreferredSize(newSize);
}
public boolean checkContainerForWin(int c, int r) {
System.out.println("checking container c=" + c + " r=" + r + " for win");
// if there is no plate in the container to check
// -> return false
if (this.getPlateContainer(c, r) == null || !this.getPlateContainer(c, r).containsPlate())
return false;
// check all possible winnings clockwise
int[] sums = new int[8];
for (int i = 0; i < 4; i++) {
PlateContainer currentContainer = null;
for (int pc = 0; pc < 8; pc++) {
currentContainer = null;
int checkC = 0;
int checkR = 0;
switch (pc) {
case 0:
// check top
checkC = c;
checkR = r + i;
break;
case 1:
// check top right vert
checkC = c + i;
checkR = r + i;
break;
case 2:
// check right
checkC = c + i;
checkR = r;
break;
case 3:
// check bottom right vert
checkC = c + i;
checkR = r - i;
break;
case 4:
checkC = c;
checkR = r - i;
// check bottom
break;
case 5:
// check bottom left vert
checkC = c - i;
checkR = r - i;
break;
case 6:
// check left
checkC = c - i;
checkR = r;
break;
case 7:
// check top left vert
checkC = c - i;
checkR = r + i;
break;
default:
currentContainer = null;
break;
}
currentContainer = this.getPlateContainer(checkC, checkR);
if (currentContainer != null && currentContainer.containsPlate())
sums[pc] += currentContainer.getContainedPlate().getValue();
}
}
for (int sum : sums) {
if (Math.abs(sum) == 4) {
if (sum > 0)
System.out.println("negative won");
else
System.out.println("positive won");
return true;
}
}
return false;
}
public PlateContainer getPlateContainer(int containerColumn, int containerRow) {
if (this.BoardContainers.length > containerColumn && containerColumn >= 0
&& this.BoardContainers[containerColumn].length > containerRow && containerRow >= 0)
return this.BoardContainers[containerColumn][containerRow];
return null;
}
/*
* void winningCondition() { for (int i = 0; i <= 2; i++) for (int j = 0; j <=
* 6; j++) { if (GameBoard[i][j] == p && GameBoard[i + 1][j] == p && GameBoard[i
@ -157,9 +256,7 @@ public class GameBoard extends JPanel {
*
* for (int i = 0; i < 3; i++) for (int j = 6; j > 2; j--) { if (GameBoard[i][j]
* == p && GameBoard[i + 1][j - 1] == p && GameBoard[i + 2][j - 2] == p &&
* GameBoard[i + 3][j - 3] == p) finish = true; }
*
* }
* GameBoard[i + 3][j - 3] == p) finish = true; } }
*/
/*

View file

@ -17,15 +17,14 @@
*/
package de.itsblue.ConnectFour;
import java.awt.*;
/**
* Plate models to plate or 'coin' to be inserted into
* game grid. It therefore has a type (X or O) which
* comes with a color (X = RED; O = BLACK) and a value
* (X = -1; O = 1) for further processing. this is a
* helper class and does not come with any standalone
* usage.
* Plate models to plate or 'coin' to be inserted into game grid. It therefore
* has a type (X or O) which comes with a color (X = RED; O = BLACK) and a value
* (X = -1; O = 1) for further processing. this is a helper class and does not
* come with any standalone usage.
*
* @author Oliver Schappacher
* @author Dorian Zedler

View file

@ -86,8 +86,7 @@ public class PlateContainer extends JPanel {
public Plate removePlate() {
if (!this.containsPlate())
return null;
this.remove(this.getComponents()[0]);
Plate ret = this.containedPlate;
this.containedPlate = null;