Advertisement
Guest User

Untitled

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