Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Battleship like fortran code

Status
Not open for further replies.

guillemc23

Technical User
Nov 18, 2013
6
ES
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:
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')

read(10,fmt='(7x,10i4)')(i, i=1,10)
read (10,*)
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? '
    read(*,*) k,l

            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 (*,*) 'Tocado'
                    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'
  read (*,*)
  
end do
end program
 
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.




 
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.
 
What you told me only works if there's just one ship, but if there's more, it keeps saying just hit, not sunk.

 
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'.

 
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
 
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.

 
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
 
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
 
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!


 
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.
 
Try shooting 10,2; then 10,4 and then 10,3... it's not working for ships larger than 2
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top