Rock... Paper... Scissors... Program it!

Post gaming related scripts
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Rock... Paper... Scissors... Program it!

Lets see who can make the best rock paper scissors script!
This is mine!

Code: Select all

``````inputbox, 1, Rock... Paper... Scissors..., Type in rock paper or scissors!, ,w600 h200
If ErrorLevel
{
msgbox , , Cancelled, You have cancelled!
exitapp
}
else
random, 2, 1, 3
;1=rock 2=paper 3=scissors
If 2 contains 1
{
2 = rock
}
If 2 contains 2
{
2 = paper
}
If 2 contains 3
{
2 = scissors
}
msgbox , , Opponent got..., Your opponent got %2%!
;game
If 1 contains rock
If 2 contains paper
{
msgbox , , Loose, You lost!
}
If 1 contains paper
If 2 contains rock
{
msgbox , , Win, You win!
}
If 1 contains rock
If 2 contains rock
{
msgbox , , Tie!, It was a tie!
}
If 1 contains scissors
If 2 contains rock
{
msgbox , , Lost, You lost!
}
If 1 contains scissors
If 2 contains paper
{
msgbox , , Win, You win!
}
If 1 contains scissors
If 2 contains scissors
{
msgbox , , Tie!, It was a tie!
}
If 1 contains paper
if 2 contains scissors
{
msgbox , , Lost, You lost!
}
If 1 contains paper
If 2 contains paper
{
msgbox , , Tie!, It was a tie!
}
``````
TheDewd
Posts: 1131
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Rock... Paper... Scissors... Program it!

Here's my attempt... Now time to do a "Rock, Paper, Scissors, Lizard, Spock".

Code: Select all

``````#SingleInstance, Force

PlayerWins := 0
ComputerWins := 0
ChoiceArr := {1: "Rock", 2: "Paper", 3: "Scissors"}
WinnerArr := {1: "Player", 2: "Computer", 3: "Tie"}

Gui, +LastFound -Resize +HwndGuiHwnd
Gui, Margin, 10, 10
Gui, Add, Button, xm ym w100 h26 gButtonHandler, % ChoiceArr[1]
Gui, Add, Button, x+m ym w100 h26 gButtonHandler, % ChoiceArr[2]
Gui, Add, Button, x+m ym w100 h26 gButtonHandler, % ChoiceArr[3]
Gui, Add, ListView, xm y+m w320 r6, % "#|" WinnerArr[1] " (0)|" WinnerArr[2] " (0)|Score"
Gui, Show, AutoSize, % "Rock, Paper, Scissors"
return

ButtonHandler:
Choice1 := (A_GuiControl = ChoiceArr[1] ? 1 : A_GuiControl = ChoiceArr[2] ? 2 : 3)
Random, Choice2, 1, 3

If (Choice1 = 1) {
Res := (Choice2 = 1 ? WinnerArr[3] : Choice2 = 2 ? WinnerArr[2] : WinnerArr[1])
}

If (Choice1 = 2) {
Res := (Choice2 = 1 ? WinnerArr[1] : Choice2 = 2 ? WinnerArr[3] : WinnerArr[2])
}

If (Choice1 = 3) {
Res := (Choice2 = 1 ? WinnerArr[2] : Choice2 = 2 ? WinnerArr[1] : WinnerArr[3])
}

PlayerWins := (Res = WinnerArr[1] ? PlayerWins + 1 : PlayerWins)
ComputerWins := (Res = WinnerArr[2] ? ComputerWins + 1 : ComputerWins)

LV_Insert(1,, GameNum, ChoiceArr[Choice1], ChoiceArr[Choice2], (Res <> "Tie" ? Res " Wins!" : Res))
LV_ModifyCol(2,, WinnerArr[1] " (" PlayerWins ")")
LV_ModifyCol(3,, WinnerArr[2] " (" ComputerWins ")")

Loop, % LV_GetCount("Column") {
LV_ModifyCol(A_Index, "AutoHdr")
}

Gui, Show, AutoSize, % "Rock, Paper, Scissors [" PlayerWins ":" ComputerWins "]"
return``````
Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Re: Rock... Paper... Scissors... Program it!

Here's my attempt... Now time to do a "Rock, Paper, Scissors, Lizard, Spock".

Code: Select all

``````#SingleInstance, Force

PlayerWins := 0
ComputerWins := 0
ChoiceArr := {1: "Rock", 2: "Paper", 3: "Scissors"}
WinnerArr := {1: "Player", 2: "Computer", 3: "Tie"}

Gui, +LastFound -Resize +HwndGuiHwnd
Gui, Margin, 10, 10
Gui, Add, Button, xm ym w100 h26 gButtonHandler, % ChoiceArr[1]
Gui, Add, Button, x+m ym w100 h26 gButtonHandler, % ChoiceArr[2]
Gui, Add, Button, x+m ym w100 h26 gButtonHandler, % ChoiceArr[3]
Gui, Add, ListView, xm y+m w320 r6, % "#|" WinnerArr[1] " (0)|" WinnerArr[2] " (0)|Score"
Gui, Show, AutoSize, % "Rock, Paper, Scissors"
return

ButtonHandler:
Choice1 := (A_GuiControl = ChoiceArr[1] ? 1 : A_GuiControl = ChoiceArr[2] ? 2 : 3)
Random, Choice2, 1, 3

If (Choice1 = 1) {
Res := (Choice2 = 1 ? WinnerArr[3] : Choice2 = 2 ? WinnerArr[2] : WinnerArr[1])
}

If (Choice1 = 2) {
Res := (Choice2 = 1 ? WinnerArr[1] : Choice2 = 2 ? WinnerArr[3] : WinnerArr[2])
}

If (Choice1 = 3) {
Res := (Choice2 = 1 ? WinnerArr[2] : Choice2 = 2 ? WinnerArr[1] : WinnerArr[3])
}

PlayerWins := (Res = WinnerArr[1] ? PlayerWins + 1 : PlayerWins)
ComputerWins := (Res = WinnerArr[2] ? ComputerWins + 1 : ComputerWins)

LV_Insert(1,, GameNum, ChoiceArr[Choice1], ChoiceArr[Choice2], (Res <> "Tie" ? Res " Wins!" : Res))
LV_ModifyCol(2,, WinnerArr[1] " (" PlayerWins ")")
LV_ModifyCol(3,, WinnerArr[2] " (" ComputerWins ")")

Loop, % LV_GetCount("Column") {
LV_ModifyCol(A_Index, "AutoHdr")
}

Gui, Show, AutoSize, % "Rock, Paper, Scissors [" PlayerWins ":" ComputerWins "]"
return``````
Nice!
jeeswg
Posts: 5279
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Rock... Paper... Scissors... Program it!

Great. I have now written a game in AutoHotkey. I'm pretty good at guessing what the computer is going to do next.

Code: Select all

``````;paper scissors stone (aka rock paper scissors)
;by jeeswg

;P > R, R > S, S > P
vScoreU := 0, vScoreP := 0
oArray := ["P","R","S"]
oArray2 := {"P":"paper","R":"rock","S":"scissors"}
Loop
{
InputBox, vInput,, rock / paper / scissors
vUser := Format("{:U}", SubStr(vInput, 1, 1))
if !(vUser ~= "[PRS]")
continue
Random, vRand, 1, 3
vPC := oArray[vRand]
if (vUser = vPC)
vPrompt := "draw", vScoreU += 1, vScoreP += 1
else if (vUser vPC ~= "PR|RS|SP")
vPrompt := "you win", vScoreU += 1
else
vPrompt := "you lose", vScoreP += 1
MsgBox, % "you: " oArray2[vUser] "`r`n" "PC: " oArray2[vPC] "`r`n`r`n" vPrompt "`r`n`r`n" "you: " vScoreU "`r`n" "PC: " vScoreP
}
``````
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Re: Rock... Paper... Scissors... Program it!

Great. I have now written a game in AutoHotkey. I'm pretty good at guessing what the computer is going to do next.

Code: Select all

``````;paper scissors stone (aka rock paper scissors)
;by jeeswg

;P > R, R > S, S > P
vScoreU := 0, vScoreP := 0
oArray := ["P","R","S"]
oArray2 := {"P":"paper","R":"rock","S":"scissors"}
Loop
{
InputBox, vInput,, rock / paper / scissors
vUser := Format("{:U}", SubStr(vInput, 1, 1))
if !(vUser ~= "[PRS]")
continue
Random, vRand, 1, 3
vPC := oArray[vRand]
if (vUser = vPC)
vPrompt := "draw", vScoreU += 1, vScoreP += 1
else if (vUser vPC ~= "PR|RS|SP")
vPrompt := "you win", vScoreU += 1
else
vPrompt := "you lose", vScoreP += 1
MsgBox, % "you: " oArray2[vUser] "`r`n" "PC: " oArray2[vPC] "`r`n`r`n" vPrompt "`r`n`r`n" "you: " vScoreU "`r`n" "PC: " vScoreP
}
``````
This is pretty good but it would be nice if that cancel button in the inputbox is functional. I like the scores, something I didn't add to mine. Also, I noticed that the computer always does scissors... technical glitch, or coincidence.
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Re: Rock... Paper... Scissors... Program it!

I programmed mine with the random command as the choosing command for the opponent's turn, very useful command, random...
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Re: Rock... Paper... Scissors... Program it!

At the moment, TheDewd has the best one, and also, making mine took a while thinking of all the possible combinations of 3 words, rock, paper, and scissors.
Xeo786
Posts: 189
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Rock... Paper... Scissors... Program it!

How about mixing AHK GUI ActiveX and HTML

Code: Select all

``````; html took from https://stackoverflow.com/questions/21739506/html-javascript-rock-paper-scissors-game-not-working-properly
game =
(
<HTML>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<STYLE type=text/CSS>
.s{font-size:60px}
.b{font-size:75px;background-color:##FF4000;height:85px}
</STYLE>
<SCRIPT type="text/javascript">
var p=['Rock','Scissors','Paper',['win','&#10004;'],['lose','&#9760;']]

function g(n){ document.getElementById('g').innerHTML=p[n]; var cp=[0,1,2]
cp.sort(function(a,b){return Math.random()-0.5});
r=Math.floor(3*(Math.random()));
document.getElementById('i').innerHTML=p[cp[r]];r=cp[r];
if(r===n){w(1); return}
else if((r===0 && n===1) || (r===1 && n==2) || (r==2 && n===0)){w(2)}
else{w(0)}

}
function w(w){
pf=document.getElementById('gam'); pftr=pf.getElementsByTagName('tr');pftd=pf.getElementsByTagName('td');
for(b=0;b<3;b++){pftd[b].innerHTML='&nbsp;'} w===1?pftd[w].innerHTML='tie':pftd[w].innerHTML='win'
}
</SCRIPT>
<BODY scroll="auto">
<OBJECT ID="VoiceObj" CLASSID="clsid:96749377-3391-11D2-9EE3-00C04F797396" ></OBJECT>
<TABLE BORDER=0 style="height:200px;width:500px;text-align:center;font-size:50px" id="gam">
<TR>
<TD width="33`%">  </TD><TD rowspan=2 id="ww">  </TD><TD width="33`%" >  </TD>
</TR>
<TR>
<TD id="g">  </TD><TD id="i">  </TD>
</TR>
</TABLE>
<TR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Player
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;Computer

</TR>
<TR>
</Table>
<hr style="margin:30px">

<TD><input type="button" class="b" value="Paper" onclick="g(2)"></TD>
<TD><input type="button" class="b" value="Rock" onclick="g(0)"></TD>
<TD><input type="button" class="b" value="Scissors" onclick="g(1)"></TD>

</TR>
</TABLE>

</BODY></HTML>
)

Gui, Add, ActiveX, w800 h400 vdocument, HTMLFile
document.write(game)
Gui, Show
return
``````
"When there is no gravity, there is absolute vacuum" -Game changer theory
SpeedMaster
Posts: 235
Joined: 12 Nov 2016, 16:09

Re: Rock... Paper... Scissors... Program it!

Hello,
I used some function of my snake puzzle game and `CLASS CtlColors` (by Just_me)
Here's my attempt...

Code: Select all

``````; Rock Paper Scissors v1.0
; by Speedmaster
; AHK 1.1+
; (Class CtlColors) by Just_me

putgridtext(10,10,2,2,120,120,,"wingdings","5","5")

putgridtext(10,270,3,1,75,61,,,"10","0") ;s h s

gui, font, s8 italic
gui, add, text,x170 y+4,  by Speedmaster
gui, font, s8 normal

putgridtext(90,95,2,1,40,40,  ,"blue","85" ,"0","-border 0x201", "")

putgridtext(10,270,1,1,247,61,,"black","-1","-1","border 0x201 hidden1", "Play Again")

putgridtext(10,10,2,1,40,40,  ,"black","85","0" ,"-border 0x201", "")

gui, font, wingdings

gui, -dpiscale
gui, color,  white
gui, +Resize

Gui, Show, AutoSize, % "Rock Paper Scissors v1.0"

CtlColors.Attach(1_1_1, "trans", "black")
CtlColors.Attach(1_2_1, "trans", "black")
CtlColors.Attach(1_1_2, "trans", "black")
CtlColors.Attach(1_2_2, "trans", "black")

CtlColors.Attach(2_1_1, "trans", "black")
CtlColors.Attach(2_2_1, "trans", "black")
CtlColors.Attach(2_3_1, "trans", "black")

CtlColors.Attach(3_1_1, "trans", "black")
CtlColors.Attach(3_2_1, "trans", "black")

CtlColors.Attach(4_1_1, "trans", "black")

cellfont("1_1_2", "s60", "wingdings")
cellfont("1_2_2", "s60", "wingdings")

cellfont("1_1_1", "s60", "wingdings")
cellfont("1_2_1", "s60", "wingdings")
drawchar("1_1_1", chr(0x4B))
drawchar("1_2_1", ":")

cellfont("2_1_1", "s40", "wingdings")
cellfont("2_2_1", "s40", "wingdings")
cellfont("2_3_1", "s40", "wingdings")

CtlColors.Change(2_1_1, "099145", "white")
CtlColors.Change(2_2_1, "A569BF", "white")
CtlColors.Change(2_3_1, "E05073", "white")

drawchar("2_1_1", chr(0x43))
drawchar("2_2_1", chr(0x49))
drawchar("2_3_1", chr(0x41))

score_Pl:=0
score_cp:=0
win_pl:=0
win_cp:=0

cpu:=0

player:=1

return

;f2::
;debug:=!debug
;return

clickcell:

;if (debug)
;msgbox, % a_guicontrol

if (end=1)
if a_guicontrol in 4_1_1
{
score_Pl:=score_cp:=0
drawchar("3_1_1", "")
drawchar("3_2_1", "")
CtlColors.Change(3_1_1, "trans", "black")
CtlColors.Change(3_2_1, "trans", "black")

CtlColors.Change(1_1_1, "trans", "black")
CtlColors.Change(1_2_1, "trans", "black")
drawchar("1_1_1", chr(0x4B))

CtlColors.Change(1_1_2, "trans", "black")
CtlColors.Change(1_2_2, "trans", "black")
cellfont("1_1_2", "s60", "wingdings")
cellfont("1_2_2", "s60", "wingdings")
drawchar("1_1_2", "")
drawchar("1_2_2", "")

hidecell("4_1_1")
showcell("2_1_1")
showcell("2_2_1")
showcell("2_3_1")

end:=0
player:=1

enablecell("2_1_1")
enablecell("2_2_1")
enablecell("2_3_1")
}

if a_guicontrol in 1_1_1,1_2_1,1_1_2,1_2_2,3_1_1,3_1_2,3_1_3,1_1_3,1_2_3,4_1_1
return

if (player=1)
{

if (a_guicontrol="2_1_1")
{
drawchar("1_1_2", chr(0x43))
pl:=1
}

if (a_guicontrol="2_2_1")
{
drawchar("1_1_2", chr(0x49))
pl:=2
}

if (a_guicontrol="2_3_1")
{
drawchar("1_1_2", chr(0x41))
pl:=3
}

player:=0
cpu:=1
}

if (cpu=1)
{
cpu:=0

random,rand, 1,3

if (rand=1)
drawchar("1_2_2", chr(0x43))
if (rand=2)
drawchar("1_2_2", chr(0x49))
if (rand=3)
drawchar("1_2_2", chr(0x41))

if ((pl=1) && (rand=2)) || ((pl=2) && (rand=3)) || ((pl=3) && (rand=1))
{
score_cp++
drawchar("3_2_1", score_cp)
drawchar("1_1_1", chr(0x4C))
CtlColors.Change(1_2_2, "00FF00", "black")
CtlColors.Change(1_1_1, "trans", "red")

CtlColors.Change(1_2_1, "trans", "green")
}

else
{
if (pl!=rand)
{
score_pl++
drawchar("3_1_1", score_pl)

CtlColors.Change(1_1_1, "trans", "green")
drawchar("1_1_1", chr(0x4A))

CtlColors.Change(1_2_1, "trans", "red")

CtlColors.Change(1_1_2, "00FF00", "black")

}
}

(score_cp) > (score_pl) ? CtlColors.Change(3_2_1, "trans", "green") CtlColors.Change(3_1_1, "trans", "red") : (score_cp) < (score_pl) ? CtlColors.Change(3_2_1, "trans", "red") CtlColors.Change(3_1_1, "trans", "green") : CtlColors.Change(3_2_1, "trans", "blue") CtlColors.Change(3_1_1, "trans", "blue")

sleep, 1500
drawchar("1_2_2", "")
drawchar("1_1_2", "")
drawchar("1_1_1", chr(0x4B))

CtlColors.Change(1_1_1, "trans", "black")
CtlColors.Change(1_2_1, "trans", "black")
CtlColors.Change(1_1_2, "trans", "black")
CtlColors.Change(1_2_2, "trans", "black")

if (score_pl=5)
{
win_pl++
drawchar("5_1_1", win_pl)

CtlColors.Change(1_2_1, "trans", "red")

cellfont("1_1_2", "s60", "webdings")

CtlColors.Change(1_1_2, "2554BE", "yellow")
drawchar("1_1_2", chr(0x25))  ;trophee

CtlColors.Change(1_1_1, "trans", "green")
drawchar("1_1_1", chr(0x4A))

cellfont("1_2_2", "s60", "wingdings")
CtlColors.Change(1_2_2, "6DBDC6", "red")
drawchar("1_2_2", chr(0xFB))  ;croix

CtlColors.Change(4_1_1, "7080BE", "white")
showcell("4_1_1")
disablecell("2_1_1")
disablecell("2_2_1")
disablecell("2_3_1")
hidecell("2_1_1")
hidecell("2_2_1")
hidecell("2_3_1")
end:=1
return
}

if (score_cp=5)
{
win_cp++
drawchar("5_2_1", win_cp)

CtlColors.Change(1_2_1, "trans", "green")

cellfont("1_2_2", "s60", "webdings")
CtlColors.Change(1_2_2, "2554BE", "yellow")
drawchar("1_2_2", chr(0x25))

cellfont("1_1_2", "s60", "wingdings")
CtlColors.Change(1_1_2, "6DBDC6", "red")
drawchar("1_1_2", chr(0xFB))  ;croix

CtlColors.Change(1_1_1, "trans", "red")
drawchar("1_1_1", chr(0x4c))

CtlColors.Change(4_1_1, "7080BE", "white")
showcell("4_1_1")
hidecell("2_1_1")
hidecell("2_2_1")
hidecell("2_3_1")
end:=1
return
}
player:=1
}
return

drawchar(varname, chartodraw:="@", color:="")
{
global
stringsplit, draw, varname, _
out:=  draw1 . "_" . draw2 . "_" draw3
guicontrol,, %out%, %chartodraw%
colorcell(out, color)
}

ColorCell(cell_to_paint, color:="red")
{
GuiControl, +c%color%  , %cell_to_paint%
GuiControl, hide  , %cell_to_paint%
GuiControl, show  , %cell_to_paint%
}

CellFont(cell, params:="", fontname:="")
{
Gui, Font, %params%, %fontname%
GuiControl, font , %cell%
GuiControl, hide  , %cell%
GuiControl, show  , %cell%
}

HideCell(cell_to_hide)
{
guicontrol, hide1, %cell_to_hide%
}

ShowCell(cell_to_show)
{
guicontrol, hide0, %cell_to_show%
}

disablecell(cell)
{
global
guicontrol, disable, %cell%
guicontrol, hide, %cell%
guicontrol, show, %cell%
}

enablecell(cell)
{
global
guicontrol, enable, %cell%
guicontrol, hide, %cell%
guicontrol, show, %cell%
}

guiclose:
esc::
exitapp
return

PutGridText(GridPosX="10",GridPosY=10, number_of_columns=3, number_of_raws=3, width_of_cell=100, height_of_cell=100,fontsize:=18,fontcolor:="red", XCellSpacing=0, YCellSpacing=0, options="center 0x200", fill:=" ")
{
global

if gridnumber=
GridNumber:=0

GridNumber:=GridNumber+1
MaxCols_Grid%GridNumber%:=number_of_columns
MaxRows_Grid%GridNumber%:=number_of_raws

gui, font, s%fontsize%

gui, add, text,  hidden w0 h0 x%GridPosX% y%GridPosY% section

Row:=0 Col:=0

loop, %number_of_raws% {
Row:= ROw+1

loop, %number_of_columns% {
Col:= Col+1
if Col=1
gui, add, text, section  %options% c%fontcolor% 0x200 BackGroundTrans border hidden0 w%width_of_cell% h%height_of_cell% xs y+%YCellSpacing% hwnd%GridNumber%_%Col%_%Row% v%GridNumber%_%Col%_%Row% gclickcell, %fill%
else
gui, add, text,          %options% c%fontcolor% 0x200 BackGroundTrans border hidden0 w%width_of_cell% h%height_of_cell%  x+%XCellSpacing% ys hwnd%GridNumber%_%Col%_%Row% v%GridNumber%_%Col%_%Row% gclickcell, %fill%
}
Col:=0

}

}

; https://autohotkey.com/boards/viewtopic.php?t=2197
; ======================================================================================================================
; AHK 1.1+
; ======================================================================================================================
; Function:          Auxiliary object to color controls on WM_CTLCOLOR... notifications.
;                    Supported controls are: Checkbox, ComboBox, DropDownList, Edit, ListBox, Radio, Text.
;                    Checkboxes and Radios accept only background colors due to design.
; Namespace:         CtlColors
; Tested with:       1.1.25.02
; Tested on:         Win 10 (x64)
; Change log:        1.0.04.00/2017-10-30/just me  -  added transparent background (BkColor = "Trans").
;                    1.0.03.00/2015-07-06/just me  -  fixed Change() to run properly for ComboBoxes.
;                    1.0.02.00/2014-06-07/just me  -  fixed __New() to run properly with compiled scripts.
;                    1.0.01.00/2014-02-15/just me  -  changed class initialization.
;                    1.0.00.00/2014-02-14/just me  -  initial release.
; ======================================================================================================================
; This software is provided 'as-is', without any express or implied warranty.
; In no event will the authors be held liable for any damages arising from the use of this software.
; ======================================================================================================================
Class CtlColors {
; ===================================================================================================================
; Class variables
; ===================================================================================================================
; Registered Controls
Static Attached := {}
; OnMessage Handlers
Static HandledMessages := {Edit: 0, ListBox: 0, Static: 0}
; Message Handler Function
Static MessageHandler := "CtlColors_OnMessage"
; Windows Messages
Static WM_CTLCOLOR := {Edit: 0x0133, ListBox: 0x134, Static: 0x0138}
; HTML Colors (BGR)
Static HTML := {AQUA: 0xFFFF00, BLACK: 0x000000, BLUE: 0xFF0000, FUCHSIA: 0xFF00FF, GRAY: 0x808080, GREEN: 0x008000
, LIME: 0x00FF00, MAROON: 0x000080, NAVY: 0x800000, OLIVE: 0x008080, PURPLE: 0x800080, RED: 0x0000FF
, SILVER: 0xC0C0C0, TEAL: 0x808000, WHITE: 0xFFFFFF, YELLOW: 0x00FFFF}
; Transparent Brush
Static NullBrush := DllCall("GetStockObject", "Int", 5, "UPtr")
; System Colors
Static SYSCOLORS := {Edit: "", ListBox: "", Static: ""}
; Error message in case of errors
Static ErrorMsg := ""
; Class initialization
Static InitClass := CtlColors.ClassInit()
; ===================================================================================================================
; Constructor / Destructor
; ===================================================================================================================
__New() { ; You must not instantiate this class!
If (This.InitClass == "!DONE!") { ; external call after class initialization
This["!Access_Denied!"] := True
Return False
}
}
; ----------------------------------------------------------------------------------------------------------------
__Delete() {
If This["!Access_Denied!"]
Return
This.Free() ; free GDI resources
}
; ===================================================================================================================
; ClassInit       Internal creation of a new instance to ensure that __Delete() will be called.
; ===================================================================================================================
ClassInit() {
CtlColors := New CtlColors
Return "!DONE!"
}
; ===================================================================================================================
; CheckBkColor    Internal check for parameter BkColor.
; ===================================================================================================================
CheckBkColor(ByRef BkColor, Class) {
This.ErrorMsg := ""
If (BkColor != "") && !This.HTML.HasKey(BkColor) && !RegExMatch(BkColor, "^[[:xdigit:]]{6}\$") {
This.ErrorMsg := "Invalid parameter BkColor: " . BkColor
Return False
}
BkColor := BkColor = "" ? This.SYSCOLORS[Class]
:  "0x" . SubStr(BkColor, 5, 2) . SubStr(BkColor, 3, 2) . SubStr(BkColor, 1, 2)
Return True
}
; ===================================================================================================================
; CheckTxColor    Internal check for parameter TxColor.
; ===================================================================================================================
CheckTxColor(ByRef TxColor) {
This.ErrorMsg := ""
If (TxColor != "") && !This.HTML.HasKey(TxColor) && !RegExMatch(TxColor, "i)^[[:xdigit:]]{6}\$") {
This.ErrorMsg := "Invalid parameter TextColor: " . TxColor
Return False
}
TxColor := TxColor = "" ? ""
:  "0x" . SubStr(TxColor, 5, 2) . SubStr(TxColor, 3, 2) . SubStr(TxColor, 1, 2)
Return True
}
; ===================================================================================================================
; Attach          Registers a control for coloring.
; Parameters:     HWND        - HWND of the GUI control
;                 BkColor     - HTML color name, 6-digit hexadecimal RGB value, or "" for default color
;                 ----------- Optional
;                 TxColor     - HTML color name, 6-digit hexadecimal RGB value, or "" for default color
; Return values:  On success  - True
;                 On failure  - False, CtlColors.ErrorMsg contains additional informations
; ===================================================================================================================
Attach(HWND, BkColor, TxColor := "") {
; Names of supported classes
Static ClassNames := {Button: "", ComboBox: "", Edit: "", ListBox: "", Static: ""}
; Button styles
Static BS_CHECKBOX := 0x2, BS_RADIOBUTTON := 0x8
; Editstyles
; Default class background colors
Static COLOR_3DFACE := 15, COLOR_WINDOW := 5
; Initialize default background colors on first call -------------------------------------------------------------
If (This.SYSCOLORS.Edit = "") {
This.SYSCOLORS.Static := DllCall("User32.dll\GetSysColor", "Int", COLOR_3DFACE, "UInt")
This.SYSCOLORS.Edit := DllCall("User32.dll\GetSysColor", "Int", COLOR_WINDOW, "UInt")
This.SYSCOLORS.ListBox := This.SYSCOLORS.Edit
}
This.ErrorMsg := ""
; Check colors ---------------------------------------------------------------------------------------------------
If (BkColor = "") && (TxColor = "") {
This.ErrorMsg := "Both parameters BkColor and TxColor are empty!"
Return False
}
; Check HWND -----------------------------------------------------------------------------------------------------
If !(CtrlHwnd := HWND + 0) || !DllCall("User32.dll\IsWindow", "UPtr", HWND, "UInt") {
This.ErrorMsg := "Invalid parameter HWND: " . HWND
Return False
}
This.ErrorMsg := "Control " . HWND . " is already registered!"
Return False
}
Hwnds := [CtrlHwnd]
; Check control's class ------------------------------------------------------------------------------------------
Classes := ""
WinGetClass, CtrlClass, ahk_id %CtrlHwnd%
This.ErrorMsg := "Unsupported control class: " . CtrlClass
Return False
ControlGet, CtrlStyle, Style, , , ahk_id %CtrlHwnd%
If (CtrlClass = "Edit")
Classes := ["Edit", "Static"]
Else If (CtrlClass = "Button") {
IF (CtrlStyle & BS_RADIOBUTTON) || (CtrlStyle & BS_CHECKBOX)
Classes := ["Static"]
Else
Return False
}
Else If (CtrlClass = "ComboBox") {
VarSetCapacity(CBBI, 40 + (A_PtrSize * 3), 0)
NumPut(40 + (A_PtrSize * 3), CBBI, 0, "UInt")
DllCall("User32.dll\GetComboBoxInfo", "Ptr", CtrlHwnd, "Ptr", &CBBI)
Hwnds.Insert(NumGet(CBBI, 40 + (A_PtrSize * 2, "UPtr")) + 0)
Hwnds.Insert(Numget(CBBI, 40 + A_PtrSize, "UPtr") + 0)
Classes := ["Edit", "Static", "ListBox"]
}
If !IsObject(Classes)
Classes := [CtrlClass]
; Check background color -----------------------------------------------------------------------------------------
If (BkColor <> "Trans")
If !This.CheckBkColor(BkColor, Classes[1])
Return False
; Check text color -----------------------------------------------------------------------------------------------
If !This.CheckTxColor(TxColor)
Return False
; Activate message handling on the first call for a class --------------------------------------------------------
For I, V In Classes {
If (This.HandledMessages[V] = 0)
OnMessage(This.WM_CTLCOLOR[V], This.MessageHandler)
This.HandledMessages[V] += 1
}
; Store values for HWND ------------------------------------------------------------------------------------------
If (BkColor = "Trans")
Brush := This.NullBrush
Else
Brush := DllCall("Gdi32.dll\CreateSolidBrush", "UInt", BkColor, "UPtr")
For I, V In Hwnds
This.Attached[V] := {Brush: Brush, TxColor: TxColor, BkColor: BkColor, Classes: Classes, Hwnds: Hwnds}
; Redraw control -------------------------------------------------------------------------------------------------
DllCall("User32.dll\InvalidateRect", "Ptr", HWND, "Ptr", 0, "Int", 1)
This.ErrorMsg := ""
Return True
}
; ===================================================================================================================
; Change          Change control colors.
; Parameters:     HWND        - HWND of the GUI control
;                 BkColor     - HTML color name, 6-digit hexadecimal RGB value, or "" for default color
;                 ----------- Optional
;                 TxColor     - HTML color name, 6-digit hexadecimal RGB value, or "" for default color
; Return values:  On success  - True
;                 On failure  - False, CtlColors.ErrorMsg contains additional informations
; Remarks:        If the control isn't registered yet, Add() is called instead internally.
; ===================================================================================================================
Change(HWND, BkColor, TxColor := "") {
; Check HWND -----------------------------------------------------------------------------------------------------
This.ErrorMsg := ""
HWND += 0
Return This.Attach(HWND, BkColor, TxColor)
CTL := This.Attached[HWND]
; Check BkColor --------------------------------------------------------------------------------------------------
If (BkColor <> "Trans")
If !This.CheckBkColor(BkColor, CTL.Classes[1])
Return False
; Check TxColor ------------------------------------------------------------------------------------------------
If !This.CheckTxColor(TxColor)
Return False
; Store Colors ---------------------------------------------------------------------------------------------------
If (BkColor <> CTL.BkColor) {
If (CTL.Brush) {
If (Ctl.Brush <> This.NullBrush)
DllCall("Gdi32.dll\DeleteObject", "Prt", CTL.Brush)
This.Attached[HWND].Brush := 0
}
If (BkColor = "Trans")
Brush := This.NullBrush
Else
Brush := DllCall("Gdi32.dll\CreateSolidBrush", "UInt", BkColor, "UPtr")
For I, V In CTL.Hwnds {
This.Attached[V].Brush := Brush
This.Attached[V].BkColor := BkColor
}
}
For I, V In Ctl.Hwnds
This.Attached[V].TxColor := TxColor
This.ErrorMsg := ""
DllCall("User32.dll\InvalidateRect", "Ptr", HWND, "Ptr", 0, "Int", 1)
Return True
}
; ===================================================================================================================
; Detach          Stop control coloring.
; Parameters:     HWND        - HWND of the GUI control
; Return values:  On success  - True
;                 On failure  - False, CtlColors.ErrorMsg contains additional informations
; ===================================================================================================================
Detach(HWND) {
This.ErrorMsg := ""
HWND += 0
CTL := This.Attached[HWND].Clone()
If (CTL.Brush) && (CTL.Brush <> This.NullBrush)
DllCall("Gdi32.dll\DeleteObject", "Prt", CTL.Brush)
For I, V In CTL.Classes {
If This.HandledMessages[V] > 0 {
This.HandledMessages[V] -= 1
If This.HandledMessages[V] = 0
OnMessage(This.WM_CTLCOLOR[V], "")
}  }
For I, V In CTL.Hwnds
This.Attached.Remove(V, "")
DllCall("User32.dll\InvalidateRect", "Ptr", HWND, "Ptr", 0, "Int", 1)
CTL := ""
Return True
}
This.ErrorMsg := "Control " . HWND . " is not registered!"
Return False
}
; ===================================================================================================================
; Free            Stop coloring for all controls and free resources.
; Return values:  Always True.
; ===================================================================================================================
Free() {
For K, V In This.Attached
If (V.Brush) && (V.Brush <> This.NullBrush)
DllCall("Gdi32.dll\DeleteObject", "Ptr", V.Brush)
For K, V In This.HandledMessages
If (V > 0) {
OnMessage(This.WM_CTLCOLOR[K], "")
This.HandledMessages[K] := 0
}
This.Attached := {}
Return True
}
; ===================================================================================================================
; IsAttached      Check if the control is registered for coloring.
; Parameters:     HWND        - HWND of the GUI control
; Return values:  On success  - True
;                 On failure  - False
; ===================================================================================================================
IsAttached(HWND) {
}
}
; ======================================================================================================================
; CtlColors_OnMessage
; This function handles CTLCOLOR messages. There's no reason to call it manually!
; ======================================================================================================================
CtlColors_OnMessage(HDC, HWND) {
Critical
If CtlColors.IsAttached(HWND) {
CTL := CtlColors.Attached[HWND]
If (CTL.TxColor != "")
DllCall("Gdi32.dll\SetTextColor", "Ptr", HDC, "UInt", CTL.TxColor)
If (CTL.BkColor = "Trans")
DllCall("Gdi32.dll\SetBkMode", "Ptr", HDC, "UInt", 1) ; TRANSPARENT = 1
Else
DllCall("Gdi32.dll\SetBkColor", "Ptr", HDC, "UInt", CTL.BkColor)
Return CTL.Brush
}
}

``````
Attachments
rock_paper_scissors.png (9.61 KiB) Viewed 875 times
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Re: Rock... Paper... Scissors... Program it!

If I had to say, Speed Master and TheDewd won this! Also, Xeo786 was close to this because of his interesting combination of AHK Gui, ActiveX, and HTML.
TheDewd
Posts: 1131
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Rock... Paper... Scissors... Program it!

I plan to improve my version soon.
Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
Raptor537
Posts: 7
Joined: 13 Dec 2017, 23:37

Small Edit

I've made a small edit to mine.

Code: Select all

``````loop
{
inputbox, 1, Rock... Paper... Scissors..., Type in rock paper or scissors!, ,w600 h200
If ErrorLevel
{
msgbox , , Cancelled, You have cancelled!
exitapp
}
else
random, 2, 1, 3
;1=rock 2=paper 3=scissors
If 2 contains 1
{
2 = rock
}
If 2 contains 2
{
2 = paper
}
If 2 contains 3
{
2 = scissors
}
msgbox , , You got..., You got %1%!
msgbox , , Opponent got..., Your opponent got %2%!
;game
If 1 contains rock
If 2 contains paper
{
msgbox , , Loose, You lost!
}
If 1 contains paper
If 2 contains rock
{
msgbox , , Win, You win!
}
If 1 contains rock
If 2 contains rock
{
msgbox , , Tie!, It was a tie!
}
If 1 contains scissors
If 2 contains rock
{
msgbox , , Lost, You lost!
}
If 1 contains scissors
If 2 contains paper
{
msgbox , , Win, You win!
}
If 1 contains scissors
If 2 contains scissors
{
msgbox , , Tie!, It was a tie!
}
If 1 contains paper
if 2 contains scissors
{
msgbox , , Lost, You lost!
}
If 1 contains paper
If 2 contains paper
{
msgbox , , Tie!, It was a tie!
}
}
``````