Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. /* CS 125 - Intro to Computer Science II
  2. * File Name: CS125_Project6.cpp
  3. * Project 6 - Due 4/15/2019
  4. * Instructor: Dr. Dan Grissom
  5. *
  6. * Name 1: Mason Welge
  7. * Name 2: FirstName LastName
  8. * Description: Insert your meaningful description for Project 6.
  9. */
  10.  
  11. using namespace std;
  12. #include <iostream>
  13. #include <vector>
  14. #include <map>
  15. #include <string>
  16.  
  17.  
  18. //Prefixes: 'H' is hidden, 'R' is revealed
  19. //Suffixes: 'E' is empty, 'S' is a part of a ship
  20. enum cellType { HE, RE, HS, RS };
  21.  
  22. //Create a 2D array containing a grid of cells, using cellType
  23. //Since we'll only have 7 rows, set the initial capacity to 7.
  24. cellType grid[7][7];
  25.  
  26. //Create more readable references to the enum
  27. cellType hiddenEmpty = HE;
  28. cellType hiddenShip = HS;
  29. cellType revealedEmpty = RE;
  30. cellType revealedShip = RS;
  31.  
  32. int gameBoard[7][7];
  33. string size [7][7];
  34. string guessPlace[7][7];
  35. map<string, int> guess;
  36. int co1;
  37. int co2;
  38. string coordinates;
  39. int ship;
  40. int hp;
  41.  
  42. // Method headers
  43. void printGrid();
  44. void pChart();
  45. void guessPlace();
  46. void guessPlaceHint();
  47.  
  48. int main() {
  49. // Your program should always output your name and the project number.
  50. // DO NOT DELETE OR COMMENT OUT. Replace with relevant info.
  51. cout << "Mason Welge" << endl;
  52. cout << "Project 6" << endl << endl;
  53.  
  54. // Your code should go below this line
  55. do {
  56. cout << "Please enter coordinates of ship length 2" << endl;
  57. cout << "Starting Row (0-6): " << endl;
  58. cin >> co1;
  59. cout << "Starting Column (0-6): " << endl;
  60. cin >> co2;
  61. cout << "From the starting point, would you like to move right or down? (d/r)" << endl;
  62. //coordinates = scan.next().toLowerCase();
  63. cin >> coordinates;
  64. // controls where it can go
  65. if ((co2 < 6) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-") && (coordinates == ("r") || coordinates == ("R"))){
  66. size[co1][co2] = "S";
  67. size[co1][co2+1] = "S";
  68. ship = ship +1;
  69.  
  70. } else if ((co1 < 6) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-") && (coordinates == ("d") || coordinates == ("D"))){
  71. size[co1][co2] = "S";
  72. size[co1+1][co2] = "S";
  73. ship = ship +1;
  74. } else {
  75. cout << "There has already been a ship placed here, try again!" << endl;
  76.  
  77. }
  78. } while(ship < 1);
  79. pChart();
  80. // placing second ship
  81. do {
  82. cout << "Please enter coordinates of ship length 2" << endl;
  83. cout << "Starting Row (0-6): " << endl;
  84. cin >> co1;
  85. cout << "Starting Column (0-6): " << endl;
  86. cin >> co2;
  87. cout << "From the starting point, would you like to move right or down? (d/r)" << endl;
  88. cin >> coordinates;
  89. // controls where it can go
  90. if ((co2 < 5) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1][co2+1] == "-" && size[co1][co2 +2] == "-") && (coordinates == ("r") || coordinates == ("R"))){
  91. size[co1][co2] = "S";
  92. size[co1][co2+1] = "S";
  93. size[co1][co2+2] = "S";
  94. ship = ship +1;
  95.  
  96. } else if ((co1 < 5) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1+1][co2]=="-" && size[co1+2][co2]=="-") && (coordinates == ("d") || coordinates == ("D"))){
  97. size[co1][co2] = "S";
  98. size[co1+1][co2] = "S";
  99. size[co1+2][co2] = "S";
  100. ship = ship +1;
  101. } else {
  102. cout << "There has already been a ship placed here, try again!" << endl;
  103.  
  104. }
  105. } while(ship < 2);
  106. pChart();
  107. // placing third ship
  108. do {
  109. cout << "Please enter coordinates of ship length 2" << endl;
  110. cout << "Starting Row (0-6): " << endl;
  111. cin >> co1;
  112. cout << "Starting Column (0-6): " << endl;
  113. cin >> co2;
  114. cout << "From the starting point, would you like to move right or down? (d/r)" << endl;
  115. cin >> coordinates;
  116. // controls where it can go
  117. if ((co2 < 4) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1][co2+1] == "-" && size[co1][co2 +2] == "-" && size[co1][co2 +3] == "-") && (coordinates == ("r") || coordinates == ("R"))){
  118. size[co1][co2] = "S";
  119. size[co1][co2+1] = "S";
  120. size[co1][co2+2] = "S";
  121. size[co1][co2+3] = "S";
  122. ship = ship +1;
  123.  
  124. } else if ((co1 < 4) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1+1][co2]=="-" && size[co1+2][co2]=="-" && size[co1+3][co2]=="-") && (coordinates == ("d") || coordinates == ("D"))){
  125. size[co1][co2] = "S";
  126. size[co1+1][co2] = "S";
  127. size[co1+2][co2] = "S";
  128. size[co1+3][co2] = "S";
  129. ship = ship +1;
  130. } else {
  131. cout << "There has already been a ship placed here, try again!" << endl;
  132.  
  133. }
  134. } while(ship < 3);
  135. pChart();
  136. // ship placement over, guessing begins
  137. cout << "Here's the final grid... press ENTER to begin playing!" << endl;
  138. for(int i=0; i<20;i++) {
  139. cout << "" << endl;
  140. }
  141. //void guessPlace();
  142.  
  143. int numTurns = 0;
  144.  
  145. string key;
  146.  
  147. vector<int> nameRow;
  148. vector<int> nameColumn;
  149. // counts amount of guesses
  150. do {
  151. cout << "Guess #" << numTurns << "!" << endl;
  152. cout << "Guess a Row (0-6): " << endl;
  153. cin >> co1;
  154. cout << "Guess a Column (0-6): " << endl;
  155. cin >> co2;
  156.  
  157. key = co1 + "" + co2;
  158. if (!guess.find(key)->first) {
  159.  
  160. // replaces - with m or h depending on guess
  161. if (size[co1][co2]=="-") {
  162. guessPlace[co1][co2]="m";
  163. cout << "Miss!" << endl;
  164.  
  165. } else {
  166. guess.insert({key, 1});
  167. guessPlace[co1][co2]="H";
  168. cout << "Hit!" << endl;
  169. hp++;
  170. }
  171. // counts # of turns taken
  172. numTurns++;
  173. guess.insert({key, 1});
  174. } else {
  175. cout << "r\\c = " << co1 + "\\" << co2 << "has already been guessed " << guess.find(key)-> second << " times" << endl;
  176. guess.insert({key, guess.find(key)-> second + 1});
  177. }
  178. nameRow.push_back(co1);
  179. nameColumn.push_back(co2);
  180. guessPlaceHint();
  181. } while (hp < 9);
  182. cout << "Guesses\t |\t rows \t columns" << endl;
  183. cout << "---------------------------------" << endl;
  184. for (int i = 0; i < nameRow.size(); i++) {
  185. cout << (i+1) << "\t|" + nameRow.at(i) << "\t" << nameColumn.at(i) << endl;
  186. }
  187.  
  188. }
  189.  
  190.  
  191.  
  192. ///////////////////////////////////////////////////////////////////////
  193. // Prints the game grid
  194. // NOTE: You can change the parameters and return type of this method
  195. // as you see fit.
  196. void printGrid() {
  197. cout << "--------------- Welcome to Battleship! ---------------"<< endl;
  198. cout << "r\\c" << endl;
  199. //
  200. for(int m =0; m<7;m++) {
  201. cout <<"\t"+m << endl;
  202. }
  203. cout <<""<< endl;
  204.  
  205. for(int i=0;i<7;i++) {
  206. cout << i << endl;
  207. cout <<"\t"<< endl;
  208. for(int j=0;j<7;j++) {
  209. // making every value of size a -
  210. size[i][j]="-";
  211. cout << size[i][j]+"\t"<< endl;
  212.  
  213. }
  214. cout << "" << endl;
  215. }
  216.  
  217. }
  218. void pChart () {
  219. cout << "r\\c" << endl;
  220. for(int m =0; m<7;m++) {
  221. cout << "\t"+m << endl;
  222. }
  223. cout << "" << endl;
  224.  
  225. for(int i=0;i<7;i++) {
  226. cout << i << endl;
  227. cout << "\t" << endl;
  228. for(int j=0;j<7;j++) {
  229. cout << size[i][j]+"\t" << endl;
  230.  
  231. }
  232. cout << "" << endl;
  233. }
  234. }
  235. void guessPlace() {
  236. cout << "--------------- Welcome to Battleship! ---------------" << endl;
  237. cout << "r\\c" << endl;
  238.  
  239. for(int m =0; m<7;m++) {
  240. cout << "\t"+m << endl;
  241. }
  242. cout << "" << endl;
  243.  
  244. for(int i=0;i<7;i++) {
  245. cout << i << endl;
  246. cout << "\t" << endl;
  247. for(int j=0;j<7;j++) {
  248.  
  249. size[i][j]="-";
  250. cout << size[i][j]+"\t" << endl;
  251. }
  252.  
  253. cout << "" << endl;
  254. }
  255. }
  256. void guessPlaceHint () {
  257. cout << "r\\c" << endl;
  258. //
  259. for(int m =0; m<7;m++) {
  260. cout << "\t"+m << endl;
  261. }
  262. cout << "" << endl;
  263.  
  264. for(int i=0;i<7;i++) {
  265. cout << i << endl;
  266. cout << "\t" << endl;
  267. for(int j=0;j<7;j++) {
  268. cout << size[i][j]+"\t" << endl;
  269.  
  270. }
  271. cout << "" << endl;
  272. }
  273. }
  274.  
  275.  
  276.  
  277. /******************************************************************************
  278. Insert 2 test cases, which represent program input/output for two different
  279. combinations of inputs. You may literally copy and paste your console contents,
  280. but your two test cases should be DIFFERENT from any test cases given in the
  281. Project description itself.
  282.  
  283. ------------
  284. Test Case 1:
  285. ------------
  286. (Your test case I/O here.)
  287.  
  288. ------------
  289. Test Case 2:
  290. ------------
  291. (Your test case I/O here.)
  292. ******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement