This commit is contained in:
nora 2023-01-14 16:03:33 +01:00
parent 3d90a3ea0a
commit d415bb7ae9
6 changed files with 84 additions and 86 deletions

View file

@ -67,65 +67,7 @@ public class Connect4ArenaMain {
}
boolean isWinning(Stone[] board, Stone forColor) {
var increment = 0;
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;
return RustPlayer.isWinning(board, forColor);
}
public enum Stone {

View file

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