Trying to code a wordle style game. The letter grid is a grid of types. All was going well until .......
In the checkIn function if the entered 5 letter word is not a word in the list then I want to clear that word on the grid display. Even though I reset the content of the grid (lines 167-173) and do a Sync, the previous word remains in the display. I also tried a drawGrid() before the Sync but that did not work. Obviously I am missing something simple but I don't know what.
Any help greatly appreciated
// Project: wordle
// Created: 24-10-06
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "wordle" )
SetWindowSize( 256,512, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 512,1024 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
setclearcolor(80,150,255)
setprintsize(40)
// some defines
global WRONG: WRONG = makecolor(100,100,100)
global CORRECT: CORRECT = makecolor(0,200,0)
global MAYBE: MAYBE = Makecolor(0,200,200)
global BEGIN: BEGIN = makecolor(255,255,255)
global BEGIN_FILL: BEGIN_FILL = makecolor(0,0,250,80)
global cellwidth = 95
global xOff = 15
global curWord as string
global curGuess as string = ""
global r,c as integer
//load our word list
dim words[3854] as string
read = opentoread("5.txt")
lnNo = 1
while fileeof(read) = 0
words[lnNo] = readline(read)
lnNo = lnNo + 1
endwhile
closefile(read)
// define some types
type grLet
x as integer
y as integer
color as integer
content as string
endtype
//an array to hold the grid
dim grid[5,5] as grLet
global firstRow$ = "QWERTYUIOP"
global secondRow$ = "ASDFGHJKL"
global thirdRow$ = "ZXCVBNM"
function initGrid()
for row = 0 to 4
for col = 0 to 4
grid[row,col].x = xOff + col*cellwidth
grid[row,col].y =30 + row*cellwidth
grid[row,col].color = BEGIN_FILL
grid[row,col].content = ""
next
next
endfunction
function drawGrid()
for row = 0 to 4
for col = 0 to 4
drawbox (grid[row,col].x+3,grid[row,col].y+3,grid[row,col].x+cellwidth-3,grid[row,col].y+cellwidth-3,BEGIN,BEGIN,BEGIN,BEGIN, 0)
drawbox (grid[row,col].x+6,grid[row,col].y+6,grid[row,col].x+cellwidth-6,grid[row,col].y+cellwidth-6,grid[row,col].color,grid[row,col].color,grid[row,col].color,grid[row,col].color,1)
thisText = createtext(grid[row,col].content)
settextcolor(thisText,250,250,250,255)
settextsize(thisText,80)
settextx(thisText,grid[row,col].x + 30)
settexty(thisText,grid[row,col].y + 5)
drawtext(thisText)
next
next
endfunction
function drawKeyboard()
//row 1
for i = 1 to 10
num = Asc(mid(firstrow$,i,1))
AddVirtualButton(num,30+(i-1)*50,800,40)
imgN = loadimage(mid(firstrow$,i,1) + "n.png")
setvirtualbuttonimageup(num,imgN)
imgO = loadimage(mid(firstrow$,i,1) + "o.png")
SetVirtualButtonImageDown(num,imgO)
next
//row 2
for i = 1 to 9
num = Asc(mid(secondRow$,i,1))
AddVirtualButton(num,55+(i-1)*50,850,40)
imgN = loadimage(mid(secondRow$,i,1) + "n.png")
setvirtualbuttonimageup(num,imgN)
imgO = loadimage(mid(secondRow$,i,1) + "o.png")
SetVirtualButtonImageDown(num,imgO)
next
//row 3
FOR I = 1 TO 7
num = Asc(mid(thirdRow$,i,1))
AddVirtualButton(num,105+(i-1)*50,900,40)
imgN = loadimage(mid(thirdRow$,i,1) + "n.png")
setvirtualbuttonimageup(num,imgN)
imgO = loadimage(mid(thirdRow$,i,1) + "o.png")
SetVirtualButtonImageDown(num,imgO)
next
//enter and back
AddVirtualButton(1,400,975,50)
setvirtualbuttonsize(1,120,50)
SetVirtualButtonImageUp(1,loadimage("ENTn.png"))
SetVirtualButtonImageDown(1,loadimage("ENTo.png"))
AddVirtualButton(2,120,975,50)
setvirtualbuttonsize(2,120,50)
SetVirtualButtonImageUp(2,loadimage("BAKn.png"))
SetVirtualButtonImageDown(2,loadimage("BAKo.png"))
endfunction
function isInList(word$)
start# = Timer()
res = words.find(word$)
end# = Timer()
took# = end# - start#
if res = -1
message("Not in list" + " took " + str(took#))
elseif res > 0
message("Found at " + str(res) + " took " + str(took#))
endif
endfunction res
function init()
initGrid()
drawGrid()
drawKeyboard()
curWord = words[random(1,3853)]
r = 0
c = 0
curGuess = ""
endfunction
function checkIn()
if len(curGuess) < 5
for i = 65 to 90
if GetVirtualButtonPressed(i) = 1
curGuess = curGuess + chr(i)
grid[r,c].content = chr(i)
c = c + 1
exit
endif
next
endif
if GetVirtualButtonPressed(1) = 1 and c = 5
res = words.find(curGuess)
if res = -1
message ("Could not find " + curGuess)
curGuess = ""
for i = 0 to 4
grid[r,i].content = ""
next
sync()
endif
endif
/*if GetVirtualButtonPressed(2) = 1 and c > 0
curGuess = Left(curGuess,(c-1))
c = c-1
endif */
endfunction
init()
do
checkIn()
drawGrid()
print(curWord)
Sync()
loop