AutoHotkey Community

It is currently May 26th, 2012, 4:57 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Mastermind Solver
PostPosted: January 10th, 2009, 10:07 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
This solves Mastermind puzzles in 4 to 5 attempts on average. It uses a simple but surprisingly powerful algorithm. It searches for all possible combinations that fit what has already occurred and then picks one of these at random.
The standard game uses 4 pegs, of 6 possible colors (red,yellow orange,green blue brown) and allows for any combination.

Code:
start:
posscombinations=
loop 5556
{
iteration:=a_index+1110  ;hence loop 1111 to 6666
bust=
loop 4
   if (substr(iteration, a_index,1) <1) or (substr(iteration, a_index,1) >6)         
      bust=1
if bust
   continue
possible=1
loop, %pastattempts%
   {
   possible=1
   correct1=0
   correct2=0
   guess:=substr(results,a_index*6-5,4)
   iteration1:=iteration
   loop 4
      {
      guesscolor:=substr(guess,a_index,1)
      if (substr(iteration1,a_index,1)=guesscolor)
         {
         correct1+=1 ;correct1 is correct color and positon
         stringreplace,guess,guess,%guesscolor%,a   ;stops double counting
         stringreplace,iteration1,iteration1,%guesscolor%,b
         }
      }
   loop 4
      {
      guesscolor:=substr(guess,a_index,1)
      ifinstring, iteration1,%guesscolor%
         {
         correct2+=1  ;correct2 is corrrect color but incorrect position
         stringreplace,guess,guess,%guesscolor%,c
         stringreplace,iteration1,iteration1,%guesscolor%,d
         }
      }

   if (correct1<>substr(results,a_index*6-1,1)) or (correct2<>substr(results,a_index*6,1))
      possible=0
   if not possible
      break
   }
if possible
   posscombinations.=iteration
}
pastattempts+=1
numberposss:=floor(strlen(posscombinations)/4)
random,randomchoice,1,%numberposss%
nextguess:=substr(posscombinations, randomchoice*4-3,4)
nextguesscols:=nextguess
nextguesscols:=regexreplace(nextguesscols,"1"," red ")
nextguesscols:=regexreplace(nextguesscols,"2"," yellow ")
nextguesscols:=regexreplace(nextguesscols,"3"," orange ")
nextguesscols:=regexreplace(nextguesscols,"4"," green ")
nextguesscols:=regexreplace(nextguesscols,"5"," blue ")
nextguesscols:=regexreplace(nextguesscols,"6"," brown ")
if numberposss=1
   goto end
if numberposss=0
   {
   msgbox,48,Error,An error has occurred. There are no remaining possible answers.
   exitapp
   }
else
   msgbox,1,,   Attempt %pastattempts%     %nextguesscols%      %numberposss% possibilities remain
ifmsgbox cancel
   exitapp   
inputbox,correct1,Correct Position,How many are in the correct position?,,200,150
inputbox,correct2,Correct Color,How many are the right color but not in the correct position?,,200,150   
results.= nextguess . correct1 . correct2
goto start
end:
msgbox,,Game Over,Attempt %pastattempts% `nThe only remaining possibility is `n%nextguesscols%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2009, 3:52 pm 
Offline

Joined: February 24th, 2008, 1:04 pm
Posts: 40
cool!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 11th, 2009, 11:52 pm 
Great script!
Is there a way to modify it to use 5 pegs and 8 colors ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 4:59 am 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
modified it to change number of pegs and number of colors
Code:
pegnumber:=5      ;user defined variables
colornumber:=8


if colornumber>9
   {
   msgbox,,,The maximum number of colors allowed is 9
   exitapp
   }

loop % pegnumber-1
   {
   loopnumber.=colornumber-1
   startnumber.=1
   }
loopnumber.=colornumber
startnumber.=0


start:
posscombinations=
loop %loopnumber%
{
iteration:=a_index+startnumber
bust=
loop %pegnumber%
   if (substr(iteration, a_index,1) <1) or (substr(iteration, a_index,1) >colornumber)         
      bust=1
if bust
   continue
possible=1
loop %pastattempts%
   {
   possible=1
   correct1=0
   correct2=0
   guess:=substr(results,a_index*(pegnumber+2)-(pegnumber+1),pegnumber)
   iteration1:=iteration
   loop %pegnumber%
      {
      guesscolor:=substr(guess,a_index,1)
      if (substr(iteration1,a_index,1)=guesscolor)
         {
         correct1+=1 ;correct1 is correct color and positon
         stringreplace,guess,guess,%guesscolor%,a   ;stops double counting
         stringreplace,iteration1,iteration1,%guesscolor%,b
         }
      }
   loop %pegnumber%
      {
      guesscolor:=substr(guess,a_index,1)
      ifinstring, iteration1,%guesscolor%
         {
         correct2+=1  ;correct2 is corrrect color but incorrect position
         stringreplace,guess,guess,%guesscolor%,c
         stringreplace,iteration1,iteration1,%guesscolor%,d
         }
      }

   if (correct1<>substr(results,a_index*(pegnumber+2)-1,1)) or (correct2<>substr(results,(pegnumber+2),1))
      possible=0
   if not possible
      break
   }
if possible
   posscombinations.=iteration
}
pastattempts+=1
numberposss:=floor(strlen(posscombinations)/pegnumber)
random,randomchoice,1,%numberposss%
nextguess:=substr(posscombinations, randomchoice*pegnumber-(pegnumber-1),pegnumber)
nextguesscols:=nextguess
nextguesscols:=regexreplace(nextguesscols,"1"," red ")
nextguesscols:=regexreplace(nextguesscols,"2"," yellow ")
nextguesscols:=regexreplace(nextguesscols,"3"," orange ")
nextguesscols:=regexreplace(nextguesscols,"4"," green ")
nextguesscols:=regexreplace(nextguesscols,"5"," blue ")
nextguesscols:=regexreplace(nextguesscols,"6"," brown ")
nextguesscols:=regexreplace(nextguesscols,"7"," violet ")
nextguesscols:=regexreplace(nextguesscols,"8"," cyan ")
nextguesscols:=regexreplace(nextguesscols,"9"," black ")
if numberposss=1
   goto end
if numberposss=0
   {
   msgbox,48,Error,An error has occurred. There are no remaining possible answers.
   exitapp
   }
else
   msgbox,1,,   Attempt %pastattempts%     %nextguesscols%      %numberposss% possibilities remain
ifmsgbox cancel
   exitapp   
inputbox,correct1,Correct Position,How many are in the correct position?,,200,150
inputbox,correct2,Correct Color,How many are the right color but not in the correct position?,,200,150   
results.= nextguess . correct1 . correct2
goto start
end:
msgbox,,Game Over,Attempt %pastattempts% `nThe only remaining possibility is `n%nextguesscols%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 19th, 2009, 6:09 am 
Thank You so much!
I really like the modified version :D


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group