×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Battleship like fortran code

Battleship like fortran code

(OP)
Hi, I'm from Spain, so please, don't be too bad with my english :D

I'm just having a little trouble trying to finish some kind of Battleship code, I'm stuck with boats 3 and 4 units long, and I haven't found any other place to ask for some help/advice.

The code I posted here works perfectly fine for boats 1 or 2 units long. The *.dat file is a matrix made of O's where you can place your ships in, by placing an * on that position. The code opens that file as a Matrix and sees if your shoot hit an * or not. I just want to know what to do when ships are bigger.

I know it's such a stupid program, but i just can't see how to do it...

Thanks a lot for your attention.

CODE --> fortran

program battleship

implicit none
integer :: i, j, k ,l
character :: A(10,10), V(3,3)

do

j=0
open (unit = 10, file = 'tablero.dat', status= 'unknown')

do i = 1,10

read(10,fmt='(i2,5x,10a4)') j,A(i,:)                                ! Open tablero.dat file

enddo

do
if (any(A=='*')) then                                                 ! If there's a boat not sunk, keep going

write (*,*) ''
write (*,*) ' Where do you want to shoot? '

if (A(k,l)=='*') then                                       ! If there's a boat in (k,l), see if it's sunk or not
A(k,l)='T'
V=A(k-1:k+1,l-1:l+1)
if (any(V=='*')) then
write (*,*) k,',',l
write (*,*) ''
else
write (*,*) k,',',l
write (*,*) ' Sunk'
write (*,*) ''
end if
write (*,fmt='(10a4)') A

else                                                        ! If there's no boat, check if there's one close
V=A(k-1:k+1,l-1:l+1)
if (any(V=='*')) then
write (*,*) k,',',l
write (*,*) 'Almost'
write (*,*) ''
A(k,l)='A'
write (*,fmt='(10a4)') A

else                                                        ! If no boats close, shot missed
write (*,*) k,',',l
write (*,*) ' Water '
write (*,*) ''
A(k,l)='W'
write (*,fmt='(10a4)') A                                ! This shows the whole matrix each time you shoot
end if
end if

else
exit
end if

end do

write (*,*) ' '
write (*,*) ' Congrats, you won the game! '
write (*,*) ' '
write (*,*) ' Press enter to play again'

end do
end program 

RE: Battleship like fortran code

What happens if you change this

CODE

if (A(k,l)=='*') then                                       ! If there's a boat in (k,l), see if it's sunk or not
A(k,l)='T'
V=A(k-1:k+1,l-1:l+1)
if (any(V=='*')) then 
to this?

CODE

if (A(k,l)=='*') then                                       ! If there's a boat in (k,l), see if it's sunk or not
A(k,l)='T'
! V=A(k-1:k+1,l-1:l+1)
if (any(A=='*')) then 

I wish you had posted a sample input file, too, that way I could easily do some testing...right now, I am too lazy to figure out how in the world your 'tablero.dat' file is supposed to look like...it seems way too complicated...I thought it would simply look like:

CODE

OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOO*OOOO
OOOOO*OOOO
OOOOO*OOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO 
but apparently not.

RE: Battleship like fortran code

I am the office now and can't get to the your file (untrusted site according to company proxy).

Say, have you tried my proposed change? Let me know.

RE: Battleship like fortran code

(OP)
What you told me only works if there's just one ship, but if there's more, it keeps saying just hit, not sunk.

RE: Battleship like fortran code

Ah!....from your code, I had gathered that you were playing with only 1 ship...I did not see anything that looked like it would work for more than one.

For example, if all ships use the same marker '*'...how do you tell them apart? For example, if I place the 2-long and 3-long ships at right angles like this:

CODE

OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOO223OOOO
OOOOO3OOOO
OOOOO3OOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO 
It looks like your input matrix would look like this:

CODE

OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOO***OOOO
OOOOO*OOOO
OOOOO*OOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO 
and so, much infomation is lost to the point that you don't know what is what.

So, maybe, you need a different strategy...because the game happens to have 2 ships that are 3-long (a ship and a submarine, I believe), maybe numbers are not the best choice, but letters:

CODE

OOOOOOOOOD
OOOOOOOOOD
OOOOOOOOOD
OOOAABOOOD
OOOOOBOOOD
OOOOOBOOOO
OOOOOOOOOO
OOOOOOOOOO
OCCCOOOOOO 

Then, in your game, depending on what letter the hit in the first place, then you test to see if there are more of just those left.

As far as giving a hint of being close to a ship, maybe instead of checking for '*' you could check for any with an ascii value less than 'O'.

RE: Battleship like fortran code

(OP)
I understand what you say, but in battleship, ships can't be diagonal or touching each other, so there wouldn't br any problem. The thing is that if you have one million ships, you don't have all those letters in the alphabet, that's why I used *.

The idea I had on my mind was to make it go all the way in the line and column of the matrix the place you shot, and keep doing that until you discover something different of * or T. At that point, it stops and it sees if all the spots in between are hit or not.

I'm a bit stuck with that, and I don't know how to write and make the computer understand that's what I want him to.

Once again, sorry for my English :D

RE: Battleship like fortran code

million ships? well, is it Battleship Game or not! Focus on the game, for now; the day you need deal with a million ships it is going to be a different program and you are not going to be using a matrix with 'O' and '*' to input ship coordinates.

By the way, I don't see the rule about the ships not being allowed to touch; needless to say, they are not allowed to overlap or intersect, but as far as I can tell, they can be right next to each other, i.e., occupying adjacent cells.

On a second thought, you could make your game scale-able if you use an INTEGER matrix instead of your a CHARACTER and then you can use numbers to input ship data...then, you can go to a million! All you would have to do is input the data with 'cell' data separated by a space, for example, this is the start of a matrix that could be used to input up to 999 different ship IDs and still maintain a nice text alignment:
[cdoe]
0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 24 24 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 12 0 0 0 0 0 0 999 0
0 12 0 0 0 0 0 0 999 0
0 12 0 0 0 0 0 0 999 0
0 0 0 0 0 0 0 0 999 0
[/code]
of course, maintaining alignment is just an obsession of mine and nice for humans to see, but it does not matter and your could even store the information in binary...whatever, you get the picture.

RE: Battleship like fortran code

opps, sorry, typo...

CODE

0   0   1   0   0   0   0   0   0   0
0   0   1   0   0   0   0   0   0   0
0   0   1   0   0   0   0  24  24   0
0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0
0  12   0   0   0   0   0   0 999   0
0  12   0   0   0   0   0   0 999   0
0  12   0   0   0   0   0   0 999   0
0   0   0   0   0   0   0   0 999   0 

RE: Battleship like fortran code

still!...it seems the first line is no honored.

RE: Battleship like fortran code

(OP)
Yeah, that's an easy way to do it, the thing is that I have to do it the other way... And i just can't find no one and no place to seek for help

RE: Battleship like fortran code

Hey, I still can't get to your tablero.dat file, the computer tells me that the site cannot be trusted. Oh, well...judging from the read statements, your file should look like this:

CODE

1   2   3   4   5   6   7   8   9  10

1        O   O   O   O   O   O   O   O   O   O
2        O   O   O   O   O   O   O   O   O   *
3        O   O   *   *   O   O   O   O   O   *
4        O   O   O   O   O   O   O   O   O   *
5        O   O   O   O   O   *   O   O   O   *
6        O   O   O   O   O   *   O   O   O   O
7        O   O   O   O   O   *   O   O   O   O
8        O   O   O   O   O   O   O   O   O   O
9        O   O   O   O   O   O   O   O   O   O
10        O   O   O   O   O   O   O   O   O   O 
If I test the program with this file, the program works just fine....with one little problem...when giving coordinates to the program, you really need to read them from your tablero.dat file, because if you read them from the terminal, from the matrix as displayed by the program, you end up reading the coordinates transposed!

RE: Battleship like fortran code

Hhhhmmm...again, never mind the misaligned first line, it seems that the CODE tags do not really respect the very first line, at least if it does not start with non-blank character.

RE: Battleship like fortran code

(OP)
Try shooting 10,2; then 10,4 and then 10,3... it's not working for ships larger than 2

RE: Battleship like fortran code

Like I said, according to me, you got the coordinates transposed!

Just to make sure we are in the same page, matrix indexes are (row, column)

The matrix that I have posted above shows a 4-unit long ship in (2,10), (3,10), (4,10), (5,10) and I can play the game and sink it! Using THOSE coordinates.

The problem is that the way your program displays the matrix back to the terminal is in a transposed manner since you are NOT explicitly using indexes and when you read THAT matrix, you get the wrong coordinates.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!