Not a working state, implementing winning conditions
Signed-off-by: Malte Tammena <malte.tammena@gmx.de>
This commit is contained in:
parent
0dda4b6d0c
commit
3fe047f1bb
|
@ -36,11 +36,6 @@ public class Game {
|
||||||
*/
|
*/
|
||||||
private int[][] board;
|
private int[][] board;
|
||||||
|
|
||||||
/**
|
|
||||||
* Unplayed pieces in the game.
|
|
||||||
*/
|
|
||||||
private int piecesLeft;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the game still running?
|
* Is the game still running?
|
||||||
*/
|
*/
|
||||||
|
@ -81,6 +76,7 @@ public class Game {
|
||||||
if(gameOn) {
|
if(gameOn) {
|
||||||
turn = !turn;
|
turn = !turn;
|
||||||
logGame();
|
logGame();
|
||||||
|
checkState();
|
||||||
} else {
|
} else {
|
||||||
System.out.println("*******************");
|
System.out.println("*******************");
|
||||||
System.out.println("Thanks for Playing!");
|
System.out.println("Thanks for Playing!");
|
||||||
|
@ -114,6 +110,42 @@ public class Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int crawl(int i, int j) {
|
||||||
|
int symbol = this.board[i][j];
|
||||||
|
if (i > 0) {
|
||||||
|
if (j > 0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void checkState() {
|
||||||
|
outer:for (int i = 0; i < this.board.length; i++) {
|
||||||
|
for (int j = 0; j < this.board[i].length; j++) {
|
||||||
|
if (crawl(i, j) >= 4) {
|
||||||
|
log("We have a winner!");
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// --- CHECK DRAW ---
|
||||||
|
boolean draw = true;
|
||||||
|
outer:for (int i = 0; i < this.board.length; i++) {
|
||||||
|
for (int j = 0; j < this.board[i].length; j++) {
|
||||||
|
if (this.board[i][j] == 0) {
|
||||||
|
draw = false;
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (draw) {
|
||||||
|
this.gameOn = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deep copies the board.
|
* Deep copies the board.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
package player;
|
package player;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class Player1 implements Player{
|
public class Player1 implements Player{
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
private Random ran;
|
||||||
|
|
||||||
public Player1(String name){
|
public Player1(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.ran = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int move(int[][] board){
|
public int move(int[][] board){
|
||||||
for(int i=0;i<board.length;i++){
|
int choice = ran.nextInt(7);
|
||||||
if(board[i][0] == 0){
|
while (board[choice][0] != 0) {
|
||||||
return i;
|
choice = ran.nextInt(7);
|
||||||
}
|
}
|
||||||
}
|
return choice;
|
||||||
return -1; // that means the board is full
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName(){
|
public String getName(){
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
package player;
|
package player;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class Player2 implements Player{
|
public class Player2 implements Player{
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
private Random ran;
|
||||||
|
|
||||||
public Player2(String name){
|
public Player2(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.ran = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int move(int[][] board){
|
public int move(int[][] board){
|
||||||
for(int i=0;i<board.length;i++){
|
int choice = ran.nextInt(7);
|
||||||
if(board[i][0] == 0){
|
while (board[choice][0] != 0) {
|
||||||
return i;
|
choice = ran.nextInt(7);
|
||||||
}
|
}
|
||||||
}
|
return choice;
|
||||||
return -1; // that means the board is full
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName(){
|
public String getName(){
|
||||||
|
|
Loading…
Reference in a new issue