• Print

Author Topic: Battleship with AI  (Read 213 times)

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Battleship with AI
« on: April 25, 2018, 07:43:29 am »
Yep! Petr's game got me curious about a QB64 version except I did not want to play with Lan or another person. Neither am I profecient in Petr's language to understand his code. So I built my own version of Battleship from scratch with a code start very much like I posted here for Prithak (and any one else who wants head start with board game)
http://www.qb64.net/forum/index.php?topic=14838.msg128732#msg128732

EDIT: Oh I thought Petr had some background info for game, well here is good start:
https://en.wikipedia.org/wiki/Battleship_game

Here is the source to my first version with AI playing a decent game (finally!) in a zip file (only one source bas).

Here are some screen shots of significant times while getting the AI going, the last (Battleship 1.png) is how the current version looks with some debug data still displayed (kind of interesting to watch the hits on each ship though wouldn't be allowed in real Battleship Game, though marking off sunk ships would be.)

I have a report from a Linux user at TJP that the source is failing to compile in C++, so I need help with feedback from other Linux users, what their results are?
http://qb64.thejoyfulprogrammer.com/showthread.php?tid=1321&pid=6930&rndtime=15246674821112043500#pid6930
Help?
UPDATE: he got it going! yeah!

I am more into getting bare bones code working with AI than fancy graphics and sound effects so feel free to offer your mods for that if you get anything you like going. ;)


Update 2018-04-25 PM: I have updated the zip file with a new improved source file that lists only the ships SUNK.
I have also changed out the snapshot so the latest version is shown (with this date).

Update 2018-04-26: I have updated the zip file again with another improved source PLUS
3 png image tiles
6 wav sound files
1 New Player Instructions.txt file
The source code now has an outer Play Again loop and an auto setup for the Player if don't want to setup ships manually.
Thanks to Johnno at TJP for the assets files and to Petr for comparing versions that gave me the idea for autosetup for Player.
« Last Edit: April 26, 2018, 08:38:07 pm by bplus »
Will you still love me, will you still need me, when I'm (QB) 64?

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #1 on: April 25, 2018, 08:15:14 am »
Dang! My access to Walter's forum, TJP, is not working???

I currently can't reply to Johnno who got Battleship going but found a bug possibly...

It may be possible to sink all computer's ships and if Computer sinks all ships also on very same loop, it will declare itself as winner,

I will have to look into it.

Hi Walter,
Are you updating your forum or am I banned for mentioning getting Help for Linux problems here and at QB64.org?
Will you still love me, will you still need me, when I'm (QB) 64?

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #2 on: April 25, 2018, 08:35:45 am »
Oh looks like Walter is forum pioneering again!  :D

Update: yep! I am not banned (yet!)  ;D 

(Perhaps I hang out at JB forum too much and get paranoid (dang, poor Richard) but I learn so much about code + people!)
« Last Edit: April 25, 2018, 09:03:37 am by bplus »
Will you still love me, will you still need me, when I'm (QB) 64?

Petr

  • Hero Member
  • *****
  • Posts: 642
Re: Battleship with AI
« Reply #3 on: April 25, 2018, 09:13:12 am »
Hi BPlus.  ;D

 Thank you for your interest. The basic rules for this game (as I have programmed my own game as we played it on paper, and as my version of the game generates) are as follows: Each player has 1 ship of 5 pieces straight or vertically, 2 ships of 4 divisions straight or vertically, 3 ships of 3 divisions straight or vertically, 4 vessels of 2 divisions straight or vertically, and 5 vessels of 1 division. For the deployment of ships, they must not touch either the wall or the corner - this control  not in your game yet. It is played in a 10x10 square. It is true that I left the artificial intelligence in my game on the RND (that is, the computer is completely stupid), the only thing I did, was computer shoot not to one place twice. LAN version of my game ..... I can not write anything about it. I just do not know why the data transfer itself is interrupted. I do not understand it at all.
Coding is relax.

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #4 on: April 25, 2018, 09:34:54 am »
Quote from: Petr on April 25, 2018, 09:13:12 am
Hi BPlus.  ;D

 Thank you for your interest. The basic rules for this game (as I have programmed my own game as we played it on paper, and as my version of the game generates) are as follows: Each player has 1 ship of 5 pieces straight or vertically, 2 ships of 4 divisions straight or vertically, 3 ships of 3 divisions straight or vertically, 4 vessels of 2 divisions straight or vertically, and 5 vessels of 1 division. For the deployment of ships, they must not touch either the wall or the corner - this control  not in your game yet. It is played in a 10x10 square. It is true that I left the artificial intelligence in my game on the RND (that is, the computer is completely stupid), the only thing I did, was computer shoot not to one place twice. LAN version of my game ..... I can not write anything about it. I just do not know why the data transfer itself is interrupted. I do not understand it at all.

Oh hey Petr! wow that's allot of ships! 1 + 2 + 3 + 4 + 5 = 15 compare to my 5 = 1 + 1 + 2 + 1 no 1 hit ships! (that's just plain diabolic!!!)  :D

So that's 1*5 + 2*4 + 3*3 + 2*4 + 1*5 = 5 + 8 + 9 + 8 + 5 = 35 hits (you must have great sound effect for blowing up that you want to show off!)

But you don't allow positioning of ships along sides and corners ??? that eliminates allot of blank space 10 + 10 + 9 + 9 = 38 less spaces.

10x10 = 100 - 38 - 35 leaves 27 empty spaces or you random chances of hit are greater than you random chances for a miss!

I thought this a game of search and destroy strategy. With 1 hit ships, you have to bomb every single space to get every last one of the little buggers!  :o
Will you still love me, will you still need me, when I'm (QB) 64?

Petr

  • Hero Member
  • *****
  • Posts: 642
Re: Battleship with AI
« Reply #5 on: April 25, 2018, 11:37:31 am »
Of course, a modification can be made. Perhaps let the player choose the size of the array for boats, the number of boats and their shape and it is possible to allow or prohibit the loading of boats side by side and corner to each other or prohibit it. There are many ways to modify, including shipbuilding, sinking or hitting animation.
To artificial intelligence. In case, that exists spaces between ships and corner of ships, then the computer attempts to fire to the Y + 1 position compared to the previous shot, if it hit boat, continues until it destroy or tries Y - 1 or X + 1 or X - 1. If the enemy's ship's counter is 1 down after the shot, it means that it has sunk a ship of size 1, so it will fire 2 places away, not next to the destroyed point of a ship size 1.
Coding is relax.

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #6 on: April 25, 2018, 11:49:49 am »
Hi Petr,

I found a shot of the toy I learned the game of Battleship, complete with red and white pegs. There were 5 ships and you could park them along the sides.

But yes! of course, we are programmers and we can code this game any old (or new) way we want!!!  ;)
Will you still love me, will you still need me, when I'm (QB) 64?

Petr

  • Hero Member
  • *****
  • Posts: 642
Re: Battleship with AI
« Reply #7 on: April 25, 2018, 12:27:59 pm »
You're right, I just meant the limitation that there should not be a side boat next to another boat, not that it should not be on the edge of the game field.  :)
Coding is relax.

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #8 on: April 25, 2018, 02:53:09 pm »
Ha! Well ships parked side by side is what makes it challenging for an AI!  :D
Will you still love me, will you still need me, when I'm (QB) 64?

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #9 on: April 25, 2018, 06:31:30 pm »
I have updated the zip file and snapshot with a new improved version of Battleship with AI.

This now only lists the ships SUNK and no longer tells which ship is getting hits and where, as that should be part of the guessing game.
Will you still love me, will you still need me, when I'm (QB) 64?

Prithak

  • Jr. Member
  • **
  • Posts: 87
  • Programmer: A machine turns Coffee into Code.
Re: Battleship with AI
« Reply #10 on: April 25, 2018, 08:32:43 pm »
Can anyone please explain this game to me?? I didn't understand it at all!

Thanks,

Prithak :)
Q: 0 is False and 1 is True, right?
A: 1

QBExile

  • Newbie
  • *
  • Posts: 33
Re: Battleship with AI
« Reply #11 on: April 26, 2018, 05:51:07 am »
Quoted from bplus
EDIT: Oh I thought Petr had some background info for game, well here is good start:
Battleship gamehttps://en.wikipedia.org/wiki/Battleship_(game)

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #12 on: April 26, 2018, 06:48:32 am »
Thanks QBExile,
(only don't try the link with () around game, you will get a query because the ) is missing (at least that's how it didn't work for me and my browser).

Update: Ha! well your link does work! ???

BTW How is your game coming? When I was searching the forum, I read a reply from you (I think) that you were working on a Battleship Game too; but I did not find another post related to that.



Hi Prithak and all players new to Battleship,

I have written up an New Player Instruction.txt file that should word wrap OK.
Quote
The object of the game is to sink all the Computer's ships before it sinks all yours.

Both the Player and the Computer are given 5 ships to lay out on a 10x10 grid.

The ships are a straight line of squares (2 to 5 squares) forming a long rectangle.
The ships are laid vertically or horizontally on the 10x10 cell grid without overlap.
Each square must be hit by the opponent in order to sink the ship.

The 5 ships are:
Carrier    - 5 squares to hit
Battleship - 4 squares to hit
Cruiser    - 3 squares to hit
Submarine  - 3 squares to hit
Destroyer  - 2 squares to hit

The game is started by each opponent laying out their ships secretly to the other.
You the Player must setup your ships on the right board.
They are setup in same order I listed above.

So the first ship to set up will be the Carrier that is 5 squares long.
First you will be prompted whether you want to lay it out horizontally or vertically, h or v ?

Then if horizontally is chosen, you click the cell where you want the left most square of the ship to go.
Like wise if vertical v was chosen, you click the board cell where you want the top most square to go.

If there is room on board to lay out all 5 across AND this ship does not overlap another, then the rest of the ship will be drawn in.
(Of course, the first ship can't overlap another but every other ship has that potential.)
If there is not room or the ship would overlap another, then you must start over with the prompt to lay the ship horizontally or vertically...

When you get all 5 of your ships laid out on the 10x10 grid on the left, the shooting match begins!

You will be prompted to click a cell on the left 10x10 board to guess where a Computer's ship might be.
If you hit a square on one of the Computers ships a red dot = hit appears at that cell.
If you miss all the Computers ships, a white dot will appear = miss

The Computer will then take a shot and your board will show a red or white dot according to the Computer's hit or miss.

Then it's your turn again. If you had a hit the last turn you will likely want to find the rest of the ship to sink it.
So click above, below, left or right of the hit = red dot.
A 2nd hit will tell you if the ship is laid out horizontally or vertically.
A 2nd hit would actually sink a Destroyer because it is only 2 squares long.

So you scout around the 10x10 board making random shots (or systematically cover the board with shots)
until you find a ship, sink it and go hunting for the next ship to sink until you get all 5.

Meanwhile the Computer is doing the same thing, so which ever opponent sinks all the ships first, wins!

Oh a caveat!
It is possible to align the ships side by side or one end up next to another ship (as long as they don't overlap).
This makes it confusing as you might be hitting 2 different ships with your shots, so pay close attention to which ship is announced sunk
you might have more hits in the same area than how many it took to sink the ship.

https://en.wikipedia.org/wiki/Battleship_game


Thanks Prithak, I guess I assumed everyone knew this game, my bad. Let me know if this helps get you going. I will include a modified New Player Instruction file with the zip pack, in next update with help from your feedback.
« Last Edit: April 26, 2018, 07:32:29 am by bplus »
Will you still love me, will you still need me, when I'm (QB) 64?

Petr

  • Hero Member
  • *****
  • Posts: 642
Re: Battleship with AI
« Reply #13 on: April 26, 2018, 08:23:39 am »
My version also includes a manual boat insert option, you only need to turn it on in the / setup menu. (Turn off the autogenerator item in the setup menu), in the basic version the automatic generator is set. When inserting, click once on the desired ship, rotate right-clicking it. Move to position, left-click, place the ship. If you put them wrong and do not fit all the ships on the map, press R to reset the map (in my version). It has both possibilities and maintains gaps between ships as well as in manual insertion mode. The rules are the same, the number of boats is different, as I write above with BPlus.

I thank Bplus for writing the rules. On the left is the window (map) with your boats, the blue is the sea, the red hit areas, the right is the map with the boats of the computer. - I write about my version again.

Coding is relax.

bplus

  • Hero Member
  • *****
  • Posts: 736
  • B = B + _
Re: Battleship with AI
« Reply #14 on: April 26, 2018, 11:34:48 am »
Hi Petr,

I like your ideas for positioning and repositioning "boats" (I am using "ships" ;) ), it gives me ideas for new versions. In mine, if you change your mind about a boat/ship, tough luck, wait for next game... (for the moment).

So your auto-generator, automatically sets up boats/ships for the player, another good idea!

I also am debating Player's board (with his boats/ships) on left and the hits tracking on the right.
Will you still love me, will you still need me, when I'm (QB) 64?

  • Print