public void setResult1(GameCard card) { // store no matter exist already in list result1 = card; myMem.AddToMemory(result1); }
public virtual int MatchedCardPos (GameCard realCard) //Interface FUNCTION 3 { Debug.Log ("Finding a card in memory to match card " + realCard.id + " at position " + realCard.position); int result = -1; int startNode = 0; for (int i=0; i<numOfLines && (result == -1); i++) {result = FindMatchWithInput (store [i], realCard.id, realCard.position, startNode, i);} return result; }
public void AddToMemory (GameCard realCard) //Interface FUNCTION 1 { //1. hashing int key = Hashing(realCard); //2. duplicating MemCard headCard = new MemCard (); headCard.id = realCard.id; headCard.position = realCard.position; headCard.next = store[key]; //3. storing store [key] = headCard; Debug.Log ("Card " + headCard.id + " at position " + headCard.position + " added to " + key + "-th Memory Track "); //4. flush useless memory if(limitedSize) {DeleteLastWhenOutOfRange (ref store[key], 0);} }
//----------------------Below are important Functions doing actual searching------------------------------------ protected bool FindCardsMatchWithinMem (MemCard line, int targetNode, ref bool endLine, int trackNum) { //Function: helper for HaveMatchedPair() //Method: Feed a GameCard from Mem into FindMatchWithInput() //1. find the card for (int i=0; line != null && i < targetNode; i++) { line = line.next; } //2. if it is the end, endline & not found; This part is really important when memory line is not full if (line == null) { Debug.Log ("Pairing search over in track " + trackNum); endLine = true; return false; } //3. if not end, Pass to find match Debug.Log ("Memory track " + trackNum + " -node " + targetNode + ": Try to see if " + line.id + " at position " + line.position + " can match with any other node"); GameCard tempCard = new GameCard(); tempCard.id = line.id; tempCard.position = line.position; int result = MatchedCardPos (tempCard); if (result != -1) { UpdatePair(line, result); //just update the pair, not directly return return true; } return false; }
protected override int Hashing (GameCard targetCard) { return 0; }
public override int MatchedCardPos (GameCard realCard) { Debug.Log ("Finding a card in memory to match card " + realCard.id + " at position " + realCard.position); int result = -1; int startNode = 0; int key = Hashing (realCard); //only search a single line result = FindMatchWithInput (store [key], realCard.id, realCard.position, startNode, key); return result; }
protected override int Hashing (GameCard targetCard) { return ((targetCard.id%100) < (numOfCards/4))? 0 : 1; }
//-----------------------------Below are helpers------------------------------------------- protected virtual int Hashing (GameCard targetCard) { //divide into left & right half int x = targetCard.position % numOfCards; return (x < (numOfCards / 2)) ? 0 : 1; }