
package ch.hearc.pokerface.gameengine.gamecore.state;

import java.util.List;

import ch.hearc.pokerface.gameengine.gamecore.GameEngine;
import ch.hearc.pokerface.gameengine.player.Player;
import ch.hearc.pokerface.gameengine.player.Role;

public class BettingState extends State
{
	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	private static boolean	postSmallBlind	= false;
	private static boolean	postBigBlind	= false;

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	@Override
	public void addCads(GameEngine ge)
	{
		//Nothing
	}

	@Override
	public void bet(GameEngine ge)
	{
		do
		{
			ge.updateGUI();
			if (!postSmallBlind && !postBigBlind)//In this case, if everybody checks, the big blind can play twice
			{
				firstBetProcessing(ge, ge.getCurrentPlayer());
			}
			else
			{
				normalBetProcessing(ge, ge.getCurrentPlayer());
			}
			ge.updateGUI();
		} while(!allChecked(ge));

		ge.setNewState();
		ge.updateGUI();

		//Reinitialise the player's bet
		Player firstPlayer = ge.getCurrentPlayer();
		Player currentPlayer = firstPlayer;
		do
		{
			currentPlayer.endBettingState();
			currentPlayer = ge.getPlayers().get(ge.changeCurrentPlayer());
		} while(currentPlayer != firstPlayer);
	}

	@Override
	public void nextSate(GameEngine ge)
	{
		switch(ge.getOldState())
		{
			case PreFlopState:
				ge.setState(new FlopState());
				break;

			case FlopState:
				ge.setState(new TurnState());
				break;

			case TurnState:
				ge.setState(new RiverState());
				break;

			case RiverState:
				//Each time, it's a new instance of BettingState, so we must reinitialize the static attributes
				postBigBlind = false;
				postSmallBlind = false;
				ge.setState(new PreFlopState());
				ge.showdown();
				break;

			default:
				break;
		}
	}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private boolean allChecked(GameEngine ge)
	{
		boolean allChecked = true;
		int nbUnfoldedPlayer = ge.getUnfoldedPlayer();
		int nbAllInPlayer = ge.getAllInPlayer();

		if (nbAllInPlayer != nbUnfoldedPlayer)
		{
			List<Player> players = ge.getPlayers();
			for(Player p:players)
			{
				//If the player isn't folded and his bet = betSpend or has all in
				if (p.isFolded() || (p.getBetSpending() < ge.getPot().getBet() && p.getBankroll() != 0))
				{
					allChecked = false;
				}
			}
			allChecked = (allChecked || (nbUnfoldedPlayer - nbAllInPlayer) <= 1);
		}

		return allChecked;
	}

	private void normalBetProcessing(GameEngine ge, Player player)
	{
		//If he checks, he's considered as the last player who has "raised"
		if(ge.getPot().getBet() == 0)
		{
			ge.setIndexLastRaise(player);
		}
		do
		{
			if (player.getBankroll() != 0)//If not all in
			{
				//If player -> wait()
				//Else IA computes
				player.doAction();
			}
			ge.updateGUI();
			ge.changeCurrentPlayer();
			player = ge.getCurrentPlayer();
			ge.updateGUI();
		}while(player != ge.getLastRaisePlayer());
	}

	private void firstBetProcessing(GameEngine ge, Player player)
	{
		boolean hasBigBlindToPlayTwice = false;
		boolean isThePlayerTheLastRaisePlayer = false;
		int nbDecision = 2;
		while(!isThePlayerTheLastRaisePlayer)
		{
			++nbDecision;
			if (!postSmallBlind)
			{
				ge.betSmallBlind();
				postSmallBlind = true;
			}
			else if (!postBigBlind)
			{
				ge.betBigBlind();
				postBigBlind = true;
			}
			else if (player.getBankroll() != 0)//If not all in
			{
				//If player -> wait()
				//Else IA computes
				player.doAction();
			}
			ge.updateGUI();
			ge.changeCurrentPlayer();
			player = ge.getCurrentPlayer();
			ge.updateGUI();

			isThePlayerTheLastRaisePlayer = player == ge.getLastRaisePlayer();

			//If everybody checks, the big blind can play twice
			if (isThePlayerTheLastRaisePlayer && player.getRole() == Role.BigBlind)
			{
				hasBigBlindToPlayTwice = true;
				isThePlayerTheLastRaisePlayer = false;
			}
			else if(hasBigBlindToPlayTwice)
			{
				isThePlayerTheLastRaisePlayer = true;
			}
		}
	}
}

@@ -2585,32 +2585,64 @@
 players)%0D%0A%09%09%09%7B%0D%0A
+%09%09%09%09if (!p.isFolded())%0D%0A%09%09%09%09%7B%0D%0A%09
 %09%09%09%09//If the pla
@@ -2704,28 +2704,13 @@
 %09%09%09%09
+%09
 if (
-p.isFolded() %7C%7C 
 (p.g
@@ -2775,27 +2775,29 @@
 != 0))%0D%0A%09%09%09%09
+%09
 %7B%0D%0A
+%09
 %09%09%09%09%09allChec
@@ -2806,24 +2806,32 @@
 d = false;%0D%0A
+%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%7D%0D%0A%09%09%09%7D%0D
@@ -3084,16 +3084,17 @@
 d%22%0D%0A%09%09if
+ 
 (ge.getP
@@ -3415,16 +3415,17 @@
 ();%0D%0A%09%09%7D
+ 
 while(pl
@@ -3630,29 +3630,8 @@
 e;%0D%0A
-%09%09int nbDecision = 2;
 %0D%0A%09%09
@@ -3678,26 +3678,8 @@
 %09%7B%0D%0A
-%09%09%09++nbDecision;%0D%0A
 %09%09%09i
@@ -4410,16 +4410,17 @@
 %09else if
+ 
 (hasBigB

Fix bug fold