is winning

This commit is contained in:
nora 2023-01-09 14:57:12 +01:00
parent 125990e659
commit ac8486962d
2 changed files with 216 additions and 168 deletions

View file

@ -67,8 +67,65 @@ public class Connect4ArenaMain {
} }
boolean isWinning(Stone[] board, Stone forColor) { boolean isWinning(Stone[] board, Stone forColor) {
// TODO: provide an implementation var increment = 0;
throw new IllegalStateException("Not implemented yet"); for (int i = 27; i >= 0; i--) {
if (forColor != board[i]) {
increment = 0;
continue;
}
// Horizontal reset
if (i == 20 || i == 13 || i == 6) {
increment = 0;
}
increment++;
// Horizontal
if (increment == 4) {
return true;
}
if (i > 20) {
// Vertical
var vertical = true;
for (int y = i - 7; y >= 0; y -= 7) {
if (board[y] != forColor) {
vertical = false;
break;
}
}
if (vertical) {
return true;
}
// Diagonally
if (i <= 24) {
var diagonal = true;
for (int y = i; y >= 0; y -= 7 - 1) {
if (board[y] != forColor) {
diagonal = false;
break;
}
}
if (diagonal) {
return true;
}
}
if (i >= 24) {
var diagonal = true;
for (int y = i; y >= 0; y -= 7 + 1) {
if (board[y] != forColor) {
diagonal = false;
break;
}
}
if (diagonal) {
return true;
}
}
}
}
return false;
} }
public enum Stone { public enum Stone {
@ -134,6 +191,7 @@ public class Connect4ArenaMain {
/** /**
* Givent the current {@link #board}, find a suitable position-index to play to. * Givent the current {@link #board}, find a suitable position-index to play to.
*
* @return the position to play to as defined by {@link Connect4Player#play(int)}. * @return the position to play to as defined by {@link Connect4Player#play(int)}.
*/ */
protected abstract int play(); protected abstract int play();
@ -166,6 +224,7 @@ public class Connect4ArenaMain {
} }
return sb.toString(); return sb.toString();
} }
@Override @Override
protected int play() { protected int play() {
System.out.println("where to to put the next " + myColor + "?"); System.out.println("where to to put the next " + myColor + "?");

View file

@ -3,8 +3,6 @@ package ch.bbw.m411.connect4;
public class RustPlayer extends Connect4ArenaMain.DefaultPlayer { public class RustPlayer extends Connect4ArenaMain.DefaultPlayer {
private static native int rustPlay(byte player, byte[] board); private static native int rustPlay(byte player, byte[] board);
private static native boolean isWinning(byte player, byte[] forColor);
static { static {
// This actually loads the shared object that we'll be creating. // This actually loads the shared object that we'll be creating.
// The actual location of the .so or .dll may differ based on your // The actual location of the .so or .dll may differ based on your
@ -29,15 +27,6 @@ public class RustPlayer extends Connect4ArenaMain.DefaultPlayer {
return boardBuf; return boardBuf;
} }
public static boolean isWinning(Connect4ArenaMain.Stone[] board, Connect4ArenaMain.Stone forColor) {
byte player = switch (forColor) {
case BLUE -> 0;
case RED -> 1;
};
byte[] boardBuf = RustPlayer.encodeBoard(board);
return RustPlayer.isWinning(player, boardBuf);
}
@Override @Override
protected int play() { protected int play() {
byte player = switch (this.myColor) { byte player = switch (this.myColor) {