Merge remote-tracking branch 'origin/master' into malte-ai

This commit is contained in:
Malte Tammena 2017-10-20 14:15:59 +02:00
commit caeb331d94
4 changed files with 92 additions and 80 deletions

View file

@ -204,14 +204,14 @@ public class Game {
* @return the state of the game, -1 = still undecided , 0 = draw, 1 = Player 1 wins, ... * @return the state of the game, -1 = still undecided , 0 = draw, 1 = Player 1 wins, ...
*/ */
private int checkState(boolean output) { private int checkState(boolean output) {
if(MaurizioAI.checkWin(this.board, p1.getID())){ if(checkWin(this.board, p1.getID())){
this.gameOn = false; this.gameOn = false;
if(output){ if(output){
System.out.println(p1.getP().getName() + " wins!"); System.out.println(p1.getP().getName() + " wins!");
} }
return p1.getID(); // player 1 wins return p1.getID(); // player 1 wins
} }
if(MaurizioAI.checkWin(this.board, p2.getID())){ if(checkWin(this.board, p2.getID())){
this.gameOn = false; this.gameOn = false;
if(output){ if(output){
System.out.println(p2.getP().getName() + " wins!"); System.out.println(p2.getP().getName() + " wins!");
@ -278,4 +278,83 @@ public class Game {
} }
System.out.println(); System.out.println();
} }
public static boolean checkWin(int[][] board, int player){
int winLength = 4;
boolean win = false;
// check columns for win
for(int i=0;i<board.length;i++){
for(int j=0;j<(board[0].length-winLength+1);j++){
if(board[i][j] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[i][j+k] != board[i][j]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
// check rows for win
for(int i=0;i<board[0].length;i++){
for(int j=0;j<(board.length-winLength+1);j++){
if(board[j][i] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[j+k][i] != board[j][i]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
// check diagonals for win
//bottom-left to top-right diagonals
for(int i=0;i<(board.length-winLength+1);i++){
for(int j=(winLength-1);j<board[0].length;j++){
if(board[i][j] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[i+k][j-k] != board[i][j]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
//top-left to bottom-right diagonals
for(int i=0;i<(board.length-winLength+1);i++){
for(int j=0;j<(board[0].length-winLength+1);j++){
if(board[i][j] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[i+k][j+k] != board[i][j]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
return false;
}
} }

View file

@ -5,4 +5,5 @@ public interface Player {
public int move(int[][] game); public int move(int[][] game);
public String getName(); public String getName();
public void setPlayerID(int id); public void setPlayerID(int id);
public void setEnemyID(int id);
} }

View file

@ -45,7 +45,11 @@ public class MalteAI implements Player{
@Override @Override
public void setPlayerID(int id) { public void setPlayerID(int id) {
this.id = id; this.id = id;
this.enemyID = id == 1 ? 2: 1; }
@Override
public void setEnemyID(int id) {
this.enemyID = enemyID;
} }
@Override @Override

View file

@ -9,6 +9,7 @@ public class MaurizioAI implements Player {
private String name; private String name;
private Random ran; private Random ran;
private int id; private int id;
private int enemyID;
public MaurizioAI(String name){ public MaurizioAI(String name){
this.name = name; this.name = name;
@ -19,6 +20,10 @@ public class MaurizioAI implements Player {
this.id = id; this.id = id;
} }
public void setEnemyID(int id) {
this.enemyID = id;
}
public int move(int[][] board){ public int move(int[][] board){
int choice = ran.nextInt(7); int choice = ran.nextInt(7);
while (board[choice][0] != 0) { while (board[choice][0] != 0) {
@ -27,83 +32,6 @@ public class MaurizioAI implements Player {
return choice; return choice;
} }
public static boolean checkWin(int[][] board, int player){
int winLength = 4;
boolean win = false;
// check columns for win
for(int i=0;i<board.length;i++){
for(int j=0;j<(board[0].length-winLength+1);j++){
if(board[i][j] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[i][j+k] != board[i][j]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
// check rows for win
for(int i=0;i<board[0].length;i++){
for(int j=0;j<(board.length-winLength+1);j++){
if(board[j][i] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[j+k][i] != board[j][i]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
// check diagonals for win
//bottom-left to top-right diagonals
for(int i=0;i<(board.length-winLength+1);i++){
for(int j=(winLength-1);j<board[0].length;j++){
if(board[i][j] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[i+k][j-k] != board[i][j]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
//top-left to bottom-right diagonals
for(int i=0;i<(board.length-winLength+1);i++){
for(int j=0;j<(board[0].length-winLength+1);j++){
if(board[i][j] == player){
win = true;
for(int k=1;k<winLength;k++){
if(board[i+k][j+k] != board[i][j]){
win = false;
break;
}
}
if(win){
return true;
}
}
}
}
return false;
}
public String getName(){ public String getName(){
return this.name; return this.name;