Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. void printPremadeBoards()
  9. {
  10. char cMatBoard1[10][10], cMatBoard2[10][10], cMatBoard3[10][10];
  11. ifstream fBoard;
  12. fBoard.open("board1.txt");
  13. if (fBoard.is_open())
  14. {
  15. for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
  16. {
  17. for (int iColCounter = 0; iColCounter < 10; iColCounter++)
  18. {
  19. fBoard >> cMatBoard1[iRowCounter][iColCounter];
  20. }
  21. }
  22. }
  23. fBoard.close();
  24.  
  25. fBoard.open("board2.txt");
  26. if (fBoard.is_open())
  27. {
  28. for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
  29. {
  30. for (int iColCounter = 0; iColCounter < 10; iColCounter++)
  31. {
  32. fBoard >> cMatBoard2[iRowCounter][iColCounter];
  33. }
  34. }
  35. }
  36. fBoard.close();
  37.  
  38. fBoard.open("board3.txt");
  39. if (fBoard.is_open())
  40. {
  41. for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
  42. {
  43. for (int iColCounter = 0; iColCounter < 10; iColCounter++)
  44. {
  45. fBoard >> cMatBoard3[iRowCounter][iColCounter];
  46. }
  47. }
  48. }
  49. fBoard.close();
  50.  
  51. cout << "Board #1 Board #2 Board #3" << "\n";
  52.  
  53. for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
  54. {
  55. for (int iColCounter = 0; iColCounter < 10; iColCounter++)
  56. {
  57. cout << cMatBoard1[iColCounter][iRowCounter] << " ";
  58. }
  59. cout << " ";
  60. for (int iColCounter = 0; iColCounter < 10; iColCounter++)
  61. {
  62. cout << cMatBoard2[iColCounter][iRowCounter] << " ";
  63. }
  64.  
  65. cout << " ";
  66. for (int iColCounter = 0; iColCounter < 10; iColCounter++)
  67. {
  68. cout << cMatBoard3[iColCounter][iRowCounter] << " ";
  69. }
  70. cout << "\n";
  71. }
  72. }
  73.  
  74. void processInput()
  75. {
  76. char cInput = 0;
  77. while (true)
  78. {
  79. cout << "* To choose one of these boards, enter '1' for board #1, enter '2' for board #2, or enter '3' for board #3." << "\n";
  80. cout << "* To randomize a board, press 'r'." << "\n";
  81. cout << "* To create the board yourself, press 'c'." << "\n";
  82.  
  83. cin >> cInput;
  84. switch (cInput)
  85. {
  86. case '1':
  87.  
  88. goto success;
  89. break;
  90. case '2':
  91.  
  92. goto success;
  93. break;
  94. case '3':
  95.  
  96. goto success;
  97. break;
  98. case 'h':
  99. case 'H':
  100.  
  101. goto success;
  102. break;
  103. case 'r':
  104. case 'R':
  105.  
  106. goto success;
  107. break;
  108. case 'c':
  109. case 'C':
  110.  
  111. goto success;
  112. break;
  113.  
  114. default:
  115. cout << "Invalid answer!" << "\n";
  116. continue;
  117. }
  118.  
  119. success:
  120. cout << "Yes";
  121. break;
  122. }
  123. }
  124.  
  125. void printIntro()
  126. {
  127. cout << "Welcome to BATTLESHIP. If you don't know how to play, press 'h'. Otherwise, you must select your board." << "\n";
  128. cout << "You can select one of the pre-made ones, create a random one, or arrange the ships by yourself." << "\n";
  129. cout << "These are the pre-made boards:" << "\n";
  130.  
  131. printPremadeBoards();
  132. processInput();
  133. }
  134.  
  135. /*
  136. randomizeBoard
  137. Randomizes a board, providing random ship orientation, X position and Y position.
  138.  
  139. Parameters: cMatBoard (the board to randomize)
  140. Returns: nothing.
  141. */
  142. void randomizeBoard(char cMatBoard[10][10])
  143. {
  144. int iCounter = 5;
  145. while (iCounter > 0)
  146. {
  147. bool bSuccess = true;
  148. int iCurrentShip = iCounter;
  149.  
  150. // since there are two 3-blocks ships, when iCounter is 1 we will create another one
  151. if (iCounter == 1)
  152. {
  153. iCurrentShip = 3;
  154. }
  155.  
  156. // randomize orientation, x position and y position
  157. int iOrientation = rand() % 2;
  158. int iRandRow = rand() % 10;
  159. int iRandCol = rand() % 10;
  160. if (iOrientation == 0) // 0 = horizontal
  161. {
  162. // check if the ship won't go out of bounds
  163. if (iRandCol + iCurrentShip < 10)
  164. {
  165. // make sure that the ship won't be over any other existing ships
  166. for (int iCol = iRandCol; iCol < iRandCol + iCurrentShip; iCol++)
  167. {
  168. if (cMatBoard[iRandRow][iCol] != '0')
  169. {
  170. bSuccess = false;
  171. break;
  172. }
  173. }
  174.  
  175. // if we make it to here, then we can proceed to create the ship
  176. if (bSuccess)
  177. {
  178. for (int iCol = iRandCol; iCol < iRandCol + iCurrentShip; iCol++)
  179. {
  180. cMatBoard[iRandRow][iCol] = '0' + iCurrentShip;
  181. }
  182. }
  183. }
  184. else
  185. {
  186. continue;
  187. }
  188. }
  189. else // 1 = vertical
  190. {
  191. // check if the ship won't go out of bounds
  192. if (iRandRow + iCurrentShip < 10)
  193. {
  194. // make sure that the ship won't be over any other existing ships
  195. for (int iRow = iRandRow; iRow < iRandRow + iCurrentShip; iRow++)
  196. {
  197. if (cMatBoard[iRow][iRandCol] != '0')
  198. {
  199. bSuccess = false;
  200. break;
  201. }
  202. }
  203.  
  204. // if we make it to here, then we can proceed to create the ship
  205. if (bSuccess)
  206. {
  207. for (int iRow = iRandRow; iRow < iRandRow + iCurrentShip; iRow++)
  208. {
  209. cMatBoard[iRow][iRandCol] = '0' + iCurrentShip;
  210. }
  211. }
  212. }
  213. else
  214. {
  215. continue;
  216. }
  217. }
  218.  
  219. // if we didn't fail on the creation, proceed onto the next ship
  220. if (bSuccess)
  221. {
  222. iCounter--;
  223. }
  224. }
  225. }
  226.  
  227. /*
  228. createBoard
  229. Fills a matrix with 0, which is an empty battleship board.
  230.  
  231. Parameters: cMatBoard (the matrix to fill)
  232. Returns: nothing.
  233. */
  234. void createBoard(char cMatBoard[10][10])
  235. {
  236. for (int iRow = 0; iRow < 10; iRow++)
  237. {
  238. for (int iCol = 0; iCol < 10; iCol++)
  239. {
  240. cMatBoard[iRow][iCol] = '0';
  241. }
  242. }
  243. }
  244.  
  245. void printBoard(char cMatBoard[10][10])
  246. {
  247. for (int iRow = 0; iRow < 10; iRow++)
  248. {
  249. for (int iCol = 0; iCol < 10; iCol++)
  250. {
  251. cout << cMatBoard[iRow][iCol] << " ";
  252. }
  253. cout << "\n";
  254. }
  255. }
  256.  
  257. int main()
  258. {
  259. char cMatBoard[10][10];
  260.  
  261. srand(time(NULL));
  262.  
  263. printIntro();
  264.  
  265. while (true)
  266. {
  267.  
  268. }
  269. return 0;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement