/* GenChess, a genesis chess engine
 * Copyright (C) 2022, Justin Madru (justin.jdm64@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.chess.genesis.controller;

import android.content.*;
import com.chess.genesis.api.*;
import com.chess.genesis.engine.*;
import com.chess.genesis.util.*;

public class ComputerPlayer extends LocalPlayer
{
	private Engine engine;

	public ComputerPlayer(int ycol, IGameModel model)
	{
		super(ycol, model);
		engine = Engine.create(model.getBoard());
	}

	@Override
	public String getStmName(boolean overwrite)
	{
		var name = super.getStmName(overwrite);
		if (overwrite && model.getBoard().getStm() == yColor)
			name = name + " (thinking)";
		return name;
	}

	@Override
	public void finalizeMove(Move move, Context context)
	{
		engine.setEndTime(0);
		super.finalizeMove(move, context);
	}

	@Override
	public void takeTurn()
	{
		Util.runThread(() -> { runEngine(); });
	}

	private void runEngine()
	{
		var board = model.getBoard();
		engine.setBoard(board);
		var move = engine.getMove();
		if (engine.getEndTime() == 0) {
			Util.runThread(() -> { runEngine(); });
			return;
		}

		model.currentMove();

		var vMove = board.newMove();
		if (board.validMove(move, vMove))
			model.applyMove(vMove, true);
	}
}

@@ -1500,32 +1500,30 @@
 l.runThread(
-() -%3E %7B 
+this::
 runEngine();
@@ -1515,29 +1515,24 @@
 s::runEngine
-(); %7D
 );%0A%09%7D%0A%0A%09priv
@@ -1700,16 +1700,14 @@
 ead(
-() -%3E %7B 
+this::
 runE
@@ -1715,13 +1715,8 @@
 gine
-(); %7D
 );%0A%09
@@ -1758,39 +1758,8 @@
 );%0A%0A
-%09%09var vMove = board.newMove();%0A
 %09%09if
@@ -1784,16 +1784,28 @@
 move
-, vMove)
+) == Move.VALID_MOVE
 )%0A%09%09
@@ -1825,10 +1825,9 @@
 ove(
-vM
+m
 ove,

use method reference and correct validMove() in ComputerPlayer