Some time ago, I created a small web application for a card game for fun. The player plays against the computer, and basically it works great. Sometimes, although a computer player gets into a cycle, the point of the game is to lose all your cards, and if you do not have a card to play, you take a bunch. Sometimes the computer plays x, y, z, takes a bunch, plays x, yz, takes a bunch, etc.
I keep track of the steps I took, so anytime I have an array that looks something like this: [C2, D5, H2, S4, C5, H2, S4, C5, H2, S4, C5 ]
In this case, I see that I got into the cycle of the game H2, S4, C5, then took a bunch, and then repeated.
So, the generalized problem is, what is the best way to detect duplicate patterns in a list? Perhaps I could whip something using a simple loop, trying to find the card I'm going to play, and if I find this at position x, then I could check if the pattern from x to n is repeated in position x- ( nx) to x, but this seems like a problem that might have a good algorithm for it. How would you encode this with the following function signature:
function findLoops(previousMoves, nextMove, maxPatternLength) {
ps this is not homework, the game exists and is located at http://www.idiot-cardgame.com , if anyone is interested :)