Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. //Code written by Timothy Proffitt,Austen Magill,and David Maldonado
  2. #include <stdio.h>
  3. #include<stdlib.h>
  4. #include <ctype.h>
  5. #include<time.h>
  6. #define SIZE 10
  7. #define cls system("cls")
  8. #define PAUSE system("PAUSE")
  9.  
  10. typedef struct {
  11. int Column;
  12. int Row;
  13. }segment;
  14.  
  15. typedef struct {
  16. segment Starter;
  17. int shipSize;
  18. char status;
  19. }ship;
  20.  
  21. void InitializeBoard(const char* array[SIZE][SIZE]);
  22. void printBoard(const char* array[SIZE][SIZE]);
  23. void getShot(char *actionString);
  24. int numChartoInt(char actionString[4]);
  25. char mainMenu(char cMenuChoice);
  26. void makeFirstSegments(int firstSegPositions[5][2], char enemyships[SIZE][SIZE]);
  27. void makeOtherSegments(ship ship, char enemyShips[SIZE][SIZE]);
  28. void printFirstSegments(int firstSegments[5][2]);
  29. void setGig(int gig, int gig2, int firstSegments[5][2]);
  30.  
  31.  
  32. int firstSegPositions[5][2] = {};
  33. const char* enemyShips[SIZE][SIZE] = {};
  34.  
  35. void setGig(int gig, int gig2, int firstSegments[5][2])
  36. {
  37. gig = firstSegments[0][0];
  38. gig = firstSegments[0][1];
  39. printf("\ngig=%i\n,gig1=2\n", gig, gig2);
  40. printf("firstSegments[0][0]=%i\n", firstSegments[0][0]);
  41. printf("firstSegments[0][1]=%i\n", firstSegments[0][1]);
  42.  
  43.  
  44. }
  45.  
  46. void printFirstSegments(int firstSegments[5][2])
  47. {
  48. int goob = 1, c = 0;
  49. for (int i = 0; i < 5; i++) {
  50. printf("ship %i is at (%i.%i)\n", goob, firstSegments[i][c], firstSegments[i][c + 1]);
  51. goob++;
  52. }
  53. PAUSE;
  54.  
  55. }
  56. void makeFirstSegments(int firstSegPositions[5][2], const char* enemyShips[SIZE][SIZE])
  57. {
  58. srand(time(NULL));
  59.  
  60. for (int i = 0; i < 5; i++) {
  61. for (int c = 0; c < 2; c++)
  62. {
  63. firstSegPositions[i][c] = rand() % 10 + 1;
  64. firstSegPositions[i][c] = rand() % 10 + 1;
  65.  
  66. int place1, place2;
  67. place1 = firstSegPositions[i][c - 1] - 1;
  68. place2 = firstSegPositions[i][c] - 1;
  69.  
  70. enemyShips[place1][place2] = "0";
  71. }
  72. }
  73. }
  74.  
  75.  
  76.  
  77. void makeOtherSegments(segment starter, int shipSize, char enemyShips[SIZE][SIZE])
  78. {
  79. int i, c, rorc;
  80.  
  81. rorc = rand() % 4;
  82.  
  83.  
  84.  
  85.  
  86. /*Cannot go over the border(over 10 or under 0) cannot go on top of already placed ships*/
  87. /*Something like
  88.  
  89. rorc = rand() % 4;
  90.  
  91. if (rorc = 3)
  92. {
  93. if (enemyShips[starter.Xcord][starter.Ycord]!='0' <-Not already placed ship
  94. &&starter.Xcord<10&&starter.Ycord<10 <-not past border
  95. &&starter.Xcord>0&&starter.Ycord>0 <-not past border
  96. &&enemyships[starter.Xcord][starter.Ycord]!='H'||'M' <-do not place if ship in conflicting state
  97. {
  98. enemyShips[starter.Column][starter.Row+1] = '0';
  99. }
  100. }
  101.  
  102. if (rorc = 2)
  103. {
  104. if (enemyShips[starter.Column][starter.Row-1] != '0'&&starter.Row + shipSize < 10 && starter.Row < 0)
  105. {
  106. enemyShips[starter.Column][starter.Row-1] = '0';
  107. }
  108. }
  109.  
  110. */
  111. }
  112.  
  113.  
  114.  
  115. //prompts user for coordinates and saves them in actionstring
  116. void getShot(char *actionString) {
  117.  
  118. printf("enter coordinates (e.x. B3):");
  119. scanf_s(" %c", &actionString[0]);
  120. scanf_s("%c", &actionString[1]);
  121. scanf_s("%c", &actionString[2]);
  122.  
  123. }
  124.  
  125.  
  126.  
  127. //replaces Ycord switch case with better method that is guaranteed by C-standard
  128. int numChartoInt(char actionString[4])
  129. {
  130. int iYcord = 0;
  131. iYcord = actionString[1] - '1' + 1;
  132.  
  133. if (actionString[1] == '1'&&actionString[2] == '0')
  134. {
  135. iYcord = 10;
  136. }
  137.  
  138. return iYcord;
  139. }
  140.  
  141.  
  142. //Xcord switch case
  143. int charXcordtoInt(char cXcord, char *actionString) {
  144. int iXcord = 0;
  145.  
  146. switch (cXcord) {
  147. case 'A':iXcord = 1; break;
  148. case 'B':iXcord = 2; break;
  149. case 'C':iXcord = 3; break;
  150. case 'D':iXcord = 4; break;
  151. case 'E':iXcord = 5; break;
  152. case 'F':iXcord = 6; break;
  153. case 'G':iXcord = 7; break;
  154. case 'H':iXcord = 8; break;
  155. case 'I':iXcord = 9; break;
  156. case 'J':iXcord = 10; break;
  157. default:iXcord = 0; break;
  158. }
  159.  
  160. return iXcord;
  161. }
  162.  
  163. void InitializeBoard(const char *array[SIZE][SIZE]) {
  164.  
  165. for (int s = 0; s < 10; s++) {
  166.  
  167. for (int i = 0; i < 10; i++) {
  168.  
  169. array[s][i] = "*";
  170. }
  171. }
  172. }
  173.  
  174. void printBoard(const char* array[SIZE][SIZE])
  175. {
  176. int sideCounter = 0;
  177. printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n");
  178. printf("\t--\t--\t--\t--\t--\t--\t--\t--\t--\t--\n");
  179. for (int s = 0; s < SIZE; s++) {
  180. printf("[%i]", sideCounter + 1);
  181. sideCounter++;
  182. for (int i = 0; i < SIZE; i++) {
  183.  
  184. printf("\t%c ", array[s][i]);
  185. }
  186. printf("\n");
  187. }
  188. }
  189.  
  190. int shipsandcounter(void) //unfinished
  191. {
  192. int phShipStatus = 0, phMissiles = 0, phShipHit = 0, phShipMisses = 0; // ph = Placement holder || this is just to get the skeleton of what's needed.
  193. printf("\nSeminole state\t\t %i\t Missiles fired: %i\n", phShipStatus, phMissiles);
  194. printf("Airforce Academy\t %i\n", phShipStatus);
  195. printf("Valencia Destroyer\t %i\t Ship Hits:\t %i\n", phShipStatus, phShipHit);
  196. printf("Eskimo University\t %i\n", phShipStatus);
  197. printf("Deland High School\t %i\t Ship Misses:\t %i\n\n", phShipStatus, phShipMisses);
  198. return 0;
  199. }
  200.  
  201. char mainMenu(char cMenuChoice) {
  202.  
  203.  
  204. printf("Valencia Battleship Game\nA] Start a New Game\nB] Resume an Existing Game\nC] View Best Scores\nD] Quit\n Your Choice: ");
  205. scanf_s("%c", &cMenuChoice);
  206.  
  207. cMenuChoice = toupper(cMenuChoice);
  208. return cMenuChoice;
  209. }
  210.  
  211. int main(void)
  212. {
  213.  
  214. //Code starts
  215. const char* gameBoard[SIZE][SIZE];
  216. char cXcord = NULL, cYcord = NULL;
  217. int iXcord, iYcord;
  218. int ShipCount = 0;
  219. char actionString[4];
  220. char cMenuAction = NULL;
  221. mainMenu(cMenuAction);//uncomment this to get MainMenu working
  222.  
  223. ship Deland = { NULL };
  224.  
  225.  
  226.  
  227.  
  228.  
  229. InitializeBoard(enemyShips);
  230.  
  231. //Generates and then places the starting segment(One coordinate) for each the ships
  232. makeFirstSegments(firstSegPositions, enemyShips);
  233. PAUSE;
  234. int gig, gig1;
  235. gig = firstSegPositions[0][0];
  236. gig1 = firstSegPositions[0][1];
  237. printf("\nInside of fucntion test:\nWHY DO THESE NUMBERS NOT WORK gig: %i gig1: %i\n", gig, gig1);
  238.  
  239. setGig(Deland.Starter.Column, Deland.Starter.Row, firstSegPositions);
  240.  
  241.  
  242. printFirstSegments(firstSegPositions);
  243. PAUSE;
  244. int x = firstSegPositions[0][1];
  245. //makeOtherSegments(Deland.Starter.Column, &enemyShips);
  246.  
  247.  
  248. InitializeBoard(gameBoard);
  249.  
  250.  
  251.  
  252.  
  253. while (ShipCount >= 0) {
  254. while (actionString[0] != 'Q' & actionString[1] != 'Q') {
  255. char actionChar1, actionChar2;
  256.  
  257.  
  258.  
  259. printf("\n======gameBoard=======\n");
  260. printBoard(gameBoard);
  261. shipsandcounter();
  262. printf("\n======enemyShips=======\n");
  263. printBoard(enemyShips);
  264.  
  265.  
  266.  
  267. getShot(actionString);//gets coordinates of shot from user
  268.  
  269.  
  270. cXcord = actionString[0];
  271. cYcord = actionString[1];
  272. cls;//Clear screen to make it look prettier.
  273.  
  274. cXcord = toupper(cXcord);//always caps
  275.  
  276.  
  277.  
  278. //numbers from actionString into integers
  279. iXcord = charXcordtoInt(cXcord, actionString);
  280. iYcord = numChartoInt(actionString);
  281.  
  282.  
  283. printf("\n You shot at (%i,%i)!\n", iXcord, iYcord);
  284.  
  285. //checks if enemyShips has a ship '0', if it does, print 'H' on gameBoard
  286. if (enemyShips[iYcord - 1][iXcord - 1] == "0") {
  287. gameBoard[iYcord - 1][iXcord - 1] = "H";
  288. printf("The shot Hit!");
  289. }
  290. else
  291. {
  292. gameBoard[iYcord - 1][iXcord - 1] = "M";
  293. printf("\nThe shot Missed!\n");
  294. }
  295.  
  296. }
  297. }
  298. PAUSE;
  299.  
  300. return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement