package tp.pr5.logic;

import tp.pr5.Resources.Resources;

public class ReversiRules implements GameRules {

	private int dimX = Resources.DIMX_REVERSI;
	private int dimY = Resources.DIMY_REVERSI;
	private Counter winner;
	
	public ReversiRules() {
		winner = Counter.EMPTY;
	}

	public Counter winningMove(Move lastMove, Board b) {
		boolean valid = false;
		Counter c = Counter.EMPTY;

		valid = checkHorizontal(b, lastMove.column, lastMove.getRow(), true); // True = left Part
		valid = checkHorizontal(b, lastMove.column, lastMove.getRow(), false); // False = right part
		
		return c;
	}

	public boolean checkHorizontal(Board b, int x, int y, boolean left) {
		boolean valid = false;
		int auxColumn = x, total = 0;
		Counter color = b.getPosition(x, y);
 
		if (left) {
			while((auxColumn > 1) && (b.getPosition(auxColumn - 1, y) != color)) {
				total += 1; auxColumn -= 1; 
			} 
			if ((total >= 1) && (b.getPosition(auxColumn - 1, y) == color)) {
				valid = true; // If there is unless one Counter of the other color and the last counter is the same as the move color
				// TODO Si se han formado celdas y la ltima ficha es del mismo color que la primera 
				// TODO: Marcar las celdas
				// Posicin final para guardar: (auxColumn, y)
			}
		}
		else {
			while((auxColumn < b.getWidth()) && (b.getPosition(auxColumn + 1, y) != color)) {
				total += 1; auxColumn += 1; 
			} 
			if ((total >= 1) && (b.getPosition(auxColumn + 1, y) == color)) {
				valid = true; // If there is unless one Counter of the other color and the last counter is the same as the move color
				
				// TODO Si se han formado celdas y la ltima ficha es del mismo color que la primera 
				// TODO: Marcar las celdas
				// Posicin final para guardar: (auxColumn, y)
			}			
		}

		if (valid) {
			System.out.println("Valid move!");
		}
		
		
		return valid;
	}
	
	public Board newBoard() {
		Board b = new Board(dimX, dimY);
		
		b.setPosition(4, 4, Counter.BLACK);
		b.setPosition(5, 5, Counter.BLACK);
		b.setPosition(5, 4, Counter.WHITE);
		b.setPosition(4, 5, Counter.WHITE);
		
		return b;
	}

	public boolean isDraw(Counter lastMove, Board b) {
		// TODO Auto-generated method stub
		return false;
	}

	public Counter initialPlayer() {
		return Counter.BLACK;
	}

	public Counter nextTurn(Counter lastMove, Board b) {
		Counter nextTurn = Counter.EMPTY;
		
		if (lastMove == Counter.WHITE) {
			nextTurn = Counter.BLACK;
		}
		else if (lastMove == Counter.BLACK) {
			nextTurn = Counter.WHITE;
		}

		return nextTurn;
	}

	public int getDimX() {return this.dimX;}
	public int getDimY() {return this.dimY;}
	public int intRules() {return 3;}

}

@@ -1911,16 +1911,94 @@
  dimY);%0A
+%09%09int initX = dimX / 2, initY = dimY / 2;%0A%0A%09%09// Initializing board with tiles%0A
 %09%09%0A%09%09b.s
@@ -2012,12 +2012,22 @@
 ion(
-4, 4
+initX, initX+1
 , Co
@@ -2024,37 +2024,37 @@
 nitX+1, Counter.
-BLACK
+WHITE
 );%0A%09%09b.setPositi
@@ -2060,12 +2060,20 @@
 ion(
-5, 5
+initX, initY
 , Co
@@ -2106,12 +2106,24 @@
 ion(
-5, 4
+initX+1, initY+1
 , Co
@@ -2120,37 +2120,37 @@
 nitY+1, Counter.
-WHITE
+BLACK
 );%0A%09%09b.setPositi
@@ -2156,12 +2156,22 @@
 ion(
-4, 5
+initX+1, initY
 , Co

Initialize board with tiles, making moduralizable