int main() { printf("%s\n","BattleShip!"); player_score=0; computer_score=0; srand(time(0)); inut_hitmap(); inut_map(); inut_buffer(); place_ships(); while(1) { show_map(); shoot();//defult shooting algerithm just shoots at random coordnets; really sucks as a player gethit(); if(win()>1 || ships_left()<=0) break; } if(computer_score>player_score) printf("%s\n","computer wins"); else if(computer_score<player_score) printf("%s\n","player wins"); else if(computer_score==player_score) printf("%s\n","tie, howd that happen"); }
/* Functions */ int main(int argc, char *argv[] ) { FILE *rules, *map; /* Pointers to rules/map file streams */ Grid board; /* Store info about the game board */ Ship ships[MAX_SHIPS]; /* Store info about each ship */ int **answer; /* Store the solution of the game */ unsigned int numShips = 0; /* Store the total numberof ships */ unsigned int xGuess, yGuess;/* Store the player's current guess */ /* Temp Variables */ int status = 0; /* exit status */ int i = 0, j = 0; /* loop indeces */ /* There must be at least three parameters */ if(argc < 3) { return params_missing(); } /* Save rules file into "rules", it's OK if "standard.rules" DNE * Rules file must be readable unless it is standard.rules */ if((rules = fopen(argv[1], "r")) == NULL) { if(!strcmp(argv[1], "standard.rules")) { fclose(rules); /* Looking for "standard.rules" but file DNE so create it * with the default contents */ rules = fopen(argv[1], "w"); fprintf(rules, "8 8\n5\n5\n4\n3\n2\n1\n\n"); fclose(rules); rules = fopen(argv[1], "r"); } else { return rules_missing(); } fclose(rules); } /* Save map file into "map" * Map file must be readable */ if((map = fopen(argv[2], "r")) == NULL ) { fclose(map); return maps_missing(); } /* Save and check rules file * Rules must conform to specification */ if((status = parse_rules(rules, ships, &board, &numShips))) { return status; } /* Initialize the answer array */ answer = (int**)malloc(board.width * sizeof(int*)); for(i = 0; i < board.width; i++) { answer[i] = (int*)malloc(board.height * sizeof(int)); for(j = 0; j < board.height; j++) { answer[i][j] = 1; } } /* Save and check map file * Map must conform to specification */ if((status = parse_map(map, ships, &board, &numShips))) { return status; } /* Populate the solution */ if((status = place_ships(ships, numShips, answer))) { return status; } /* Interaction Loop */ for(;;) { display_board(board.width, board.height, answer); display_prompt(); switch(get_prompt(&xGuess, &yGuess)) { case 0: make_guess(&xGuess, &yGuess, &board, answer); break; case 1: printf("Bad guess\n"); break; case -1: return bad_guess(); } } return 0; }