
-----------------------------------
ShadowBeast
Fri Nov 02, 2007 9:41 am

Battleship (=\) attempt(using GUI), finding which button was clicked
-----------------------------------
i want to create a VERY simple battleship game, where there's a grid and randomally generated enemy ships are "hidden", then when i click one of the buttons on the grid it either hit, so it shows me i hit, or it shows me the distance from the next ship, i can fail 10 attempts...

so far i managed to do so it will count your failed attempt if you click a button, and to randomally generate ships on the battlefield, but i can't seem to be able to find what button was clicked so i can't know of i hit an enemy battleship or whats my distance...

in short, how can i know what button was clicked?
its quite urgent.
here is my code:

import GUI
var cnt:=10
procedure attack()
locate(1,1)
cnt := cnt - 1
put "Number of attempts left: "
put cnt
end attack

var grid : array 1..10,1..10 of int
for i:1..10
grid(Rand.Int(1,10),Rand.Int(1,10)) := 1
end for
var button : array 1..10,1..10 of int
for i:1..10
for j:1..10
button(i,j):= GUI.CreateButton(i*30,j*30,0,"",attack)
end for
end for 
loop 
exit when GUI.ProcessEvent 
end loop


thanks in advance.

-----------------------------------
Tony
Fri Nov 02, 2007 11:50 am

RE:Battleship (=\) attempt(using GUI), finding which button was clicked
-----------------------------------
when you create a button with

button(i,j):= GUI.CreateButton(i*30,j*30,0,"",attack) 

the button should tell the attack procedure which one it's actually attacking, not one at random.

attach(i,j) seems to make a lot of sense

-----------------------------------
ShadowBeast
Tue Nov 06, 2007 9:08 am

Re: Battleship (=\) attempt(using GUI), finding which button was clicked
-----------------------------------
thats exactly what i have problems with, this code does not compile:


import GUI
var grid : array 1 .. 10, 1 .. 10 of int
for i : 1 .. 10
    grid (Rand.Int (1, 10), Rand.Int (1, 10)) := 1
end for
var cnt := 10
procedure attack (i : int, j : int)
    locate (1, 1)
    if (grid (i, j) = 1) then
        put "Win!"
    end if
    cnt := cnt - 1
end attack
var sbutton : int
for i : 1 .. 10
    for j : 1 .. 10
        sbutton := GUI.CreateButton (i * 30, j * 30, 0, "", attack(i, j))
    end for
end for
loop
    exit when GUI.ProcessEvent
end loop


the red part is the part with the problem, it doesn't work, it can't pass values to the procedure, what can i do?
(sorry its not in code tags, can't color inside a code tag)

-----------------------------------
Nick
Tue Nov 06, 2007 10:06 am

RE:Battleship (=\) attempt(using GUI), finding which button was clicked
-----------------------------------
well u are randomly selecting grid's co-ords so instead of
for i : 1 .. 10 
grid (Rand.Int (1, 10), Rand.Int (1, 10)) := 1 
end for 

use
for i : 1 .. 10 
for ii:1..10
grid (i,ii) := Rand.Int(1,10)
end for
end for 

-----------------------------------
ShadowBeast
Tue Nov 06, 2007 10:11 am

RE:Battleship (=\) attempt(using GUI), finding which button was clicked
-----------------------------------
what?
no...
i'm randomally generating where the ships were placed, by placing "1" (a ship)
in random coordinates...
thats not the problem, my problem is determining which button was clicked on the button grid, if i can't pass arguments to the procedure
