|
Post by bplus on Oct 1, 2024 20:21:41 GMT
Can you count the number of bats in this: Can you write a program to show them one by one?
|
|
|
Post by bplus on Oct 3, 2024 14:55:40 GMT
First hint look for a B, see it is at (Xcol, Yrow)
2nd hint from every B there are 8 directions to look for the word BAT: up, down, left right, and 4 diagonals up and right, down and right, down and left and down and right.
Going up from x, y is +0 x and -1 y Going down is +0 x and +1 y ... ect
So put all these one step directions into an dx, dy array d = change in x or y
Dim DX(1 to 8), DY(1 to 8)
up DX(1) = 0 : DY(1) = -1 down DX(2) = 0 : DY(2) = 1 ... ect for 8 directions
|
|
|
Post by bplus on Oct 4, 2024 15:22:59 GMT
OK here is the setup. xy is the number of rows and columns of letters in the puzzle file. L$() is a 2D array to hold all the letters from the file. Already started the DX() and DY() arrays that handle the 8 directions a BAT word can go from any B. _Title "Bat counter from word search" 'by B+ 2018-10-16 from ' Bat counter from word search.txt for JB [B+=MGA] 2018-10-16 ' Happy Halloween! Here is your trick! ' Treat me with the correct count of BAT ' 2024-10-04 modified for teaching QB64
' I Shared some of these for a more advanced Word Search App Dim Shared xy As Integer 'for square block of letters xy is one side of square xy = 20 Dim Shared L$(1 To xy, 1 To xy) ' array to hold letters Dim Shared DX(1 To 8) As Integer, DY(1 To 8) As Integer ' dx, dy steps DX(1) = 1: DY(1) = 0 DX(2) = 1: DY(2) = 1 DX(3) = 0: DY(3) = 1 DX(4) = -1: DY(4) = 1 DX(5) = -1: DY(5) = 0 DX(6) = -1: DY(6) = -1 DX(7) = 0: DY(7) = -1 DX(8) = 1: DY(8) = -1
' load puzzle letters put this file in same folder as your source .bas file and make sure you Run/Compile the EXE to that source folder! Open "bat count word search.txt" For Input As #1 For y = 1 To xy Input #1, fline$ For x = 1 To xy L$(x, y) = Mid$(fline$, x, 1) Next Next Close #1
'show puzzle For y = 1 To xy For x = 1 To xy Print L$(x, y); " "; ' we are putting a space between each letter to spread them Next Print Next Locate xy + 2, 5: Input "OK, press enter when ready... "; wate$
|
|
|
Post by bplus on Oct 5, 2024 15:42:58 GMT
Counting BATs, final installment: From every B found in array, check the 8 directions from B. If the word will fit in the array building in that direction build the word and see if it is BAT. If so show it in yellow as the latest found BAT. After user sees it, color it bright white as counted found BAT. Continue through the array in this manner and then show the final count at the end. _Title "Bat counter from word search" 'by B+ 2018-10-16 from ' Bat counter from word search.txt for JB [B+=MGA] 2018-10-16 ' Happy Halloween! Here is your trick! ' Treat me with the correct count of BAT ' 2024-10-04 modified for teaching QB64
' I Shared some of these for a more advanced Word Search App Dim Shared xy As Integer 'for square block of letters xy is one side of square xy = 20 Dim Shared L$(1 To xy, 1 To xy) ' array to hold letters Dim Shared DX(1 To 8) As Integer, DY(1 To 8) As Integer ' dx, dy steps DX(1) = 1: DY(1) = 0 DX(2) = 1: DY(2) = 1 DX(3) = 0: DY(3) = 1 DX(4) = -1: DY(4) = 1 DX(5) = -1: DY(5) = 0 DX(6) = -1: DY(6) = -1 DX(7) = 0: DY(7) = -1 DX(8) = 1: DY(8) = -1
' load puzzle letters Open "bat count word search.txt" For Input As #1 For y = 1 To xy Input #1, fline$ For x = 1 To xy L$(x, y) = Mid$(fline$, x, 1) Next Next Close #1
'show puzzle For y = 1 To xy For x = 1 To xy Print L$(x, y); " "; ' we are putting a space between each letter to spread them Next Print Next Locate xy + 2, 5: Input "OK, press enter when ready... "; wate$
' find and display and count the bats For y = 1 To xy For x = 1 To xy ' go through array looking for B
If L$(x, y) = "B" Then ' to start just look for a B
For D = 1 To 8 ' now test all 8 directions from B
'will BAT fit? from "B" at x, y b$ = "" b1 = 2 * DX(D) + x > 0 And 2 * DX(D) + x <= xy b2 = 2 * DY(D) + y > 0 And 2 * DY(D) + y <= xy ' taking 2 steps from x, y are we still in the letters array?
' b1 <> 0 says yes the x's are in the array ' b2 <> 0 says yes the y's are in the array If b1 And b2 Then ' letters are all inside array b$ = "B": xx = x + DX(D): yy = y + DY(D) 'take first step For i = 2 To 3 ' add letters to base B b$ = b$ + L$(xx, yy) xx = xx + DX(D): yy = yy + DY(D) Next
'now word is built check it If b$ = "BAT" Then 'show our NEW result in Yellow
' show it! Color 14 xx = x: yy = y For i = 1 To 3 Locate yy, 2 * xx - 1: Print L$(xx, yy); xx = xx + DX(D): yy = yy + DY(D) Next batCount = batCount + 1 'count it
'let user see it before start looking for next Locate xy + 2, 5: Print "Bat at ("; x; ", "; y; "), bat count = "; batCount Locate xy + 3, 5: Input "OK, press enter when ready... "; wate$
'after user sees it color it bright white as counted BAT Color 15 xx = x: yy = y For i = 1 To 3 Locate yy, 2 * xx - 1: Print L$(xx, yy); xx = xx + DX(D): yy = yy + DY(D) Next End If End If Next End If Next Next Locate xy + 4, 5: Print "Total BATs found = "; batCount;
|
|