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 initialPlayer() {
		return Counter.BLACK;
	}

	/**************************
	 	Winning move
	***************************/
	
	public Counter winningMove(Move lastMove, Board b) {
		Counter colorWinner = Counter.EMPTY;
		int totalWhite, totalBlack;

		if (b.isFull()) { // Si est lleno el tablero, vemos qu color tiene ms fichas
			totalWhite = countTiles(Counter.WHITE, b);
			totalBlack = countTiles(Counter.BLACK, b);
			
			if (totalWhite < totalBlack) {
				colorWinner = Counter.BLACK; 
			}
			else if (totalWhite > totalBlack) {
				colorWinner = Counter.WHITE;	// Hay ms fichas BLANCAS! Gana
			} 
			System.out.println("w:" + totalWhite);
			System.out.println("b:" + totalBlack);			
		}
		winner = colorWinner;
		return colorWinner;
	}
	
	// Return total number of appearances of a Color 
	
	public int countTiles(Counter c, Board b) { 
		int total = 0; 
		
		for(int i = 1; i <= dimX; i++) {
			for(int j = 1; j <= dimX; j++) {
				if (b.getPosition(i, j) == c) 
					total++;
			}
		}

		return total;
	}
	

	/**************************
	 	Other functions
	***************************/
	
	public boolean isDraw(Counter lastMove, Board b) {
		boolean isDraw = false;
		
		if ((b.isFull()) && (winner == Counter.EMPTY)) {
			isDraw = true;
		}
		else {
			isDraw = false;
		} 
		 
		return isDraw;
	}
	
	public Board newBoard() { // Creates a Board with some tiles on the center
		Board b = new Board(dimX, dimY);
		int initX = dimX / 2, initY = dimY / 2;
 
		b.setPosition(initX, initX+1, Counter.WHITE); // Initializing board with tiles
		b.setPosition(initX, initY, Counter.BLACK);
		b.setPosition(initX+1, initY+1, Counter.BLACK);
		b.setPosition(initX+1, initY, Counter.WHITE);
		
		return b;
	}

	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;
	}

	/**************************
	 	Getters and setters 
	***************************/
	
	public int getDimX() {
		return this.dimX;
	}
	public int getDimY() {
		return this.dimY;
	}
	public int intRules() {
		return 3;
	}

}

@@ -380,16 +380,41 @@
 ng move%0A
+%09 * @throws InvalidMove %0A
 %09*******
@@ -427,32 +427,32 @@
 **************/%0A
-
 %09%0A%09public Counte
@@ -564,24 +564,30 @@
 ck;%0A
+%09%09 
 %0A%09%09if (
-b.isFull(
+isGameOver(b
 )) %7B
@@ -596,16 +596,17 @@
 / Si est
+%C3%A1
  lleno e
@@ -618,40 +618,74 @@
 lero
-, vemos qu color tiene ms fichas
+ o ya no hay movimientos disponibles para el blanco o negro...%0A%09%09%09
 %0A%09%09%09
@@ -930,16 +930,17 @@
 // Hay m
+%C3%A1
 s fichas
@@ -963,93 +963,9 @@
 %09%09%7D 
-%0A%09%09%09System.out.println(%22w:%22 + totalWhite);%0A%09%09%09System.out.println(%22b:%22 + totalBlack);%09
+ 
 %09%09%0A%09
@@ -1288,14 +1288,175 @@
 al;%0A
+%09%7D %0A%0A%09private boolean isGameOver(Board b) %7B%0A%09%09return (b.isFull()) %7C%7C (!Resources.canMakeMove(b, Counter.BLACK) && %0A%09%09%09%09!Resources.canMakeMove(b, Counter.WHITE));%0A
 %09%7D%0A%09%0A
-%0A
 %09/**
@@ -1618,88 +1618,170 @@
 %09%0A%09%09
+%09
 if (
-(b.isFull()) && (winner == Counter.EMPTY)) %7B%0A%09%09%09isDraw = true
+isGameOver(b)) %7B%0A%0A%09%09%09int totalWhite = countTiles(Counter.WHITE, b),%0A%09%09%09totalBlack = countTiles(Counter.BLACK, b)
 ;%0A%09%09
-%7D%0A%09%09else
+%09 %0A%09%09%09if ((totalWhite == totalBlack))
  %7B%0A
+%09
 %09%09%09i
@@ -1792,20 +1792,24 @@
 w = 
-fals
+tru
 e;%0A
+%09
 %09%09%7D %0A
+%09%09%7D%0A
 %09%09 %0A

Update ReversiRules.java