Merge remote-tracking branch 'origin/master' into malte-ai
This commit is contained in:
commit
caeb331d94
|
@ -204,14 +204,14 @@ public class Game {
|
|||
* @return the state of the game, -1 = still undecided , 0 = draw, 1 = Player 1 wins, ...
|
||||
*/
|
||||
private int checkState(boolean output) {
|
||||
if(MaurizioAI.checkWin(this.board, p1.getID())){
|
||||
if(checkWin(this.board, p1.getID())){
|
||||
this.gameOn = false;
|
||||
if(output){
|
||||
System.out.println(p1.getP().getName() + " wins!");
|
||||
}
|
||||
return p1.getID(); // player 1 wins
|
||||
}
|
||||
if(MaurizioAI.checkWin(this.board, p2.getID())){
|
||||
if(checkWin(this.board, p2.getID())){
|
||||
this.gameOn = false;
|
||||
if(output){
|
||||
System.out.println(p2.getP().getName() + " wins!");
|
||||
|
@ -278,4 +278,83 @@ public class Game {
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,4 +5,5 @@ public interface Player {
|
|||
public int move(int[][] game);
|
||||
public String getName();
|
||||
public void setPlayerID(int id);
|
||||
public void setEnemyID(int id);
|
||||
}
|
||||
|
|
|
@ -45,7 +45,11 @@ public class MalteAI implements Player{
|
|||
@Override
|
||||
public void setPlayerID(int id) {
|
||||
this.id = id;
|
||||
this.enemyID = id == 1 ? 2: 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnemyID(int id) {
|
||||
this.enemyID = enemyID;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,6 +9,7 @@ public class MaurizioAI implements Player {
|
|||
private String name;
|
||||
private Random ran;
|
||||
private int id;
|
||||
private int enemyID;
|
||||
|
||||
public MaurizioAI(String name){
|
||||
this.name = name;
|
||||
|
@ -19,6 +20,10 @@ public class MaurizioAI implements Player {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public void setEnemyID(int id) {
|
||||
this.enemyID = id;
|
||||
}
|
||||
|
||||
public int move(int[][] board){
|
||||
int choice = ran.nextInt(7);
|
||||
while (board[choice][0] != 0) {
|
||||
|
@ -27,83 +32,6 @@ public class MaurizioAI implements Player {
|
|||
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(){
|
||||
return this.name;
|
||||
|
|
Loading…
Reference in a new issue