Working game, needing stateChanges
This commit is contained in:
parent
fdfd1219df
commit
ed10fdb24b
3
Makefile
3
Makefile
|
@ -13,7 +13,8 @@ LIBARIES=
|
|||
CLASSES=\
|
||||
src/game/Main.java \
|
||||
src/game/Game.java \
|
||||
src/player/Player.java
|
||||
src/player/Player.java \
|
||||
src/player/PlayerBasic.java
|
||||
|
||||
OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class)
|
||||
|
||||
|
|
|
@ -24,16 +24,19 @@ public class Game {
|
|||
/**
|
||||
* The board on which is played.
|
||||
* [7] columns, [6] rows
|
||||
* 0 - Empty Space
|
||||
* 1 - Player One
|
||||
* 2 - Player Two
|
||||
* " " - Empty Space
|
||||
*/
|
||||
private int[][] board;
|
||||
private String[][] board;
|
||||
|
||||
/**
|
||||
* Unplayed pieces in the game.
|
||||
*/
|
||||
private int piecesLeft;
|
||||
|
||||
/**
|
||||
* Is the game still running?
|
||||
*/
|
||||
private boolean gameOn;
|
||||
private String gameOn;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
@ -42,7 +45,12 @@ public class Game {
|
|||
public Game(Player p1, Player p2) {
|
||||
this.p1 = p1;
|
||||
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;
|
||||
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) {
|
||||
// TODO: Think of another possibility
|
||||
makeMove(first, );
|
||||
switch(gameOn) {
|
||||
case "draw":
|
||||
log("The game ended in a draw");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
int choice;
|
||||
int choice = 0;
|
||||
long before = System.nanoTime();
|
||||
while (invalid) {
|
||||
log(p.getName() + "makes a move!");
|
||||
choice = p.move();
|
||||
if (this.board[choice][0] == 0) {
|
||||
choice = p.move(this.board);
|
||||
if (this.board[choice][0] == " ") {
|
||||
invalid = false;
|
||||
}
|
||||
}
|
||||
int pos = 0;
|
||||
while (this.board[choice][pos + 1] == 0) {
|
||||
pos++;
|
||||
long after = System.nanoTime();
|
||||
double diff = (after - before) / 1000000000.0;
|
||||
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.
|
||||
*/
|
||||
private void log(String s) {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the current board to stdout.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package game;
|
|||
import player.Player;
|
||||
// import player.Player1;
|
||||
// import player.Player2;
|
||||
import player.PlayerBasic;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
@ -10,5 +11,6 @@ public class Main {
|
|||
// Player p1 = new Player1();
|
||||
// Player p2 = new Player2();
|
||||
// new Game(p1, p2).start();
|
||||
new Game(new PlayerBasic(), new PlayerBasic()).start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package player;
|
|||
|
||||
public interface Player {
|
||||
|
||||
public int move(int[][] game);
|
||||
public int move(String[][] game);
|
||||
public String getName();
|
||||
public String getSymbol();
|
||||
}
|
||||
|
|
28
src/player/PlayerBasic.java
Normal file
28
src/player/PlayerBasic.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue