mirror of
https://github.com/Noratrieb/minmax.git
synced 2026-01-15 07:45:04 +01:00
minmax
This commit is contained in:
parent
9900001888
commit
84750781f2
9 changed files with 636 additions and 0 deletions
|
|
@ -0,0 +1,194 @@
|
|||
package ch.bbw.m411.connect4;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Plays a game of Connect Four on a 4x7 board (a variation of the original 6x7 board).
|
||||
* The pieces fall straight down, occupying the lowest available space within the column.
|
||||
*/
|
||||
public class Connect4ArenaMain {
|
||||
|
||||
static final int WIDTH = 7;
|
||||
|
||||
static final int HEIGHT = 4;
|
||||
|
||||
static final int NOMOVE = -1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Connect4ArenaMain().play(new HumanPlayer(), new GreedyPlayer());
|
||||
}
|
||||
|
||||
static String toDebugString(Stone[] board) {
|
||||
var sb = new StringBuilder();
|
||||
for (int r = 0; r < HEIGHT; r++) {
|
||||
for (int c = 0; c < WIDTH; c++) {
|
||||
var value = board[r * WIDTH + c];
|
||||
sb.append(value == null ? "." : (value == Stone.RED ? "X" : "O"));
|
||||
}
|
||||
sb.append("-");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Connect4Player play(Connect4Player red, Connect4Player blue) {
|
||||
if (red == blue) {
|
||||
throw new IllegalStateException("must be different players (simply create two instances)");
|
||||
}
|
||||
var board = new Stone[WIDTH * HEIGHT];
|
||||
red.initialize(Arrays.copyOf(board, board.length), Stone.RED);
|
||||
blue.initialize(Arrays.copyOf(board, board.length), Stone.BLUE);
|
||||
var lastMove = NOMOVE;
|
||||
var currentPlayer = red;
|
||||
for (int round = 0; round < board.length; round++) {
|
||||
var currentColor = currentPlayer == red ? Stone.RED : Stone.BLUE;
|
||||
System.out.println(HumanPlayer.toPrettyString(board) + currentColor + " to play next...");
|
||||
lastMove = currentPlayer.play(lastMove);
|
||||
if (lastMove < 0 || lastMove >= WIDTH * HEIGHT) {
|
||||
throw new IllegalStateException("move is outside of valid range: " + lastMove);
|
||||
}
|
||||
if (board[lastMove] != null) {
|
||||
throw new IllegalStateException("position " + lastMove + " is already occupied @" + toDebugString(board));
|
||||
}
|
||||
if (lastMove > WIDTH && board[lastMove - WIDTH] == null) {
|
||||
throw new IllegalStateException("position " + lastMove + " is mid-air @" + toDebugString(board));
|
||||
}
|
||||
board[lastMove] = currentColor;
|
||||
if (isWinning(board, currentColor)) {
|
||||
System.out.println(
|
||||
HumanPlayer.toPrettyString(board) + "...and the winner is: " + currentColor + " @ " + toDebugString(board));
|
||||
return currentPlayer;
|
||||
}
|
||||
currentPlayer = currentPlayer == red ? blue : red;
|
||||
}
|
||||
System.out.println(HumanPlayer.toPrettyString(board) + "...it's a DRAW @ " + toDebugString(board));
|
||||
return null; // null implies a draw
|
||||
}
|
||||
|
||||
boolean isWinning(Stone[] board, Stone forColor) {
|
||||
// TODO: provide an implementation
|
||||
throw new IllegalStateException("Not implemented yet");
|
||||
}
|
||||
|
||||
public enum Stone {
|
||||
RED, BLUE;
|
||||
|
||||
public Stone opponent() {
|
||||
return this == RED ? BLUE : RED;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Connect4Player {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Perform a next move, will only be called if the Game is not over yet.
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* An abstract helper class to keep track of a board (and whatever we or the opponent played).
|
||||
*/
|
||||
public abstract static class DefaultPlayer implements Connect4Player {
|
||||
|
||||
Stone[] board;
|
||||
|
||||
Stone myColor;
|
||||
|
||||
@Override
|
||||
public void initialize(Stone[] board, Stone colorToPlay) {
|
||||
this.board = board;
|
||||
myColor = colorToPlay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int play(int opponendPlayed) {
|
||||
if (opponendPlayed != NOMOVE) {
|
||||
board[opponendPlayed] = myColor.opponent();
|
||||
}
|
||||
var playTo = play();
|
||||
board[playTo] = myColor;
|
||||
return playTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)}.
|
||||
*/
|
||||
abstract int play();
|
||||
|
||||
}
|
||||
|
||||
public static class HumanPlayer extends DefaultPlayer {
|
||||
|
||||
static String toPrettyString(Stone[] board) {
|
||||
var sb = new StringBuilder();
|
||||
for (int r = HEIGHT - 1; r >= 0; r--) {
|
||||
for (int c = 0; c < WIDTH; c++) {
|
||||
var index = r * WIDTH + c;
|
||||
if (board[index] == null) {
|
||||
if (index < WIDTH || board[index - WIDTH] != null) {
|
||||
sb.append("\033[37m" + index + "\033[0m ");
|
||||
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
|
||||
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
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package ch.bbw.m411.connect4;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.assertj.core.api.AbstractBooleanAssert;
|
||||
import org.assertj.core.api.WithAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class Connect4MainTest implements WithAssertions {
|
||||
|
||||
protected Connect4ArenaMain newInstance() {
|
||||
return new Connect4ArenaMain();
|
||||
}
|
||||
|
||||
Connect4ArenaMain.Stone[] fromString(String boardStr) {
|
||||
var board = boardStr.codePoints()
|
||||
.map(Character::toLowerCase)
|
||||
.filter(x -> List.of('x', 'o', '.')
|
||||
.contains((char) x))
|
||||
.mapToObj(x -> x == 'x' ? Connect4ArenaMain.Stone.RED : (x == 'o' ? Connect4ArenaMain.Stone.BLUE : null))
|
||||
.toArray(Connect4ArenaMain.Stone[]::new);
|
||||
assertThat(board).hasSize(Connect4ArenaMain.WIDTH * Connect4ArenaMain.HEIGHT);
|
||||
return board;
|
||||
}
|
||||
|
||||
AbstractBooleanAssert<?> assertThatXWin(String boardStr) {
|
||||
var board = fromString(boardStr);
|
||||
return assertThat(newInstance().isWinning(board, Connect4ArenaMain.Stone.RED)).as(Connect4ArenaMain.toDebugString(board));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isWin() {
|
||||
assertThatXWin("xxxx... ....... ....... .......").isTrue();
|
||||
assertThatXWin(".xxxx.. ....... ....... .......").isTrue();
|
||||
assertThatXWin("..xxxx. ....... ....... .......").isTrue();
|
||||
assertThatXWin("...xxxx ....... ....... .......").isTrue();
|
||||
assertThatXWin("...x... ...x... ...x... ...x...").isTrue();
|
||||
assertThatXWin("......x ......x ......x ......x").isTrue();
|
||||
assertThatXWin("xooo... .xoo... ..xo... ...x...").isTrue();
|
||||
assertThatXWin(".ooxo.. .oxoo.. .xxxx.. .......").isTrue();
|
||||
assertThatXWin(".ooxo.x .oxoo.. .ooxx.. .xxxx..").isTrue();
|
||||
assertThatXWin("oooo... xxxx... ....... .......").isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void noWin() {
|
||||
assertThatXWin("....... ....... ....... .......").isFalse();
|
||||
assertThatXWin("xxx.xx. ....... ....... .......").isFalse();
|
||||
assertThatXWin("xxx.xxx xxx.xxx xxx.xxx .......").isFalse();
|
||||
assertThatXWin("xx.x.xx xx.x.xx xx.x.xx .......").isFalse();
|
||||
assertThatXWin("ooo.ooo xxx.xxx xxx.xxx xxx.xxx").isFalse();
|
||||
assertThatXWin("oo.o.oo xx.x.xx xx.x.xx xx.x.xx").isFalse();
|
||||
assertThatXWin("oooo... ....... ....... .......").isFalse();
|
||||
assertThatXWin("xxx.xx. xxx.xx. xxx.... o......").isFalse();
|
||||
assertThatXWin("xxxo... x.x.... x.o.... o.x....").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void inAGreedyBattleTheFirstPlayerWillWin() {
|
||||
var red = new Connect4ArenaMain.GreedyPlayer();
|
||||
var blue = new Connect4ArenaMain.GreedyPlayer();
|
||||
assertThat(newInstance().play(red, blue)).isSameAs(red);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue