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);
}
| |