Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added console/build.gradle
Empty file.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.cs.games.mancala.console;

import com.cs.games.mancala.model.Game;
import com.cs.games.mancala.model.Move;
import com.cs.games.mancala.model.Player;
import com.cs.games.mancala.model.*;
import com.cs.games.mancala.player.HumanPlayer;
import com.cs.games.mancala.player.MoveSupplier;
import com.cs.games.mancala.player.ai.ComputerPlayer;
import com.cs.games.mancala.player.ai.RandomPlayer;
import com.cs.games.mancala.player.human.GameInput;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Command line mancala game
*/
Expand Down Expand Up @@ -45,21 +49,8 @@ public static void main(String[] args) {
}

Game game = new Game(p1Supplier, p2Supplier);
while(!game.getBoard().isGameOver()) {
// TODO dump board

// ask for next move
MoveSupplier player = game.getPlayer();
Move move = player.selectFrom(game.getBoard());
if (move == null) {
System.out.println("Undo!");
game.undo();
}
else {
System.out.println("Move: "+move.getCup());
game.doMove(move);
}
}
game.run();

System.out.println("Final scores:");
System.out.println("\t P1: "+game.getBoard().getScore(Player.ONE));
System.out.println("\t P2: "+game.getBoard().getScore(Player.TWO));
Expand All @@ -84,6 +75,55 @@ public static PlayerType getType(GameInput input) {
return playerTypes[selected];
}

public static void render(Board board) {
System.out.println("--------------Player one----------------");
System.out.println("| | 0 | 1 | 2 | 3 | 4 | 5 | |");
System.out.println("| |-----------------------------| |");
renderCupRow(board, Player.ONE);
System.out.println("| |-----------------------------| |");
renderCupRow(board, Player.TWO);
System.out.println("| |-----------------------------| |");
System.out.println("| | 5 | 4 | 3 | 2 | 1 | 0 | |");
System.out.println("--------------Player two----------------");
}

private static void renderCupRow(Board board, Player p) {
Iterable<Cup> cups;
if (p == Player.ONE) {
System.out.print ("| ");
cups = Cup.playerCups(p);
}
else {
cups = new Iterable<Cup>() {
@Override
public Iterator<Cup> iterator() {
return new LinkedList<Cup>(StreamSupport.stream(Cup.playerCups(p).spliterator(), false)
.collect(Collectors.toList())).descendingIterator();
}
};
}

for(Cup c : cups)
{
System.out.print("|"+renderCup(board, c));
}
if (p == Player.TWO) {
System.out.println("| |");
}
else {
System.out.println("|");
}
}

private static String renderCup(Board board, Cup c){
int cupCount = board.getBeadCount(c);
String value = Integer.toString(cupCount);
if (value.length() < 2) {
value = " " + value;
}
return " "+value+" ";
}

public static MoveSupplier moveSupplier(PlayerType type, GameInput input) {
switch (type) {
default:
Expand Down
Empty file added core/build.gradle
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cs.games.mancala;

public class BoardLayout {

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Cup {
*/
private final Player player;
/**
* The cup number (0 - 7)
* The cup number (0 - 6)
*/
private final int cupNumber;
/**
Expand Down Expand Up @@ -119,6 +119,15 @@ public static Cup parse(String s) {
return BOARD_LAYOUT[p.number][index];
}

public static Iterable<? extends Cup> cups() {
return new Iterable<Cup>() {
@Override
public Iterator<Cup> iterator() {
return Cup.iterator();
}
};
}


public Player getPlayer() {
return player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,22 @@ public void undo() {
public MoveSupplier getPlayer() {
return players[board.getNextPlayer().number];
}

public void run() {
while(!board.isGameOver()) {

// ask for next move
MoveSupplier player = getPlayer();
System.out.println("Player: "+(board.getNextPlayer().number + 1));
Move move = player.selectFrom(board);
if (move == null) {
System.out.println("Undo!");
undo();
}
else {
System.out.println("Move: "+move.getCup());
doMove(move);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cs.games.mancala.model;

public interface GameListener {
void boardChanged(Board b);
void moving(Move move);
void moved(Move move);
void undoing(Move move);
void undone(Move move);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* A computer player can search through a vast array of possible next moves to
* evaluate which one maximises the pay-off to the player.
*
* @author <A HREF="mailto:chris.senior@teradyne.com?subject=com.cs.games.mancala.model.ai.ComputerPlayer">Chris Senior</A>
* @author Chris Senior
*/
public class ComputerPlayer implements MoveSupplier {

Expand All @@ -24,7 +24,7 @@ public class ComputerPlayer implements MoveSupplier {
*/
private static final Logger LOG = getLogger(ComputerPlayer.class);

private int depth = 5;
private int depth = 2;

@Override
public Move selectFrom(Board board) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.cs.games.mancala.player;

import com.cs.games.mancala.console.Main;
import com.cs.games.mancala.model.Board;
import com.cs.games.mancala.model.Cup;
import com.cs.games.mancala.model.Move;
import com.cs.games.mancala.model.Player;
import com.cs.games.mancala.player.human.GameInput;

import java.util.Iterator;
Expand All @@ -20,12 +23,7 @@ public HumanPlayer(GameInput input) {
@Override
public Move selectFrom(Board board) {
List<Move> moves = board.nextMoves();
System.out.println("Moves:");
Iterator iter = moves.iterator();
while (iter.hasNext()) {
Move m = (Move) iter.next();
System.out.println(m);
}
Main.render(board);
int chosen = -1;
while (chosen < 0 || chosen > moves.size()) {
System.out.println("Please choose a move (or undo): ");
Expand All @@ -35,13 +33,22 @@ public Move selectFrom(Board board) {
}
try {
chosen = Integer.parseInt(command);
for(Move move : moves){
if (move.getCup().getCupNumber() == chosen)
{
return move;
}
}
System.out.println("Unrecognised move?");
} catch (Exception e) {
System.out.println("Error: Must be a number!");
}
}

return moves.get(chosen);
}


@Override
public String getDisplayName() {
return "Human console interactive player";
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-5.0-all.zip
Loading