Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Game] AHK Mastermind


  • Please log in to reply
9 replies to this topic
derRaphael
  • Members
  • 872 posts
  • Last active: Mar 19 2013 04:42 PM
  • Joined: 23 Nov 2007
You all know the old school Mastermind game where your opponent picks four colours out of six possible and you have twelve attempts to guess those?
Well, here it is:
AHK MasterMind

The Game itself is actually described in those few words from lemma above. It uses CheckBoxes to indicate Right Color (unchecked and enabled), Right Position & Color (enabled and checked).
It is a very simple Game .. nothing fancy. Simply Enjoy!
Posted Image
Download

; AHK Mastermind
; (c) 2008 derRaphael / Zlib License Style Released

  #NoTrayIcon
  SetTitleMatchMode, 2

  BLOCK  := Chr(0xDB) Chr(0xDB), CNTbl  := "RYOGBN"
  CTable := "ff0000|ffff00|ff8000|00ff00|0000ff|804000"

  Menu, Game, Add, New Game`tCTRL+N, ReStart
  Menu, Game, Add, Exit Game`tALT+F4, GuiClose
  Menu, GameMenu, Add, &Game, :Game

  GoSub, initGame
  GoSub, initRound
return

initGame:
  Round  := 12
  Guess  := "----"
  Loop, 12
  {
    L := A_Index
    Loop, 4
      Gui, Add, CheckBox, % "hwndR" L "_" A_Index " vlR" L "_" A_index 
                          . " Disabled x"  ((A_Index>1) ? "p+15" : "10")
                          . " h10 w10"
    Gui, Font, s36 cd0d0d0, Terminal
    Loop, 4
      Gui, Add, Text, % "+Border hwndG" L "_" a_index " vlG" L "_" A_index 
                      . " y" ((A_Index>1)? "p":"p-5") " xp+21", % BLOCK
  }
  Gui, Font,s13, Lucida Console
  Loop, 4
    Gui, Add, Button, % "gBC w35 h35 +Center x"
                      . ((A_Index>1)? "p+38":10) ((A_Index>1)? "yp":"")
                      , &%A_Index%

  Gui, Font
  Gui, Add, Button, h20 x10 +Center w150 gCheck, &Check my guess
  Gui, Menu, GameMenu
  Gui, Show,, MasterMind
return

initRound:
  youWontKnow := ""
  Random,,WinActive("A")
  Loop, 4
  {
     Random, rndm, 1, StrLen(CNTbl)
     youWontKnow .= s(CNTbl,rndm,1)
  }
;  SetTimer, TT, 100
return

TT:
   ToolTip, % youWontKnow
return

Check:
   If InStr(Guess,"-")
      MsgBox,64,Hint,You are missing one or more colors!
   else 
   {
     CCP := CCC := 0, YWK := youWontKnow
     Loop, Parse, Guess
       If InStr(YWK,A_LoopField) 
       {
           StringReplace, YWK, YWK, A_LoopField
           CCC += 1
       }
     Loop, 4
       If (s(youWontKnow,A_Index,1)=s(Guess,A_Index,1))
           CCP += 1
     Loop, %CCC%
     {
       id := "R" Round "_" A_Index,  id := %id%
       Control,Enable,,,ahk_id %id%
     }
     Loop, %CCP%
     {
       id := "R" Round "_" A_Index,  id := %id%
       Control,Check,,,ahk_id %id%
;       GuiControl,, %id%, 1

     }
     Guess := "----", Round -= 1
   }

   if !(Round) AND (CCP!=4)
   {
      MsgBox,64,Oh nooooo!,You've lost The Game! Try harder next time!
      Gosub, ReStart
   }
   if (CCP=4)
   {
      MsgBox,64,Congratulations!,You've won the Game!
      Gosub, ReStart
   }
return

#IfWinActive, MasterMind
^n::
ReStart:
  Gui, 1:Destroy
  GoSub, InitGame
  GoSub, InitRound
return

BC:
  StringRight, ButtonNumber, A_GuiControl, 1
  Gui, Font, s36 ce0e0e0, Terminal
  GuiControl, Font, % "lG" Round "_" ButtonNumber

  SetFormat, Integer, hex
  Gui, 1:+Disabled
  Gui, 2:+ToolWindow +Owner +AlwaysOnTop

  Loop, Parse, CTable, |
  {
    a := A_Index, x := (a>1) ? "p+15" : 1
    t := s(CNTbl,a,1), alf := A_LoopField
    Hotkey, %t%, CC, ON
    Gui, 2:Font
    Gui, 2:Font, s36 c%alf%, Terminal
    Gui, 2:Add, Text, vB%t% gCC +Border x%x% y1, % BLOCK
    x := (a>1) ? "p+6" : 7
    N := ("0x" s(alf,1,2)), N := N//2, c := (s(N,3)="0") ? "00" : s(N,3)
    N := ("0x" s(alf,3,2)), N := N//2, c .= (s(N,3)="0") ? "00" : s(N,3)
    N := ("0x" s(alf,5,2)), N := N//2, c .= (s(N,3)="0") ? "00" : s(N,3)
    Gui, 2:Font, s10 bold c%c%, Lucida Console
    Gui, 2:Add, Text, gCC x%x% y3 +BackgroundTrans vT%t%, &%t%
  } 

  Gui, 2:Show, h21 w127, Pick the Color!
return

CC:
  if A_ThisHotkey
     Pos := InStr(CNTbl,A_ThisHotkey)
  if A_GuiControl
     Pos := InStr(CNTbl,s(A_GuiControl,2,1))
  Counter := 0

  Loop, Parse, CTable, |
    If Counter = %Pos%
      break
    else
      Color := A_LoopField, Counter+=1

  Gosub, 2GuiEscape

  Gui, 1:Default
  Gui, Font, s36 c%Color%, Terminal
  GuiControl, Font, % "lG" Round "_" ButtonNumber

  Guess := s(Guess,1,ButtonNumber-1) s(CNTbl,Pos,1) s(Guess,ButtonNumber+1)
return

2GuiClose:
2GuiEscape:
  Gui, Font, s36 cd0d0d0, Terminal
  GuiControl, Font, % "lG" Round "_" ButtonNumber

  Loop, Parse, CNTbl
     Hotkey, %A_LoopField%, CC, OFF

  Gui, 1:-Disabled
  Gui, 2:Destroy
return

GuiClose:
  ExitApp

s(a,b,c=1024)
{
 return, % SubStr(a,b,c)
}


greets
derRaphael

All scripts, unless otherwise noted, are hereby released under CC-BY

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007
It's not working for me. :? It comes out skewed and doesn't seem to respond properly.
Posted Image

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006
What took you so long? :p

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
DerRaphael,

Sweet! I've played several games and it works fine for me. I have several suggestions, if you're interested.

1. It would be great if the game window was bigger; it comes out pretty small in higher resolutions.

2. I would like to have the color picker permanently displayed at the top (in the menu bar if possible) and be able to drag and drop the colors onto the board. This way I could play games a lot faster. All the clicking slows me down. :p

3. If a player makes a guess and none of the choices are correct, nothing happens on the board, which is fine; however, this was a little confusing at first, because I didn't know if the game was working or not. Perhaps all the check boxes could start the game enabled, and if a guess is made with 0 correct choices, then the boxes become disabled. A gray check mark could then be used for guessing a correct color in the wrong location.

4. And finally, there's one nuance of the game I'm not sure about: if I guess all four of one color (let's say Red) and there is 1 Red peg in the code, the game replies with 1 check mark and all 3 other boxes enabled, meaning wrong position. Is this correct? Should the 1 Red peg in the code return 4 replies to my guess? The Wikipedia article doesn't explain this point, and I haven't played the board game in too many years to remember. :D

Thanks for the code, DerRaphael, it's a treat!

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
Ok, I got impatient so I did it myself, :lol: using DerRaphael's code for inspiration. My version is a bit simpler, but it allows faster game play. :D As you can see in the Pic, I also shortened the game from 12 turns to 8 (I think 12 is too easy), but there is a setting in the code to choose any number of rounds. ENJOY!

>Download<

EDIT: I made a simple 8-bit icon for the game, for those who compile.
Posted Image >Download Icon<

Posted Image

Thanks to SoggyDog for the original suggestion.

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007
Nice, I like that interface better, though it would be a little clearer if the color tray were at the bottom. (I thought at first that my guesses went in the four black boxes at the top.)

But there's still something odd about the the feedback it gives. Here's how it works in most mastermind games:
If the answer is [color=red]RR[/color]B[color=green]G:[/color]

[color=red]RRRR[/color] = XX
[color=red]RR[/color][color=orange]OO[/color] = XX
[color=orange]O[/color][color=red]RR[/color][color=orange]O[/color] = X/
[color=orange]OO[/color][color=red]RR[/color] = //
[color=orange]OOO[/color][color=red]R[/color] = /
B[color=red]RR[/color]B = X//
BB[color=orange]OO[/color] = /
Where X mean Right Color, Right Place, and / means Right Color, Wrong Place.

Or in words, each position in the guess can only be counted once, and each position in the answer can only be counted once. And obviously, a Right Place count takes priority over a Wrong Place count. And it's symmetrical, by which that you could switch the answer and guess and get the same result.

Lirave
  • Members
  • 14 posts
  • Last active:
  • Joined: 23 Dec 2007
i think it must be this way

If the answer is [color=red]RR[/color]B[color=green]G:[/color]

[color=red]RRRR[/color] = XX//
[color=red]RR[/color][color=orange]OO[/color] = XX
[color=orange]O[/color][color=red]RR[/color][color=orange]O[/color] = X/
[color=orange]OO[/color][color=red]RR[/color] = //
[color=orange]OOO[/color][color=red]R[/color] = /
B[color=red]RR[/color]B = XX//
BB[color=orange]OO[/color] = //
Where X mean Right Color, Right Place, and / means Right Color, Wrong Place.



jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006

it would be a little clearer if the color tray were at the bottom.

Instead of moving the color picker, I put question marks over the 4 code boxes at the top (the original pic is updated to show the change). I hope this helps.

there's still something odd about the the feedback it gives.

I think you're right, this is what I was getting at in #4 of my first post. The way you explain it is the way I remember playing as a kid. EDIT: the game's feedback has been updated.

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
The download link above has been updated with a new MasterMind version.

Most importantly, the feedback has been fixed... again. :roll: The previous version returned incorrect responses to certain input when the code happened to contain three of the same color. Secondly, a new feature has been added: the option to choose the code length. I had intended to add some kind of control anchoring as well, since this feature shrinks or enlarges the GUI; but it's been sitting on my desktop for months now, so I've got to admit it's just not getting done. If you're looking for a challenge (i.e. a longer code to crack) you'll have to settle for an asymmetrical GUI. :p

Since this is the fourth time I've updated this script, a version log is becoming necessary to keep it all straight. :lol:

v1.4 Jun. 15, 2009 – updated feedback + added code length option
v1.3 Feb. 10, 2008 – updated feedback
v1.2 Feb. 03, 2008 – updated feedback
v1.1 Jan. 30, 2008 – added question marks to the code boxes
v1.0 Jan. 30, 2008 – original release

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
http://en.wikipedia....nd_(board_game)

Oh man, I used to have this original piece. :(

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.