Guest User

Untitled

a guest
Aug 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. /*Final Project
  2. Battleship Game
  3. Eric Schmid
  4. Zach Williams*/
  5.  
  6. #include <stdio.h>
  7. #include <time.h>
  8.  
  9. struct ship{
  10. char name[20];
  11. int size;
  12. };
  13.  
  14. void PrintGrid(int array1[][10], int array2[][10]);
  15. void AssignCompGrid(int array[][10], struct ship *s);
  16. int IntersectCheck(int array[][10], int n1, int n2, int align, struct ship *s);
  17. int CharToInt(char letter);
  18. char IntToChar(int num);
  19. void AssignPlayerGrid(int array[][10], struct ship *s);
  20.  
  21. int main(){
  22.  
  23. int i, j, Comp_hide_grid[10][10], Player_grid[10][10], Comp_view_grid[10][10];
  24. //Zero out the grids for computer and human players
  25. for(i=0; i<10; i++){
  26. for(j=0; j<10; j++){
  27. Comp_hide_grid[i][j] = 0;
  28. Player_grid[i][j] = 0;
  29. Comp_view_grid[i][j] = 0;
  30. }
  31. }
  32.  
  33. //Set up each of the ship types
  34. struct ship Aircraft_Carrier;
  35. strcpy(Aircraft_Carrier.name, "Aircraft Carrier");
  36. Aircraft_Carrier.size = 5;
  37.  
  38. struct ship Battleship;
  39. strcpy(Battleship.name, "Battleship");
  40. Battleship.size = 4;
  41.  
  42. struct ship Cruiser;
  43. strcpy(Cruiser.name, "Cruiser");
  44. Cruiser.size = 3;
  45.  
  46. struct ship Submarine;
  47. strcpy(Submarine.name, "Submarine");
  48. Submarine.size = 3;
  49.  
  50. struct ship Destroyer;
  51. strcpy(Destroyer.name, "Destroyer");
  52. Destroyer.size = 2;
  53.  
  54. struct ship Hit;
  55. strcpy(Hit.name, "X");
  56. Hit.size = 1;
  57.  
  58. struct ship Miss;
  59. strcpy(Miss.name, "O");
  60. Miss.size = 1;
  61.  
  62. AssignCompGrid(Comp_hide_grid, &Aircraft_Carrier);
  63. AssignCompGrid(Comp_hide_grid, &Battleship);
  64. AssignCompGrid(Comp_hide_grid, &Cruiser);
  65. AssignCompGrid(Comp_hide_grid, &Submarine);
  66. AssignCompGrid(Comp_hide_grid, &Destroyer);
  67. printf("\n");
  68. for(i=0; i<10; i++){
  69. for(j=0; j<10; j++){
  70. printf("%d ", Comp_hide_grid[i][j]);
  71. }
  72. printf("\n");
  73. }
  74.  
  75. AssignPlayerGrid(Player_grid, &Aircraft_Carrier);
  76. AssignPlayerGrid(Player_grid, &Battleship);
  77. AssignPlayerGrid(Player_grid, &Cruiser);
  78. AssignPlayerGrid(Player_grid, &Submarine);
  79. AssignPlayerGrid(Player_grid, &Destroyer);
  80.  
  81. for(i=0; i<10; i++){
  82. for(j=0; j<10; j++){
  83. printf("%d ", Player_grid[i][j]);
  84. }
  85. printf("\n");
  86. }
  87.  
  88. int turn=1, play=1, coord2, compcoord1, compcoord2;
  89. char coord1, temp[3], coord1char;
  90. while(play){
  91. if(turn){
  92. printf("Your turn: \n");
  93. printf("Enter the target coordinates: \n");
  94. scanf("%s", temp);
  95. coord1 = temp[0];
  96. coord2 = temp[1]-'0';
  97. coord1 = CharToInt(coord1);
  98. if(Comp_hide_grid[coord1][coord2-1] == 1)
  99. printf("Direct hit!\n");
  100. else
  101. printf("Miss!\n");
  102.  
  103. printf("\n");
  104.  
  105. turn=0;
  106. }
  107.  
  108. else{
  109. printf("Computer's turn: \n");
  110. coord1=rand()%10;
  111. coord2=rand()%10;
  112. coord1char=IntToChar(coord1);
  113. printf("The computer fired at point %c%d. \n", coord1char, coord2+1);
  114. if(Player_grid[coord1][coord2] == 1)
  115. printf("Direct hit!\n");
  116. else
  117. printf("Miss!\n");
  118.  
  119. printf("\n");
  120.  
  121. turn = 1;
  122. }
  123. }
  124.  
  125. //PrintGrid(Player_grid[10][10], Comp_view_grid[10]);
  126.  
  127. system("PAUSE");
  128. return 0;
  129. }
  130.  
  131. void AssignCompGrid(int array[][10], struct ship *s){
  132. int i, n1, n2, align, check=1;
  133. srand(time(0));
  134. while(check){
  135. n1=rand()%10;
  136. n2=rand()%10;
  137. align = rand()%2;//test decides whether the ship is placed vertical(1) or horizontal(0), if all other conditions are ok
  138. if(IntersectCheck(array, n1, n2, align, s)){//check for intersections
  139. check = 0;
  140. }
  141. }
  142. //printf("%d %d", n1, n2); Disregard this line, i'm working on testing this out.
  143.  
  144. if(n1+(s->size)>9 && n2+(s->size)>9){//if initial point is located in the lower right corner
  145. for(i=0; i<(s->size); i++){
  146. if(align)
  147. array[n1][n2-i] = 1;
  148. else
  149. array[n1-i][n2] = 1;
  150. }
  151. }
  152.  
  153. else if(align||n1+(s->size)>9){//If starting point of ship is too close to the bottom, make sure ship is aligned horizontal
  154. for(i=0; i<(s->size); i++){
  155. array[n1][n2+i] = 1;
  156. }
  157. }
  158.  
  159. else if(!align||n2+(s->size)>9){//If starting point too close to the right, make sure ship is vertical
  160. for(i=0; i<(s->size); i++){
  161. array[n1+i][n2] = 1;
  162. }
  163. }
  164. }
  165.  
  166. void AssignPlayerGrid(int array[][10], struct ship *s){
  167.  
  168. int i, j=0, coord2, coord4;
  169. char coord1, coord3, temp1[3], temp2[3];
  170. while(j==0){
  171. printf("Please enter the beginning and ending coordinates of your %s: \n", (s->name));
  172. scanf("%s %s", temp1, temp2);
  173. coord1 = temp1[0];
  174. coord2 = temp1[1]-'0';
  175. coord3 = temp2[0];
  176. coord4 = temp2[1]-'0';
  177. coord1 = CharToInt(coord1);
  178. coord3 = CharToInt(coord3);
  179. coord2--;
  180. coord4--;
  181.  
  182. if(coord1==coord3){
  183. if(abs(coord4-coord2) !=((s->size)-1))
  184. printf("You have entered invalid coordinates. \n");
  185.  
  186. else{
  187. for(i=0; i<=(coord4-coord2); i++){
  188. array[coord1][coord2+i] = 1;
  189. j=1;
  190. }
  191. }
  192. }
  193. else if(coord2==coord4){
  194. if(abs(coord3-coord1) != ((s->size)-1)){
  195. printf("You have entered invalid coordinates.\n");
  196. }
  197. else{
  198. for(i=0; i<=(coord3-coord1); i++){
  199. array[coord1+i][coord2] = 1;
  200. j=1;
  201. }
  202. }
  203. }
  204. else{
  205. printf("You have entered invalid coordinates.\n");
  206. }
  207. }
  208. }
  209.  
  210. int IntersectCheck(int array[][10], int n1, int n2, int align, struct ship *s){
  211. int i, check;
  212. if(align){
  213. for(i=0; i<(s->size); i++){
  214. if(array[n1][n2+i] == 1||array[n1][n2-i]==1){
  215. check=0;
  216. break;
  217. }
  218. else{
  219. check=1;
  220. break;
  221. }
  222. }
  223. }
  224. else{
  225. for(i=0; i=(s->size); i++){
  226. if(array[n1+i][n2] == 1||array[n1-i][n2]==1){
  227. check=0;
  228. break;
  229. }
  230. else{
  231. check=1;
  232. break;
  233. }
  234. }
  235. }
  236. return check;
  237. }
  238.  
  239.  
  240. /*void PrintGrid(){
  241.  
  242. int i, j, k;
  243.  
  244. for (i=0; i<2; i++){
  245. if (i==0){
  246. printf("Your grid\n");
  247. }
  248. else {
  249. printf("Computer's grid\n");
  250. }
  251. for (j=0; j<10; j++){
  252. for (k=0; k<10; k++){
  253. printf("[ ]");
  254. }
  255. printf("\n");
  256. }
  257. }
  258. }*/
  259.  
  260. int CharToInt(char letter){
  261. int num;
  262. num = (int)letter-65;
  263. return num;
  264. }
  265.  
  266. char IntToChar(int num){
  267. char letter;
  268. letter = (char)(num+65);
  269. return letter;
  270. }
Add Comment
Please, Sign In to add comment