Battleship Improvements

Hey guys, I'm supposed to make a sort of battleship version that is 1 player, that ends the game in as lowest moves as possible. So far, i have this as my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
#include <cstdlib>
#include <iostream>
#include "bot.h"
#include "screen.h"

using namespace std;

int ROWS;
int COLS;

int iter = 0;

/* Initialization procedure, called when the game starts:

   init (rows, cols, num, screen, log) 
 
   Arguments:
    rows, cols = the boards size
    num        = the number of ships 
    screen     = a screen to update your knowledge about the game
    log        = a cout-like output stream
*/
void init(int rows, int cols, int num, Screen &screen, ostream &log) 
{
  ROWS = rows;
  COLS = cols;
  log << "Start." << endl;
}


/* The procedure handling each turn of the game:
 
   next_turn(sml, lrg, num, gun, screen, log)
 
   Arguments:
    sml, lrg = the sizes of the smallest and the largest ships that are currently alive
    num      = the number of ships that are currently alive
    gun      = a gun.
               Call gun.shoot(row, col) to shoot: 
                  Can be shot only once per turn. 
                  Returns MISS, HIT, HIT_N_SUNK, ALREADY_HIT, or ALREADY_SHOT.
    screen   = a screen to update your knowledge about the game
    log      = a cout-like output stream
*/
void next_turn(int sml, int lrg, int num, Gun &gun, Screen &screen, ostream &log)
{

  int r = iter / COLS;
  int c = iter % COLS;
  iter += 1;
  
  log << "Smallest: " << sml << " Largest: " << lrg << ". ";
  log << "Shoot at " << r << " " << c << endl;

  result res = gun.shoot(r, c);

  // add result on the screen
  if (res == MISS)
    screen.mark(r, c, 'x', BLUE); 
  else 
    if (res == HIT)
      screen.mark(r, c, '@', GREEN); 
  else 
    if (res == HIT_N_SUNK)
      screen.mark(r, c, 'S', RED); 

}

The code works, however, this code goes by sinking ships one at a time, so it takes like 250+ moves. Would anyone know how i can modify it so that i can shot at random unexplored maps? I was thinking of making a global 2D array to store where i hit and miss, or whenever I hit_n_sunk an entire ship, to make the surrounding squares as a "miss", but I'm not sure how to do that, could anyone help out?
in normal battleship you can put ships touching, unless you prevented that here marking around a dead ship as miss may not be correct.

you are on the right path. a mask of the board to see where you have been and where you have not been is a good start. Then you have all sorts of approaches...

one approach is largest ship seeker. That is, if your largest ship is 5 squares long, for example, you can skip 4 squares and do a grid that is ensured to hit that ship by the time it has run all the shots to cover the grid. It may hit other ships also, but it will for sure get that one.

random shots works also, though it is wasteful.

Random with some smarts is better, for example always put at least 1 space between tries (no ship is smaller than 2) or something.

I have no clue how to write the code for a largest ship seeker, any help to the code would be great!
you have code to iterate over every square, right?

now skip N rows and N columns so that the biggest ship can't hide in there but its not hitting every square. That is all I was saying.

I think, using what you have, that just means

iter+= 3 or 5 or whatever your biggest ship is.

Topic archived. No new replies allowed.