Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- #include <time.h>
- #define SIZE 10
- #define FILENAME "C:/Documents and Settings/3413/Desktop/HighScores.txt"
- void printBoard(int arr[SIZE][SIZE], int hidden); //Prints the board. If 'hidden' = 1, print hidden board
- void randomizeShips(int arr[SIZE][SIZE],int num_ships); //Sets 3 ships into the board, with at least one vertical
- int checkHit(int arr[SIZE][SIZE], int row, int col); //Returns a 1 if hit, a 0 if miss
- int checkWin(int arr[SIZE][SIZE]); //Returns a 1 if all battleships have been sunk, otherwise 0
- void InitializeBoard(int arr[SIZE][SIZE]); //Sets all board locations to a value (i.e. “0” or “~”)
- void takeTurn(int *row, int *col);
- //void printWin();
- void highScores();
- // movesTaken();
- void shipSunk();
- //int hitAmount[3] = {0, 0, 0};
- int main(void)
- {
- //Variables: Hidden Array, Users array
- int board[SIZE][SIZE];
- int row, col;
- int hits = 0;
- int misses = 0;
- //Display Instructions
- printf("Welcome to Battleship\n\n\n\n\n\n\n");
- system("PAUSE");
- system("CLS");
- printf("|--------------|\n");
- printf("| Instructions:|\n");
- printf("|--------------|\n");
- printf("|---------------------------------------------|\n");
- printf("| 1. You are playing against the computer |\n");
- printf("| 2. To win, you must sink all 3 of your |\n");
- printf("| opponents ships |\n");
- printf("| 3. To choose your space, enter the number of|\n");
- printf("| the row, and then the number of the |\n");
- printf("| column |\n");
- printf("| ie: 63 |\n");
- printf("|---------------------------------------------|\n\n\n\n\n\n\n");
- system("PAUSE");
- system("CLS");
- //Initialize the board
- InitializeBoard(board);
- printBoard(board, 0);
- //Input ships on board
- randomizeShips(board, 3);
- //Loop the following until the user has won:
- //Display board to the user
- printBoard(board, 1);
- //If in 'Debug Mode,' print out hidden board
- //Ask the user which location they want to select
- do
- {
- takeTurn(&row, &col);
- checkHit(board, row, col);
- }while(hits < 9);
- //Update board
- printBoard(board, 0);
- //Tell user if hit or miss
- //If its a hit, check to see if they won
- //checkWin(board);
- //Tell user if they won
- //printWin();
- //Give user their score (How many moves it took them to win)
- //movesTaken();
- }
- //Board is set up as follows:
- /*
- 0 = no hit or miss or ship
- 1 = ship1(hasnt been hit)
- 2 = ship2 (hasnt been hit)
- 3 = ship3 (hasnt been hit)
- 4 = hit
- 5 = miss
- */
- void InitializeBoard(int arr[SIZE][SIZE])
- {
- int row, col;
- for(row = 0; row < SIZE; row++)
- for(col = 0; col < SIZE; col++)
- arr[row][col] = 0;
- printf("\n");
- }
- void printBoard(int arr[SIZE][SIZE], int hidden)
- {
- int row, col;
- for(row = 0; row < SIZE; row++){
- for(col = 0; col < SIZE; col++){
- if((hidden == 0) && (arr[row][col] == 0)) //No hits/miss/ship
- printf("%2c", '~');
- else if((hidden == 0) && (arr[row][col] == 1)) //ship (dont show to user)
- printf("%2c", '~');
- else if((hidden == 0) && (arr[row][col] == 2))
- printf("%2c", '~');
- else if((hidden == 0) && (arr[row][col] == 3))
- printf("%2c", '~');
- else if((hidden == 0) && (arr[row][col] == 4))
- printf("%2c", 'H');
- else if((hidden == 0) && (arr[row][col] == 5))
- printf("%2c", 'M');
- else if((hidden == 1) && (arr[row][col] == 0))
- printf("%2c", '~');
- else if((hidden == 1) && (arr[row][col] == 1))
- printf("%2c", '1');
- else if((hidden == 1) && (arr[row][col] == 2))
- printf("%2c", '2');
- else if((hidden == 1) && (arr[row][col] == 3))
- printf("%2c", '3');
- else if((hidden == 1) && (arr[row][col] == 4))
- printf("%2c", 'H');
- else if((hidden == 1) && (arr[row][col] == 5))
- printf("%2c", 'M');
- }
- printf("\n");
- }
- }
- void randomizeShips(int arr[SIZE][SIZE],int num_ships)
- {
- int placed_ships = 0;
- int loop = 0;
- int Horizontal = 0;
- int Vertical = 0;
- int i = 0;
- int row = 0;
- int col = 0;
- //Loop the place 3 ships. Loop ends when all 3 ships are placed.
- //OR the loop has been run 100 times.
- srand(time(0));
- while((placed_ships < num_ships)&&(loop < 100))
- {
- Horizontal = rand()%2;
- if(Horizontal == 1)
- {
- row = rand()%SIZE;
- col = rand()%(SIZE-3);
- if((arr[row][col] == 0) && (arr[row][col+1] == 0) && (arr[row][col+2] == 0))
- {
- arr[row][col] = placed_ships+1;
- arr[row][col+1] = placed_ships+1;
- arr[row][col+2] = placed_ships+1;
- placed_ships++;
- }
- }
- else
- {
- col = rand()%SIZE;
- row = rand()%(SIZE-3);
- if(arr[row][col] == 0 && arr[row+1][col] == 0 && arr[row+2][col] == 0)
- {
- arr[row][col] = placed_ships+1;
- arr[row+1][col] = placed_ships+1;
- arr[row+2][col] = placed_ships+1;
- placed_ships++;
- }
- }
- }
- system("CLS");
- printBoard(arr, 0);
- }
- /*
- void promptUser(int *row, int *col)
- {
- //Ask the user to enter a value (Explain it needs to be A1, B2, C3 etc)
- //Scan in the value into a character and an int.
- //Dummy proof if desired/needed (Make sure letter is between 'A' ad 'J' and number between '1' and '10'.
- //Convert letter to row number (Use 'If' statments or ASCII or case)
- //Convert the number to a column number( subtract 1 from what they give you).
- }
- */
- void takeTurn(int *row, int *col)
- {
- printf("Enter a coordinate\n");
- printf("Row \n");
- scanf("%d", row);
- printf("Column \n");
- scanf("%d", col);
- }
- int checkHit(int arr[SIZE][SIZE], int row, int col) //Returns a 1 if hit, a 0 if miss
- {
- int misses = 0;
- int hits = 0;
- if(arr[row][col] == '~')
- {
- printf("Miss\n");
- arr[row][col] = '0';
- misses++;
- }
- else
- {
- printf("Hit!!\n");
- arr[row][col] = '1';
- hits++;
- }
- }
- //void shipSunk()
- //{
- // int hitAmount[3] = {0, 0, 0};
- // if('1' == 3)
- // {
- // printf("You sunnk my Destroyer!\n");
- // }
- // else if('2' == 3)
- // {
- // printf("You sunk my Cruiser!\n");
- // }
- // else if('3' == 3)
- // {
- // printf("You sunk my Submarine!\n");
- // }
- //}
- /*
- int checkWin(int arr[SIZE][SIZE]) //Returns a 1 if all battleships have been sunk, otherwise 0
- {
- //Search through the entire array (Use pointers or nested "for" loops).
- if(hits == 9)
- {
- printWin();
- }
- //For each value, you check if it is 1, 2, or 3.
- //If you find a 1, 2, or 3, you return 0(Zero), which means they havent won.
- //If you dont find a 1, 2, or 3, you return a 1, which means they have won.
- }
- char myChar[81];
- gets(myChar)
- row = myChar[0]
- if(myChar[2] != "\0")
- col = 9;
- else
- col = myChar;
- }
- // printWin()
- //{
- // printf("Congratulations! You won!!\n");
- // system("PAUSE");
- //}
- */
- void highScores()
- {
- char name[3];
- int score;
- FILE * sensor = fopen (FILENAME, "w");
- if (sensor == NULL)
- {
- printf("Error opening file.\n");
- }
- else
- {
- scanf("%s", name);
- fprintf(sensor, "%3.3s %d\n", name, score);
- fclose(sensor);
- }
- }
Add Comment
Please, Sign In to add comment