public void simulate_move(Node node_from, Node node_target, bool quick_move = false) { // First select action move piece = node_from.Piece; Piece tPiece = node_target.Piece; piece.set_speed(200f); if (Click(node_from) && piece && Has(piece) && Click(piece)) { piece.Pickup(); piece.Compute(); piece.HighlightPossibleMoves(); piece.HighlightPossibleEats(); GameManager.Instance.GameState.Grab(); } // Move action process piece.set_speed(200f); if (tPiece == null) { if (piece.IsPossibleMove(node_target)) { if (Rules.IsCheckMove(this, piece, node_target, true)) { Debug.Log("Move checked"); // do nothing } else { piece.MoveToXZ(node_target, Drop); GameManager.Instance.GameState.Place(); } } } else { if (piece.IsPossibleEat(node_target)) { if (Rules.IsCheckEat(this, piece, node_target, true)) { Debug.Log("Eat checked"); // do nothing } else { GCPlayer oppPlayer = GameManager.Instance.Opponent(this); oppPlayer.RemovePiece(tPiece); AddEatenPieces(tPiece); // tPiece.ScaleOut(0.2f, 1.5f); piece.MoveToXZ(node_target, Drop); GameManager.Instance.GameState.Place(); } } } piece.set_speed(5f); }
public void OnInputEvent(InputActionType action) { switch (action) { case InputActionType.GRAB_PIECE: if (GameManager.Instance.temporal_move != null) { break; } Node gNode = Finder.RayHitFromScreen <Node>(Input.mousePosition); if (gNode == null) { break; } piece = gNode.Piece; if (piece == null) { break; } if (!piece.IsReady) { break; } if (Click(gNode) && piece && Has(piece) && Click(piece)) { piece.Pickup(); piece.Compute(); piece.HighlightPossibleMoves(); piece.HighlightPossibleEats(); GameManager.Instance.temporal_source_position = piece.ChessCoords; GameManager.Instance.GameState.Grab(); } //check clickable for tile and piece then pass Player //check if player has piece - PIECE //check if player has piece if not empty - NODE break; case InputActionType.CANCEL_PIECE: if (piece != null) { //if (!piece.IsReady) break; piece.Drop(); piece = null; GameManager.Instance.GameState.Cancel(); } break; case InputActionType.PLACE_PIECE: // Event afer select a target position Node tNode = Finder.RayHitFromScreen <Node>(Input.mousePosition); if (tNode == null) { break; } Piece tPiece = tNode.Piece; GameManager.Instance.temporal_target_position = tNode.ChessCoords; if (tPiece == null) { if (piece.IsPossibleMove(tNode)) { if (Rules.IsCheckMove(this, piece, tNode, true)) { Debug.Log("Move checked"); // do nothing } else { piece.MoveToXZ(tNode, Drop); GameManager.Instance.GameState.Place(); } } } else { if (piece.IsPossibleEat(tNode)) { if (Rules.IsCheckEat(this, piece, tNode, true)) { Debug.Log("Eat checked"); // do nothing } else { GCPlayer oppPlayer = GameManager.Instance.Opponent(this); oppPlayer.RemovePiece(tPiece); AddEatenPieces(tPiece); tPiece.ScaleOut(0.2f, 1.5f); piece.MoveToXZ(tNode, Drop); GameManager.Instance.GameState.Place(); } } } if (GameManager.Instance.temporal_move == null) { GameManager.Instance.temporal_move = Tuple.Create <string, string>( GameManager.Instance.temporal_source_position, GameManager.Instance.temporal_target_position); } break; } }
public void UpdateAI() { if (grid == null) { grid = GameObject.FindObjectOfType <Grid>(); } if (myTurn && !didTurn) { didTurn = true; State boardState = GetBoardState(); Debug.Log("State visited counter: " + boardState.TimesVisited); if (brain == null) { brain = new StateAgent(boardState); } else { brain.SetState(boardState); } //get action from brain, execute. StateAction action = brain.GetChosenActionForCurrentState(); string[] moves = Regex.Split(action.ActionString, "to"); string[] from = Regex.Split(moves[0], "-"); string[] to = Regex.Split(moves[1], "-"); Debug.Log(action.ActionString + ", Quality: " + action.GetDeepEvaluation() + " (" + action.ActionEvaluation + ") --- " + brain.LearnedStates + "///" + brain.EvaluatedActions); if (action.GetDeepEvaluation() != action.ActionEvaluation) { Debug.Log("///////////////////////////////////////////////////////////////"); } foreach (Node n in grid.grid) { n.UnhighlightEat(); n.UnhighlightMove(); } Node fromNode = grid.GetNodeAt(int.Parse(from[1]), int.Parse(from[0])); Node toNode = grid.GetNodeAt(int.Parse(to[1]), int.Parse(to[0])); fromNode.HighlightMove(); toNode.HighlightEat(); piece = fromNode.Piece; piece.Pickup(); GameManager.Instance.GameState.Grab(); int reward = 0; Piece tPiece = toNode.Piece; if (tPiece == null) { if (piece.IsPossibleMove(toNode)) { if (Rules.IsCheckMove(this, piece, toNode, true)) { Debug.Log("Move checked, not allowed"); // do nothing brain.EvaluateLastAction(-10000); GameManager.Instance.GameState.Checkmate(); GameManager.Instance.GameOver(GameManager.Instance.PlayerOponent, GameOverType.CHECKMATE); } else { piece.MoveToXZ(toNode, Drop); GameManager.Instance.GameState.Place(); } } } else { if (piece.IsPossibleEat(toNode)) { if (Rules.IsCheckEat(this, piece, toNode, true)) { Debug.Log("Eat checked"); // do nothing brain.EvaluateLastAction(-10000); GameManager.Instance.GameState.Checkmate(); GameManager.Instance.GameOver(GameManager.Instance.PlayerOponent, GameOverType.CHECKMATE); } else { GCPlayer oppPlayer = GameManager.Instance.Opponent(this); oppPlayer.brain.EvaluateLastAction(-tPiece.GetPieceValue()); reward = tPiece.GetPieceValue(); oppPlayer.RemovePiece(tPiece); AddEatenPieces(tPiece); tPiece.ScaleOut(0.2f, 1.5f); piece.MoveToXZ(toNode, Drop); GameManager.Instance.GameState.Place(); } } } State newState = GetBoardState(); brain.PerformStateAction(action, newState); brain.EvaluateLastAction(reward); } }