Time for action – checking for victory
Our matchesMade
, matchesNeededToWin
, and playerHasWon
variables have been standing at the ready this whole time. Let's finally make use of them.
Add these few lines to the
FlipCardFaceUp
function, where you're detecting a match:if(aCardsFlipped[0].id == aCardsFlipped[1].id) { // Match! aCardsFlipped[0].isMatched = true; aCardsFlipped[1].isMatched = true; matchesMade ++; if(matchesMade >= matchesNeededToWin) { playerHasWon = true; }
Add a new function call to the
OnGUI
function:function OnGUI () { GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height)); BuildGrid(); if(playerHasWon) BuildWinPrompt(); GUILayout.EndArea(); }
And, now, we'll use some
GUILayout
commands that we learned in the last chapter to display a "win" prompt to the player. Write this new function apart from the other functions, and make sure it's not wedged inside any other function's curlies:function...