mirror of
https://github.com/Noratrieb/minmax.git
synced 2026-01-14 23:35:04 +01:00
is winning
This commit is contained in:
parent
125990e659
commit
ac8486962d
2 changed files with 216 additions and 168 deletions
|
|
@ -10,185 +10,244 @@ import java.util.Scanner;
|
||||||
*/
|
*/
|
||||||
public class Connect4ArenaMain {
|
public class Connect4ArenaMain {
|
||||||
|
|
||||||
static final int WIDTH = 7;
|
static final int WIDTH = 7;
|
||||||
|
|
||||||
static final int HEIGHT = 4;
|
static final int HEIGHT = 4;
|
||||||
|
|
||||||
static final int NOMOVE = -1;
|
static final int NOMOVE = -1;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Connect4ArenaMain().play(new RustPlayer(), new GreedyPlayer());
|
new Connect4ArenaMain().play(new RustPlayer(), new GreedyPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
static String toDebugString(Stone[] board) {
|
static String toDebugString(Stone[] board) {
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
for (int r = 0; r < HEIGHT; r++) {
|
for (int r = 0; r < HEIGHT; r++) {
|
||||||
for (int c = 0; c < WIDTH; c++) {
|
for (int c = 0; c < WIDTH; c++) {
|
||||||
var value = board[r * WIDTH + c];
|
var value = board[r * WIDTH + c];
|
||||||
sb.append(value == null ? "." : (value == Stone.RED ? "X" : "O"));
|
sb.append(value == null ? "." : (value == Stone.RED ? "X" : "O"));
|
||||||
}
|
}
|
||||||
sb.append("-");
|
sb.append("-");
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
Connect4Player play(Connect4Player red, Connect4Player blue) {
|
Connect4Player play(Connect4Player red, Connect4Player blue) {
|
||||||
if (red == blue) {
|
if (red == blue) {
|
||||||
throw new IllegalStateException("must be different players (simply create two instances)");
|
throw new IllegalStateException("must be different players (simply create two instances)");
|
||||||
}
|
}
|
||||||
var board = new Stone[WIDTH * HEIGHT];
|
var board = new Stone[WIDTH * HEIGHT];
|
||||||
red.initialize(Arrays.copyOf(board, board.length), Stone.RED);
|
red.initialize(Arrays.copyOf(board, board.length), Stone.RED);
|
||||||
blue.initialize(Arrays.copyOf(board, board.length), Stone.BLUE);
|
blue.initialize(Arrays.copyOf(board, board.length), Stone.BLUE);
|
||||||
var lastMove = NOMOVE;
|
var lastMove = NOMOVE;
|
||||||
var currentPlayer = red;
|
var currentPlayer = red;
|
||||||
for (int round = 0; round < board.length; round++) {
|
for (int round = 0; round < board.length; round++) {
|
||||||
var currentColor = currentPlayer == red ? Stone.RED : Stone.BLUE;
|
var currentColor = currentPlayer == red ? Stone.RED : Stone.BLUE;
|
||||||
System.out.println(HumanPlayer.toPrettyString(board) + currentColor + " to play next...");
|
System.out.println(HumanPlayer.toPrettyString(board) + currentColor + " to play next...");
|
||||||
lastMove = currentPlayer.play(lastMove);
|
lastMove = currentPlayer.play(lastMove);
|
||||||
if (lastMove < 0 || lastMove >= WIDTH * HEIGHT) {
|
if (lastMove < 0 || lastMove >= WIDTH * HEIGHT) {
|
||||||
throw new IllegalStateException("move is outside of valid range: " + lastMove);
|
throw new IllegalStateException("move is outside of valid range: " + lastMove);
|
||||||
}
|
}
|
||||||
if (board[lastMove] != null) {
|
if (board[lastMove] != null) {
|
||||||
throw new IllegalStateException("position " + lastMove + " is already occupied @" + toDebugString(board));
|
throw new IllegalStateException("position " + lastMove + " is already occupied @" + toDebugString(board));
|
||||||
}
|
}
|
||||||
if (lastMove > WIDTH && board[lastMove - WIDTH] == null) {
|
if (lastMove > WIDTH && board[lastMove - WIDTH] == null) {
|
||||||
throw new IllegalStateException("position " + lastMove + " is mid-air @" + toDebugString(board));
|
throw new IllegalStateException("position " + lastMove + " is mid-air @" + toDebugString(board));
|
||||||
}
|
}
|
||||||
board[lastMove] = currentColor;
|
board[lastMove] = currentColor;
|
||||||
if (isWinning(board, currentColor)) {
|
if (isWinning(board, currentColor)) {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
HumanPlayer.toPrettyString(board) + "...and the winner is: " + currentColor + " @ " + toDebugString(board));
|
HumanPlayer.toPrettyString(board) + "...and the winner is: " + currentColor + " @ " + toDebugString(board));
|
||||||
return currentPlayer;
|
return currentPlayer;
|
||||||
}
|
}
|
||||||
currentPlayer = currentPlayer == red ? blue : red;
|
currentPlayer = currentPlayer == red ? blue : red;
|
||||||
}
|
}
|
||||||
System.out.println(HumanPlayer.toPrettyString(board) + "...it's a DRAW @ " + toDebugString(board));
|
System.out.println(HumanPlayer.toPrettyString(board) + "...it's a DRAW @ " + toDebugString(board));
|
||||||
return null; // null implies a draw
|
return null; // null implies a draw
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
public enum Stone {
|
// Horizontal reset
|
||||||
RED, BLUE;
|
if (i == 20 || i == 13 || i == 6) {
|
||||||
|
increment = 0;
|
||||||
|
}
|
||||||
|
|
||||||
public Stone opponent() {
|
increment++;
|
||||||
return this == RED ? BLUE : RED;
|
// Horizontal
|
||||||
}
|
if (increment == 4) {
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public interface Connect4Player {
|
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) {
|
||||||
* Called before the game starts and guaranteed to only be called once per livetime of the player.
|
var diagonal = true;
|
||||||
*
|
for (int y = i; y >= 0; y -= 7 + 1) {
|
||||||
* @param board the starting board, usually an empty board.
|
if (board[y] != forColor) {
|
||||||
* @param colorToPlay the color of this player
|
diagonal = false;
|
||||||
*/
|
break;
|
||||||
void initialize(Stone[] board, Stone colorToPlay);
|
}
|
||||||
|
}
|
||||||
|
if (diagonal) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public enum Stone {
|
||||||
* Perform a next move, will only be called if the Game is not over yet.
|
RED, BLUE;
|
||||||
* Each player has to keep an internal state of the 4x7 board, wher the 0-index is on the bottom row.
|
|
||||||
* The index-layout looks as:
|
|
||||||
* <pre>
|
|
||||||
* 21 22 23 24 25 26 27
|
|
||||||
* 14 15 16 17 18 19 20
|
|
||||||
* 7 8 9 10 11 12 13
|
|
||||||
* 0 1 2 3 4 5 6
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @param opponendPlayed the last index where the opponent played to (in range 0 - width*height exclusive)
|
|
||||||
* or -1 if this is the first move.
|
|
||||||
* @return an index to play to (in range 0 - width*height exclusive)
|
|
||||||
*/
|
|
||||||
int play(int opponendPlayed);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public Stone opponent() {
|
||||||
* An abstract helper class to keep track of a board (and whatever we or the opponent played).
|
return this == RED ? BLUE : RED;
|
||||||
*/
|
}
|
||||||
public abstract static class DefaultPlayer implements Connect4Player {
|
}
|
||||||
|
|
||||||
Stone[] board;
|
public interface Connect4Player {
|
||||||
|
|
||||||
Stone myColor;
|
/**
|
||||||
|
* Called before the game starts and guaranteed to only be called once per livetime of the player.
|
||||||
|
*
|
||||||
|
* @param board the starting board, usually an empty board.
|
||||||
|
* @param colorToPlay the color of this player
|
||||||
|
*/
|
||||||
|
void initialize(Stone[] board, Stone colorToPlay);
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void initialize(Stone[] board, Stone colorToPlay) {
|
* Perform a next move, will only be called if the Game is not over yet.
|
||||||
this.board = board;
|
* Each player has to keep an internal state of the 4x7 board, wher the 0-index is on the bottom row.
|
||||||
myColor = colorToPlay;
|
* The index-layout looks as:
|
||||||
}
|
* <pre>
|
||||||
|
* 21 22 23 24 25 26 27
|
||||||
|
* 14 15 16 17 18 19 20
|
||||||
|
* 7 8 9 10 11 12 13
|
||||||
|
* 0 1 2 3 4 5 6
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param opponendPlayed the last index where the opponent played to (in range 0 - width*height exclusive)
|
||||||
|
* or -1 if this is the first move.
|
||||||
|
* @return an index to play to (in range 0 - width*height exclusive)
|
||||||
|
*/
|
||||||
|
int play(int opponendPlayed);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public int play(int opponendPlayed) {
|
* An abstract helper class to keep track of a board (and whatever we or the opponent played).
|
||||||
if (opponendPlayed != NOMOVE) {
|
*/
|
||||||
board[opponendPlayed] = myColor.opponent();
|
public abstract static class DefaultPlayer implements Connect4Player {
|
||||||
}
|
|
||||||
var playTo = play();
|
|
||||||
board[playTo] = myColor;
|
|
||||||
return playTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
Stone[] board;
|
||||||
* 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)}.
|
|
||||||
*/
|
|
||||||
protected abstract int play();
|
|
||||||
|
|
||||||
}
|
Stone myColor;
|
||||||
|
|
||||||
public static class HumanPlayer extends DefaultPlayer {
|
@Override
|
||||||
|
public void initialize(Stone[] board, Stone colorToPlay) {
|
||||||
|
this.board = board;
|
||||||
|
myColor = colorToPlay;
|
||||||
|
}
|
||||||
|
|
||||||
static String toPrettyString(Stone[] board) {
|
@Override
|
||||||
var sb = new StringBuilder();
|
public int play(int opponendPlayed) {
|
||||||
for (int r = HEIGHT - 1; r >= 0; r--) {
|
if (opponendPlayed != NOMOVE) {
|
||||||
for (int c = 0; c < WIDTH; c++) {
|
board[opponendPlayed] = myColor.opponent();
|
||||||
var index = r * WIDTH + c;
|
}
|
||||||
if (board[index] == null) {
|
var playTo = play();
|
||||||
if (index < WIDTH || board[index - WIDTH] != null) {
|
board[playTo] = myColor;
|
||||||
sb.append("\033[37m" + index + "\033[0m ");
|
return playTo;
|
||||||
if (index < 10) {
|
}
|
||||||
sb.append(" ");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sb.append("\033[37m.\033[0m ");
|
|
||||||
}
|
|
||||||
} else if (board[index] == Stone.RED) {
|
|
||||||
sb.append("\033[1;31mX\033[0m ");
|
|
||||||
} else {
|
|
||||||
sb.append("\033[1;34mO\033[0m ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.append("\n");
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
protected int play() {
|
|
||||||
System.out.println("where to to put the next " + myColor + "?");
|
|
||||||
var scanner = new Scanner(System.in, StandardCharsets.UTF_8);
|
|
||||||
return Integer.parseInt(scanner.nextLine());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* 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)}.
|
||||||
|
*/
|
||||||
|
protected abstract int play();
|
||||||
|
|
||||||
public static class GreedyPlayer extends DefaultPlayer {
|
}
|
||||||
|
|
||||||
@Override
|
public static class HumanPlayer extends DefaultPlayer {
|
||||||
protected int play() {
|
|
||||||
for (int c = 0; c < WIDTH; c++) {
|
static String toPrettyString(Stone[] board) {
|
||||||
for (int r = 0; r < HEIGHT; r++) {
|
var sb = new StringBuilder();
|
||||||
var index = r * WIDTH + c;
|
for (int r = HEIGHT - 1; r >= 0; r--) {
|
||||||
if (board[index] == null) {
|
for (int c = 0; c < WIDTH; c++) {
|
||||||
return index;
|
var index = r * WIDTH + c;
|
||||||
}
|
if (board[index] == null) {
|
||||||
}
|
if (index < WIDTH || board[index - WIDTH] != null) {
|
||||||
}
|
sb.append("\033[37m" + index + "\033[0m ");
|
||||||
throw new IllegalStateException("cannot play at all");
|
if (index < 10) {
|
||||||
}
|
sb.append(" ");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
sb.append("\033[37m.\033[0m ");
|
||||||
|
}
|
||||||
|
} else if (board[index] == Stone.RED) {
|
||||||
|
sb.append("\033[1;31mX\033[0m ");
|
||||||
|
} else {
|
||||||
|
sb.append("\033[1;34mO\033[0m ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int play() {
|
||||||
|
System.out.println("where to to put the next " + myColor + "?");
|
||||||
|
var scanner = new Scanner(System.in, StandardCharsets.UTF_8);
|
||||||
|
return Integer.parseInt(scanner.nextLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GreedyPlayer extends DefaultPlayer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int play() {
|
||||||
|
for (int c = 0; c < WIDTH; c++) {
|
||||||
|
for (int r = 0; r < HEIGHT; r++) {
|
||||||
|
var index = r * WIDTH + c;
|
||||||
|
if (board[index] == null) {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("cannot play at all");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue