Guest User

Untitled

a guest
Apr 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #define SIZE 10
  7. #define FILENAME "C:/Documents and Settings/3413/Desktop/HighScores.txt"
  8.  
  9. void printBoard(int arr[SIZE][SIZE], int hidden); //Prints the board. If 'hidden' = 1, print hidden board
  10. void randomizeShips(int arr[SIZE][SIZE],int num_ships); //Sets 3 ships into the board, with at least one vertical
  11. int checkHit(int arr[SIZE][SIZE], int row, int col); //Returns a 1 if hit, a 0 if miss
  12. int checkWin(int arr[SIZE][SIZE]); //Returns a 1 if all battleships have been sunk, otherwise 0
  13. void InitializeBoard(int arr[SIZE][SIZE]); //Sets all board locations to a value (i.e. “0” or “~”)
  14. void takeTurn(int *row, int *col);
  15. //void printWin();
  16. void highScores();
  17. // movesTaken();
  18. void shipSunk();
  19.  
  20.  
  21. //int hitAmount[3] = {0, 0, 0};
  22.  
  23. int main(void)
  24. {
  25. //Variables: Hidden Array, Users array
  26. int board[SIZE][SIZE];
  27. int row, col;
  28. int hits = 0;
  29. int misses = 0;
  30. //Display Instructions
  31. printf("Welcome to Battleship\n\n\n\n\n\n\n");
  32.  
  33. system("PAUSE");
  34. system("CLS");
  35. printf("|--------------|\n");
  36. printf("| Instructions:|\n");
  37. printf("|--------------|\n");
  38. printf("|---------------------------------------------|\n");
  39. printf("| 1. You are playing against the computer |\n");
  40. printf("| 2. To win, you must sink all 3 of your |\n");
  41. printf("| opponents ships |\n");
  42. printf("| 3. To choose your space, enter the number of|\n");
  43. printf("| the row, and then the number of the |\n");
  44. printf("| column |\n");
  45. printf("| ie: 63 |\n");
  46. printf("|---------------------------------------------|\n\n\n\n\n\n\n");
  47.  
  48. system("PAUSE");
  49. system("CLS");
  50.  
  51. //Initialize the board
  52. InitializeBoard(board);
  53. printBoard(board, 0);
  54. //Input ships on board
  55. randomizeShips(board, 3);
  56. //Loop the following until the user has won:
  57. //Display board to the user
  58. printBoard(board, 1);
  59. //If in 'Debug Mode,' print out hidden board
  60. //Ask the user which location they want to select
  61.  
  62. do
  63. {
  64. takeTurn(&row, &col);
  65. checkHit(board, row, col);
  66. }while(hits < 9);
  67. //Update board
  68. printBoard(board, 0);
  69. //Tell user if hit or miss
  70. //If its a hit, check to see if they won
  71.  
  72. //checkWin(board);
  73. //Tell user if they won
  74. //printWin();
  75. //Give user their score (How many moves it took them to win)
  76. //movesTaken();
  77.  
  78. }
  79. //Board is set up as follows:
  80. /*
  81. 0 = no hit or miss or ship
  82. 1 = ship1(hasnt been hit)
  83. 2 = ship2 (hasnt been hit)
  84. 3 = ship3 (hasnt been hit)
  85. 4 = hit
  86. 5 = miss
  87. */
  88.  
  89. void InitializeBoard(int arr[SIZE][SIZE])
  90. {
  91. int row, col;
  92. for(row = 0; row < SIZE; row++)
  93. for(col = 0; col < SIZE; col++)
  94. arr[row][col] = 0;
  95. printf("\n");
  96. }
  97. void printBoard(int arr[SIZE][SIZE], int hidden)
  98. {
  99.  
  100. int row, col;
  101. for(row = 0; row < SIZE; row++){
  102. for(col = 0; col < SIZE; col++){
  103.  
  104. if((hidden == 0) && (arr[row][col] == 0)) //No hits/miss/ship
  105. printf("%2c", '~');
  106. else if((hidden == 0) && (arr[row][col] == 1)) //ship (dont show to user)
  107. printf("%2c", '~');
  108. else if((hidden == 0) && (arr[row][col] == 2))
  109. printf("%2c", '~');
  110. else if((hidden == 0) && (arr[row][col] == 3))
  111. printf("%2c", '~');
  112. else if((hidden == 0) && (arr[row][col] == 4))
  113. printf("%2c", 'H');
  114. else if((hidden == 0) && (arr[row][col] == 5))
  115. printf("%2c", 'M');
  116. else if((hidden == 1) && (arr[row][col] == 0))
  117. printf("%2c", '~');
  118. else if((hidden == 1) && (arr[row][col] == 1))
  119. printf("%2c", '1');
  120. else if((hidden == 1) && (arr[row][col] == 2))
  121. printf("%2c", '2');
  122. else if((hidden == 1) && (arr[row][col] == 3))
  123. printf("%2c", '3');
  124. else if((hidden == 1) && (arr[row][col] == 4))
  125. printf("%2c", 'H');
  126. else if((hidden == 1) && (arr[row][col] == 5))
  127. printf("%2c", 'M');
  128. }
  129. printf("\n");
  130. }
  131. }
  132.  
  133. void randomizeShips(int arr[SIZE][SIZE],int num_ships)
  134. {
  135. int placed_ships = 0;
  136. int loop = 0;
  137. int Horizontal = 0;
  138. int Vertical = 0;
  139. int i = 0;
  140. int row = 0;
  141. int col = 0;
  142. //Loop the place 3 ships. Loop ends when all 3 ships are placed.
  143. //OR the loop has been run 100 times.
  144. srand(time(0));
  145.  
  146. while((placed_ships < num_ships)&&(loop < 100))
  147. {
  148.  
  149. Horizontal = rand()%2;
  150.  
  151. if(Horizontal == 1)
  152. {
  153. row = rand()%SIZE;
  154. col = rand()%(SIZE-3);
  155.  
  156. if((arr[row][col] == 0) && (arr[row][col+1] == 0) && (arr[row][col+2] == 0))
  157. {
  158. arr[row][col] = placed_ships+1;
  159. arr[row][col+1] = placed_ships+1;
  160. arr[row][col+2] = placed_ships+1;
  161. placed_ships++;
  162. }
  163. }
  164. else
  165. {
  166. col = rand()%SIZE;
  167. row = rand()%(SIZE-3);
  168. if(arr[row][col] == 0 && arr[row+1][col] == 0 && arr[row+2][col] == 0)
  169. {
  170. arr[row][col] = placed_ships+1;
  171. arr[row+1][col] = placed_ships+1;
  172. arr[row+2][col] = placed_ships+1;
  173. placed_ships++;
  174. }
  175. }
  176.  
  177.  
  178. }
  179. system("CLS");
  180. printBoard(arr, 0);
  181.  
  182.  
  183. }
  184. /*
  185. void promptUser(int *row, int *col)
  186. {
  187. //Ask the user to enter a value (Explain it needs to be A1, B2, C3 etc)
  188. //Scan in the value into a character and an int.
  189. //Dummy proof if desired/needed (Make sure letter is between 'A' ad 'J' and number between '1' and '10'.
  190. //Convert letter to row number (Use 'If' statments or ASCII or case)
  191. //Convert the number to a column number( subtract 1 from what they give you).
  192. }
  193. */
  194. void takeTurn(int *row, int *col)
  195. {
  196. printf("Enter a coordinate\n");
  197. printf("Row \n");
  198. scanf("%d", row);
  199. printf("Column \n");
  200. scanf("%d", col);
  201. }
  202. int checkHit(int arr[SIZE][SIZE], int row, int col) //Returns a 1 if hit, a 0 if miss
  203. {
  204.  
  205. int misses = 0;
  206. int hits = 0;
  207.  
  208. if(arr[row][col] == '~')
  209. {
  210. printf("Miss\n");
  211. arr[row][col] = '0';
  212. misses++;
  213. }
  214. else
  215. {
  216. printf("Hit!!\n");
  217. arr[row][col] = '1';
  218. hits++;
  219. }
  220. }
  221. //void shipSunk()
  222. //{
  223. // int hitAmount[3] = {0, 0, 0};
  224. // if('1' == 3)
  225. // {
  226. // printf("You sunnk my Destroyer!\n");
  227. // }
  228. // else if('2' == 3)
  229. // {
  230. // printf("You sunk my Cruiser!\n");
  231. // }
  232. // else if('3' == 3)
  233. // {
  234. // printf("You sunk my Submarine!\n");
  235. // }
  236.  
  237. //}
  238. /*
  239. int checkWin(int arr[SIZE][SIZE]) //Returns a 1 if all battleships have been sunk, otherwise 0
  240. {
  241. //Search through the entire array (Use pointers or nested "for" loops).
  242.  
  243. if(hits == 9)
  244. {
  245. printWin();
  246. }
  247. //For each value, you check if it is 1, 2, or 3.
  248. //If you find a 1, 2, or 3, you return 0(Zero), which means they havent won.
  249. //If you dont find a 1, 2, or 3, you return a 1, which means they have won.
  250.  
  251.  
  252. }
  253.  
  254.  
  255. char myChar[81];
  256. gets(myChar)
  257. row = myChar[0]
  258. if(myChar[2] != "\0")
  259. col = 9;
  260. else
  261. col = myChar;
  262.  
  263.  
  264. }
  265.  
  266.  
  267.  
  268. // printWin()
  269. //{
  270. // printf("Congratulations! You won!!\n");
  271. // system("PAUSE");
  272. //}
  273.  
  274.  
  275.  
  276. */
  277. void highScores()
  278. {
  279. char name[3];
  280. int score;
  281.  
  282. FILE * sensor = fopen (FILENAME, "w");
  283. if (sensor == NULL)
  284. {
  285. printf("Error opening file.\n");
  286. }
  287. else
  288. {
  289. scanf("%s", name);
  290. fprintf(sensor, "%3.3s %d\n", name, score);
  291. fclose(sensor);
  292. }
  293. }
Add Comment
Please, Sign In to add comment