Working game, needing stateChanges

This commit is contained in:
Malte Tammena 2017-10-13 14:43:08 +02:00
parent fdfd1219df
commit ed10fdb24b
5 changed files with 100 additions and 21 deletions

View file

@ -13,7 +13,8 @@ LIBARIES=
CLASSES=\ CLASSES=\
src/game/Main.java \ src/game/Main.java \
src/game/Game.java \ src/game/Game.java \
src/player/Player.java src/player/Player.java \
src/player/PlayerBasic.java
OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class) OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class)

View file

@ -24,16 +24,19 @@ public class Game {
/** /**
* The board on which is played. * The board on which is played.
* [7] columns, [6] rows * [7] columns, [6] rows
* 0 - Empty Space * " " - Empty Space
* 1 - Player One
* 2 - Player Two
*/ */
private int[][] board; private String[][] board;
/**
* Unplayed pieces in the game.
*/
private int piecesLeft;
/** /**
* Is the game still running? * Is the game still running?
*/ */
private boolean gameOn; private String gameOn;
/** /**
* Constructor. * Constructor.
@ -42,7 +45,12 @@ public class Game {
public Game(Player p1, Player p2) { public Game(Player p1, Player p2) {
this.p1 = p1; this.p1 = p1;
this.p2 = p2; this.p2 = p2;
this.board = new int [7][6]; this.board = new String[7][6];
for (int i = 0; i < this.board.length; i++) {
for (int j = 0; j < this.board[i].length; j++) {
this.board[i][j] = " ";
}
}
} }
/** /**
@ -59,44 +67,83 @@ public class Game {
first = this.p2; first = this.p2;
second = this.p1; second = this.p1;
} }
String symFirst = first.getSymbol();
String symSecond = symFirst;
while (symSecond.equals(symFirst)) {
symSecond = second.getSymbol();
}
this.piecesLeft = 42;
gameOn = "running";
while(gameOn == "running") {
makeMove(first, symFirst);
updateState();
makeMove(second, symSecond);
updateState();
}
while(gameOn) { switch(gameOn) {
// TODO: Think of another possibility case "draw":
makeMove(first, ); log("The game ended in a draw");
} }
} }
/** /**
* Calls a players functions to make a move. * Calls a players functions to make a move.
*/ */
private void makeMove(Player p, int nr) { private void makeMove(Player p, String symbol) {
boolean invalid = true; boolean invalid = true;
int choice; int choice = 0;
long before = System.nanoTime();
while (invalid) { while (invalid) {
log(p.getName() + "makes a move!"); choice = p.move(this.board);
choice = p.move(); if (this.board[choice][0] == " ") {
if (this.board[choice][0] == 0) {
invalid = false; invalid = false;
} }
} }
int pos = 0; long after = System.nanoTime();
while (this.board[choice][pos + 1] == 0) { double diff = (after - before) / 1000000000.0;
pos++; log(p.getName() + " made a move in " + diff + "seconds");
addPiece(choice, symbol);
}
private void addPiece(int column, String symbol) {
int pos = 5;
while (pos > 0 && this.board[column][pos] != " ") {
pos--;
}
this.board[column][pos] = symbol;
this.piecesLeft--;
log("" + this.piecesLeft);
logGame();
}
/**
* Check the game state. Update this.gameOn if necessary.
*/
private void updateState() {
if (this.piecesLeft == 0) {
this.gameOn = "draw";
} }
this.board[choice][pos] = nr;
} }
/** /**
* Log player actions. * Log player actions.
*/ */
private void log(String s) { private void log(String s) {
System.out.println(s);
} }
/** /**
* Prints the current board to stdout. * Prints the current board to stdout.
*/ */
private void logGame() { private void logGame() {
String out = "";
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(this.board[j][i]);
}
System.out.println();
}
} }
} }

View file

@ -3,6 +3,7 @@ package game;
import player.Player; import player.Player;
// import player.Player1; // import player.Player1;
// import player.Player2; // import player.Player2;
import player.PlayerBasic;
public class Main { public class Main {
@ -10,5 +11,6 @@ public class Main {
// Player p1 = new Player1(); // Player p1 = new Player1();
// Player p2 = new Player2(); // Player p2 = new Player2();
// new Game(p1, p2).start(); // new Game(p1, p2).start();
new Game(new PlayerBasic(), new PlayerBasic()).start();
} }
} }

View file

@ -2,6 +2,7 @@ package player;
public interface Player { public interface Player {
public int move(int[][] game); public int move(String[][] game);
public String getName(); public String getName();
public String getSymbol();
} }

View file

@ -0,0 +1,28 @@
package player;
import java.util.Random;
public class PlayerBasic implements Player {
private String name;
private Random ran;
private char symbol = 'A';
public PlayerBasic() {
ran = new Random();
this.name = "Player " + ran.nextInt();
}
public int move(String[][] board) {
return ran.nextInt(7);
}
public String getName() {
return name;
}
public String getSymbol() {
this.symbol++;
return "" + this.symbol;
}
}