Search Unity

Player Turn Changing Even When Conditions Not Met

Discussion in 'Scripting' started by wraith1821, May 28, 2018.

  1. wraith1821

    wraith1821

    Joined:
    Nov 26, 2017
    Posts:
    30
    I'm pretty sure something is missing here, and I haven't a clue what it is.

    When the conditions of the function aren't met the game piece doesn't appear which is correct. However if same square is clicked a second time the next players piece will appear if their conditions are met. How do I prevent moving to the next player if the conditions aren't met.

    Code (CSharp):
    1. public void PlaySquareButton(int whatSquare)
    2.     {
    3.  
    4.  
    5.         if (playerTurn == 0)
    6.         {
    7.             playerTurn = 1;
    8.             if ((squareIntForComparison[whatSquare] >= lastPlayedSquareValueO) || (squareIntForComparison[whatSquare] == 0))
    9.             {
    10.  
    11.                 bigTurnIcons[0].SetActive(true);
    12.                 bigTurnIcons[1].SetActive(false);
    13.                 lastPlayedSquareValueO = boardSquareValue[whatSquare];
    14.                 markedSquare[whatSquare] = playerTurn + 1;                                    
    15.                 gameBoardSquares[whatSquare].image.sprite = playerGamePieces[playerTurn];      
    16.                 gameBoardSquares[whatSquare].interactable = false;                            
    17.  
    18.             }
    19.             else if (playerTurn == 1)
    20.             {
    21.  
    22.                 playerTurn = 0;
    23.  
    24.                 if ((squareIntForComparison[whatSquare] >= lastPlayedSquareValueX) || (squareIntForComparison[whatSquare] == 0))
    25.                 {
    26.  
    27.                     bigTurnIcons[1].SetActive(true);
    28.                     bigTurnIcons[0].SetActive(false);
    29.                     lastPlayedSquareValueX = boardSquareValue[whatSquare];
    30.                     markedSquare[whatSquare] = playerTurn + 1;                                    
    31.                     gameBoardSquares[whatSquare].image.sprite = playerGamePieces[playerTurn];      
    32.                     gameBoardSquares[whatSquare].interactable = false;                          
    33.  
    34.                 }
    35.             }
    36.         }
    37.  
    38.                 CheckForWinner();                                                                      
    39.     }
    Thanks for any help
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I think you'd want to only change the value of 'playerTurn' if the condition is met? Currently, you are changing it regardless.
     
  3. wraith1821

    wraith1821

    Joined:
    Nov 26, 2017
    Posts:
    30
    Thanks for the reply. Code below reflects changes. Now it doesn't change to next player, which is excellent, but it doesn't let the player create a piece either. after an invalid move is attempted. Looks like its changing isvalidmove to false which is correct, but now I need it to be true to restart the player turn.

    Code (CSharp):
    1. public void PlaySquareButton(int whatSquare)
    2.     {
    3.  
    4.  
    5.         if (playerTurn == 0 && isValidMove)
    6.         {
    7.             playerTurn = 1;
    8.             if ((squareIntForComparison[whatSquare] < lastPlayedSquareValueO))
    9.             {
    10.                 isValidMove = false;
    11.              
    12.  
    13.             }
    14.             else if((squareIntForComparison[whatSquare] == 0) || (squareIntForComparison[whatSquare] >= lastPlayedSquareValueO))
    15.             {
    16.                 isValidMove = true;
    17.                 bigTurnIcons[0].SetActive(true);
    18.                 bigTurnIcons[1].SetActive(false);
    19.                 lastPlayedSquareValueO = boardSquareValue[whatSquare];
    20.                 markedSquare[whatSquare] = playerTurn + 1;                                     //Store Which PLayer Clicked Which Square for win check
    21.                 gameBoardSquares[whatSquare].image.sprite = playerGamePieces[playerTurn];      //Check To Place Correct Symbol (GamePiece)
    22.                 gameBoardSquares[whatSquare].interactable = false;                             //Set Button.Interactable To False So It Cannot Be CLicked Again
    23.             }
    24.         }
    25.         else if (playerTurn == 1 && isValidMove)
    26.         {
    27.             playerTurn = 0;
    28.             if ((squareIntForComparison[whatSquare] < lastPlayedSquareValueX))
    29.             {
    30.                 isValidMove = false;
    31.              
    32.             }
    33.             else if((squareIntForComparison[whatSquare] == 0) || (squareIntForComparison[whatSquare] >= lastPlayedSquareValueX))
    34.             {
    35.                 isValidMove = true;
    36.                 bigTurnIcons[1].SetActive(true);
    37.                 bigTurnIcons[0].SetActive(false);
    38.                 lastPlayedSquareValueX = boardSquareValue[whatSquare];
    39.                 markedSquare[whatSquare] = playerTurn + 1;                                     //Store Which PLayer Clicked Which Square for win check
    40.                 gameBoardSquares[whatSquare].image.sprite = playerGamePieces[playerTurn];      //Check To Place Correct Symbol (GamePiece)
    41.                 gameBoardSquares[whatSquare].interactable = false;                             //Set Button.Interactable To False So It Cannot Be CLicked Again
    42.             }
    43.         }
    44.         CheckForWinner();
    45.     }
     
    Last edited: May 29, 2018