Hello,
I'm really just getting into writing code, Dark Basic and tic tac toe is where I want to start - simple on paper, definitely challenging in practice, as I've been on and off attempting this for months now.
GOAL:
Write a 2-player, code-only tic-tac-toe game with mouse input.
STRETCH GOALS:
- Add media such as sound fx
- Create an AI
- Create a menu
I am absolutely not interested in the stretch goals and don't want to discuss or think about them until the original goal is met.
So let me begin with how I am approaching this currently:
SETUP:
sync on : sync rate 40
centerX = screen width()/2
centerY = screen height()/2
divi = screen height()/10
do
cls
mx = mousex()
my = mousey()
print str$(mx),", ", str$(my)
sync
loop
Since I want the game grid centered on screen I've created variables for this purpose, the next variable "divi" divides the screen's height into 10 pieces. The purpose is best explained by
this illustration. Inside the main loop I have "mx" and "my" keeping track of my mouse position and printed out in the corner of the screen for debugging purposes. Moving on: drawing the game board.
GAME BOARD:
do
cls
mx = mousex()
my = mousey()
print str$(mx),", ", str$(my)
gosub draw_grid
sync
loop
draw_grid:
gridX = centerX - divi*3
gridY = divi*2
line gridX, gridY*2, gridX + divi*6, gridY*2
line gridX, gridY*3, gridX + divi*6, gridY*3
line gridX + gridY, gridY, gridX + gridY, gridY*4
line gridX + gridY*2, gridY, gridX + gridY*2, gridY*4
return
The position of the grid is based on the center of the screen, while the proportions are based on the variable "divi". I made two variables "gridX" and "gridY" which marks the top-left boundary of the playing field. From there it's just calculating the lines using these pieces of information.
This is the result from this sub-routine.
MOUSE DETECTION:
do
cls
mx = mousex()
my = mousey()
print str$(mx),", ", str$(my)
gosub draw_grid
gosub mouse
sync
loop
mouse:
boxcenterX = centerX - gridY
boxcenterY = centerY - gridY
idX = 1
idY = 1
for h = 1 to 3
for v = 1 to 3
left = boxcenterX - divi +1
right = boxcenterX + divi -1
top = boxcenterY - divi +1
bottom = boxcenterY + divi -1
if mX > left and mX < right and mY > top and mY < bottom
box left, top, right, bottom, rgb(15, 15, 15),rgb(15, 15, 15),rgb(15, 15, 15),rgb(15, 15, 15)
text boxcenterX, boxcenterY, str$(idX) + ", " + str$(idY)
endif
inc boxcenterY, gridY
if v = 3
boxcenterY = centerY-gridY
boxcenterX = boxcenterX + gridY
endif
if idX = 3 then idX =0
inc idX
next v
if h = 3
boxcenterX = centerX-gridY
endif
if idY = 3 then idY = 0
inc idY
next h
return
"boxcenterX" and "boxcenterY" mark the center of the top-left box in the grid. That box is assigned an ID of 1,1 using the "idX" and "idY" variables for debugging purposes. Using the center of that box, the for next loop that follows marks the center of the remaining boxes by jumping over using the distance "gridY" which is a full box length. In this process it also assigns the boundaries of each box in the "left" "right" "top" "bottom" variables. Using these boundaries I am able to detect which square the mouse position is currently located in. I got the idea from
this person's post, and quite heavily modified it. This is an older image I made, which used obscure experimental variables back then,
but it illustrates the function of this for next loop nevertheless.
WHAT NEXT...?
This is the point where I got stuck. I have been experimenting for the last few days how to move on. I can't quite figure out - when I click on a tile, how should I store that information? I've gotten to the point where I click on a tile and print a gigantic "X" in that tile but as soon as I release the mouse button, it vanishes. It has something to do with the main loop's "CLS" and "SYNC" functions, but those are a necessity for the program, I can't just remove them.
Also, I've been trying my dang hardest to wrap my mind around arrays, but I understand that they are the key to solving this problem. Honestly, hours of reading still hasn't helped me. I suppose I understand the purpose of an array, maybe even its function. But for the love of code I do not understand how to implement one in code, in practice.
I would love for somebody to present me with some extremely simple problems that implement array, so that I could solve them. I would love a couple that follow a slow but steady learning curve, so that I can master it and figure out how I can implement it into my tic-tac-toe game.
Thanx guys, I'll check back frequently and update!
EDIT: It may help to see the full code as it is right now. Just copy/paste into your DarkBasic Pro editor and launch:
sync on : sync rate 40
centerX = screen width()/2
centerY = screen height()/2
divi = screen height()/10
do
cls
mX = mousex()
mY = mousey()
print str$(mX),", ", str$(mY)
gosub draw_grid
gosub mouse
sync
loop
draw_grid:
gridX = centerX - divi*3
gridY = divi*2
line gridX, gridY*2, gridX + divi*6, gridY*2
line gridX, gridY*3, gridX + divi*6, gridY*3
line gridX + gridY, gridY, gridX + gridY, gridY*4
line gridX + gridY*2, gridY, gridX + gridY*2, gridY*4
return
mouse:
boxcenterX = centerX - gridY
boxcenterY = centerY - gridY
idX = 1
idY = 1
for h = 1 to 3
for v = 1 to 3
left = boxcenterX - divi +1
right = boxcenterX + divi -1
top = boxcenterY - divi +1
bottom = boxcenterY + divi -1
if mX > left and mX < right and mY > top and mY < bottom
box left, top, right, bottom, rgb(15, 15, 15),rgb(15, 15, 15),rgb(15, 15, 15),rgb(15, 15, 15)
text boxcenterX, boxcenterY, str$(idX) + ", " + str$(idY)
endif
inc boxcenterY, gridY
if v = 3
boxcenterY = centerY-gridY
boxcenterX = boxcenterX + gridY
endif
if idX = 3 then idX =0
inc idX
next v
if h = 3
boxcenterX = centerX-gridY
endif
if idY = 3 then idY = 0
inc idY
next h
return