Battleships game (not sure if this is the correct place)

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
adidas345
Posts: 5
Joined: Fri Feb 26, 2016 10:00 am

Battleships game (not sure if this is the correct place)

Post by adidas345 » Fri Feb 26, 2016 10:14 am

I am unsure if this is the correct place to post this but I am in need of serious help if possible.

I have been trying to code a working game of battleships for at least 3 months now. The project is on LiveCode 7.0 . I've been trying to code a 10 by 10 grid for 0's to show up and the user needs to input the coordinates of where they would like to place their ship, the coordinates they choose should display on the grid of 0's as an X and replace the 0 in that coordinate. The line of code needs to be present in only the "place large ship" button but I cannot code it so that the X appears in the selected coordinate position. however I cannot make any progression as it seems that this level of coding is slightly more advanced than I thought, if anyone could take a look at my program and help me to get it working from here on it would be greatly appreciated as this is very important. If anyone has previously created a game like this I would be grateful if you could help or even send me a previous battleships game code so I can see what I need to do to finish mine thanks.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Battleships game (not sure if this is the correct place)

Post by dunbarx » Fri Feb 26, 2016 3:13 pm

Hi.

How are you locating those "X"'s? Is there an existing grid of fields? Or are you placing objects in a cartesian plane? In other words(pseudo):

Code: Select all

ask for coordinates somehow
create field
set the loc of last field to "X,Y"
Put "X" into last field
The good news is that this will be simple and straightforward to accomplish, as soon as we understand what sort of structure you already have (or need to have).

Craig Newman

adidas345
Posts: 5
Joined: Fri Feb 26, 2016 10:00 am

Re: Battleships game (not sure if this is the correct place)

Post by adidas345 » Fri Feb 26, 2016 3:52 pm

I have used a 2d array to create my grid of zeros. I've used a nested loop to create a 10 x 10 grid which then has to have the 0 in a certain location changed to an X for a location of a ship.
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Here's an example of how it is outputted using the 2D array. But if I wanted to change the coordinate (2,2) to the location of a ship it should look like this:
0 0 0 0
0 X 0 0
0 0 0 0
0 0 0 0

The part of the code I cannot seem to do is to get the X to replace the 0 in that position in the 2D array. I'll post screenshots of my code later on as I am replying to this using my phone, thanks again.

adidas345
Posts: 5
Joined: Fri Feb 26, 2016 10:00 am

Re: Battleships game (not sure if this is the correct place)

Post by adidas345 » Fri Feb 26, 2016 4:14 pm

For whatever reason it is not letting me attach the file so I have pasted my code below from the point of the grid being made and where I wish to enter the code to get it to output:

end Get_Largeposition

on DLargeArray @ArrayLarge, @CoordinateLarge, Row, Column

repeat with Row = 0 to 9
repeat with Column = 0 to 9
put 0 into ArrayLarge[Row][Column]
end repeat
end repeat

put "X" into CoordinateLarge

end DLargeArray

on Display_ongrid ArrayLarge, CoordinateLarge, ArrayLargeRow, ArrayLargeColumn, Row, Column

repeat with Row = 0 to 9
repeat with Column = 0 to 9
put ArrayLarge[Row][Column] & " " after field "PlayersHitMisses"
end repeat
put return after field "PlayersHitMisses"
end repeat

repeat with loop = 1 to 4
put ArrayLargeRow[loop] after field "PlayersHitMisses"
put ArrayLargeColumn[loop] after field "PlayersHitMisses"
end repeat


end Display_ongrid

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Battleships game (not sure if this is the correct place)

Post by dunbarx » Fri Feb 26, 2016 5:29 pm

Hi.

You cannot post until you have 11 (I think) posts. Anyway, does this help:

Code: Select all

on mouseUp
   repeat with Row = 0 to 9
      repeat with Column = 0 to 9
         put 0 into ArrayLarge[Row][Column]
      end repeat
   end repeat
   
   put "X" into ArrayLarge[2][2]
end mouseUp 
Craig

adidas345
Posts: 5
Joined: Fri Feb 26, 2016 10:00 am

Re: Battleships game (not sure if this is the correct place)

Post by adidas345 » Fri Feb 26, 2016 6:40 pm

Unfortunately not, the coordinate I'm trying to input into the grid is a value that the user inputs themselves using the variables LargeColumnArray and LargeRowArray, and these values are then represented by the X's on the grid.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Battleships game (not sure if this is the correct place)

Post by dunbarx » Fri Feb 26, 2016 7:17 pm

Then I am still confused. When you say "grid", do you mean in the physical space on the card? I assume when we both say "array", we mean an array variable in LC.

But if "grid" means what I think, then if the user inputs "2,2" or whatever, this can be mapped to both the array and the physical space, as in:

Code: Select all

getTheUserInput -- "2,2"
put "X" into fld "f22" --one way of doing it, if fields are pre-loaded

--or another way

clone img "X"
set the loc of last image to item 1 of it * 100,item 2 of it * 100 --or whatever
Tell me if I am getting anywhere at all with this. It is likely a matter of semantics. How does the user input data? In what form: (2,2)? (200,200?)?

Craig

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Battleships game (not sure if this is the correct place)

Post by russellf » Fri Feb 26, 2016 7:21 pm

What about just making all the grid pieces buttons? Then you can toggle them when they hit them and also present a graphic background.

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Battleships game (not sure if this is the correct place)

Post by quailcreek » Fri Feb 26, 2016 7:25 pm

Hi adidas345,
Craig tried t get you started by creating fields. If I were creating this game I'd start by making a grid of grouped fields or buttons to place the x's and o's into. Then when the user enters the coordinates I'd check to see which field is closest and place the x into that field. The fields, of course, would have to be square with no borders and opaque turned off and lockText turned on.
Tom
MacBook Pro OS Mojave 10.14

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Battleships game (not sure if this is the correct place)

Post by jacque » Sat Feb 27, 2016 6:26 pm

I'd go with the button layout too. If you name each button for its position it will be trivial to identify and work with them. That is, name the buttons:

1-1
1-2
1-3
...
8-8
8-9
8-10
etc

When the user clicks a button its name identifies the position in the grid.

This would work with fields too. If you use buttons though you can set the icon to show different images. You can use images in fields too but it's more work. If you just want to use X or O then use fields.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

adidas345
Posts: 5
Joined: Fri Feb 26, 2016 10:00 am

Re: Battleships game (not sure if this is the correct place)

Post by adidas345 » Sun Feb 28, 2016 12:41 am

Thanks for your suggestions, you all seem to be agreeing on the button method so I'll attempt it again using this approach, seems so much easier now that I think about it, I'll post back if I am successful or if I'm in need of more help, thanks once again.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Battleships game (not sure if this is the correct place)

Post by dunbarx » Sun Feb 28, 2016 6:14 am

Hi.

It isn't important whether you use buttons (changing their label) or fields (putting text into them), the thing is to plan the most robust world that your app lives in.

Preloading and prenaming solves many management nightmares. The preloading means you never have to think about where all those "X'"s appear on screen. Prenaming gives a straightforward method of accessing a particular "cell".

Write back with your progress.

Craig

Post Reply