protected float[] prob; //= {1.0f, 0.6f, 0.25f, 0.1f}; public void PrintLine (MemCard line, int lineIndex, int size) //Debug Function { if (line != null) { Debug.Log ("Memory Track " + lineIndex + ": " + line.id + " at position " + line.position); PrintLine (line.next, lineIndex, (size + 1)); } else { Debug.Log ("Track finished with size: " + size); } }
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; }
//Features: //1. single line, finite lineSize //2. Hashing position, when in memory always return //3. can toggle: lineSize //4. Recommend lineSize: 4-6 public FifoMemory (int _linesize, int _numofcards) // Constructor { //1. create storage lineSize = _linesize; numOfCards = _numofcards; store = new MemCard[numOfLines]; for (int i=0; i< numOfLines; i++) { store [i] = null; } limitedSize = true; }
//Features: //1. 2 lines, Infinite lineSize //2. Never delete //3. Hashing ID not position, only need to search one track for match public PerfectMemory (int _numofcards) //Constructor { numOfCards = _numofcards; store = new MemCard[numOfLines]; limitedSize = false; }
//Features: //1. 2 lines, finite lineSize //2. Hashing position, near position easier to mixed up //3. Not alsways return, has grading //4. Parameter flexible //5. can toggle: lineSize, probabilities in each node //6. Recommend probability {1, 0.6, 0.25, 0.1}, lineSize: 4 public PosSensitiveMemory (int _linesize, int _numofcards, float[] probability) // Constructor { //1. create storage lineSize = _linesize; numOfCards = _numofcards; store = new MemCard[numOfLines]; for (int i=0; i< numOfLines; i++) { store [i] = null; } //2. setup probabilties prob = new float[lineSize]; for (int i=0; i<lineSize; i++) { prob [i] = probability[i]; } //3. not perfect limitedSize = true; }
private void UpdatePair(MemCard card1, int anotherLocation) { pair [0].id = card1.id; pair [1].id = pair[0].id; pair [0].position = card1.position; pair [1].position = anotherLocation; }
private void DeleteLastWhenOutOfRange (ref MemCard line, int scanIndex) { //helper for AddToMemory(), keep lineSize <= 4 //when storage full for a sigle line, first in first out if (line != null) { if (scanIndex == lineSize) {line = null; Debug.Log ("Exceed capacity, delete last node of this memory Track, ");} else {DeleteLastWhenOutOfRange (ref line.next, (scanIndex + 1));} } }
protected int FindMatchWithInput (MemCard line, int tid, int tposition, int nodeNum, int trackNum) { //Function: helper for MarcheCardPos() & FindCardsMatchWithinMem() //Method: Recursion //1. check for end search if (line == null) {Debug.Log (trackNum + "-th track result: No Match");return -1;} //2. if not end, find a match else if (line.id == tid && line.position != tposition) { Debug.Log ("Found actual match in " + trackNum + "-th memory track with same content at game location " + line.position); if (ShouldReturn(nodeNum)) {return line.position;} } //3. if not end & not match, go to next return FindMatchWithInput (line.next, tid, tposition, (nodeNum + 1), trackNum); }