Skip to content
Snippets Groups Projects
Commit dbeb69d4 authored by sebastian.seifert's avatar sebastian.seifert
Browse files

Grobe Game Mechanic

parent 859c6916
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,8 @@ import java.util.Scanner;
public class Board implements GameMechanic{
private char[][] board;
int size = 3;
Player currentPlayer;
private int size = 3;
private Player currentPlayer;
public void init_gameboard() throws Exception {
board = new char[size][size];
......@@ -26,19 +26,11 @@ public class Board implements GameMechanic{
}
public boolean makeMove(Coords c, char player) throws Exception {
public boolean makeMove(Coords c) throws Exception {
if(!isValidMove(c)){
return false;
}
if(player == 'o'){
this.board[c.getX()][c.getY()] = 'o';
} else if(player == 'x'){
this.board[c.getX()][c.getY()] = 'x';
} else{
throw new IllegalArgumentException("Invalid player: " + player);
}
this.board[c.getX()][c.getY()] = currentPlayer.getPlayerSymbol();
return true;
}
......@@ -111,7 +103,7 @@ public class Board implements GameMechanic{
public boolean isValidMove(Coords c) {
if(c.getX() > 2 || c.getY() > 2){
if(c.getX() > 2 || c.getY() > 2 || c.getX() < 0 || c.getY() < 0){
return false;
}
return this.board[c.getX()][c.getY()] == '-';
......@@ -136,13 +128,57 @@ public class Board implements GameMechanic{
}
}
public Player getCurrentPlayer(){
return currentPlayer;
}
public static void main(String[] args) throws Exception {
System.out.println("Test");
Board board = new Board();
board.init_gameboard();
board.printBoard();
Player p1 = new Player('x');
Player p2 = new Player('o');
board.switchPlayer(p1);
Scanner scanner = new Scanner(System.in);
int printSpieler = 1;
Coords coords = new Coords();
while(true){
System.out.println("Spieler " + printSpieler + " nimm einen Zug junge los;)");
System.out.println("X Koordinate:");
coords.setX(scanner.nextInt());
System.out.println("Y Koordinate:");
coords.setY(scanner.nextInt());
board.makeMove(coords);
board.printBoard();
char idk = board.checkForWinner();
if(idk != '-'){
System.out.println("Du hast gewonnen: " + idk);
break;
}
else if(board.checkForDraw()){
System.out.println("Zuende");
break;
}
if(printSpieler == 1){
board.switchPlayer(p2);
printSpieler = 2;
} else{
board.switchPlayer(p1);
printSpieler = 1;
}
}
}
......
......@@ -9,6 +9,14 @@ public class Coords {
y = Y;
}
public void setX(int X){
this.x = X;
}
public void setY(int Y){
this.y = Y;
}
public int[] getCoords(){
int[] coordVector = new int[2];
coordVector[0] = x;
......
......@@ -4,7 +4,7 @@ public interface GameMechanic {
void init_gameboard() throws Exception;
char[][] getBoard();
boolean makeMove(Coords c, char player) throws Exception;
boolean makeMove(Coords c) throws Exception;
char checkForWinner();
boolean checkForDraw();
boolean isValidMove(Coords c);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment