Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7.  
  8. #include "battleshipHeader.h"
  9. //#include "battleship.h"
  10.  
  11. // main function
  12.  
  13. int main()
  14. {
  15. /***** variable declarations *****/
  16.  
  17. // variables for player
  18. short player = 0; /* 0 -> player1, 1 -> player2 */
  19. short shot = 0; /* holds temp value if ship has been shot */
  20. int menuSelect = 0; /* option for player to place ship manually or randomly */
  21.  
  22. // variables for directions
  23. int north = 0; /* holds change of value when going north */
  24. int south = 0; /* holds change of value when going south */
  25. int east = 0; /* holds change of value when going east */
  26. int west = 0; /* holds change of value when going west */
  27.  
  28. // variables for counters
  29. int i = 0;
  30. int counter = 1;
  31.  
  32. // variable for ship symbol
  33. char shipSymbol = '\0'; /* temporary value to save character symbol of the ship */
  34.  
  35. Player playerOne;
  36. Player playerTwo;
  37.  
  38. srand(time(0));
  39.  
  40. // call function welcomeScreen
  41. welcomeScreen();
  42.  
  43. // clear the screen
  44. clearScreen();
  45.  
  46. // initialize players
  47. initializePlayer(&playerOne, PLAYERONE);
  48.  
  49. // clear the screen
  50. clearScreen();
  51.  
  52. initializePlayer(&playerTwo, PLAYERTWO);
  53.  
  54. // clear the screen
  55. clearScreen();
  56.  
  57. // get the human player ship placement method
  58. menuSelect = displayMenu();
  59.  
  60. // clear the screen
  61. clearScreen();
  62.  
  63. // evaluate player 1's ship placement option
  64. switch(menuSelect)
  65. {
  66. case MANUAL:
  67. printf("%s selected to place ships manually\n", playerOne.name);
  68. manualShipPlace(&playerOne);
  69. break;
  70. case RANDOM:
  71. printf("%s selected to place ships randomly\n", playerOne.name);
  72. randomShipPlace(&playerOne);
  73. break;
  74. }
  75.  
  76. randomShipPlace(&playerTwo);
  77.  
  78. //displayGameBoard(playerOne);
  79. //displayGameBoard(playerTwo);
  80.  
  81. playGame(&playerOne, &playerTwo);
  82.  
  83. // end program
  84. return 0;
  85. }
  86.  
  87. void clearScreen()
  88. {
  89. // send the clear screen command Windows
  90. system("cls");
  91. // send the clear screen command for UNIX flavor operating systems
  92. // system("clear");
  93. }
  94.  
  95. // welcomeScreen function
  96. void welcomeScreen ()
  97. {
  98. printf ("BBBBB AAAA TTTTTT TTTTTT LL EEEEEE SSSSS HH HH II PPPP\n");
  99. printf ("BB BB AA AA TT TT LL EE SS HH HH II PP PP\n");
  100. printf ("BBBBB AA AA TT TT LL EEEE SSSS HHHHHH II PPPP\n");
  101. printf ("BB BB AAAAAA TT TT LL EE SS HH HH II PP\n");
  102. printf ("BBBBB AA AA TT TT LLLLLL EEEEEE SSSSS HH HH II PP\n");
  103. printf ("\n\n");
  104. printf ("RULES OF THE GAME:\n");
  105. printf ("1. This is a two player game.\n");
  106. printf ("2. Player 1 is you and Player 2 is the computer.\n");
  107. printf ("3. Player 1 will be prompted if user wants to manually input coordinates\n");
  108. printf (" for the game board or have the computer randomly generate a game board\n");
  109. printf ("4. There are five types of ships to be placed by longest length to the\n");
  110. printf (" shortest; [c] Carrier has 5 cells, [b] Battleship has 4 cells, [r] Cruiser\n");
  111. printf (" has 3 cells, [s] Submarine has 3 cells, [d] Destroyer has 2 cells\n");
  112. printf ("5. The computer randomly selects which player goes first\n");
  113. printf ("6. The game begins as each player tries to guess the location of the ships\n");
  114. printf (" of the opposing player's game board; [X] hit and [~] miss\n");
  115. printf ("7. First player to guess the location of all ships wins\n\n");
  116.  
  117. printf("\t\t\t\tHit <ENTER> to continue!\n");
  118.  
  119. char enter;
  120. scanf("%c", &enter );
  121. }
  122.  
  123. int displayMenu()
  124. {
  125. // variable for menu selection value
  126. int select;
  127.  
  128. while(select != MANUAL && select != RANDOM)
  129. {
  130. // prompt the user
  131. printf ("Select the option for human player ship placement:\n");
  132. printf ("[1] Manually\n");
  133. printf ("[2] Randomly\n");
  134. printf ("Enter Option: ");
  135.  
  136. select = getchar();
  137.  
  138. if(select != MANUAL && select != RANDOM)
  139. printf("Invalid option, try again\n");
  140.  
  141. fflush(stdin);
  142. }
  143.  
  144.  
  145. return select;
  146. }
  147.  
  148. void displayGameBoard(Player player)
  149. {
  150. int row;
  151. int col;
  152.  
  153. printf("\n %s's Game Board\n\n", player.name);
  154. printf("---------------------------------------------\n");
  155. printf("| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n");
  156. printf("---------------------------------------------\n");
  157.  
  158. for(row = 0; row < ROWS; row++)
  159. {
  160. // display the row number
  161. printf("| %d ", row);
  162.  
  163. for(col = 0; col < COLS; col++)
  164. {
  165. printf("| %c ", player.gameBoard.board[row][col]);
  166. }
  167.  
  168. printf("|\n");
  169. }
  170.  
  171. printf("---------------------------------------------\n");
  172. }
  173.  
  174. void initializePlayer(Player * player, char * name)
  175. {
  176. char playerName[20];
  177. GameBoard playerBoard;
  178. Ship ships[NUM_SHIPS];
  179.  
  180. printf("Enter %s's name\n", name);
  181. gets(playerName);
  182.  
  183. strcpy(player->name, playerName);
  184. player->gameBoard = playerBoard;
  185. memcpy(player->ships, ships, NUM_SHIPS);
  186.  
  187. // initialize player board
  188. initializeBoard(player);
  189. // displayGameBoard(*player);
  190. // getchar();
  191. clearScreen();
  192.  
  193. // initialize ships
  194. initializeShips(player);
  195. // displayShips(*player);
  196. // getchar();
  197. }
  198.  
  199. void initializeBoard(Player * player)
  200. {
  201. memset(player->gameBoard.board, WATER, sizeof(player->gameBoard.board));
  202. }
  203.  
  204. void initializeShips(Player * player)
  205. {
  206. int shipNum;
  207.  
  208. for(shipNum = 0; shipNum < NUM_SHIPS; shipNum++)
  209. {
  210. Ship s;
  211.  
  212. switch(shipNum)
  213. {
  214. // BATTLESHIP
  215. case battleship:
  216. strcpy(s.name, "BATTLESHIP");
  217. s.length = BATTLESHIP_SZ;
  218. s.symbol = BATTLESHIP;
  219. break;
  220. // CARRIER
  221. case carrier:
  222. strcpy(s.name, "CARRIER");
  223. s.length = CARRIER_SZ;
  224. s.symbol = CARRIER;
  225. break;
  226. // CRUISER
  227. case cruiser:
  228. strcpy(s.name, "CRUISER");
  229. s.length = CRUISER_SZ;
  230. s.symbol = CRUISER;
  231. break;
  232. // DESTROYER
  233. case destroyer:
  234. strcpy(s.name, "DESTROYER");
  235. s.length = DESTROYER_SZ;
  236. s.symbol = DESTROYER;
  237. break;
  238. // SUBMARINE
  239. case submarine:
  240. strcpy(s.name, "SUBMARINE");
  241. s.length = SUBMARINE_SZ;
  242. s.symbol = SUBMARINE;
  243. break;
  244. }
  245.  
  246. s.sunk = false;
  247. s.start.column = -1;
  248. s.start.row = -1;
  249. player->ships[shipNum] = s;
  250. }
  251. }
  252.  
  253. void displayShips(Player player)
  254. {
  255. int shipNum;
  256.  
  257. printf("%s's Ship Data\n\n", player.name);
  258.  
  259. for(shipNum = 0; shipNum < NUM_SHIPS; shipNum++)
  260. {
  261. printf("Name: %s\n", player.ships[shipNum].name);
  262. printf("Length: %d\n", player.ships[shipNum].length);
  263. printf("Symbol: %c\n", player.ships[shipNum].symbol);
  264. printf("Is sunk: %s\n", ( (player.ships[shipNum].sunk) ? "True" : "False") );
  265. printf("Location: row: %d col: %d\n\n", player.ships[shipNum].start.row, player.ships[shipNum].start.column);
  266. }
  267. }
  268.  
  269. bool isValid (Player player, int row, int col, int length, char orient)
  270. {
  271. int r;
  272. int c;
  273.  
  274. bool valid = true;
  275.  
  276. if( (row < 0 || (( (row + length) > 9)&& orient == 'V') ) )
  277. return false;
  278.  
  279. if( (col < 0 || ( ( (col + length) > 9) && orient == 'H' ) ))
  280. return false;
  281.  
  282. if(orient != 'H' && orient != 'V')
  283. return false;
  284.  
  285. switch(orient)
  286. {
  287. case HORIZONTAL:
  288. for(c = col; c < (length + col); c++)
  289. {
  290. if (player.gameBoard.board[row][c] != WATER)
  291. {
  292. return false;
  293. }
  294. }
  295. break;
  296. case VERTICAL:
  297. for(r = row; r < (length + row); r++)
  298. {
  299. if (player.gameBoard.board[r][col] != WATER)
  300. {
  301. return false;
  302. }
  303. }
  304. break;
  305. }
  306.  
  307. return valid;
  308. }
  309.  
  310.  
  311. void placeShip(Player * player, int row, int col, Ship ship, char orient)
  312. {
  313. int r;
  314. int c;
  315.  
  316. // printf("Placing ship %s with %d cells, starting at row/column %d/%d orient %c\n ", ship.name, ship.length, row, col, orient);
  317.  
  318. switch(orient)
  319. {
  320. case HORIZONTAL:
  321. for(c = col; c < (ship.length + col); c++)
  322. {
  323. player->gameBoard.board[row][c] = ship.symbol;
  324. }
  325. break;
  326. case VERTICAL:
  327. for(r = row; r < (ship.length + row); r++)
  328. {
  329. player->gameBoard.board[r][col] = ship.symbol;
  330. }
  331. break;
  332. }
  333. }
  334.  
  335. void manualShipPlace (Player * player)
  336. {
  337. int shipNum;
  338. int row;
  339. int col;
  340. char orient;
  341.  
  342. bool valid = false;
  343.  
  344. for (shipNum = 0; shipNum < NUM_SHIPS; shipNum++)
  345. {
  346. while (valid == false)
  347. {
  348. displayGameBoard (*player);
  349.  
  350. printf ("Enter the row and col (e.g. 0 0) for the first cell of ship %s:\n", player->ships[shipNum].name);
  351. printf ("Row col ");
  352. scanf("%d %d", &row, &col);
  353.  
  354. fflush(stdin);
  355.  
  356. printf("What is the orientation for this ship? Enter H for horizontal, V for vertical\n");
  357. scanf("%c", &orient);
  358. orient = toupper(orient);
  359.  
  360. printf("Checking row %d col %d orient %c...\n", row, col, orient);
  361.  
  362. valid = isValid(*player, row, col, player->ships[shipNum].length, orient);
  363.  
  364. if (valid)
  365. {
  366. placeShip(player, row, col, player->ships[shipNum], orient);
  367. }
  368. else
  369. {
  370. valid = false;
  371. printf ("Invalid, try again\n");
  372. }
  373. }
  374.  
  375. valid = false;
  376. }
  377. }
  378.  
  379. void randomShipPlace (Player * player)
  380. {
  381. int shipNum;
  382. int row;
  383. int col;
  384. int const DIR = 2;
  385. int const H = 0;
  386. int const V = 1;
  387. char orient;
  388. int o;
  389.  
  390. bool valid = false;
  391.  
  392. fflush (stdin);
  393.  
  394. for (shipNum = 0; shipNum < NUM_SHIPS; shipNum++)
  395. {
  396. while (valid == false)
  397. {
  398. row = (rand() % ROWS);
  399. col = (rand() % COLS);
  400.  
  401. o = (rand() % DIR);
  402.  
  403. if(o == H)
  404. orient = HORIZONTAL;
  405. else if(o == V)
  406. orient = VERTICAL;
  407.  
  408. valid = isValid(*player, row, col, player->ships[shipNum].length, orient);
  409.  
  410. if (valid)
  411. {
  412. placeShip(player, row, col, player->ships[shipNum], orient);
  413. }
  414. else
  415. {
  416. valid = false;
  417. }
  418. }
  419.  
  420. valid = false;
  421. }
  422. }
  423.  
  424. //playGame project
  425. //come back to it
  426.  
  427. void playGame(Player * playerOne, Player * playerTwo)
  428. {
  429. int currentPlayer = PLAYER1;
  430. Location target;
  431.  
  432. while(true){
  433. switch(currentPlayer)
  434. {
  435. case PLAYER1: {
  436. displayGameBoard(*playerOne);
  437. target = getTarget(*playerOne);
  438. checkShot(playerTwo, target);
  439. }
  440.  
  441. case PLAYER2: {
  442. target = getRandomTarget();
  443. checkShot(playerOne, target);
  444.  
  445. }
  446. clearScreen();
  447.  
  448. }
  449.  
  450. }
  451. int switchPlayer(int currentPlayer)
  452. {
  453. if (currentPlayer == PLAYER1)
  454. {
  455. return PLAYER2;
  456. }
  457. if (currentPlayer == PLAYER2)
  458. {
  459. return PLAYER1;
  460. }
  461.  
  462.  
  463. }
  464.  
  465. /* Location getTarget(Player player)
  466. {
  467. Location target;
  468. fflush(stdin);
  469. scanf()
  470. }
  471. */
  472.  
  473. /* Location getRandomTarget()
  474. {
  475. target.row = rand(ROWS);
  476. target.column = rand(COLS);
  477. return target;
  478. } */
  479.  
  480. /* void checkShot(Player * player, Location target)
  481. {
  482. player->gameBoard.board[target.row][target.column];
  483. switch (symbol)
  484. {
  485. case WATER: {
  486. printf("You done missed");
  487. getchar();
  488. updatePlayerData(player, target, MISS, symbol);
  489. }
  490. case CARRIER:, case BATTLESHIP:, case CRUISER:, case SUBMARINE:, case DESTROYER:
  491. {
  492. printf("You got a hit chief");
  493. getchar();
  494. updatePlayerData(player, target, HIT, symbol);
  495. }
  496. case HIT:, case MISS:, default
  497. {
  498. printf("You tried that already and now you lost a turn stupid");
  499. getchar();
  500. }
  501.  
  502. }
  503. }
  504. */
  505.  
  506. void updatePlayerData(Player * player, Location location, char result, char ship)
  507. {
  508. player->gameBoard.board[location.row][location.column] = result;
  509.  
  510. if (result == HIT)
  511. {
  512. checkShips(player, location, ship);
  513. }
  514.  
  515. }
  516.  
  517. void checkShips(Player * player, Location location, char ship)
  518. {
  519. //ship array constants
  520. const int battleshipIdx;
  521. const int carrierIdx;
  522. const int cruiserIdx;
  523. const int destroyerIdx;
  524. const int submarineIdx;
  525. // some more variables
  526. int sunkCount = 0;
  527. int shipNum;
  528.  
  529. switch (ship)
  530. {
  531. case BATTLESHIP:
  532. {
  533. player->ships[battleshipIdx].length--;
  534. if (length == 0);
  535. {
  536. printf("%s's BATTLESHIP is sunk!", player->name);
  537. }
  538. }
  539. case CARRIER:
  540. {
  541. player->ships[carrierIdx].length--;
  542. if (length == 0);
  543. {
  544. printf("%s's CARRIER is sunk!", player->name);
  545. }
  546. }
  547. case CRUISER:
  548. {
  549. player->ships[cruiserIdx].length--;
  550. if (length == 0);
  551. {
  552. printf("%s's CRUISER is sunk!", player->name);
  553. }
  554. }
  555. case DESTROYER:
  556. {
  557. player->ships[destroyerIdx].length--;
  558. if (length == 0);
  559. {
  560. printf("%s's DESTROYER is sunk!", player->name);
  561. }
  562. }
  563. case SUBMARINE:
  564. {
  565. player->ships[submarineIdx].length--;
  566. if (length == 0);
  567. {
  568. printf("%s's SUBMARINE is sunk!", player->name);
  569. }
  570. }
  571. }
  572. //insert for loop
  573. if (sunkCount == NUM_SHIPS);
  574. {
  575. endGame(*player)
  576. }
  577. }
  578.  
  579. void endGame(Player player)
  580. {
  581. ("The loser HAS LOST THE GAME!");
  582. getchar();
  583. exit(0);
  584. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement