# [JAVA] Hilfe bei meinem Code zu Othello/Reversi (Nullpointer exception)



## Jpad (6. Dezember 2017)

Hallo,
ich muss gerade für die Uni eine KI für das Othello/Reversi Spiel Programmieren, die dann gegen eine gegnerische KI spielt. Meine KI lade ich dann auf den Uniserver welcher mit 3 Schritten ein Spiel laufen lässt: 
1. Konstuktoraufruf ( nur einmal beim Aufruf des Spiels)
2.Init (zu Beginn jeder neue Spielpartie. Ungefähr mit einem Resetknopf gleichzusetzen)
3.NextMove (führt die Spielzüge des jeweiligen Spieler aus)

Dabei hat jede KI sein eigenes Spielfeld  und die KI erstellen abwechselnd durch die NextMove-Methode ein sogenanntes Move-Objekt,  welches die x und y Koordinaten des Spielzuges beinhaltet. Dieses Move-Objekt wird dann vom Server entwertet und als "prevMove" an den Gegner weitergeleitet, damit dieser sein Spielfeld damit aktualisieren kann. Wenn ich dann ein Spiel starte erhalte ich allerdings eine Fehlermeldung die sagt : "execution error: java.lang.NullPointerException". Mein Verdacht liegt in der Init, wenn ich die newBoard-Methode aufrufe.
Ich weiss allerdings nicht was ich falsch gemacht habe und komme einfach nicht weiter..  
Für jegliche Hilfe wäre ich wirklich sehr dankbar !! 


Code meiner Hauptklasse:

```
package ki;

import java.util.Random;
import szte.mi.Move;
import szte.mi.Player;

public class OthelloPlayer implements Player {
	static int player = 0;
	static int enemy = 0;
	static GameBoard gameboard;
	Model model;
	int[] movelistX;
	int[] movelistY;
	int[] countlist;
	int firstmove = 0;
	Move move;
	// OthelloPlayer p = new OthelloPlayer();

	public OthelloPlayer() {
		gameboard = new GameBoard();
		model = new Model();
		//Arraylänge 24, da davon ausgegangen wird, dass es nie mehr als 24 legale Züge pro Runde geben wird
		movelistX = new int[24];
		movelistY = new int[24];
		countlist = new int[24];
	}

	public void init(int order, long t, Random rnd) {
		gameboard.newBoard(gameboard.board);
		setPlayer(order);
		firstmove=order;
	}

	public Move nextMove(Move prevMove, long tOpponent, long t) {
		// ---------Einfügen des gegnerischen Spielzuges ins eigene Spielfeld----------
		int x = prevMove.x, y = prevMove.y;
		gameboard.board[x][y] = enemy;
		model.combo(x, y);

		// --------------------------Spielerzug---------------------------------------
		if(firstmove==0) {
			firstmove++;
			return new Move(5,4);
		}
		updateLists();
		x = movelistX[bestMove()];
		y = movelistY[bestMove()];
		move = new Move(x, y);
		gameboard.set(x, y, player);
		return move;

	}

	// setzt alle legalen Spielzüge in Arrays
	public void updateLists() {
		int temp = 0;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (model.checkValid(i, j)) {
					if (model.count(i, j) > 0) {
						movelistX[temp] = i;
						movelistY[temp] = j;
						countlist[temp] = model.count(i, j);
						temp++;
					}
				}
			}
		}
	}

	//findet den bestmöglichen Zug durch vergleich in der Liste
	public int bestMove() {
		int best = countlist[0];
		for (int i = 0; i < countlist.length; i++) {
			if (best < countlist[i]) {
				best = i;
			}
		}
		return best;
	}

	public void setPlayer(int x) {
		if (x == 0) {
			enemy = 1;
		} else if (x == 1) {
			enemy = 0;
		}
		player = x;
	}

	public int getPlayer() {
		return player;
	}

	public int getEnemy() {
		return enemy;
	}
}
```

Code der Spielfeldklasse:

```
package ki;


public class GameBoard {
	int boardsize = 8;
	int[][] board;
	public GameBoard() {
		board = new int[8][8];
	}
	
	//initialisiert neues Spielbrett -1 leeres Feld, 0 schwarzer Spieler, 1 weisser Spieler
	public void newBoard(int[][] board) {
		for (int i=0;i<boardsize;i++) {
			for(int j=0;j<boardsize;j++) {
				board[i][j]=-1;
			}
		}
		board[3][3]=0;
		board[4][4]=0;
		board[4][3]=1;
		board[3][4]=1;
	}
	
	public int get(int x,int y) {
		return board[x][y];
	}
	
	public void set(int x,int y,int p) {
		board[x][y]=p;
	}
}
```


Viele Grüße und eine gute Nacht,


Jpad


----------



## taks (6. Dezember 2017)

gameboard = new GameBoard();
Hier initialisierst du das gameboard mit der Grösse 8x8 Veriable "board"

Wieso gibst du dann hier die Grösse nochmals mit?
gameboard.newBoard(gameboard.board);

Du kannst einfach die variable board des gameboard Objekt nehmen



public void init(int order, long t, Random rnd) {
		gameboard.newBoard();
]

public void newBoard() {
]


PS: Debugger wäre sonst ein gutes Tool. Oder evtl. mit einem try/catch arbeiten. Oder zur Not einfach überall eine message werfen ^^


----------



## Jpad (6. Dezember 2017)

Danke erstmal für dine Antwort. 
Ich weiss nicht wieso ich das so gemacht habe, dass ich die Größe da nochmal mitgebe. Bin  noch relativ neu in Java und kenne mich leider nicht gut genug aus, um effizient zu Programmieren.

Ich habe diese Übergabeparameter jetzt mal rausgenommen, so wie du es Vorgeschlagen hast. Der Fehler bleibt aber weiterhin bestehen 
Das Ding ist, dass die Datei die ich benutze, um meine KI gegen den Gegner spielen zu lassen, als Debugger dienen soll.. Wie man sieht hilft es einem noob wie mir aber nur recht wenig weiter


----------



## taks (6. Dezember 2017)

So kannst du eventuell auftretende Exception auffangen und weisst dank der Message an welcher Stelle das Problem auftritt.



```
public void init(int order, long t, Random rnd) {
	      try{
                     gameboard.newBoard(gameboard.board);
              }
              catch (Exception e) {
                     System.out.println("Fehler beim initialisieren des gameboard aufgetreten");
             }
		setPlayer(order);
		firstmove=order;
	}
```


----------



## Jpad (6. Dezember 2017)

Danke für die Info. 
 Ich wusste bereits, dass das Problem in der Zeile liegt. Ich weiss aber seit Tagen nicht aus welchem Grund er Auftritt 

LG


----------



## taks (6. Dezember 2017)

Ich glaub das Problem liegt anderswo.
Mit dem Code unten kann ich das GameBoard ohne Fehler initialisieren.
Hast du es mit dem Try/Catch um das newBoard versucht? Kommt die Fehlermeldung?


```
import java.util.Random;
public class asdad {
	public static void main(String[] args) {
		Random i = new Random();
		OthelloPlayer player = new OthelloPlayer();
		player.init(1, 1, i);
	}
}
```


```
import java.util.Random;
public class OthelloPlayer {
	static int player = 0;
	static int enemy = 0;
	static GameBoard gameboard;
	int[] movelistX;
	int[] movelistY;
	int[] countlist;
	int firstmove = 0;

	public OthelloPlayer() {
		gameboard = new GameBoard();
		movelistX = new int[24];
		movelistY = new int[24];
		countlist = new int[24];
	}

	public void init(int order, long t, Random rnd) {
		gameboard.newBoard();
		setPlayer(order);
		firstmove=order;
	}

	public int bestMove() {
		int best = countlist[0];
		for (int i = 0; i < countlist.length; i++) {
			if (best < countlist[i]) {
				best = i;
			}
		}
		return best;
	}

	public void setPlayer(int x) {
		if (x == 0) {
			enemy = 1;
		} else if (x == 1) {
			enemy = 0;
		}
		player = x;
	}

	public int getPlayer() {
		return player;
	}

	public int getEnemy() {
		return enemy;
	}
}
```


```
public class GameBoard {
	int boardsize = 8;
	int[][] board;
	public GameBoard() {
		board = new int[8][8];
	}
	
	//initialisiert neues Spielbrett -1 leeres Feld, 0 schwarzer Spieler, 1 weisser Spieler
	public void newBoard() {
		for (int i=0;i<boardsize;i++) {
			for(int j=0;j<boardsize;j++) {
				board[i][j]=-1;
			}
		}
		board[3][3]=0;
		board[4][4]=0;
		board[4][3]=1;
		board[3][4]=1;
		System.out.println("huhu");
	}
	
	public int get(int x,int y) {
		return board[x][y];
	}
	
	public void set(int x,int y,int p) {
		board[x][y]=p;
	}
}
```


----------



## Jpad (6. Dezember 2017)

Hallo,
ja ich hab es mit try/catch versucht und stoße immernoch auf das selbe Problem.. Bin langsam nur noch am verzweifeln mit meinem Code 

LG

Jpad


----------



## taks (7. Dezember 2017)

Dann mach doch in jeder Methode diesen Try/Catch mit einer Eindeutigen Meldung und du wirst dann an der Fehlermeldung sagen können an welchem Ort das Problem Auftritt.


----------



## RicoBrassers (7. Dezember 2017)

Ist so aktuell recht schlecht nachzuvollziehen.

Führe das Programm nochmal aus und kopiere uns mal die gesamte Exception hier rein.
Im Idealfall fügst du dabei noch die aktualisierten Dateien an (falls du seit dem Start dieses Threads noch Änderungen am Code gemacht haben solltest), damit man mit korrekten Zeilennummern arbeiten kann.


----------



## Jpad (7. Dezember 2017)

Ok hab jetzt mal mögliche "Checkpoints" mit System outprints markiert, und du hattest recht taks. Das Problem liegt nicht in der Boardinitialisierung, sondern wohl in derModel.checkUp Zeile 88 bzw allgemein in den check Methoden .
Ich poste mal meinen neuen Code und die Kommandozeilenausgabe ^^

Spielerklasse:

```
package ki;

import java.util.Random;
import szte.mi.Move;
import szte.mi.Player;

public class OthelloPlayer implements Player {
	static int player = 0;
	static int enemy = 0;
	static GameBoard gameboard;
	Model model;
	int[] movelistX;
	int[] movelistY;
	int[] countlist;
	int firstmove = 0;
	Move move;
	Random rnd;

	public OthelloPlayer() {
		//Arraylänge 24, da davon ausgegangen wird, dass es nie mehr als 24 legale Züge pro Runde geben wird 
	}

	public void init(int order, long t, Random rnd) {
		this.rnd=rnd;
		System.out.println("A");
		try{
 					movelistX = new int[24];
 					movelistY = new int[24];
 					countlist = new int[24];
 					model = new Model();
                    gameboard = new GameBoard();
                    System.out.println("C");
              }
              catch (Exception e) {
                     System.out.println("Fehler beim initialisieren des Gameboards aufgetreten");
             }
 	try {
		setPlayer(order);
		System.out.println("D");
		firstmove=order;
 	}catch(Exception a) {
 		System.err.println("Fehler");
 	}
	}

	public Move nextMove(Move prevMove, long tOpponent, long t) {
		int x, y;
		// ---------Einfügen des gegnerischen Spielzuges ins eigene Spielfeld----------
		if(prevMove!=null) {
		System.out.println("E");
		x = prevMove.x;
		y = prevMove.y;
		System.out.println("F");
		gameboard.board[x][y] = enemy;
		System.out.println("G");
		model.combo(x, y);
		System.out.println("H");
		}
		// --------------------------Spielerzug---------------------------------------
		if(firstmove==0) {
			System.out.println("i");
			firstmove++;
			return new Move(5,4);
		}
		updateLists();
		int tempmove = rnd.nextInt(movelistX.length);
		x = movelistX[tempmove];
		y = movelistY[tempmove];
		gameboard.set(x, y, player);
		return new Move(x,y);

	}

	// setzt alle legalen Spielzüge in Arrays
	public void updateLists() {
		int temp = 0;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (model.checkValid(i, j)) {
					if (model.count(i, j) > 0) {
						movelistX[temp] = i;
						movelistY[temp] = j;
						countlist[temp] = model.count(i, j);
						temp++;
					}
				}
			}
		}
	}

	//findet den bestmöglichen Zug durch vergleich in der Liste
	public int bestMove() {
		int best = countlist[0];
		for (int i = 0; i < countlist.length; i++) {
			if (best < countlist[i]) {
				best = i;
			}
		}
		return best;
	}

	public void setPlayer(int x) {
		if (x == 0) {
			enemy = 1;
		} else if (x == 1) {
			enemy = 0;
		}
		player = x;
	}

	public int getPlayer() {
		return player;
	}

	public int getEnemy() {
		return enemy;
	}
}
```

Spielfeldklasse:

```
package ki;


public class GameBoard {
	int boardsize = 8;
	int[][] board;
	
	public GameBoard() {
		board = new int[8][8];
		newBoard();
	}
	
	//initialisiert neues Spielbrett -1 leeres Feld, 0 schwarzer Spieler, 1 weisser Spieler
	public void newBoard() {
			System.out.println("B");
		for (int i=0;i<boardsize;i++) {
			for(int j=0;j<boardsize;j++) {
				board[i][j]=-1;
			}
		}
		
		board[3][3]=0;
		board[4][4]=0;
		board[4][3]=1;
		board[3][4]=1;
	}
	
	public int get(int x,int y) {
		return board[x][y];
	}
	
	public void set(int x,int y,int p) {
		board[x][y]=p;
	}
}
```

Model(Spiellogik)

```
package ki;


public class Model {
	
	GameBoard gameboard= OthelloPlayer.gameboard;
	
	int player;
	int enemy;
	
	public Model() {
		player = OthelloPlayer.player;
		enemy = OthelloPlayer.enemy;
	}
	public void increaseCounter() {
		counter++;
	}
	
	public void resetCounter() {
		counter=0;
	}

    private boolean checkLeftside(int x, int y) {
        if (y == 0) {
            return false;
        }
        if (gameboard.board[x][y - 1] != enemy) {
            return false;
        } else {
            int j = y - 2;
            int tile = 0;
            boolean comboed = true;
            while ((j >= 0) && (comboed)) {
                tile = gameboard.board[x][j];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    j--;
                }
            }
            return false;
        }
    }


    private boolean checkRightside(int x, int y) {
        if (y == 7) {
            return false;
        }
        if (gameboard.board[x][y + 1] != enemy) {
            return false;
        } else {
            int j = y + 2;
            int tile = 0;
            boolean comboed = true;
            while ((j <= 7) && (comboed)) {
                tile = gameboard.board[x][j];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    j++;
                }
            }
            return false;
        }
    }

    
    private boolean checkHorizontal(int x, int y) {
        if (checkLeftside(x, y)) {
            return true;
        }
        if (checkRightside(x, y)) {
            return true;
        }
        return false;
    }
    

    private boolean checkUp(int x, int y) {
        if (x == 0) {
            return false;
        }
        if ( gameboard.board[x - 1][y] != enemy) {
            return false;
        } else {
            int i = x - 2;
            int tile = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                tile =  gameboard.board[i][y];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkDown(int x, int y) {
        if (x == 7) {
            return false;
        }
        if ( gameboard.board[x + 1][y] != enemy) {
            return false;
        } else {
            int i = x + 2;
            int tile = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                tile =  gameboard.board[i][y];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    i++;
                }
            }
           return false;
        }
    }

    
    private boolean checkVertical(int x, int y) {
        if (checkUp(x, y)) {
            return true;
        }
        if (checkDown(x, y)) {
            return true;
        }
        return false;
    }
    
    
    private boolean checkUpperLeft(int x, int y) {
    	boolean comboed = false;
        if ((y == 0) || (x == 0)) {
            return comboed;
        }
        if (gameboard.board[x - 1][y - 1] != enemy) {
            return comboed;
        } else {
            int j = y - 2;
            int i = x - 2;
            int tile = 0;
            comboed = true;
            while ((j >= 0) && (i >= 0) && (comboed)) {
                tile = gameboard.board[i][j];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    j--;
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkUpperRight(int x, int y) {
        if ((y == 7) || (x == 0)) {
            return false;
        }
        if ( gameboard.board[x - 1][y + 1] != enemy ) {
            return false;
        } else {
            int j = y + 2;
            int i = x - 2;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i >= 0) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != enemy) {
                    comboed = false;
                    if (value == player) {
                        return true;
                    }
                } else {
                    j++;
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkLowerLeft(int x, int y) {
        int max = 7;
        if ((y == 0) || (x == max)) {
            return false;
        }
        if ( gameboard.board[x + 1][y - 1] != enemy) {
            return false;
        } else {
            int j = y - 2;
            int i = x + 2;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i <= max) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != enemy) {
                    comboed = false;
                    if (value == player) {
                        return true;
                    }
                } else {
                    j--;
                    i++;
                }
            }
            return false;
        }
    }


    private boolean checkLowerRight(int x, int y) {
        if ((y == 7) || (x == 7)) {
            return false;
        }
        if ( gameboard.board[x + 1][y + 1] != enemy) {
            return false;
        } else {
            int j = y + 2;
            int i = x + 2;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i < 8) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != enemy) {
                    comboed = false;
                    if (value == player) {
                        return true;
                    }
                } else {
                    j++;
                    i++;
                }
            }
            return false;
        }
    }

    
    private boolean checkDiagonal(int x, int y) {
        if (checkUpperLeft(x, y)) {
            return true;
        }
        if (checkUpperRight(x, y)) {
            return true;
        }
        if (checkLowerLeft(x, y)) {
            return true;
        }
        if (checkLowerRight(x, y)) {
            return true;
        }
        return false;
    }
    
    
    private void comboLeftside(int x, int y) {
        if (checkLeftside(x, y)) {
            int i = y - 1;
            int tile = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                tile = gameboard.board[x][i];
                if (tile == player) {
                    comboed = false;
                } else {
                    gameboard.board[x][i] = player;
                    i--;
                }
            }
        }
    }


    private void comboRightside(int x, int y) {
        if (checkRightside(x, y)) {
            int i = y + 1;
            int value = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                value = gameboard.board[x][i];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[x][i] = player;
                    i++;
                }
            }
        }
    }

    
    private void comboDown(int x, int y) {
        if (checkDown(x, y)) {
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                value = gameboard.board[i][y];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][y] = player;
                    i++;
                }
            }
        }
    }

    
    private void comboUp(int x, int y) {
        if (checkUp(x, y)) {
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                value = gameboard.board[i][y];
                if (value == player) {
                    comboed = false;
                } else {
                     gameboard.board[i][y] = player;
                    i--;
                }
            }
        }
    }

    
    private void comboUpperLeft(int x, int y) {
        if (checkUpperLeft(x, y)) {
            int j = y - 1;
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i >= 0) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j--;
                    i--;
                }
            }
        }
    }

    
    private void comboUpperRight(int x, int y) {
        if (checkUpperRight(x, y)) {
            int j = y + 1;
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i >= 0) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j++;
                    i--;
                }
            }
        }
    }

    
    private void comboLowerLeft(int x, int y) {
        if (checkLowerLeft(x, y)) {
            int j = y - 1;
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i < 8) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                     gameboard.board[i][j] = player;
                    j--;
                    i++;
                }
            }
        }
    }

    
    private void comboLowerRight(int x, int y) {
        if (checkLowerRight(x, y)) {
            int j = y + 1;
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i < 8) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j++;
                    i++;
                }
            }
        }
    }

    public void combo(int x, int y) {
    	System.out.println("Combo Anfang");
        comboUp(x, y);
        comboDown(x, y);
        comboLeftside(x, y);
        comboRightside(x, y);
        comboUpperLeft(x, y);
        comboUpperRight(x, y);
        comboLowerLeft(x, y);
        comboLowerRight(x, y);
        System.out.println("Combo ende");
    }
```

Kommandoausgabe:
A
B
C
D
i
E
F
G
Combo Anfang
2 execution error: java.lang.NullPointerException


Viele Grüße und nochmals vielen Dank für eure Hilfe ,
Jpad


----------



## taks (7. Dezember 2017)

edit: ich schaus morgen nochmals an, bin zu Müde -.-


oder: eventuell, aber ich garantiere für nichts ^^


```
public class Model {
	
	[COLOR="#FF0000"]GameBoard gameboard= OthelloPlayer.gameboard;	
	int player;
	int enemy;
	
	public Model() {
		player = OthelloPlayer.player;
		enemy = OthelloPlayer.enemy;
	}
}
```


Du musst das gameboard zuerst erstellen bevor du etwas zuweist.
Jetzt ist nurnoch die Frage woher der OthelloPlayer kommen soll.



```
public class Model {
	
	GameBoard gameboard = New GameBoard();
       gameboard = OthelloPlayer.gameboard;
	
	int player;
	int enemy;
	
	public Model() {
		player = OthelloPlayer.player;
		enemy = OthelloPlayer.enemy;
	}
}
```


----------



## Jpad (7. Dezember 2017)

Nur kein Stress ^^


----------



## taks (7. Dezember 2017)

*Siehe Update


```
package ki;

import java.util.Random;
import szte.mi.Move;
import szte.mi.Player;

public class OthelloPlayer implements Player {
	static int player = 0;
	static int enemy = 0;
	static GameBoard gameboard;
	Model model;
	int[] movelistX;
	int[] movelistY;
	int[] countlist;
	int firstmove = 0;
	Move move;
	Random rnd;

	public OthelloPlayer() {
		//Arraylänge 24, da davon ausgegangen wird, dass es nie mehr als 24 legale Züge pro Runde geben wird 
	}

	public void init(int order, long t, Random rnd) {
		this.rnd=rnd;
		System.out.println("A");
		try{
 					movelistX = new int[24];
 					movelistY = new int[24];
 					countlist = new int[24];
 					model = new Model();
                    gameboard = new GameBoard();
                    System.out.println("C");
              }
              catch (Exception e) {
                     System.out.println("Fehler beim initialisieren des Gameboards aufgetreten");
             }
 	try {
		setPlayer(order);
		System.out.println("D");
		firstmove=order;
 	}catch(Exception a) {
 		System.err.println("Fehler");
 	}
	}

	public Move nextMove(Move prevMove, long tOpponent, long t) {
		int x, y;
		// ---------Einfügen des gegnerischen Spielzuges ins eigene Spielfeld----------
		if(prevMove!=null) {
		System.out.println("E");
		x = prevMove.x;
		y = prevMove.y;
		System.out.println("F");
		gameboard.board[x][y] = enemy;
		System.out.println("G");
		[COLOR="#00FF00"]model.combo(gameboard, x, y);		System.out.println("H");
		}
		// --------------------------Spielerzug---------------------------------------
		if(firstmove==0) {
			System.out.println("i");
			firstmove++;
			return new Move(5,4);
		}
		updateLists();
		int tempmove = rnd.nextInt(movelistX.length);
		x = movelistX[tempmove];
		y = movelistY[tempmove];
		gameboard.set(x, y, player);
		return new Move(x,y);

	}

	// setzt alle legalen Spielzüge in Arrays
	public void updateLists() {
		int temp = 0;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (model.checkValid(i, j)) {
					if (model.count(i, j) > 0) {
						movelistX[temp] = i;
						movelistY[temp] = j;
						countlist[temp] = model.count(i, j);
						temp++;
					}
				}
			}
		}
	}

	//findet den bestmöglichen Zug durch vergleich in der Liste
	public int bestMove() {
		int best = countlist[0];
		for (int i = 0; i < countlist.length; i++) {
			if (best < countlist[i]) {
				best = i;
			}
		}
		return best;
	}

	public void setPlayer(int x) {
		if (x == 0) {
			enemy = 1;
		} else if (x == 1) {
			enemy = 0;
		}
		player = x;
	}

	public int getPlayer() {
		return player;
	}

	public int getEnemy() {
		return enemy;
	}
}
```


```
package ki;


public class Model {
	
	[COLOR="#00FF00"]GameBoard gameboard= new GameBoard();	
	int player;
	int enemy;
	
	public Model() {
		player = OthelloPlayer.player;
		enemy = OthelloPlayer.enemy;
	}
	public void increaseCounter() {
		counter++;
	}
	
	public void resetCounter() {
		counter=0;
	}

    private boolean checkLeftside(int x, int y) {
        if (y == 0) {
            return false;
        }
        if (gameboard.board[x][y - 1] != enemy) {
            return false;
        } else {
            int j = y - 2;
            int tile = 0;
            boolean comboed = true;
            while ((j >= 0) && (comboed)) {
                tile = gameboard.board[x][j];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    j--;
                }
            }
            return false;
        }
    }


    private boolean checkRightside(int x, int y) {
        if (y == 7) {
            return false;
        }
        if (gameboard.board[x][y + 1] != enemy) {
            return false;
        } else {
            int j = y + 2;
            int tile = 0;
            boolean comboed = true;
            while ((j <= 7) && (comboed)) {
                tile = gameboard.board[x][j];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    j++;
                }
            }
            return false;
        }
    }

    
    private boolean checkHorizontal(int x, int y) {
        if (checkLeftside(x, y)) {
            return true;
        }
        if (checkRightside(x, y)) {
            return true;
        }
        return false;
    }
    

    private boolean checkUp(int x, int y) {
        if (x == 0) {
            return false;
        }
        if ( gameboard.board[x - 1][y] != enemy) {
            return false;
        } else {
            int i = x - 2;
            int tile = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                tile =  gameboard.board[i][y];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkDown(int x, int y) {
        if (x == 7) {
            return false;
        }
        if ( gameboard.board[x + 1][y] != enemy) {
            return false;
        } else {
            int i = x + 2;
            int tile = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                tile =  gameboard.board[i][y];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    i++;
                }
            }
           return false;
        }
    }

    
    private boolean checkVertical(int x, int y) {
        if (checkUp(x, y)) {
            return true;
        }
        if (checkDown(x, y)) {
            return true;
        }
        return false;
    }
    
    
    private boolean checkUpperLeft(int x, int y) {
    	boolean comboed = false;
        if ((y == 0) || (x == 0)) {
            return comboed;
        }
        if (gameboard.board[x - 1][y - 1] != enemy) {
            return comboed;
        } else {
            int j = y - 2;
            int i = x - 2;
            int tile = 0;
            comboed = true;
            while ((j >= 0) && (i >= 0) && (comboed)) {
                tile = gameboard.board[i][j];
                if (tile != enemy) {
                    comboed = false;
                    if (tile == player) {
                        return true;
                    }
                } else {
                    j--;
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkUpperRight(int x, int y) {
        if ((y == 7) || (x == 0)) {
            return false;
        }
        if ( gameboard.board[x - 1][y + 1] != enemy ) {
            return false;
        } else {
            int j = y + 2;
            int i = x - 2;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i >= 0) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != enemy) {
                    comboed = false;
                    if (value == player) {
                        return true;
                    }
                } else {
                    j++;
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkLowerLeft(int x, int y) {
        int max = 7;
        if ((y == 0) || (x == max)) {
            return false;
        }
        if ( gameboard.board[x + 1][y - 1] != enemy) {
            return false;
        } else {
            int j = y - 2;
            int i = x + 2;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i <= max) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != enemy) {
                    comboed = false;
                    if (value == player) {
                        return true;
                    }
                } else {
                    j--;
                    i++;
                }
            }
            return false;
        }
    }


    private boolean checkLowerRight(int x, int y) {
        if ((y == 7) || (x == 7)) {
            return false;
        }
        if ( gameboard.board[x + 1][y + 1] != enemy) {
            return false;
        } else {
            int j = y + 2;
            int i = x + 2;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i < 8) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != enemy) {
                    comboed = false;
                    if (value == player) {
                        return true;
                    }
                } else {
                    j++;
                    i++;
                }
            }
            return false;
        }
    }

    
    private boolean checkDiagonal(int x, int y) {
        if (checkUpperLeft(x, y)) {
            return true;
        }
        if (checkUpperRight(x, y)) {
            return true;
        }
        if (checkLowerLeft(x, y)) {
            return true;
        }
        if (checkLowerRight(x, y)) {
            return true;
        }
        return false;
    }
    
    
    private void comboLeftside(int x, int y) {
        if (checkLeftside(x, y)) {
            int i = y - 1;
            int tile = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                tile = gameboard.board[x][i];
                if (tile == player) {
                    comboed = false;
                } else {
                    gameboard.board[x][i] = player;
                    i--;
                }
            }
        }
    }


    private void comboRightside(int x, int y) {
        if (checkRightside(x, y)) {
            int i = y + 1;
            int value = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                value = gameboard.board[x][i];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[x][i] = player;
                    i++;
                }
            }
        }
    }

    
    private void comboDown(int x, int y) {
        if (checkDown(x, y)) {
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                value = gameboard.board[i][y];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][y] = player;
                    i++;
                }
            }
        }
    }

    
    private void comboUp(int x, int y) {
        if (checkUp(x, y)) {
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                value = gameboard.board[i][y];
                if (value == player) {
                    comboed = false;
                } else {
                     gameboard.board[i][y] = player;
                    i--;
                }
            }
        }
    }

    
    private void comboUpperLeft(int x, int y) {
        if (checkUpperLeft(x, y)) {
            int j = y - 1;
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i >= 0) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j--;
                    i--;
                }
            }
        }
    }

    
    private void comboUpperRight(int x, int y) {
        if (checkUpperRight(x, y)) {
            int j = y + 1;
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i >= 0) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j++;
                    i--;
                }
            }
        }
    }

    
    private void comboLowerLeft(int x, int y) {
        if (checkLowerLeft(x, y)) {
            int j = y - 1;
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i < 8) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                     gameboard.board[i][j] = player;
                    j--;
                    i++;
                }
            }
        }
    }

    
    private void comboLowerRight(int x, int y) {
        if (checkLowerRight(x, y)) {
            int j = y + 1;
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i < 8) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j++;
                    i++;
                }
            }
        }
    }

    [COLOR="#00FF00"]public void combo(Gameboard gameboard, int x, int y) {
    	System.out.println("Combo Anfang");
        this.gameboard = gameboard;       comboUp(x, y);
        comboDown(x, y);
        comboLeftside(x, y);
        comboRightside(x, y);
        comboUpperLeft(x, y);
        comboUpperRight(x, y);
        comboLowerLeft(x, y);
        comboLowerRight(x, y);
        System.out.println("Combo ende");
    }
```

Glaub nicht die schönnste Lösung, aber sollte klappen


----------



## Jpad (7. Dezember 2017)

edit:Ich habe es mal mit deinem Vorschlag versucht. Leider erscheint dieselbe Fehlermeldung..
Ich dachte ich darf die Zuweisung so machen, weil ich das GameBoard ja im OthelloPlayer beim Konstruktor erstelle. Der wird zu Beginn des Testprogramms aufgerufen


```
public OthelloPlayer() {
		gameboard = new GameBoard();
		model = new Model();
	}
```


Ich geb hier mal meinen aktuellen Code ein. Ich denke das ist transparenter so

OthelloPlayer
	
	



```
package ki;

import java.util.Random;
import szte.mi.Move;
import szte.mi.Player;

public class OthelloPlayer implements Player {
	static int player = 0;
	static int enemy = 0;
	static GameBoard gameboard;
	Model model;
	OthelloPlayer p;
	Move[] movelist;
	int[] gainlist;
	int firstmove = 0;
	Move move;
	Random rnd;

	public OthelloPlayer() {
		gameboard = new GameBoard();
		model = new Model();
	}

	public void init(int order, long t, Random rnd) {
		this.rnd = rnd;
		System.out.println("A");
			p = new OthelloPlayer();
			System.out.println("C");

			p.setPlayer(order);
			System.out.println("D");
			p.firstmove = order;

	}

	public Move nextMove(Move prevMove, long tOpponent, long t) {
		if(prevMove!=null) {
			p.gameboard.set(prevMove.y, prevMove.x, enemy);
			p.model.combo(gameboard,prevMove.y, prevMove.x, player);
		}
		p.gameboard.printBoard();
		return p.kiPlacement();
	}


	//findet mögliche Spielzüge und setzt diese in Arrays
	public void kiMoves() {
		int temp = 0;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (model.checkValid(i, j,player)) {
					temp++;
					System.out.println(j+" "+ i+" is a valid move");
				}
			}
		}
		movelist = new Move[temp];
		gainlist = new int[temp];
		temp = 0;
		for (int i = 0; i < 8; i++) {
			int x = i;
			for (int j = 0; j < 8; j++) {
				int y = j;
				if (model.checkValid(i, j,player)) {
					movelist[temp]= new Move(i,j);
					gainlist[temp] = model.count(x, y, player);
					temp++;
				}
			}
		}
		System.out.println("KI Move erfolgreich");
	}

	public Move kiPlacement() {
		kiMoves();
		System.out.println("P1");
			int temp = rnd.nextInt(movelist.length);
			System.out.println("P2");
			int y = movelist[temp].y;
			int x = movelist[temp].x;
			gameboard.set(x,y,player);
			System.out.println("P3");
			gameboard.printBoard();
			System.out.println("Spielfeld nach setzen");
			model.combo(gameboard,movelist[temp].x, movelist[temp].y, player);
			System.out.println("P4");
			if (movelist.length < 0) {
				return null;
			}
			System.out.println("CHECK");
		return movelist[temp];
	}	
	
	int temp = 0;
	public void tempUp() {
		temp++;
	}
	// setzt alle legalen Spielzüge in Arrays
	public void updateLists() {
		System.out.println("Update anfang");
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (model.checkValid(i, j,player)) {
					tempUp();
					System.out.println("temp erhöht");
				}
			}
		}
		movelist = new Move[temp];
		System.out.println("liste X ist"+temp);
		gainlist = new int[temp];
		temp = 0;
		for (int i = 0; i < 8; i++) {
			int x = i;
			for (int j = 0; j < 8; j++) {
				int y =j;
				if (model.checkValid(x, y,player)) {
					if (model.count(x, y,player) > 0) {
						movelist[temp]= new Move(x,y);
						gainlist[temp] = model.count(x, y,player);
						temp++;
					}
				}
			}
		}
		System.out.println("Update ende");
	}

//	public void newList() {
//	int temp = 0;
//	for (int i = 0; i < 8; i++) {
//		for (int j = 0; j < 8; j++) {
//			if (model.checkValid(i, j)) {
//				temp++;
//			}
//		}
//	}
//	movelistX = new int[temp];
//	movelistY = new int[temp];
//	temp = 0;
//	for (int i = 0; i < 8; i++) {
//		int x = i;
//		for (int j = 0; j < 8; j++) {
//			int y = j;
//			if (model.checkValid(i, j)) {
//				movelistX[temp] = x;
//				movelistY[temp] = y;
//				temp++;
//			}
//		}
//	}
//}
	
	
	
	// findet den bestmöglichen Zug durch vergleich in der Liste
	public int bestMove() {
		int best = gainlist[0];
		for (int i = 0; i < gainlist.length; i++) {
			if (best < gainlist[i]) {
				best = i;
			}
		}
		return best;
	}

	public void setPlayer(int x) {
		if (x == 0) {
			enemy = 1;
		} else if (x == 1) {
			enemy = 0;
		}
		player = x;
	}

	public int getPlayer() {
		return player;
	}

	public int getEnemy() {
		return enemy;
	}
}
```


GameBoard

```
package ki;


public class GameBoard {
	int boardsize = 8;
	int[][] board;
	
	public GameBoard() {
		board = new int[8][8];
		newBoard();
	}
	
	//initialisiert neues Spielbrett -1 leeres Feld, 0 schwarzer Spieler, 1 weisser Spieler
	public void newBoard() {
			System.out.println("B");
		for (int i=0;i<boardsize;i++) {
			for(int j=0;j<boardsize;j++) {
				board[i][j]=-1;
			}
		}
		
		board[3][3]=1;
		board[4][4]=1;
		board[4][3]=0;
		board[3][4]=0;
	}
	
	public int get(int x,int y) {
		return board[x][y];
	}
	
	public void set(int x,int y,int p) {
		board[x][y]=p;
	}
	
	public void printBoard() {
		int reihe = 0;
		int spalte = 0;

		for (int i = 0; i < 8; i++) {
			System.out.println();
			System.out.print("|");
			for (int j = 0; j < 8; j++) {
				System.out.print(board[i][j]);
				System.out.print("|");
			}
		}
		System.out.println("");
	}
}
```


Spiellogik (Fehlerquelle)

```
package ki;

public class Model {
	
	GameBoard gameboard;
	
	int player;
	int enemy;
	int counter=0;
	int gain=0;
	int pass=0;
	
	public Model() {
		gameboard = OthelloPlayer.gameboard;
		player = OthelloPlayer.player;
	}
	public int getEnemy(int currentplayer) {
		if (currentplayer==0) {
			return 1;
		}else 
		return 0;
		
	}
	public void increaseCounter() {
		counter++;
	}
	
	public void resetCounter() {
		counter=0;
	}

    private boolean checkLeftside(int x, int y,int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        if (y == 0) {
            return false;
        }
        if (gameboard.board[x][y - 1] != currentenemy) {
            return false;
        } else {
            int j = y - 2;
            int tile = 0;
            boolean comboed = true;
            while ((j >= 0) && (comboed)) {
                tile = gameboard.board[x][j];
                if (tile != currentenemy) {
                    comboed = false;
                    if (tile == currentplayer) {
                        return true;
                    }
                } else {
                    j--;
                }
            }
            return false;
        }
    }


    private boolean checkRightside(int x, int y, int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        if (y == 7) {
            return false;
        }
        if (gameboard.board[x][y + 1] != currentenemy) {
            return false;
        } else {
            int j = y + 2;
            int tile = 0;
            boolean comboed = true;
            while ((j <= 7) && (comboed)) {
                tile = gameboard.board[x][j];
                if (tile != currentenemy) {
                    comboed = false;
                    if (tile == currentplayer) {
                        return true;
                    }
                } else {
                    j++;
                }
            }
            return false;
        }
    }

    
    private boolean checkHorizontal(int x, int y,int player) {
        if (checkLeftside(x, y,player)) {
            return true;
        }
        if (checkRightside(x, y,player)) {
            return true;
        }
        return false;
    }
    

    private boolean checkUp(int x, int y, int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        if (x == 0) {
            return false;
        }
        if ( gameboard.board[x - 1][y] != currentenemy) {
            return false;
        } else {
            int i = x - 2;
            int tile = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                tile =  gameboard.board[i][y];
                if (tile != currentenemy) {
                    comboed = false;
                    if (tile == currentplayer) {
                        return true;
                    }
                } else {
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkDown(int x, int y, int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        if (x == 7) {
            return false;
        }
        if ( gameboard.board[x + 1][y] != currentenemy) {
            return false;
        } else {
            int i = x + 2;
            int tile = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                tile =  gameboard.board[i][y];
                if (tile != currentenemy) {
                    comboed = false;
                    if (tile == currentplayer) {
                        return true;
                    }
                } else {
                    i++;
                }
            }
           return false;
        }
    }

    
    private boolean checkVertical(int x, int y,int player) {
        if (checkUp(x, y,player)) {
            return true;
        }
        if (checkDown(x, y,player)) {
            return true;
        }
        return false;
    }
    
    
    private boolean checkUpperLeft(int x, int y,int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
    	boolean comboed = false;
        if ((y == 0) || (x == 0)) {
            return comboed;
        }
        if (gameboard.board[x - 1][y - 1] != currentenemy) {
            return comboed;
        } else {
            int j = y - 2;
            int i = x - 2;
            int tile = 0;
            comboed = true;
            while ((j >= 0) && (i >= 0) && (comboed)) {
                tile = gameboard.board[i][j];
                if (tile != currentenemy) {
                    comboed = false;
                    if (tile == currentplayer) {
                        return true;
                    }
                } else {
                    j--;
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkUpperRight(int x, int y, int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        if ((y == 7) || (x == 0)) {
            return false;
        }
        if ( gameboard.board[x - 1][y + 1] != currentenemy ) {
            return false;
        } else {
            int j = y + 2;
            int i = x - 2;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i >= 0) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != currentenemy) {
                    comboed = false;
                    if (value == currentplayer) {
                        return true;
                    }
                } else {
                    j++;
                    i--;
                }
            }
            return false;
        }
    }


    private boolean checkLowerLeft(int x, int y,int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        int max = 7;
        if ((y == 0) || (x == max)) {
            return false;
        }
        if ( gameboard.board[x + 1][y - 1] != currentenemy) {
            return false;
        } else {
            int j = y - 2;
            int i = x + 2;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i <= max) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != currentenemy) {
                    comboed = false;
                    if (value == currentplayer) {
                        return true;
                    }
                } else {
                    j--;
                    i++;
                }
            }
            return false;
        }
    }


    private boolean checkLowerRight(int x, int y,int currentplayer) {
    	int currentenemy = getEnemy(currentplayer);
        if ((y == 7) || (x == 7)) {
            return false;
        }
        if ( gameboard.board[x + 1][y + 1] != currentenemy) {
            return false;
        } else {
            int j = y + 2;
            int i = x + 2;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i < 8) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value != currentenemy) {
                    comboed = false;
                    if (value == currentplayer) {
                        return true;
                    }
                } else {
                    j++;
                    i++;
                }
            }
            return false;
        }
    }

    
    private boolean checkDiagonal(int x, int y, int player) {
        if (checkUpperLeft(x, y,player)) {
            return true;
        }
        if (checkUpperRight(x, y,player)) {
            return true;
        }
        if (checkLowerLeft(x, y,player)) {
            return true;
        }
        if (checkLowerRight(x, y,player)) {
            return true;
        }
        return false;
    }
    
    
    private void comboLeftside(int x, int y,int player) {
        if (checkLeftside(x, y,player)) {
            int i = y - 1;
            int tile = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                tile = gameboard.board[x][i];
                if (tile == player) {
                    comboed = false;
                } else {
                    gameboard.board[x][i] = player;
                    i--;
                }
            }
        }
    }


    private void comboRightside(int x, int y,int player) {
        if (checkRightside(x, y,player)) {
            int i = y + 1;
            int value = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                value = gameboard.board[x][i];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[x][i] = player;
                    i++;
                }
            }
        }
    }

    
    private void comboDown(int x, int y,int player) {
        if (checkDown(x, y,player)) {
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((i <= 7) && (comboed)) {
                value = gameboard.board[i][y];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][y] = player;
                    i++;
                }
            }
        }
    }

    
    private void comboUp(int x, int y, int player) {
        if (checkUp(x, y,player)) {
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((i >= 0) && (comboed)) {
                value = gameboard.board[i][y];
                if (value == player) {
                    comboed = false;
                } else {
                     gameboard.board[i][y] = player;
                    i--;
                }
            }
        }
    }

    
    private void comboUpperLeft(int x, int y, int player) {
        if (checkUpperLeft(x, y,player)) {
            int j = y - 1;
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i >= 0) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j--;
                    i--;
                }
            }
        }
    }

    
    private void comboUpperRight(int x, int y, int player) {
        if (checkUpperRight(x, y,player)) {
            int j = y + 1;
            int i = x - 1;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i >= 0) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j++;
                    i--;
                }
            }
        }
    }

    
    private void comboLowerLeft(int x, int y, int player) {
        if (checkLowerLeft(x, y,player)) {
            int j = y - 1;
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((j >= 0) && (i < 8) && (comboed)) {
                value =  gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                     gameboard.board[i][j] = player;
                    j--;
                    i++;
                }
            }
        }
    }

    
    private void comboLowerRight(int x, int y,int player) {
        if (checkLowerRight(x, y,player)) {
            int j = y + 1;
            int i = x + 1;
            int value = 0;
            boolean comboed = true;
            while ((j < 8) && (i < 8) && (comboed)) {
                value = gameboard.board[i][j];
                if (value == player) {
                    comboed = false;
                } else {
                    gameboard.board[i][j] = player;
                    j++;
                    i++;
                }
            }
        }
    }
    
     public void combo(GameBoard gameboard,int x, int y,int player) {
    	System.out.println("Combo start");
    	this.gameboard = gameboard;
        comboUp(x, y, player);
        comboDown(x, y, player);
        comboLeftside(x, y, player);
        comboRightside(x, y, player);
        comboUpperLeft(x, y, player);
        comboUpperRight(x, y, player);
        comboLowerLeft(x, y,player);
        comboLowerRight(x, y, player);
        pass=0;
        gameboard.printBoard();
        System.out.println("Combo ende");
    }

public boolean checkValid(int x, int y,int player) {
        if ( gameboard.board[x][y] != -1) {
            return false;
        }
        if ( checkVertical(x, y,player)) {
            return true;
        }
        if ( checkHorizontal(x, y,player)) {
            return true;
        }
        if ( checkDiagonal(x, y,player)) {
            return true;
        }
        return false;
    }
    
}
```

Kommandoausgabe:
B
A
B
C
D

|-1|-1|-1|-1|-1|-1|-1|-1|
|-1|-1|-1|-1|-1|-1|-1|-1|
|-1|-1|-1|-1|-1|-1|-1|-1|
|-1|-1|-1|1|0|-1|-1|-1|
|-1|-1|-1|0|1|-1|-1|-1|
|-1|-1|-1|-1|-1|-1|-1|-1|
|-1|-1|-1|-1|-1|-1|-1|-1|
|-1|-1|-1|-1|-1|-1|-1|-1|
3 2 is a valid move
2 3 is a valid move
5 4 is a valid move
4 5 is a valid move
KI Move erfolgreich
P1
2 execution error: java.lang.NullPointerException


----------



## RicoBrassers (8. Dezember 2017)

Im Konstruktor von Model greifst du statisch auf den Wert gameboard der Klasse OthelloPlayer zu. Ist denn garantiert, dass irgendwo vorher der Konstruktor von OthelloPlayer aufgerufen wird?

Liefer mal bitte deine Main-Methode (public static void main(String[])) nach, die scheint mit hier komplett zu fehlen (wie bei den bisherigen Codesamples von dir auch). 

In Java wird der Konstruktor nur dann aufgerufen, wenn man das explizit mit dem "new"-Schlüsselwort macht. Ich vermute mal, dass dies bei der Klasse OthelloPlayer entweder gar nicht oder aber viel zu spät passiert und damit OthelloPlayer.gameboard noch null ist, wenn du im Konstruktor der Klasse Model darauf zugreifen möchtest.


----------



## Jpad (9. Dezember 2017)

Die Main-Methode wird hier nicht benötigt, da sie im Testprogramm der Uni enthalten ist. In diesem wird der Konstruktor zu 100% aufgerufen. ^^
In dieser Main-Methode wird der Konstruktor aufgerufen, dann die Init Methode und dann jeweils nextMove für jeden Zug.


----------

