Placing Battleships
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Placing Battleships

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    Placing Battleships

    I'm trying to place battleships in my battleship board which I've already created using a sub routine. I'm trying to create another sub routine to do this... so far this is what I have.

    When I enter in the coordinates it just keeps giving me back the same ship, It wont let me move onto the next one... What am I doing wrong?

    # include <stdio.h>
    # include <stdlib.h>

    void get_imput(player)
    char player;
    {
    int i,j;
    char H;
    char V;
    char o;

    do{
    printf("Shots fired! Commander, it's time for battle!\n");
    printf("Enter the coordinates of your carrier #,# and its orientation (H or V).\nMake sure these coordinates are between 1-10\n");
    printf("ex. 2 3 H\n");
    scanf("%d %d %c",&i,&j,&o);
    }
    while ( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));

    printf("Enter the coordinates of your battleship in the same format\n");
    scanf("%d %d %d",&i,&j,&o);

    while( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));

    printf("Enter the coordinates of your destroyer in the same format\n");
    scanf("%d,%d %d",&i,&j,&o);

    while( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));

    printf("Enter the coordinates of your submarine in the same format\n");
    scanf("Deploy ship here %d,%d %d",&i,&j,&o);

    while( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));

    printf("Enter the coordinates of your submarine in the same format\n");
    scanf("Deploy ship here %d,%d %d",&i,&j,&o);

    while( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));
    }

    /*----------------------------------------------------------------------------
    ------------------------------------------------------------------------------*/
    void print_board(board)
    char board [10][10];
    {
    int i,j;
    int a=1;
    int c=1;

    for (a=0;a<11;a++){
    printf( "%4d",a);}
    printf("\n");
    printf(" +---+---+---+---+---+---+---+---+---+---+\n\n");

    for (i=0;i<10;i++){
    for(j=0;j<10;j++){
    board[i][j]= '|';}}

    for(i=0;i<10;i++){
    printf("%4d|",c++);

    for(j=0;j<10;j++){
    printf("%4c",board[i][j]);}
    printf("\n\n");}

    printf(" +---+---+---+---+---+---+---+---+---+---+\n\n");
    }

    /*---------------------------------------------------------------------------
    -----------------------------------------------------------------------------*/




    int main(argc, argv)
    int argc;
    char *argv[];
    {
    char board1[10][10];
    char player;
    printf(" Player1\n\n");
    print_board(board1);
    get_imput(player);


    char board2[10][10];
    printf(" Player2\n\n");
    print_board(board2);

    exit(0);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,482

    Re: Placing Battleships

    Quote Originally Posted by rycurt11 View Post
    ... What am I doing wrong?
    You forgot to add Code tags around your code snippets. So your code is now absolutely unreadable.
    Also, try to debug step-by-step to see what, where and why goes wrong!
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,575

    Re: Placing Battleships

    Please format your code before posting. Unformatted code is practically unreadable. Even for your own use, formatted code is much easier to read and understand - especially when things are not working as expected.

    First off, I don't know who/where taught you to code like this, but function definitions like

    Code:
    void get_imput(player)
    char player;
    {
    are called 'Kernighan & Ritchie' style function definitions and are not part of the c++ standard. Functions should be defined like this

    Code:
    void get_imput(char player)
    {
    with the type of parameter defined along with the variable name.

    Code:
    do {
    	printf("Shots fired! Commander, it's time for battle!\n");
    	printf("Enter the coordinates of your carrier #,# and its orientation (H or V).\nMake sure these coordinates are between 1-10\n");
    	printf("ex. 2 3 H\n");
    	scanf("%d %d %c",&i,&j,&o);
    } while ( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));
    What are you hoping to achieve with the while condition? If you are trying to check for invalid input and loop until input is valid, this while condition is wrong. You need the condition to check for invalid values (eg i > 10 || i < 1 etc etc).


    Code:
    	printf("Enter the coordinates of your battleship in the same format\n");
    	scanf("%d %d %d",&i,&j,&o);
    
    	while( i < 10 && j < 10 && i > 0 && j > 0 && (o != 'H' || o != 'V'));
    o is defined to be of type char yet you are reading it as type integer. Also, you are overwriting the values of i, j and o just entered previously without first having saved or used them. Also, what do you expect to achieve with the while loop? It will either loop forever or stop after one check. Neither is very useful!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center