Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code written by Timothy Proffitt,Austen Magill,and David Maldonado
- #include <stdio.h>
- #include<stdlib.h>
- #include <ctype.h>
- #include<time.h>
- #define SIZE 10
- #define cls system("cls")
- #define PAUSE system("PAUSE")
- typedef struct {
- int Column;
- int Row;
- }segment;
- typedef struct {
- segment Starter;
- int shipSize;
- char status;
- }ship;
- void InitializeBoard(const char* array[SIZE][SIZE]);
- void printBoard(const char* array[SIZE][SIZE]);
- void getShot(char *actionString);
- int numChartoInt(char actionString[4]);
- char mainMenu(char cMenuChoice);
- void makeFirstSegments(int firstSegPositions[5][2], char enemyships[SIZE][SIZE]);
- void makeOtherSegments(ship ship, char enemyShips[SIZE][SIZE]);
- void printFirstSegments(int firstSegments[5][2]);
- void setGig(int gig, int gig2, int firstSegments[5][2]);
- int firstSegPositions[5][2] = {};
- const char* enemyShips[SIZE][SIZE] = {};
- void setGig(int gig, int gig2, int firstSegments[5][2])
- {
- gig = firstSegments[0][0];
- gig = firstSegments[0][1];
- printf("\ngig=%i\n,gig1=2\n", gig, gig2);
- printf("firstSegments[0][0]=%i\n", firstSegments[0][0]);
- printf("firstSegments[0][1]=%i\n", firstSegments[0][1]);
- }
- void printFirstSegments(int firstSegments[5][2])
- {
- int goob = 1, c = 0;
- for (int i = 0; i < 5; i++) {
- printf("ship %i is at (%i.%i)\n", goob, firstSegments[i][c], firstSegments[i][c + 1]);
- goob++;
- }
- PAUSE;
- }
- void makeFirstSegments(int firstSegPositions[5][2], const char* enemyShips[SIZE][SIZE])
- {
- srand(time(NULL));
- for (int i = 0; i < 5; i++) {
- for (int c = 0; c < 2; c++)
- {
- firstSegPositions[i][c] = rand() % 10 + 1;
- firstSegPositions[i][c] = rand() % 10 + 1;
- int place1, place2;
- place1 = firstSegPositions[i][c - 1] - 1;
- place2 = firstSegPositions[i][c] - 1;
- enemyShips[place1][place2] = "0";
- }
- }
- }
- void makeOtherSegments(segment starter, int shipSize, char enemyShips[SIZE][SIZE])
- {
- int i, c, rorc;
- rorc = rand() % 4;
- /*Cannot go over the border(over 10 or under 0) cannot go on top of already placed ships*/
- /*Something like
- rorc = rand() % 4;
- if (rorc = 3)
- {
- if (enemyShips[starter.Xcord][starter.Ycord]!='0' <-Not already placed ship
- &&starter.Xcord<10&&starter.Ycord<10 <-not past border
- &&starter.Xcord>0&&starter.Ycord>0 <-not past border
- &&enemyships[starter.Xcord][starter.Ycord]!='H'||'M' <-do not place if ship in conflicting state
- {
- enemyShips[starter.Column][starter.Row+1] = '0';
- }
- }
- if (rorc = 2)
- {
- if (enemyShips[starter.Column][starter.Row-1] != '0'&&starter.Row + shipSize < 10 && starter.Row < 0)
- {
- enemyShips[starter.Column][starter.Row-1] = '0';
- }
- }
- */
- }
- //prompts user for coordinates and saves them in actionstring
- void getShot(char *actionString) {
- printf("enter coordinates (e.x. B3):");
- scanf_s(" %c", &actionString[0]);
- scanf_s("%c", &actionString[1]);
- scanf_s("%c", &actionString[2]);
- }
- //replaces Ycord switch case with better method that is guaranteed by C-standard
- int numChartoInt(char actionString[4])
- {
- int iYcord = 0;
- iYcord = actionString[1] - '1' + 1;
- if (actionString[1] == '1'&&actionString[2] == '0')
- {
- iYcord = 10;
- }
- return iYcord;
- }
- //Xcord switch case
- int charXcordtoInt(char cXcord, char *actionString) {
- int iXcord = 0;
- switch (cXcord) {
- case 'A':iXcord = 1; break;
- case 'B':iXcord = 2; break;
- case 'C':iXcord = 3; break;
- case 'D':iXcord = 4; break;
- case 'E':iXcord = 5; break;
- case 'F':iXcord = 6; break;
- case 'G':iXcord = 7; break;
- case 'H':iXcord = 8; break;
- case 'I':iXcord = 9; break;
- case 'J':iXcord = 10; break;
- default:iXcord = 0; break;
- }
- return iXcord;
- }
- void InitializeBoard(const char *array[SIZE][SIZE]) {
- for (int s = 0; s < 10; s++) {
- for (int i = 0; i < 10; i++) {
- array[s][i] = "*";
- }
- }
- }
- void printBoard(const char* array[SIZE][SIZE])
- {
- int sideCounter = 0;
- printf("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ\n");
- printf("\t--\t--\t--\t--\t--\t--\t--\t--\t--\t--\n");
- for (int s = 0; s < SIZE; s++) {
- printf("[%i]", sideCounter + 1);
- sideCounter++;
- for (int i = 0; i < SIZE; i++) {
- printf("\t%c ", array[s][i]);
- }
- printf("\n");
- }
- }
- int shipsandcounter(void) //unfinished
- {
- int phShipStatus = 0, phMissiles = 0, phShipHit = 0, phShipMisses = 0; // ph = Placement holder || this is just to get the skeleton of what's needed.
- printf("\nSeminole state\t\t %i\t Missiles fired: %i\n", phShipStatus, phMissiles);
- printf("Airforce Academy\t %i\n", phShipStatus);
- printf("Valencia Destroyer\t %i\t Ship Hits:\t %i\n", phShipStatus, phShipHit);
- printf("Eskimo University\t %i\n", phShipStatus);
- printf("Deland High School\t %i\t Ship Misses:\t %i\n\n", phShipStatus, phShipMisses);
- return 0;
- }
- char mainMenu(char cMenuChoice) {
- printf("Valencia Battleship Game\nA] Start a New Game\nB] Resume an Existing Game\nC] View Best Scores\nD] Quit\n Your Choice: ");
- scanf_s("%c", &cMenuChoice);
- cMenuChoice = toupper(cMenuChoice);
- return cMenuChoice;
- }
- int main(void)
- {
- //Code starts
- const char* gameBoard[SIZE][SIZE];
- char cXcord = NULL, cYcord = NULL;
- int iXcord, iYcord;
- int ShipCount = 0;
- char actionString[4];
- char cMenuAction = NULL;
- mainMenu(cMenuAction);//uncomment this to get MainMenu working
- ship Deland = { NULL };
- InitializeBoard(enemyShips);
- //Generates and then places the starting segment(One coordinate) for each the ships
- makeFirstSegments(firstSegPositions, enemyShips);
- PAUSE;
- int gig, gig1;
- gig = firstSegPositions[0][0];
- gig1 = firstSegPositions[0][1];
- printf("\nInside of fucntion test:\nWHY DO THESE NUMBERS NOT WORK gig: %i gig1: %i\n", gig, gig1);
- setGig(Deland.Starter.Column, Deland.Starter.Row, firstSegPositions);
- printFirstSegments(firstSegPositions);
- PAUSE;
- int x = firstSegPositions[0][1];
- //makeOtherSegments(Deland.Starter.Column, &enemyShips);
- InitializeBoard(gameBoard);
- while (ShipCount >= 0) {
- while (actionString[0] != 'Q' & actionString[1] != 'Q') {
- char actionChar1, actionChar2;
- printf("\n======gameBoard=======\n");
- printBoard(gameBoard);
- shipsandcounter();
- printf("\n======enemyShips=======\n");
- printBoard(enemyShips);
- getShot(actionString);//gets coordinates of shot from user
- cXcord = actionString[0];
- cYcord = actionString[1];
- cls;//Clear screen to make it look prettier.
- cXcord = toupper(cXcord);//always caps
- //numbers from actionString into integers
- iXcord = charXcordtoInt(cXcord, actionString);
- iYcord = numChartoInt(actionString);
- printf("\n You shot at (%i,%i)!\n", iXcord, iYcord);
- //checks if enemyShips has a ship '0', if it does, print 'H' on gameBoard
- if (enemyShips[iYcord - 1][iXcord - 1] == "0") {
- gameBoard[iYcord - 1][iXcord - 1] = "H";
- printf("The shot Hit!");
- }
- else
- {
- gameBoard[iYcord - 1][iXcord - 1] = "M";
- printf("\nThe shot Missed!\n");
- }
- }
- }
- PAUSE;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement