Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Final Project
- Battleship Game
- Eric Schmid
- Zach Williams*/
- #include <stdio.h>
- #include <time.h>
- struct ship{
- char name[20];
- int size;
- };
- void PrintGrid(int array1[][10], int array2[][10]);
- void AssignCompGrid(int array[][10], struct ship *s);
- int IntersectCheck(int array[][10], int n1, int n2, int align, struct ship *s);
- int CharToInt(char letter);
- char IntToChar(int num);
- void AssignPlayerGrid(int array[][10], struct ship *s);
- int main(){
- int i, j, Comp_hide_grid[10][10], Player_grid[10][10], Comp_view_grid[10][10];
- //Zero out the grids for computer and human players
- for(i=0; i<10; i++){
- for(j=0; j<10; j++){
- Comp_hide_grid[i][j] = 0;
- Player_grid[i][j] = 0;
- Comp_view_grid[i][j] = 0;
- }
- }
- //Set up each of the ship types
- struct ship Aircraft_Carrier;
- strcpy(Aircraft_Carrier.name, "Aircraft Carrier");
- Aircraft_Carrier.size = 5;
- struct ship Battleship;
- strcpy(Battleship.name, "Battleship");
- Battleship.size = 4;
- struct ship Cruiser;
- strcpy(Cruiser.name, "Cruiser");
- Cruiser.size = 3;
- struct ship Submarine;
- strcpy(Submarine.name, "Submarine");
- Submarine.size = 3;
- struct ship Destroyer;
- strcpy(Destroyer.name, "Destroyer");
- Destroyer.size = 2;
- struct ship Hit;
- strcpy(Hit.name, "X");
- Hit.size = 1;
- struct ship Miss;
- strcpy(Miss.name, "O");
- Miss.size = 1;
- AssignCompGrid(Comp_hide_grid, &Aircraft_Carrier);
- AssignCompGrid(Comp_hide_grid, &Battleship);
- AssignCompGrid(Comp_hide_grid, &Cruiser);
- AssignCompGrid(Comp_hide_grid, &Submarine);
- AssignCompGrid(Comp_hide_grid, &Destroyer);
- printf("\n");
- for(i=0; i<10; i++){
- for(j=0; j<10; j++){
- printf("%d ", Comp_hide_grid[i][j]);
- }
- printf("\n");
- }
- AssignPlayerGrid(Player_grid, &Aircraft_Carrier);
- AssignPlayerGrid(Player_grid, &Battleship);
- AssignPlayerGrid(Player_grid, &Cruiser);
- AssignPlayerGrid(Player_grid, &Submarine);
- AssignPlayerGrid(Player_grid, &Destroyer);
- for(i=0; i<10; i++){
- for(j=0; j<10; j++){
- printf("%d ", Player_grid[i][j]);
- }
- printf("\n");
- }
- int turn=1, play=1, coord2, compcoord1, compcoord2;
- char coord1, temp[3], coord1char;
- while(play){
- if(turn){
- printf("Your turn: \n");
- printf("Enter the target coordinates: \n");
- scanf("%s", temp);
- coord1 = temp[0];
- coord2 = temp[1]-'0';
- coord1 = CharToInt(coord1);
- if(Comp_hide_grid[coord1][coord2-1] == 1)
- printf("Direct hit!\n");
- else
- printf("Miss!\n");
- printf("\n");
- turn=0;
- }
- else{
- printf("Computer's turn: \n");
- coord1=rand()%10;
- coord2=rand()%10;
- coord1char=IntToChar(coord1);
- printf("The computer fired at point %c%d. \n", coord1char, coord2+1);
- if(Player_grid[coord1][coord2] == 1)
- printf("Direct hit!\n");
- else
- printf("Miss!\n");
- printf("\n");
- turn = 1;
- }
- }
- //PrintGrid(Player_grid[10][10], Comp_view_grid[10]);
- system("PAUSE");
- return 0;
- }
- void AssignCompGrid(int array[][10], struct ship *s){
- int i, n1, n2, align, check=1;
- srand(time(0));
- while(check){
- n1=rand()%10;
- n2=rand()%10;
- align = rand()%2;//test decides whether the ship is placed vertical(1) or horizontal(0), if all other conditions are ok
- if(IntersectCheck(array, n1, n2, align, s)){//check for intersections
- check = 0;
- }
- }
- //printf("%d %d", n1, n2); Disregard this line, i'm working on testing this out.
- if(n1+(s->size)>9 && n2+(s->size)>9){//if initial point is located in the lower right corner
- for(i=0; i<(s->size); i++){
- if(align)
- array[n1][n2-i] = 1;
- else
- array[n1-i][n2] = 1;
- }
- }
- else if(align||n1+(s->size)>9){//If starting point of ship is too close to the bottom, make sure ship is aligned horizontal
- for(i=0; i<(s->size); i++){
- array[n1][n2+i] = 1;
- }
- }
- else if(!align||n2+(s->size)>9){//If starting point too close to the right, make sure ship is vertical
- for(i=0; i<(s->size); i++){
- array[n1+i][n2] = 1;
- }
- }
- }
- void AssignPlayerGrid(int array[][10], struct ship *s){
- int i, j=0, coord2, coord4;
- char coord1, coord3, temp1[3], temp2[3];
- while(j==0){
- printf("Please enter the beginning and ending coordinates of your %s: \n", (s->name));
- scanf("%s %s", temp1, temp2);
- coord1 = temp1[0];
- coord2 = temp1[1]-'0';
- coord3 = temp2[0];
- coord4 = temp2[1]-'0';
- coord1 = CharToInt(coord1);
- coord3 = CharToInt(coord3);
- coord2--;
- coord4--;
- if(coord1==coord3){
- if(abs(coord4-coord2) !=((s->size)-1))
- printf("You have entered invalid coordinates. \n");
- else{
- for(i=0; i<=(coord4-coord2); i++){
- array[coord1][coord2+i] = 1;
- j=1;
- }
- }
- }
- else if(coord2==coord4){
- if(abs(coord3-coord1) != ((s->size)-1)){
- printf("You have entered invalid coordinates.\n");
- }
- else{
- for(i=0; i<=(coord3-coord1); i++){
- array[coord1+i][coord2] = 1;
- j=1;
- }
- }
- }
- else{
- printf("You have entered invalid coordinates.\n");
- }
- }
- }
- int IntersectCheck(int array[][10], int n1, int n2, int align, struct ship *s){
- int i, check;
- if(align){
- for(i=0; i<(s->size); i++){
- if(array[n1][n2+i] == 1||array[n1][n2-i]==1){
- check=0;
- break;
- }
- else{
- check=1;
- break;
- }
- }
- }
- else{
- for(i=0; i=(s->size); i++){
- if(array[n1+i][n2] == 1||array[n1-i][n2]==1){
- check=0;
- break;
- }
- else{
- check=1;
- break;
- }
- }
- }
- return check;
- }
- /*void PrintGrid(){
- int i, j, k;
- for (i=0; i<2; i++){
- if (i==0){
- printf("Your grid\n");
- }
- else {
- printf("Computer's grid\n");
- }
- for (j=0; j<10; j++){
- for (k=0; k<10; k++){
- printf("[ ]");
- }
- printf("\n");
- }
- }
- }*/
- int CharToInt(char letter){
- int num;
- num = (int)letter-65;
- return num;
- }
- char IntToChar(int num){
- char letter;
- letter = (char)(num+65);
- return letter;
- }
Add Comment
Please, Sign In to add comment