#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#include<time.h>#define BOARDROWS 9#define BOARDCOLS 11
bool dumpComputer = false;//causes dump of computer board before game if true (for debugging)// boards. 0 means empty, non-zero means ship is there, number is size, -tive means hit on a shipint playerBoard[BOARDROWS+1][BOARDCOLS+1];int computerBoard[BOARDROWS+1][BOARDCOLS+1];int compShotBoard[BOARDROWS+1][BOARDCOLS+1];voidshowBoard(int board[BOARDROWS+1][BOARDCOLS+1]);//测试输入数据intgetRand(int lowval,int highval){
return(rand()%(highval+1-lowval)+ lowval);}intgetShot(bool fromUser,int board[BOARDROWS+1][BOARDCOLS+1]){
int row,col;while(1){
if(fromUser){
printf("Give a shot (row, col):");scanf("%d%d",&row,&col);if(col<1|| col > BOARDCOLS || row <1|| row > BOARDROWS)printf("Invalid input\n");else//valid inputbreak;}else{
//computer generated
row=getRand(1,BOARDROWS);
col=getRand(1,BOARDCOLS);printf("computer shot is [%d,%d]",row,col);//debugif(compShotBoard[row][col]==0){
//make sure haven't shot here before
compShotBoard[row][col]=1;//valid shotbreak;}}}//will leave this loop with valid inputif(board[row][col]!=0){
if(board[row][col]>0)
board[row][col]=-1*board