//clears a peg in between the previous one and the new one void removePeg(GameObject startPeg, GameObject endPeg) { pegToRemove = getPegInbetween(startPeg, endPeg); //remove peg removeScript = pegToRemove.GetComponent <PegScript>(); removeScript.setHasPeg(false); removeScript.setEmpty(); }
//play game void playGame() { //Check for the selected peg previousSelectedPeg = selectedPeg; selectedPeg = checkSelectedPeg(); //get a list of all the possible valid locations if (selectedPeg != null) { //check if contains a peg selectedPegScript = selectedPeg.GetComponent <PegScript>(); if (selectedPegScript.getHasPeg() == true) { //Debug.Log ("selection is a peg"); if (previousValidMoves != null) { previousValidMoves = validMoves; clearPreviousMoves(previousValidMoves); } validMoves = checkValidMoves(selectedPeg); setValidMoves(validMoves); } //remove peg and set current peg to this one else if (selectedPegScript.getHasPeg() == false) { //Debug.Log ("selection is a hole"); selectedPegScript.setHasPeg(true); selectedPegScript.setIsSelected(true); selectedPegScript.setPegSelected(); removePeg(selectedPeg, previousSelectedPeg); //then show the currently available moves previousValidMoves = validMoves; clearPreviousMoves(previousValidMoves); validMoves = checkValidMoves(selectedPeg); setValidMoves(validMoves); } if (checkForWin()) { gameEndText.enabled = true; gameEndText.text = "You won!"; } if (!checkForMoves()) { gameEndText.enabled = true; gameEndText.text = "You Lost"; } } //check for if the player clicked on one of the valid locations //move the peg and delete the one in between }
//checks all of the game objects to search for isSelected = true //restricts selection to one peg only GameObject checkSelectedPeg() { for (int i = 0; i < board.Count; i++) { for (int j = 0; j < board [i].Length; j++) { currentPeg = board [i] [j]; currentPegScript = board [i] [j].GetComponent <PegScript> (); //checks if the selection is a peg if (currentPegScript.getIsSelected() == true && currentPegScript.getHasPeg() == true) { if (currentPeg.Equals(previousPeg)) { //continues the loop to check for a new peg continue; } else { //set previous peg to not selected if (previousPeg != null) { previousPegScript = previousPeg.GetComponent <PegScript>(); previousPegScript.setIsSelected(false); previousPegScript.setPeg(); } currentPegScript.setPegSelected(); previousPeg = currentPeg; return(currentPeg); } } //checks if the selection is an empty space else if (currentPegScript.getIsSelected() == true && currentPegScript.getHasPeg() == false) { //if its a valid move if (validMoves != null) { if (validMoves.Contains(currentPeg)) { //set the previous peg to empty if (previousPeg != null) { previousPegScript = previousPeg.GetComponent <PegScript> (); previousPegScript.setIsSelected(false); previousPegScript.setHasPeg(false); previousPegScript.setEmpty(); } currentPegScript.setPeg(); previousPeg = currentPeg; return(currentPeg); } //if its not a valid move, remove the isSelected return the seleted peg else { currentPegScript.setIsSelected(false); return(selectedPeg); } } } } } //always return the current peg in case of errant clicks return(selectedPeg); }