Thread: random generate ships on the map in battleship game

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    1

    random generate ships on the map in battleship game

    This program is in C language.
    In Battleship,(one player vs computer), you are a submarine commander patrolling your op area, a 6 by 15 cell area of ocean. Your task is to hunt down and torpedo as many ship as you can find. Intelligence reports have told you that there are 6 ship in your op area which the enemy (the computer) has placed in random positions with random orientations (N-S or E-W). The types of ship known to be in your op area are:
    Three Crigates, each size one cell,
    Two Fruisers, each size two cells,
    and One Tabblephis, size three cells.
    Your submarine can be driven around the ocean using the 'h', 'j', 'k', and 'l' keys
    h -- move one cell to the left
    j -- move one cell to down
    k -- move one cell to up
    l -- move one cell to the right
    When you are in a cell where you think there is a ship, or part of a ship, you can shoot a torpedo by hitting the Enter key. You score points for your patrol report when you hit a ship, and bonus points when you sink one. To sink a ship, you must hit all cells of that ship with a torpedo. You score one point for each torpedo hit. When you sink a ship, you get one additional point for each cell of the ship; i.e. 1 extra point for sinking a Crigate, 2 extra points for sinking a Fruiser, and 3 extra points for sinking the Tabblephis. (The maximum score is 20 points). You begin your mission with 40 torpedoes.
    The mission ends either when all of the ship have been sunk, or when you run out of torpedos. In addition, you can quit the mission (in shame :-)) at any time by hitting the 'q' key.
    One of the more difficult modules to implement in this program is the display module. So, I will join your software team and help you write that module. You will find a header file with the prototypes of the functions I provide in:
    Code:
    /*      File:   dispalay.h
    */
    extern int REALLY_RANDOM;
    void init_ocean(void);
    /* This function is given and returns nothing. It initializes the
            display showing the ocean and torpedo count and score headings. */
    void show_boat(int latitude, int longitude);
    /* This funtion is given the latitude and longitude of the sumbarine
            and returns nothing. It displays the submarine on the ocean.    */
    void show_explosion(int latitude, int longitude);
    /* This funtion is given the latitude and longitude of a hit on a Phis
            and returns nothing. It displays a permanent explosion on the
            ocean.                                                           */
    void show_miss(int latitude, int longitude);
    /* This funtion is given the latitude and longitude of a torpedo miss
            and returns nothing. It displays a permanent miss on the ocean. */
    void show_ship(char ship_type, int latitude, int longitude);
    /* This function is given a character corresponding to a Phis type
            ('T', 'F', or 'C'), and the longitude and latitude and returns
            nothing. It shows a cell of that Phis type on the ocean. It
            should be used at the end of the game to show the position of
            all Phis' on the board.                                          */
    void update_torpedo(int how_many);
    /* This function is given an integer representing the number of
            torpedos remaining and returns nothing. It updates the
            torpedo count on the screen.                                     */
    void update_score(int score);
    /* This function is given an integer representing the current
            score of the game and returns nothing. It updates the
            score on the screen.                                             */
    void write_message(char *s);
    /* This function is given a string and writes it to the bottom
            of the screen. Only the first 17 characters are displayed.
            It returns nothing.                                              */
    void clear_screen(void);
    /* This function is given and returns nothing. It clears the screen
            and shuts down the display. It should be used at the end of
            the program.                                                     */
    

    __________________________________________________ _______________
    here is what I have, driver.c:
    Code:
    
    #include<stdio.h>
    #include"display.h"
    #include"command.h"
    int main()
    {
    char ch;
    int a = 0;
    int b = 0;
    int i = 40;
    init_ocean();
    show_boat(a,b);
    while(command(&a,&b) != 'q')
    {
    update_score(0);
    update_torpedo(i);
    show_boat(a,b);
    }
    clear_screen();
    return 0;
    }
    ________________________________________________
    commmand.c:
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<curses.h>
    #include"command.h"
    #include "display.h"
    char command(int *x, int *y)
    {
    char c;
    c = getch();
    switch(c)
    {
    case 'k': if(*x > 0)*x=*x-1; return c;
    case 'l': if(*y +1< MAX_ROWS)*y = *y+1; return c;
    case 'h': if(*y > 0) *y = *y -1; return c;
    case 'j': if(*x +1< MAX_COLUMNS) *x=*x+1; return c;
    default : return c;
    }
    }
    __________________________________
    command.h:
    Code:
    
    
    Code:
    char command( int *x, int *y);
    
    #define MAX_ROWS 15
    #define MAX_COLUMNS 6
    _______________________________________
    Now I run the driver I can move the target by h,j,k,l.

    please help me on random generate ships on the map: randomship.c
    ( void show_ship(char ship_type, int latitude, int longitude);
    and update_torpedo and update_score part.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So apart from ripping off some 25 year old code, what have YOU actually done?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Battleship game
    By Sam636 in forum Game Programming
    Replies: 2
    Last Post: 01-24-2013, 07:29 PM
  2. Replies: 4
    Last Post: 05-02-2012, 10:22 PM
  3. battleship game help me plaaaaaaese:(
    By mnmn in forum C Programming
    Replies: 5
    Last Post: 03-15-2011, 07:29 AM
  4. here is my battleship game so far...come try it out
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2002, 12:31 PM
  5. c++ battleship game
    By JTtheCPPgod in forum Game Programming
    Replies: 4
    Last Post: 01-05-2002, 11:12 AM

Tags for this Thread