Author Topic: Battleship with AI  (Read 14507 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Battleship with AI
« on: April 25, 2018, 03:39:39 am »
Some background: https://en.wikipedia.org/wiki/Battleship_(game)

The game with some room for improvement with AI but still can play game well enough.

Some snaps: My first game with AI, the first time it beat me and current version with some debug info still showing

Update 2018-04-25 PM a new zip file replaces the old with code change of displaying only the ships SUNK.
The last screen shot is of the current code.

Update 2018-04-26 a new zip file again replaces the old code with the following:
New bas source file that now uses an outer Play Again loop and an Auto-setup for the Player for quick start to shooting Game.
Now using 3 png image files for tiles and 6 wav sound files for dramatic effect. Plus the New Player Instruction.txt file as promised.
My First Battleship Game with AI.PNG
* My First Battleship Game with AI.PNG (Filesize: 43.92 KB, Dimensions: 973x729, Views: 663)
Battleship AI beats its creator!.PNG
* Battleship AI beats its creator!.PNG (Filesize: 38.8 KB, Dimensions: 945x738, Views: 620)
Battleship 2018-04-25PM.PNG
* Battleship 2018-04-25PM.PNG (Filesize: 20.87 KB, Dimensions: 797x629, Views: 559)
Battleship w Johnno's assets.PNG
* Battleship w Johnno's assets.PNG (Filesize: 46.14 KB, Dimensions: 798x619, Views: 516)
* Battleship 2018-04-26.zip (Filesize: 1.38 MB, Downloads: 378)
« Last Edit: April 27, 2018, 12:11:02 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #1 on: April 25, 2018, 02:59:07 pm »
OK next update of this game, I am definitely only going to show what ships are sunk.

All the hits on each ship (immediate feedback) is not legal intelligence and the AI is not using it.

There is a sort of referee HitEval sub that "announces when a ship is sunk" to each party and it is the part of the code that is tracking the hits on the ships.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #2 on: April 25, 2018, 09:42:56 pm »
And now, the original post has been modified with new zip and screen shot with the changes explained above.

FellippeHeitor

  • Guest
Re: Battleship with AI
« Reply #3 on: April 26, 2018, 09:47:18 am »
Fun to play, bplus! Good job once again.
battleship.png
* battleship.png (Filesize: 23.87 KB, Dimensions: 806x632, Views: 552)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #4 on: April 26, 2018, 10:07:49 am »
Thanks Fellippe!

I have written up a New Player Instructions.txt file that should word wrap OK:

(Trying a code box instead of a quote box here)
Code: QB64: [Select]
  1. The object of the game is to sink all the Computer's ships before it sinks all yours.
  2.  
  3. Both the Player and the Computer are given 5 ships to lay out on a 10x10 grid.
  4.  
  5. The ships are a straight line of squares (2 to 5 squares) forming a long rectangle.
  6. The ships are laid vertically or horizontally on the 10x10 cell grid without overlap.
  7. Each square must be hit by the opponent in order to sink the ship.
  8.  
  9. The 5 ships are:
  10. Carrier    - 5 squares to hit
  11. Battleship - 4 squares to hit
  12. Cruiser    - 3 squares to hit
  13. Submarine  - 3 squares to hit
  14. Destroyer  - 2 squares to hit
  15.  
  16. The game is started by each opponent laying out their ships secretly to the other.
  17. You the Player must setup your ships on the right board.
  18. They are setup in same order I listed above.
  19.  
  20. So the first ship to set up will be the Carrier that is 5 squares long.
  21. First you will be prompted whether you want to lay it out horizontally or vertically, h or v ?
  22.  
  23. Then if horizontally is chosen, you click the cell where you want the left most square of the ship to go.
  24. Like wise if vertical v was chosen, you click the board cell where you want the top most square to go.
  25.  
  26. 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.
  27. (Of course, the first ship can't overlap another but every other ship has that potential.)
  28. 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...
  29.  
  30. When you get all 5 of your ships laid out on the 10x10 grid on the left, the shooting match begins!
  31.  
  32. You will be prompted to click a cell on the left 10x10 board to guess where a Computer's ship might be.
  33. If you hit a square on one of the Computers ships a red dot = hit appears at that cell.
  34. If you miss all the Computers ships, a white dot will appear = miss
  35.  
  36. 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.
  37.  
  38. 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.
  39. So click above, below, left or right of the hit = red dot.
  40. A 2nd hit will tell you if the ship is laid out horizontally or vertically.
  41. A 2nd hit would actually sink a Destroyer because it is only 2 squares long.
  42.  
  43. So you scout around the 10x10 board making random shots (or systematically cover the board with shots)
  44. until you find a ship, sink it and go hunting for the next ship to sink until you get all 5.
  45.  
  46. Meanwhile the Computer is doing the same thing, so which ever opponent sinks all the ships first, wins!
  47.  
  48. Oh a caveat!
  49. 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).
  50. 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
  51. you might have more hits in the same area than how many it took to sink the ship.
  52.  
  53. https://en.wikipedia.org/wiki/Battleship_game
  54.  
  55.  

I hope to include a file similar to this based on feedback I get from comments new player or old,
so your opinions would be appreciated. Thanks

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #5 on: April 27, 2018, 12:14:00 am »
The original post has been updated with newest source code, sound and image files as well as the New Player Instruction.txt file.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #6 on: May 14, 2018, 12:57:26 am »
Battleship 5-14 Update
Battleship 5-14.PNG
* Battleship 5-14.PNG (Filesize: 185.93 KB, Dimensions: 792x620, Views: 558)
* Battleship 5-14.zip (Filesize: 2.11 MB, Downloads: 343)

FellippeHeitor

  • Guest
Re: Battleship with AI
« Reply #7 on: May 14, 2018, 12:59:04 am »
Wow, this seems like it's come a long way! Haven't played this update yet but it sure looks good.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #8 on: May 14, 2018, 01:01:04 am »
Thanks lot's of help with assets from Johnno.

Have fun!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Battleship with AI
« Reply #9 on: May 14, 2018, 01:54:04 am »
Nice work bplus! I like the game!
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #10 on: May 15, 2018, 09:58:32 am »
Studying systematic board coverage of "random" shots for smarter AI.

Code: QB64: [Select]
  1. CONST gsq = 40
  2. CONST gox = 200
  3. CONST goy = 100
  4. CONST nsq = 10
  5. CONST n1 = nsq - 1
  6. CONST xmax = 800
  7. CONST ymax = 600
  8. DIM SHARED rndList(n1)
  9.  
  10. main& = _NEWIMAGE(xmax, ymax, 32)
  11. SCREEN main&
  12. _SCREENMOVE 360, 60
  13.  
  14.  
  15. drawGrid gox, goy, gsq, nsq
  16. FOR modulus = 5 TO 2 STEP -1
  17.     c$ = LTRIM$(STR$(modulus))
  18.     COLOR RGB(c$ + c$ + c$)
  19.  
  20.     FOR y = 0 TO n1
  21.         offset = y MOD modulus
  22.         maxM = INT((n1 - offset) / modulus)
  23.         getRndChoice y, modulus
  24.         FOR xindex = 0 TO maxM
  25.             col = rndList(xindex)
  26.             LINE (gox + col * gsq + 3 * (7 - modulus), goy + y * gsq + 3 * (7 - modulus))-STEP(gsq - 6 * (7 - modulus), gsq - 6 * (7 - modulus)), , BF
  27.             '_DELAY 1
  28.         NEXT
  29.     NEXT
  30.     _DELAY 2
  31.  
  32. SUB getRndChoice (rc, modulus)
  33.     offset = rc MOD modulus
  34.     maxM = INT((n1 - offset) / modulus)
  35.     FOR i = 0 TO n1
  36.         rndList(i) = i
  37.     NEXT
  38.     FOR i = 0 TO maxM
  39.         n = offset + i * modulus
  40.         SWAP rndList(i), rndList(n)
  41.     NEXT
  42.     FOR i = maxM TO 1 STEP -1
  43.         r = INT((i + 1) * RND)
  44.         SWAP rndList(i), rndList(r)
  45.     NEXT
  46.     FOR i = maxM + 1 TO n1
  47.         r = INT((n1 - maxM) * RND) + maxM + 1
  48.         SWAP rndList(i), rndList(r)
  49.     NEXT
  50.  
  51. SUB drawGrid (x, y, sq, n)
  52.     d = sq * n
  53.     FOR i = 0 TO n
  54.         LINE (x + sq * i, y)-(x + sq * i, y + d)
  55.         LINE (x, y + sq * i)-(x + d, y + sq * i)
  56.     NEXT
  57.  
  58. FUNCTION RGB& (s3$) ' New Color System 1000 colors with 3 digits!!!!!!!!!!!!!!!!
  59.     l = LEN(s3$)
  60.     IF l THEN r = 28 * VAL(MID$(s3$, 1, 1)) + 3
  61.     IF l >= 2 THEN g = 28 * VAL(MID$(s3$, 2, 1)) + 3
  62.     IF l >= 3 THEN b = 28 * VAL(MID$(s3$, 3, 1)) + 3
  63.     RGB& = _RGB32(r, g, b)
  64.  
  65.  

Sometimes studies make pretty pictures.
getRandomChoice.PNG
* getRandomChoice.PNG (Filesize: 20.95 KB, Dimensions: 478x473, Views: 542)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #11 on: May 15, 2018, 01:11:58 pm »
I haven't done the math yet but I am testing a modulus 3 coverage of board. It seems to find the Destroyer faster with less shots.

With modulus 2 coverage, you are guaranteed to find all ships including Destroyer in 50 shots or less. With modulus 3 coverage you will find all ships in 34 shots guaranteed, except Destroyer, which could fall between the cracks so to speak and take up to 98 shots to find (edit: even 99!).

So what are odds of going big with mod 3 coverage and winning or playing it safe with mod 2 coverage?

Maybe this code will give you a feel for determining if the risk of M = 3 coverage and possibly needing more than 50 shots is worth it or not.

Code: QB64: [Select]
  1. _TITLE "m3 34 m2.bas 2018-05-15,  press any key to quit..."
  2.  
  3. 'in 34 shots (m = 3) I should hit all the ships length 3 and up
  4. 'and if I haven't hit destroyer yet shift to every other square m = 2
  5.  
  6. CONST gsq = 40
  7. CONST gox = 200
  8. CONST goy = 100
  9. CONST nsq = 10
  10. CONST n1 = nsq - 1
  11. CONST xmax = 800
  12. CONST ymax = 600
  13. DIM SHARED rndList(n1)
  14. DIM hits(n1, n1)
  15. main& = _NEWIMAGE(xmax, ymax, 32)
  16. SCREEN main&
  17. _SCREENMOVE 360, 60
  18.  
  19. WHILE LEN(k$) = 0
  20.     CLS
  21.     ERASE hits
  22.     drawGrid gox, goy, gsq, nsq
  23.     shotCnt = 0
  24.     Sunk = 0
  25.     dx = rand(0, n1 - 1)
  26.     dx2 = dx + 1
  27.     dy = rand(0, n1)
  28.  
  29.     FOR i = 1 TO 10
  30.         IF i MOD 2 THEN COLOR RGB("900") ELSE COLOR RGB("999")
  31.         LINE (gox + dx * gsq + 2 * i, goy + dy * gsq + 2 * i)-STEP(2 * gsq - 4 * i, gsq - 4 * i), , BF
  32.     NEXT
  33.     WHILE LEN(k$) = 0
  34.         k$ = INKEY$
  35.         y = y + 1
  36.         IF y = nsq THEN y = 0
  37.  
  38.         IF shotCnt < 34 THEN
  39.             COLOR RGB("009")
  40.             modulus = 3
  41.             offset = y MOD modulus
  42.             maxM = INT((n1 - offset) / modulus)
  43.         ELSE
  44.             IF Sunk = 0 THEN modulus = 2: COLOR RGB("990") ELSE modulus = 3: COLOR RGB("099")
  45.             maxM = n1
  46.         END IF
  47.  
  48.         xindex = 0
  49.         getRndChoice y, modulus
  50.         testx = rndList(xindex)
  51.         shoot = 1
  52.         WHILE hits(testx, y) <> 0
  53.             xindex = xindex + 1
  54.             IF xindex > maxM THEN shoot = 0: EXIT WHILE
  55.             testx = rndList(xindex)
  56.         WEND
  57.         IF shoot <> 0 THEN
  58.             col = testx
  59.             LINE (gox + col * gsq + 3 * (7 - modulus), goy + y * gsq + 3 * (7 - modulus))-STEP(gsq - 6 * (7 - modulus), gsq - 6 * (7 - modulus)), , BF
  60.             hits(col, y) = 1
  61.             shotCnt = shotCnt + 1
  62.             IF Sunk = 0 THEN
  63.                 IF (col = dx OR col = dx2) AND y = dy THEN Sunk = 1: LOCATE 1, 1: PRINT "Destroyer Hit! in"; STR$(shotCnt); " shots."
  64.             END IF
  65.             IF Sunk THEN _DELAY .01 ELSE _DELAY .25
  66.         END IF
  67.         IF shotCnt >= 100 THEN _DELAY 5: EXIT WHILE
  68.     WEND
  69. SUB getRndChoice (rc, modulus)
  70.     offset = rc MOD modulus
  71.     maxM = INT((n1 - offset) / modulus)
  72.     FOR i = 0 TO n1
  73.         rndList(i) = i
  74.     NEXT
  75.     FOR i = 0 TO maxM
  76.         n = offset + i * modulus
  77.         SWAP rndList(i), rndList(n)
  78.     NEXT
  79.     FOR i = maxM TO 1 STEP -1
  80.         r = INT((i + 1) * RND)
  81.         SWAP rndList(i), rndList(r)
  82.     NEXT
  83.     FOR i = maxM + 1 TO n1
  84.         r = INT((n1 - maxM) * RND) + maxM + 1
  85.         SWAP rndList(i), rndList(r)
  86.     NEXT
  87.  
  88. SUB drawGrid (x, y, sq, n)
  89.     d = sq * n
  90.     FOR i = 0 TO n
  91.         LINE (x + sq * i, y)-(x + sq * i, y + d)
  92.         LINE (x, y + sq * i)-(x + d, y + sq * i)
  93.     NEXT
  94.  
  95. FUNCTION RGB& (s3$) ' New Color System 1000 colors with 3 digits!!!!!!!!!!!!!!!!
  96.     l = LEN(s3$)
  97.     IF l THEN r = 28 * VAL(MID$(s3$, 1, 1)) + 3
  98.     IF l >= 2 THEN g = 28 * VAL(MID$(s3$, 2, 1)) + 3
  99.     IF l >= 3 THEN b = 28 * VAL(MID$(s3$, 3, 1)) + 3
  100.     RGB& = _RGB32(r, g, b)
  101.  
  102. FUNCTION rand% (lo%, hi%)
  103.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  104.  
  105.  

So how is your math or probability intuition? Would you go mod 3 or mod 2?
m3 34 m2  fast find.PNG
* m3 34 m2 fast find.PNG (Filesize: 21.57 KB, Dimensions: 710x585, Views: 550)
m3 34 m2 m3 missed but still found under 50 shots.PNG
* m3 34 m2 m3 missed but still found under 50 shots.PNG (Filesize: 22.45 KB, Dimensions: 699x608, Views: 558)
m3 34 m2 a rare very bad slow find.PNG
* m3 34 m2 a rare very bad slow find.PNG (Filesize: 22.69 KB, Dimensions: 650x579, Views: 556)
« Last Edit: May 15, 2018, 01:15:06 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #12 on: May 15, 2018, 05:42:23 pm »
Upon second thought, if first 34 shots miss Destroyer can catch in next 30 for sure, don't know why I would switch to mod 2 after 34 shots in mod 3 covers the board. 30 = 3 more shots * 10 rows.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #13 on: May 18, 2018, 11:24:04 pm »
Wednesday, I come up with a really neat little function I am calling cover(modulus, bump, x, y) which returns TF according to whether the square is part a modulus plan to systematically cover board in every m squares.

Then I proceeded to make one blunder after another for 2 days finally getting the thing to work like this!
Code: QB64: [Select]
  1. _TITLE "shoot test,  press any to quit..."
  2.  
  3. 'first get a function to return ture/false to shoot spot x, y if it is part of coverage or not
  4. 'DONE and what a cool function it is too!!!
  5.  
  6. '2018-05-18 It's working!!! time to test in real app
  7.  
  8. CONST gsq = 40
  9. CONST gox = 200
  10. CONST goy = 100
  11. CONST sqPerSide = 10
  12. CONST n1 = sqPerSide - 1
  13. CONST xmax = 800
  14. CONST ymax = 600
  15. DIM SHARED sunk, row, col, lastm, colA, bump
  16. DIM SHARED hits(n1, n1)
  17. main& = _NEWIMAGE(xmax, ymax, 32)
  18. SCREEN main&
  19. _SCREENMOVE 360, 60
  20.  
  21. k$ = ""
  22. restart:
  23. shotCnt = 0: sunk = 0
  24. ERASE hits
  25. colA = -1 '<<<<<<<<<<<<<<< don't forget to signal start!
  26. 'draw grid
  27. rgb 999
  28. drawGrid gox, goy, gsq, sqPerSide
  29.  
  30. ''destroyer target
  31. dx = rand(0, n1 - 1)
  32. dx2 = dx + 1
  33. dy = rand(0, n1)
  34. 'draw the Destroyer
  35. FOR i = 1 TO 10
  36.     IF i MOD 2 THEN rgb 900 ELSE rgb 999
  37.     LINE (gox + dx * gsq + 2 * i, goy + dy * gsq + 2 * i)-STEP(2 * gsq - 4 * i, gsq - 4 * i), , BF
  38.  
  39. 'start shooting
  40. WHILE LEN(k$) = 0
  41.     k$ = INKEY$
  42.     shoot sx, sy
  43.  
  44.     'accounting
  45.     hits(sx, sy) = 1
  46.     shotCnt = shotCnt + 1
  47.  
  48.     'show shot
  49.     IF lastm = 2 THEN rgb 990 ELSE rgb 9
  50.  
  51.     LINE (gox + sx * gsq, goy + sy * gsq)-STEP(gsq, gsq), , BF
  52.  
  53.     'hit target first time?
  54.     IF sunk = 0 THEN
  55.         IF (sx = dx OR sx = dx2) AND sy = dy THEN
  56.             sunk = 1: rgb 999: LOCATE 1, 1: PRINT "Destroyer Hit! in"; STR$(shotCnt); " shots."
  57.         END IF
  58.         _DELAY .02
  59.     ELSE
  60.         _DELAY .1
  61.     END IF
  62.  
  63.     IF shotCnt = 100 THEN EXIT WHILE
  64. GOTO restart
  65.  
  66. 'first test cover  COMMENT OUT ABOVE CODE AND UNCOMMENT following for cool graphic!
  67. 'm = 3
  68. 'WHILE 1
  69. '    FOR bump = 0 TO m - 1
  70. '        CLS
  71. '        FOR y = 0 TO n1
  72. '            FOR x = 0 TO n1
  73. '                FOR mm = m TO 2 STEP -1
  74. '                    SELECT CASE mm
  75. '                        CASE 8: rgb 9
  76. '                        CASE 7: rgb 90
  77. '                        CASE 6: rgb 900
  78. '                        CASE 5: rgb 99
  79. '                        CASE 4: rgb 990
  80. '                        CASE 3: rgb 909
  81. '                        CASE 2: rgb 999
  82. '                    END SELECT
  83. '                    IF cover(mm, bump, x, y) THEN
  84. '                        LINE (gox + x * gsq + 1, goy + y * gsq + 1)-STEP(gsq - 2, gsq - 2), , BF
  85. '                    END IF
  86. '                NEXT
  87. '            NEXT
  88. '        NEXT
  89. '        _DISPLAY
  90. '        _LIMIT 1
  91. '    NEXT
  92. 'WEND
  93.  
  94. SUB drawGrid (x, y, sq, n)
  95.     d = sq * n
  96.     FOR i = 0 TO n
  97.         LINE (x + sq * i, y)-(x + sq * i, y + d)
  98.         LINE (x, y + sq * i)-(x + d, y + sq * i)
  99.     NEXT
  100.  
  101. SUB rgb (n) ' New (even less typing!) New Color System 1000 colors with up to 3 digits
  102.     s3$ = RIGHT$("000" + LTRIM$(STR$(n)), 3)
  103.     r = VAL(MID$(s3$, 1, 1)): IF r THEN r = 28 * r + 3
  104.     g = VAL(MID$(s3$, 2, 1)): IF g THEN g = 28 * g + 3
  105.     b = VAL(MID$(s3$, 3, 1)): IF b THEN b = 28 * b + 3
  106.     COLOR _RGB32(r, g, b)
  107.  
  108. FUNCTION rand% (lo%, hi%)
  109.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  110.  
  111. SUB shoot (col, row) 'col, row aren't inputs so mush as outputs like a double function return wo input parameters
  112.  
  113.     'i = nShips
  114.     'WHILE shipSunk(i) 'find smallest ship not sunk
  115.     '    i = i - 1
  116.     'WEND
  117.     'SELECT CASE i 'm for modulus, d for direction to run a check from
  118.     '    CASE nShips: m = 2 'still have destroyer
  119.     '    CASE nShips - 1: m = 3 'still have sub
  120.     '    CASE nShips - 2: m = 3 'still have cruiser
  121.     '    CASE nShips - 3: m = 4 'still have battleship
  122.     '    CASE nShips - 4: m = 5 'still have carrier
  123.     '    CASE ELSE: TxtBx "990", "m", "Aren't all the Player's ships sunk?": m = 7
  124.     'END SELECT
  125.     IF lastm = 7 THEN EXIT SUB
  126.     IF sunk THEN m = 3 ELSE m = 2
  127.     'IF m <> lastm THEN
  128.     lastm = m 'lastm  is solely needed for coloring in main
  129.     bc = 0 'if bc is reset to 0 each time don't need it shared
  130.     'END IF
  131.  
  132.     IF colA = -1 THEN 'col the Attact starts from notice it is random so player can't anticipate
  133.         colA = rand%(0, n1): col = colA: row = rand(0, n1): bump = rand(0, m - 1): bc = 0
  134.     END IF
  135.  
  136.     'm 7 is just in case some error occurs have fall back
  137.     IF m <> 7 THEN
  138.  
  139.         WHILE bc < m
  140.             cc = 1
  141.             WHILE cc <= sqPerSide
  142.                 rc = 0
  143.                 WHILE rc <= sqPerSide 'find a space to hit if one left in this column
  144.                     IF cover(m, bump, col, row) THEN 'are we on a place to cover board
  145.                         IF hits(col, row) = 0 THEN EXIT SUB 'good to go!
  146.                     END IF
  147.                     row = (row + 1) MOD sqPerSide
  148.                     rc = rc + 1
  149.                 WEND
  150.                 row = row - 1
  151.                 IF row < 0 THEN row = n1
  152.                 'still here means we checked all rows in col so check next col
  153.                 col = (col + 1) MOD sqPerSide
  154.                 cc = cc + 1
  155.             WEND
  156.             'still here ? then up the bump
  157.             bump = (bump + 1) MOD m
  158.             bc = bc + 1
  159.         WEND
  160.         m = 7 'error fall through
  161.  
  162.  
  163.     END IF 'm<> 7
  164.  
  165.     'rexamine all the hits and check all holes filled around them
  166.     'TO DO
  167.  
  168.     '  if all else fails
  169.     IF m = 7 THEN 'just find a hole not tried yet and exit!
  170.         lastm = 7
  171.         LOCATE 2, 1: PRINT "AI in m=7 mode !!! bc = "; bc
  172.         'FOR row = 0 TO n1
  173.         '    FOR col = 0 TO n1
  174.         '        IF hits(col, row) = 0 THEN EXIT SUB
  175.         '    NEXT
  176.         'NEXT
  177.     END IF
  178. END SUB '  128 lines to take a random shot!!!!
  179.  
  180. 'using a modulus m coverage with a bump so that opponent can't predict where
  181. 'the hardest place to plant the Detroyer will be
  182. FUNCTION cover (m, bump, c, r)
  183.     bm = bump MOD m 'make sure bump is in modulus
  184.     cm = (c + bm) MOD m
  185.     rm = r MOD m
  186.     IF rm = cm THEN cover = -1 ELSE cover = 0
  187.  
  188. 'turns out I did not need this but it should be noted:
  189. ' the cnt is different according to bump if modulus is odd
  190. 'amd maybe if nSquare is odd and modulus is even
  191. 'how many (non repeating) shots to cover a square n X n
  192. FUNCTION coverageCnt (nSquare, modulus, bump)
  193.     FOR y = 0 TO nSquare - 1
  194.         FOR x = 0 TO nSquare - 1
  195.             IF cover(modulus, bump, x, y) THEN cnt = cnt + 1
  196.         NEXT
  197.     NEXT
  198.     coverageCnt = cnt
  199.  
  200.  

Now I can systematically cover the whole board in a minimum of shots according to smallest ship not sunk yet.
It can even cover the whole board if the AI doesn't know what to do with a hit.

The new rgb function now takes even less letters to command 1 of 1000 colors, 7 letters or digits at most.

Quote
PS OK the code seems to be working fine in Battleship game, I can get rid of all that preplanned shooting junk!

Next, a smarter system for sinking ships once they've been hit.

What I like about this AI is that it plays like a merciless machine! systematically covering the board with a minimum of shots needed to find every ship. It has the personality of the terminator.
« Last Edit: May 18, 2018, 11:49:12 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Battleship with AI
« Reply #14 on: May 28, 2018, 08:14:32 am »
Here is latest update of Battleship with the improvements to the AI play.

I am interested to know if anyone runs into any errors specially #5 specially using Linux.

Just click first screen to get fire started:
splash1.PNG
* splash1.PNG (Filesize: 118.59 KB, Dimensions: 802x620, Views: 491)
splash2.PNG
* splash2.PNG (Filesize: 1.07 MB, Dimensions: 798x618, Views: 503)
5-AI 2018-05-24.PNG
* 5-AI 2018-05-24.PNG (Filesize: 179.49 KB, Dimensions: 784x615, Views: 530)
* Battleship 5-AI 2018-05-24.zip (Filesize: 1.56 MB, Downloads: 368)