void E_scene_main_game::click_on_chessboard(int mouseX, int mouseY)
{
	// if it's not the player's turn, don't manage click on chessboard
	if ((game_mode == PLAYER_VS_AI) && (current_color != player_color))
		return;
	
	// promotion is a special state in which the player must answer
	// to the question of what is the piece he wants for the promotion
	if (promotion)
		return;
	
	// if the game is over, there is no reason to play anymore :)
	if (game_is_over)
		return;
	
		
	int sqX, sqY;
	
	
	if (pGlobalInfos->OnLeftMouseButtonDown())
	{	
		if (chessboard->clickOnChessBoard(mouseX,mouseY,&sqX,&sqY));
		{
			// if a piece was already selected, this means the player want to move this piece
			if (SquareXSelected>-1 && SquareZSelected>-1)
			{
				FutureXSquare=sqX;
				FutureZSquare=sqY;
				
				// check if the player move is legal (ie: move is in possible_moves array)
				if (move_is_legal())
				{
					// check if the move will be a promotion
					check_for_promotion();
					
					// if so, wait for the player to answer to which piece 
					// he wants for the promotion
					if (promotion)
						return;
					
					player_do_a_move(0);
								
					
					if (game_is_over)
						return;
											
					// ------- MODE PLAYER_VS_PLAYER -------
					if (game_mode == PLAYER_VS_PLAYER)
						chessboard->changeSideToPlay(current_color);
					

					// ------- MODE PLAYER_VS_AI -------
					if (game_mode == PLAYER_VS_AI)
					{		
						thread_data[0]=&AI_comp_is_done;
						thread_data[1]=move_to_do;
						
						FAILE_AI_thread=SDL_CreateThread(AIComputeMove,thread_data);
					}
					return;
				}
			}
			// if no piece were previously selected	
			SquareXSelected=-1;
			SquareZSelected=-1;
			
			int pos=24 + (sqY+1)*12 -(sqX+3);
			if (pos>=0 && pos<145)
			{
				bool color_selected=board_to_display[pos]%2;
				
				if (current_color!=WHITE)
					color_selected=!color_selected;
							
				if (color_selected && board_to_display[pos]!=npiece && board_to_display[pos]!=frame)
				{
					SquareXSelected=sqX;
					SquareZSelected=sqY;
					UpdatePossibleMoves(SquareXSelected,SquareZSelected);
				}
			}
			
		}
	}
	
	if (pGlobalInfos->display_possible_moves() && SquareXSelected>-1 && SquareZSelected>-1)
		chessboard->displayPossibleMoves(SquareXSelected,SquareZSelected,possible_moves);
}
int CFieldHandler::PlaceStone(int index, CStoneHandler::CStone *pStone, bool update)
{
    if(pStone == 0)
    {
        printf("NULL-POINTER FH-STONE");
        return 0;
    }

    m_aField[index].m_pStone = pStone;
    if(m_Moves < 2)
    {
        m_aRestrictions[m_Moves] = index;
    }
    ++m_Moves;

    int x = index%FIELD_WIDTH;
    int y = (index-x)/FIELD_WIDTH;
    int temp = 0;

        for(int i = x-1; i>=0; i--)
        {
            if(IsFree(i+y*FIELD_WIDTH))
            {
               temp = i +1;
               if(update)
               UpdatePossibleMoves(i+y*FIELD_WIDTH);
               break;
            }
        }

        int num = 0;
        for(int i = temp; i < FIELD_WIDTH; i++)
        {
            if(!IsFree(i+y*FIELD_WIDTH))
            {
                if(i != x)
                {
                      m_aField[i+y*FIELD_WIDTH].m_Flags |= FIELD_COUNTED_X;
                    num++;
                }

            }
            else
            { if(update)
                UpdatePossibleMoves(i+y*FIELD_WIDTH);

                break;
            }
        }

        if(num > 0)
             m_aField[index].m_Flags |= FIELD_COUNTED_X;
        temp = 0;
        num = 0;
        for(int i = y-1; i>=0; i--)
        {
            if(IsFree(x+i*FIELD_WIDTH))
            {
               temp = i +1;
                if(update)
               UpdatePossibleMoves(x+i*FIELD_WIDTH);
               break;
            }
        }

        for(int i = temp; i < FIELD_HEIGHT; i++)
        {
            if(!IsFree(x+i*FIELD_WIDTH))
            {
                if(i != y)
                {
                    m_aField[x+i*FIELD_WIDTH].m_Flags |= FIELD_COUNTED_Y;
                     num++;
                }
            }
            else
            {
 if(update)
                UpdatePossibleMoves(x+i*FIELD_WIDTH);
                break;
            }
        }
        if(num > 0)
             m_aField[index].m_Flags |= FIELD_COUNTED_Y;
 if(update)
            UpdatePossibleMoves();
}