Crappy Boggle Game

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Crappy Boggle Game

04 Aug 2019, 14:26

I'm no expert by any means, but I was trying to come up with a fun way to show someone how to make a simple game in AHK.
Originally it started out as little chunks of code, then later while playing an actual game of Boggle, i got the idea to turn that lesson into a working model and it was later assembled into what you see below.

It's a functional, albeit more proof-of-concept, Boggle-like game, but I thought I would share it anyway. I saw some other game examples on the forums that were way more advanced, but didn't see Boggle so... here ya go?
It could use a lot of work like a more streamlined GUI code, or a better way to randomize letters, or validate words exist in the puzzle or are actual dictionary words, but like I said, I was just trying to help someone else learn and this is what I ended up with when I finished showing how to use various AHK examples and make something that works.

Feel free to share, improve, etc.
[Edit: Added in Scoring of # of words & Word lengths, Also a check for duplicates]
[Edit: Renamed topic to title of the game... "Crappy Boggle Game"]

preview: https://drive.google.com/open?id=1wkdnjJbq8RSrknE7yMZMMVMxOagqUh_A

Code: Select all

#SingleInstance Force
gosub, scratch

; Gui was made real quick with Smart GUI Creator
Gui, Font, S10 , Verdana
Gui, Add, edit, x250 y9 w240 r18 vscratch, %scratchpadnotes%

; Rules Link
Gui, Font, S10 , Verdana
gui, add, text, x250 y+5 r1 cgray vrules grules, Rules

; Timer
Gui, Font, S10 , Verdana
gui, add, text, x+15 w1200 r1 cblue vtimer, 3 minute timer 

; Boggle Grid of Letters
Gui, Font, S25 CDefault, Verdana
Gui, Add, Edit, center ReadOnly x12 y9 w50 h50 vA1, B
Gui, Add, Edit, center ReadOnly x72 y9 w50 h50 vA2, O
Gui, Add, Edit, center ReadOnly x132 y9 w50 h50 vA3, G
Gui, Add, Edit, center ReadOnly x192 y9 w50 h50 vA4, G
Gui, Add, Edit, center ReadOnly x12 y69 w50 h50 vB1, L
Gui, Add, Edit, center ReadOnly x72 y69 w50 h50 vB2, E
Gui, Add, Edit, center ReadOnly x132 y69 w50 h50 vB3, M
Gui, Add, Edit, center ReadOnly x192 y69 w50 h50 vB4, A
Gui, Add, Edit, center ReadOnly x12 y129 w50 h50 vC1, K
Gui, Add, Edit, center ReadOnly x72 y129 w50 h50 vC2, E
Gui, Add, Edit, center ReadOnly x132 y129 w50 h50 vC3, R
Gui, Add, Edit, center ReadOnly x192 y129 w50 h50 vC4, B
Gui, Add, Edit, center ReadOnly x12 y189 w50 h50 vD1, Y
Gui, Add, Edit, center ReadOnly x72 y189 w50 h50 vD2, M
Gui, Add, Edit, center ReadOnly x132 y189 w50 h50 vD3, E
Gui, Add, Edit, center ReadOnly x192 y189 w50 h50 vD4, !

; Time's Up Alert
Gui, Font, S20 CDefault, Verdana
Gui, Add, text,  x15 y245 w200 h50 vtimeup cred, ;TIME'S UP!!!!

; Submit Button to scramble new letters.
Gui, Font, S18 , Verdana
Gui, Add, Button,  x1 y295 w200 h30 vsubmit gsubmit, Shake the Box!

; Show this thing
Gui, Show, w500 h329, Boggle... kinda
return

; Submit does this:
submit:
gui, submit, nohide

; Begin in 3... 2... 1... GO!
gosub, startgame

; Randomize letters and create letter blocks
Letters = A,A,B,C,D,E,E,E,F,G,H,I,I,J,K,L,M,N,O,O,P,Qu,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,E,F,G,I,L,M,N,O,P,R,S,T,W

loop, parse, letters, `,
	{
	l%a_index% = %a_loopfield%
	maxletters = %a_index%
	}

; Variables for  Row 1
Random, pick, 1,%maxletters%
A1 := l%pick%
Random, pick, 1,%maxletters%
A2 := l%pick%
Random, pick, 1,%maxletters%
A3 := l%pick%
Random, pick, 1,%maxletters%
A4 := l%pick%

; Variables for  Row 2
Random, pick, 1,%maxletters%
B1 := l%pick%
Random, pick, 1,%maxletters%
B2 := l%pick%
Random, pick, 1,%maxletters%
B3 := l%pick%
Random, pick, 1,%maxletters%
B4 := l%pick%

; Variables for  Row 3
Random, pick, 1,%maxletters%
C1 := l%pick%
Random, pick, 1,%maxletters%
C2 := l%pick%
Random, pick, 1,%maxletters%
C3 := l%pick%
Random, pick, 1,%maxletters%
C4 := l%pick%

; Variables for Row 4
Random, pick, 1,%maxletters%
D1 := l%pick%
Random, pick, 1,%maxletters%
D2 := l%pick%
Random, pick, 1,%maxletters%
D3 := l%pick%
Random, pick, 1,%maxletters%
D4 := l%pick%

; Set GUI to the variables
guicontrol,,a1, %a1%
guicontrol,,a2, %a2%
guicontrol,,a3, %a3%
guicontrol,,a4, %a4%

guicontrol,,b1, %b1%
guicontrol,,b2, %b2%
guicontrol,,b3, %b3%
guicontrol,,b4, %b4%

guicontrol,,c1, %c1%
guicontrol,,c2, %c2%
guicontrol,,c3, %c3%
guicontrol,,c4, %c4%

guicontrol,,d1, %d1%
guicontrol,,d2, %d2%
guicontrol,,d3, %d3%
guicontrol,,d4, %d4%

; Start the game & Timer
gosub, Countdown
return

; Rules of Boggle.
rules:
therules =
(
Rules:
The goal of Boggle is to score points by finding words in the random letters in the grid. The letters you use must be touching vertically, horizontally, or diagonally in a chain. You can't skip, or "jump" across letters, or use the same letter twice within a given word.

Scoring:
Points are scored like so:

3-letter words = 1 point
4-letter words = 1 point
5-letter words = 2 points
6-letter words = 3 points
7-letter words = 5 points
8-letter words or greater = 11 points
)
MsgBox % therules
return

; Scratch Pad notes/text pre-game info
scratch:
scratchpadnotes =
(
This is your Scratch Pad.
You can type your discovered words in here once you begin.  Click Rules for information about scoring points.

Once you shake up the box, this Scratch Pad area will clear, and the Timer will begin.
)
return

; GUI setup for starting the game. Clear vars etc.
startgame:
GuiControl,, scratch, 
begin = 4
loop, 3 {
	begin:=begin-1
	GuiControl,, timeup, Begin in %begin%
	sleep, 1000
	}
GuiControl,, timeup, GO
sleep, 750
GuiControl,, timeup,
SetTimer, countdown, 1000
tick = 181
return

; Countdown timer start and what to do when reaching 0
Countdown:
tick:=tick-1
GuiControl,, timer, %tick%
if (tick = 0)
{
	settimer, countdown, off
	GuiControl,, timeup, TIME'S UP!!!
	GuiControl,, timer, You're out of time.
	;~ GuiControl,, submit, Shake the Box!
	gosub, score
}
return

; Read word list, tally up words and score based on Word length
score:
gui, submit, nohide
totalscore:=8word:=7word:=6word:=5word:=4word:=3word:=score8:=score7:=score6:=score5:=score4:=score3:="0" ; Clear score variables before calculating
scoremessage:=""


; Check for dpulicates
scratchduplicatecheck=
(
%scratch%
)
Sort, scratchduplicatecheck, U
scratch = %scratchduplicatecheck%

loop, parse, scratch, `n
{

	countword = %a_index%
	length := StrLen(a_loopfield)
		if (length = 3) {
			3word := 3word+1
		}
		if (length = 4) {
			4word := 4word+1
		}
		if (length = 5) {
			5word := 5word+1
		}
		if (length = 6) {
			6word := 6word+1
		}
		if (length = 7) {
			7word := 7word+1
		}
		if (length >= 8) {
			8word := 8word+1
		}
}
if (8word >= 1) {
	score8 := ( 8word * 11)
	totalscore:=totalscore+score8
	ww8 = You had %8word%x 8 letter words worth 11 points each: %score8% points.
	scoremessage = %scoremessage%%ww8%`n
}
if (7word >= 1) {
	score7 := ( 7word * 5)
	totalscore:=totalscore+score7
	ww7 = You had %7word%x 7 letter words worth 5 points each: %score7% points.
	scoremessage = %scoremessage%%ww7%`n
}
if (6word >= 1) {
	score6 := ( 6word * 3)
	totalscore:=totalscore+score6
	ww6 = You had %6word%x 6 letter words worth 3 points each: %score6% points.
	scoremessage = %scoremessage%%ww6%`n
}
if (5word >= 1) {
	score5 := ( 5word * 2)
	totalscore:=totalscore+score5
	ww5 = You had %5word%x 5 letter words worth 2 points each: %score5% points.
	scoremessage = %scoremessage%%ww5%`n
}
if (4word >= 1) {
	score4 := ( 4word * 1)
	totalscore:=totalscore+score4
	ww4 = You had %4word%x 4 letter words worth 1 point each: %score4% points.
	scoremessage = %scoremessage%%ww4%`n
}
if (3word >= 1) {
	score3 := ( 3word * 1)
	totalscore:=totalscore+score3
	ww3 = You had %3word%x 3 letter words worth 1 point each: %score3% points.
	scoremessage = %scoremessage%%ww3%`n
}
MsgBox, You had %countword% total words:`n%scoremessage%`n`nTOTAL SCORE IS: -- %totalscore% -- POINTS!
return

GuiClose:
ExitApp
Last edited by AlleyArtwork on 07 Aug 2019, 20:36, edited 1 time in total.
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Re: Crappy Boggle Game

07 Aug 2019, 20:35

I was going to edit the original code in the original post, but decided to leave it just for comparison.

I figured out a way to better randomize the letters based on the actual boggle game based on their dice configuration.
Now it rolls each dice, and then randomly assigns that result to one of the 16 squares until there are no remaining squares.
This should give a much more realistic Boggle feel.

Crappy Boggle Game version 2.

Code: Select all

/*  Crappy Boggle Game Version 2

Boggle Dice Distribution (Classic Boggle re-released)
16 Dice with the following possible letter combinations per dice:
AAEEGN, ABBJOO, ACHOPS, AFFKPS
AOOTTW, CIMOTU, DEILRX, DELRVY
DISTTY, EEGHNW, EEINSU, EHRTVW
EIOSST, ELRTTY, HIMNUQu, HLNNRZ

*/
#SingleInstance Force
gosub, scratch

; Gui was made real quick with Smart GUI Creator
Gui, Font, S10 , Verdana
Gui, Add, edit, x250 y9 w240 r18 vscratch, %scratchpadnotes%

; Rules Link
Gui, Font, S10 , Verdana
gui, add, text, x250 y+5 r1 cgray vrules grules, Rules 

; Timer
Gui, Font, S10 , Verdana
gui, add, text, x+25 w1200 r1 cblue vtimer, 3 minute timer ready!

; Boggle Grid of Letters
Gui, Font, S25 CDefault, Verdana
Gui, Add, Edit, center ReadOnly x12 y9 w50 h50 vd1, C
Gui, Add, Edit, center ReadOnly x72 y9 w50 h50 vd2, R
Gui, Add, Edit, center ReadOnly x132 y9 w50 h50 vd3, A
Gui, Add, Edit, center ReadOnly x192 y9 w50 h50 vd4, P
Gui, Add, Edit, center ReadOnly x12 y69 w50 h50 vd5, O
Gui, Add, Edit, center ReadOnly x72 y69 w50 h50 vd6, B
Gui, Add, Edit, center ReadOnly x132 y69 w50 h50 vd7, Y
Gui, Add, Edit, center ReadOnly x192 y69 w50 h50 vd8, P
Gui, Add, Edit, center ReadOnly x12 y129 w50 h50 vd9, G
Gui, Add, Edit, center ReadOnly x72 y129 w50 h50 vd10, G
Gui, Add, Edit, center ReadOnly x132 y129 w50 h50 vd11, L
Gui, Add, Edit, center ReadOnly x192 y129 w50 h50 vd12, E
Gui, Add, Edit, center ReadOnly x12 y189 w50 h50 vd13, E
Gui, Add, Edit, center ReadOnly x72 y189 w50 h50 vd14, M
Gui, Add, Edit, center ReadOnly x132 y189 w50 h50 vd15, A
Gui, Add, Edit, center ReadOnly x192 y189 w50 h50 vd16, G

; Time's Up Alert
Gui, Font, S20 CDefault, Verdana
Gui, Add, text,  x15 y245 w200 h50 vtimeup cred, ;TIME'S UP!!!!

; Submit Button to scramble new letters.
Gui, Font, S18 , Verdana
Gui, Add, Button,  x1 y295 w200 h30 vsubmit gsubmit, Shake the Box!

; Show this thing
Gui, Show, w500 h329, Crappy Boggle Game
return

; Submit does this:
submit:
gui, submit, nohide

; Randomize letters and create letter blocks
dice1 = A,A,E,E,G,N
random, dice, 1,6
loop, parse, dice1, `,
{
	r%A_Index% = %A_LoopField%
}
1dice:=r%dice%

dice2 = A,B,B,J,O,O
random, dice, 1,6
loop, parse, dice2, `,
{
	r%A_Index% = %A_LoopField%
}
2dice:=r%dice%

dice3 = A,C,H,O,P,S
random, dice, 1,6
loop, parse, dice3, `,
{
	r%A_Index% = %A_LoopField%
}
3dice:=r%dice%

dice4 = A,F,F,K,P,S
random, dice, 1,6
loop, parse, dice4, `,
{
	r%A_Index% = %A_LoopField%
}
4dice:=r%dice%

dice5 = A,O,O,T,T,W
random, dice, 1,6
loop, parse, dice5, `,
{
	r%A_Index% = %A_LoopField%
}
5dice:=r%dice%

dice6 = C,I,M,O,T,U
random, dice, 1,6
loop, parse, dice6, `,
{
	r%A_Index% = %A_LoopField%
}
6dice:=r%dice%

dice7 = D,E,I,L,R,X
random, dice, 1,6
loop, parse, dice7, `,
{
	r%A_Index% = %A_LoopField%
}
7dice:=r%dice%

dice8 = D,E,L,R,V,Y
random, dice, 1,6
loop, parse, dice8, `,
{
	r%A_Index% = %A_LoopField%
}
8dice:=r%dice%

dice9 = D,I,S,T,T,Y
random, dice, 1,6
loop, parse, dice9, `,
{
	r%A_Index% = %A_LoopField%
}
9dice:=r%dice%

dice10 = E,E,G,H,N,W
random, dice, 1,6
loop, parse, dice10, `,
{
	r%A_Index% = %A_LoopField%
}
10dice:=r%dice%

dice11 = E,E,I,N,S,U
random, dice, 1,6
loop, parse, dice11, `,
{
	r%A_Index% = %A_LoopField%
}
11dice:=r%dice%

dice12 = E,H,R,T,V,W
random, dice, 1,6
loop, parse, dice12, `,
{
	r%A_Index% = %A_LoopField%
}
12dice:=r%dice%

dice13 = E,I,O,S,S,T
random, dice, 1,6
loop, parse, dice13, `,
{
	r%A_Index% = %A_LoopField%
}
13dice:=r%dice%

dice14 = E,L,R,T,T,Y
random, dice, 1,6
loop, parse, dice14, `,
{
	r%A_Index% = %A_LoopField%
}
14dice:=r%dice%

dice15 = H,I,M,N,U,Qu
random, dice, 1,6
loop, parse, dice15, `,
{
	r%A_Index% = %A_LoopField%
}
15dice:=r%dice%

dice16 = H,L,N,N,R,Z
random, dice, 1,6
loop, parse, dice16, `,
{
	r%A_Index% = %A_LoopField%
}
16dice:=r%dice%


; Letter block order: 
Random, order, 1, 16
Loop, 15
{
   Loop
   {
      Random, Result, 1, 16
      If Result not in %order%
      Break
   }
   order = %order%,%Result%
}

; Re-arrange Gui order where D1 could still be D16 etc.
loop, parse, order, `,
{
	d%a_index% = %A_LoopField%
}
 

; Begin in 3... 2... 1... GO!
gosub, startgame

; Set GUI to the variables
; Layout is left to right, top to bottom, but the Variable inside the GUI control should randomize which block lands where.
guicontrol,,d%d1%, %1dice%
guicontrol,,d%d2%, %2dice%
guicontrol,,d%d3%, %3dice%
guicontrol,,d%d4%, %4dice%

guicontrol,,d%d5%, %5dice%
guicontrol,,d%d6%, %6dice%
guicontrol,,d%d7%, %7dice%
guicontrol,,d%d8%, %8dice%

guicontrol,,d%d9%, %9dice%
guicontrol,,d%d10%, %10dice%
guicontrol,,d%d11%, %11dice%
guicontrol,,d%d12%, %12dice%

guicontrol,,d%d13%, %13dice%
guicontrol,,d%d14%, %14dice%
guicontrol,,d%d15%, %15dice%
guicontrol,,d%d16%, %16dice%

; This is just here to confirm that the slots "d#" are randominzed to another number.
; this is the only way I could figure out to not have the same dice in the same slot every time.
; Comment in MSGbox to see
newlist =
(
D1 = D%d1%, %1dice% 
D2 = D%d2%, %2dice%
D3 = D%d3%, %3dice%
D4 = D%d4%, %4dice%

D5 = D%d5%, %5dice%
D6 = D%d6%, %6dice%
D7 = D%d7%, %7dice%
D8 = D%d8%, %8dice%

D9 = D%d9%, %9dice%
D10 = D%d10%, %10dice%
D11 = D%d11%, %11dice%
D12 = D%d12%, %12dice%

D13 = D%d13%, %13dice%
D14 = D%d14%, %14dice%
D15 = D%d15%, %15dice%
D16 = D%d16%, %16dice%
)
;~ MsgBox % newlist ; Comment this in to confirm that D1 doesn't always = D1

; Start the game & Timer
gosub, Countdown
return

; Rules of Boggle.
rules:
therules =
(
Rules:
The goal of Boggle is to score points by finding words in the random letters in the grid. The letters you use must be touching vertically, horizontally, or diagonally in a chain. You can't skip, or "jump" across letters, or use the same letter twice within a given word.

Scoring:
Points are scored like so:

3-letter words = 1 point
4-letter words = 1 point
5-letter words = 2 points
6-letter words = 3 points
7-letter words = 5 points
8-letter words or greater = 11 points
)
MsgBox % therules
return

; Scratch Pad notes/text pre-game info
scratch:
scratchpadnotes =
(
This is your Scratch Pad.
You can type your discovered words in here once you begin.  Click Rules for information about scoring points.

Once you shake up the box, this Scratch Pad area will clear, and the Timer will begin.
)
return

; GUI setup for starting the game. Clear vars etc.
startgame:
GuiControl,, scratch, 
begin = 4
loop, 3 {
	begin:=begin-1
	GuiControl,, timeup, Begin in %begin%
	sleep, 1000
	}
GuiControl,, timeup, GO
sleep, 750
GuiControl,, timeup,
SetTimer, countdown, 1000
tick = 181
return

; Countdown timer start and what to do when reaching 0
Countdown:
tick:=tick-1
GuiControl,, timer, %tick% seconds left
if (tick = 0)
{
	settimer, countdown, off
	GuiControl,, timeup, TIME'S UP!!!
	GuiControl,, timer, You're out of time.
	gosub, score
}
return

; Read word list, tally up words and score based on Word length
score:
gui, submit, nohide
totalscore:=8word:=7word:=6word:=5word:=4word:=3word:=score8:=score7:=score6:=score5:=score4:=score3:="0" ; Clear score variables before calculating
scoremessage:=""

; Check for dpulicates
scratchduplicatecheck=
(
%scratch%
)
Sort, scratchduplicatecheck, U
scratch = %scratchduplicatecheck%

; Parse & count string length
loop, parse, scratch, `n
{

	countword = %a_index%
	length := StrLen(a_loopfield)
		if (length = 3) {
			3word := 3word+1
		}
		if (length = 4) {
			4word := 4word+1
		}
		if (length = 5) {
			5word := 5word+1
		}
		if (length = 6) {
			6word := 6word+1
		}
		if (length = 7) {
			7word := 7word+1
		}
		if (length >= 8) {
			8word := 8word+1
		}
}
if (8word >= 1) {
	score8 := ( 8word * 11)
	totalscore:=totalscore+score8
	ww8 = You had %8word%x 8 letter words worth 11 points each: %score8% points.
	scoremessage = %scoremessage%%ww8%`n
}
if (7word >= 1) {
	score7 := ( 7word * 5)
	totalscore:=totalscore+score7
	ww7 = You had %7word%x 7 letter words worth 5 points each: %score7% points.
	scoremessage = %scoremessage%%ww7%`n
}
if (6word >= 1) {
	score6 := ( 6word * 3)
	totalscore:=totalscore+score6
	ww6 = You had %6word%x 6 letter words worth 3 points each: %score6% points.
	scoremessage = %scoremessage%%ww6%`n
}
if (5word >= 1) {
	score5 := ( 5word * 2)
	totalscore:=totalscore+score5
	ww5 = You had %5word%x 5 letter words worth 2 points each: %score5% points.
	scoremessage = %scoremessage%%ww5%`n
}
if (4word >= 1) {
	score4 := ( 4word * 1)
	totalscore:=totalscore+score4
	ww4 = You had %4word%x 4 letter words worth 1 point each: %score4% points.
	scoremessage = %scoremessage%%ww4%`n
}
if (3word >= 1) {
	score3 := ( 3word * 1)
	totalscore:=totalscore+score3
	ww3 = You had %3word%x 3 letter words worth 1 point each: %score3% points.
	scoremessage = %scoremessage%%ww3%`n
}
MsgBox, You had %countword% total words:`n%scoremessage%`n`nTOTAL SCORE IS: -- %totalscore% -- POINTS!
return

GuiClose:
ExitApp
Raymond888
Posts: 17
Joined: 11 Aug 2019, 19:14

Re: Crappy Boggle Game

11 Aug 2019, 19:26

Was fun to play.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 62 guests