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 69 70 71 72 73
|
// Every student will create their own file to contain the Battle Ship
// functions. A basic skeleton of this file follows.
// NOTE: if you wish to use the rand() function, do NOT call srand()--it is
// already being called by the main() program driver.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "battleship.h"
#include <cmath>
using namespace std;
// Sample skeleton for attack function
// Change the name to reflect your own name
int
Attack(int mode, int opponent)
{
int column, ret;
bool duplicate;
char row;
static char prerow;
static int precol;
int arrayShots[BS_GRID_ROWS][BS_GRID_COLS];
// You will probably need static variables here to keep track of status,
// Such as where you fired upon already, what ships were hit, etc.
switch(mode) {
case BS_MODE_NEW_GAME:
// initialize static data structures
prerow = 'B';
precol = 5;
duplicate = false;
break;
case BS_MODE_CONTINUE_GAME:
// Student must create some algorithm to decide what row and column to fire
// on, then call exactly as follows.
// Search arrayShots for duplicate of row/shot
// Put random generate in while loop that loops while duplicate is found within arrayShots
if (!(ret & BS_SHIP_HIT)){
row = (rand() % 10) + 65;
column = (rand() % 10) + 1;
ret = fire[opponent](row, column);
}
// If you hit the opponent, you must look at the integer return code
// here to help decide where to attack next!
if (ret & BS_SHIP_HIT){
// do something
prerow = row;
precol = column;
if (column < BS_GRID_COLS)
column += 1;
else
column -= 1;
ret = fire[opponent](row, column);
}
break;
}
return 0;
}
// Rename the attack function by replacing lastName above with yours.
// Make sure you replace the function name in the line below as well.
// Then insert your name in the last line.
int (*battleship[MaxPlayerCount])(int, int) = {NULL, Attack};
int (*fire[MaxPlayerCount])(char, int)= {NULL, incomingStub};
char const *playerName[MaxPlayerCount] = {"", ""};
|