Merge branch 'move-winning-method' into 'master'

Move winning method

See merge request !18
This commit is contained in:
Malte Tammena 2017-10-18 23:36:59 +02:00
commit 8b667d88fd
2 changed files with 81 additions and 79 deletions

View file

@ -202,14 +202,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!");
@ -276,4 +276,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;
}
}

View file

@ -27,83 +27,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;