Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3332438
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:41:18+00:00 2026-05-17T23:41:18+00:00

A while ago I created a small cardgame web app for fun. The player

  • 0

A while ago I created a small cardgame web app for fun. The player plays against the computer and mostly it works fine. Sometimes though the computer player gets into a loop, the point of the game is to lose all your cards and if you don’t have a card to play you take the pile. Sometimes the computer plays x,y,z, takes the pile, plays x,yz, takes the pile etc.

I keep track of the moves I’ve made, so at any point I have an array that looks something like : [C2,D5,H2,S4,C5,H2,S4,C5,H2,S4,C5]

In this case I can see that I’ve gotten into a loop of playing H2,S4,C5, then taking the pile and then repeating.

So, the generalized problem is, what’s the best way to detect repeating patterns in a list? I could probably whip something up using a simple for loop, trying to find the card I’m about to play and if I find that in position x then I could check whether the pattern from x to n repeats at position x-(n-x) to x, but this seems like the kind of problem that could have a nice algorithm for it. How would you code this given the following function signature:

function findLoops(previousMoves, nextMove, maxPatternLength) {
    //Return [loopLength, loopCount] or null if there are no loops
}

p.s. this is not a homework assignment, the game exists and is at http://www.idiot-cardgame.com if anyone is interested 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:41:18+00:00Added an answer on May 17, 2026 at 11:41 pm

    First the general question: Your suggested method

    trying to find the card I’m about to play and if I find that in position x then I could check whether the pattern from x to n repeats at position x-(n-x) to x,

    looks really good. I would suggest basically the same. It is O(n) and needs a fixed amount of storage, and is simple: what else would you wish for?

    Second: You can check for repetition in games generally if you keep a hash table of all previous game states (complete state, nothing left out). Everytime you reach a new state look up if it is in the hashtable, if its in it: you game state is looping.

    In Javascript you have builtin hastables so this is very easy to do with something similar like this:

     new_state = next_move(old_state);
     new_encoded_state = encode(new_state);  // make it into a string
     if (allstates[new_encoded_state]) {
           // we are looping!
     } else {
           allstates[new_encoded_state] = 1;
           // no looping
     }
    

    The variable allstates is not an Array but of type Object. You can have array like access with strings and this uses the Object as hastable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A while ago, I started on a project where I designed a html-esque XML
A while ago, I came across some code that marked a data member of
A while ago I read the Mocks Aren't Stubs article by Martin Fowler and
A while ago I had a query that I ran quite a lot for
A while ago, I had a discussion with a colleague about how to insert
A while ago, I set up my company's local (non-internet) client database on my
A while ago, I asked a question about $ , and got useful answers
A while ago another question referred to the (possibly urban tale) statistic that ...
A while ago I was trying to figure out a way of doing this
A good while ago, I read an article by the creator of viemu ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.