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

  • SEARCH
  • Home
  • 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

not so long ago I´ve created a small iphone app for my Daily use.
I created a little app a while ago. I created admin.py and used admin.site.register(MenuEntry)
So I'm updating an app I created a while ago. In the AndroidManifest.xml I
A (long) while ago I wrote a web-spider that I multithreaded to enable concurrent
A while ago I created a drag-over check box list which allows you to
A little while ago I created a class to deal with my LAN networking
I created SSIS packages a while ago, today I need to re-point them to
I have an application that I created a while ago for some unknown portable
I have a couple of functions I created a while ago for reading and
A while ago I worked on a web application where users could buy tickets.

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.