0
\$\begingroup\$

I am developing a Checkers game and need to implement players' turn, particularly after "Capturing". Changing turns after single steps work properly but not correctly after capturing.

In the drag and drop code below a Checker GameObject is selected and dropped at a position.

In order to successfully complete a move, we verify whether it is that Player's turn. If it is that player's turn the Checker GameObject's transform position is updated with the position of the object called 'targetObject' and other arrays are updated to reflect the changes.

These steps work as expected when making a single step move. When capturing, which requires different rules, things do not work correctly. After every capture, the Checker GameObject should detect whether another move is required. If yes, the game should not change turn. Same player must play again. If no, it is the other player's turn.

Sadly, although in Debug.Log "isCapture" is indicating the player is not required to capture and that it is the opponent's turn, the game still accepts the wrong player's move.

void LateUpdate ()
{
    //select
    if ( Input.GetMouseButtonDown(0) )
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if ( Physics.Raycast(ray, out hit) )
        {
            if ( hit.transform.gameObject.tag == "Checker" )
            {
                int team = hit.transform.gameObject.GetComponent<CheckerSeed>().team;
                if ( team == myTurn )
                {
                    hitObject = hit.transform.gameObject;
                    current = hitObject.GetComponent<CheckerSeed>().GetID();
                }
                else hitObject = null;
            }
        }
    }

    //Hold Object
    if ( Input.GetMouseButton(0) )
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if ( Physics.Raycast(ray, out hit) )
        {
            if ( hit.transform.gameObject.tag == "Position" )
            {
                targetObject = hit.transform.gameObject;
                target = targetObject.GetComponent<PositionInfo>().index;
            }
        }

    }

    //Drop Object
    if ( Input.GetMouseButtonUp(0) )
    {
        bool isCapture = hitObject.GetComponent<CheckerSeed>().MustCapture(current);
        SwitchTurn(hitObject, targetObject, isCapture);
        Debug.Log("UP: Play again is " + isCapture.ToString() + " It's Team " + myTurn.ToString());

        if ( hitObject != null && targetObject != null )
        {
            {
                int difference = target - current;
                int a = hitObject.transform.gameObject.GetComponent<CheckerSeed>().GetID();
                int b = targetObject.GetComponent<PositionInfo>().index;
                team = hitObject.GetComponent<CheckerSeed>().team;

                //single step
                if ( ( difference == 5 ) || ( difference == 6 ) || ( difference == 4 ) || ( difference == -5 ) || ( difference == -6 ) || ( difference == -4 ) )
                {
                    if ( team == myTurn )
                    {
                        if ( Rules.instance.RegularMovePossible(team, a, b) )
                        {
                            Successful();
                        }
                        if ( hitObject.GetComponent<CheckerSeed>().moved )
                        {
                            myTurn = myTurn == 2 ? 1 : 2;
                            hitObject = null;
                            targetObject = null;
                        }
                        Debug.Log("It's Team " + myTurn.ToString() + "'s turn.. Single step.");
                    }
                    return;
                }
                else
                {

                    if ( Rules.instance.CapturingPossible(team, a, b) )
                    {
                        //Capturing step... only take multiple steps if actually capturing an opponent
                        if ( hitObject.GetComponent<CheckerSeed>().Capturing(b) )
                        {
                            Capture(team, a, b);
                            Successful();
                        }
                        else Debug.Log("Jumping is not allowed except when capturing");
                    }
                    //              }
                    return;
                }
            }
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Youv'e told us what your trying to do. What is your actual question? \$\endgroup\$ – Gnemlock Dec 1 '16 at 1:09
  • \$\begingroup\$ I need to change player's turn after capturing. Or should I say How do I change player's turn after capturing? \$\endgroup\$ – Aaron99 Dec 1 '16 at 13:34
0
\$\begingroup\$

So after some debugging, I was able to trace the issue and resolve the problem. I my code to determine whether a piece could capture another piece, I was searching from the current Checker position, which became invalid by the time the Checker changed position. I need to "Look ahead", search from the future position to determine whether a capture was possible from the target position and return a boolean. After some tweaking the the affected portion of my LateUpdate method looks like this: else {

                    if ( Rules.instance.CapturingPossible(team, a, b) )
                    {
                        bool PlayerTurn = true;
                        //Capturing step... only take multiple steps if actually capturing an opponent
                        if ( hitObject.GetComponent<CheckerSeed>().Capturing(b) )
                        {
                            Capture(team, a, b);
                            PlayerTurn = Successful(playAgain);
                            if(!PlayerTurn)
                            {
                                myTurn = myTurn == 2 ? 1 : 2;
                                hitObject = null;
                                targetObject = null;
                            }
                        }
                    }
                    return;
                }
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ If this was your solution, could you accept the answer to clarify for other users? \$\endgroup\$ – Gnemlock Jan 3 '17 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.