Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,915 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,128 people online right now. Registration is fast and FREE... Join Now!




ARRAYS HELP

 
Reply to this topicStart new topic

ARRAYS HELP

momentsb4autumn
30 Dec, 2007 - 06:19 AM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 18


My Contributions
in this code, how come it's coming up weird?


CODE


#include <iostream>
using namespace std;

//defining that the rows equal 21 and the columns equal 16
#define ROWS 21
#define COLUMNS 16

void Board( double[ROWS][COLUMNS] );    //function declaration

// this is my game board
void draw(char Board[ROWS][COLUMNS])
{
    for(int x=0; x<COLUMNS;x++)
       {
       for(int y=0; y<ROWS; y++)
       {
           std::cout << "--";
       }
       std::cout << "-" <<std::endl;
       for (int y=0;y<ROWS; y++)
       {
           std::cout << "|" << Board[x][y];
          
       }
       std::cout << "|" <<std::endl;
    }
    for(int y=0; y <ROWS;y++)
    {
        std::cout << "--";
    }
    std::cout << "-" << std::endl;
}

// main function for my moving ships TEST
int main()
{
    int x, y;
    char Board[ROWS][COLUMNS];
    // drawing the board as a blank board
        
    for(unsigned int x=1; x<ROWS; ++x)
        Board[x][0]='A'+x-1;
    for(unsigned int y=1; y<COLUMNS; ++y)
        Board[0][y]='A'+y-1;
    // testing the position of a single Torpedo Boat (#)
    x=2;
    y=3;
    Board[x][y]='#'; //symbol of the ship
    char direction;
    while(1)
     {
      draw(Board);
      cout << "\nEnter a letter to move (U)p, (D)own, (L)eft, or (R)ight or press (E) to Exit: ";
      cin >> direction;

    switch (direction)
    {
    case 'U': case 'u': Board[x][y]=' '; x--; Board[x][y]='#';
        break;
    case 'D': case 'd': Board[x][y]=' '; x++; Board[x][y]='#';
        break;
    case 'L': case 'l': Board[x][y]=' '; y--; Board[x][y]='#';
        break;
    case 'R': case 'r': Board[x][y]=' '; y++; Board[x][y]='#';        
        break;
    case 'E': case 'e':
        exit(0);
        break;
    }
}
}






thanks for any help happy.gif ph34r.gif

This post has been edited by momentsb4autumn: 30 Dec, 2007 - 06:20 AM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: ARRAYS HELP
30 Dec, 2007 - 08:14 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
define weird.

What is the output and what were you expecting?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: ARRAYS HELP
30 Dec, 2007 - 08:39 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
I think you should pay closer attention to what is a row and what is a column.

You defined Board[ROWS][COLUMNS]
you have x going from 0 to COLUMNS, and y going from 0 to ROWS. Then you use Board[x][y]...Since rows != columns you run into problems.

I find it better to name my variables 'rows' and 'columns' rather than 'x' and 'y' as it helps keep in mind what is what.

here is what I did to get the code working:
CODE


#include <iostream>
using namespace std;

//defining that the rows equal 21 and the columns equal 16
#define ROWS 21
#define COLUMNS 16

void Board( double[ROWS][COLUMNS] );    //function declaration

// this is my game board
void draw(char Board[ROWS][COLUMNS])
{
    int x,y;
    for( x=0; x<COLUMNS;x++)
       {
       for(y=0; y<ROWS; y++)
       {
           std::cout << "--";
       }
       std::cout << "-" <<std::endl;
       for (y=0;y<ROWS; y++)
       {
           std::cout << "|" << Board[y][x];
          
       }
       std::cout << "|" <<std::endl;
    }
    for(y=0; y <ROWS;y++)
    {
        std::cout << "--";
    }
    std::cout << "-" << std::endl;
}

// main function for my moving ships TEST
int main()
{
    int x, y;
    char Board[ROWS][COLUMNS];
    for (x=0; x<COLUMNS; ++x) {
        for (y=0;y<ROWS;++y) {
            Board[y][x]='.';
        }
    }
    // drawing the board as a blank board
        
    for(x=1; x<COLUMNS; ++x)
        Board[0][x]='A'+x-1;
    for(y=1; y<ROWS; ++y)
        Board[y][0]='A'+y-1;
    //draw(Board);
    // testing the position of a single Torpedo Boat (#)
    x=2;
    y=3;
    Board[y][x]='#'; //symbol of the ship
    char direction;
    while(1)
    {
        draw(Board);
        cout << "\nEnter a letter to move (U)p, (D)own, (L)eft, or (R)ight or press (E) to Exit: ";
        cin >> direction;

        switch (direction)
        {
            case 'U': case 'u': Board[y][x]=' '; x--; Board[y][x]='#';
                break;
            case 'D': case 'd': Board[y][x]=' '; x++; Board[y][x]='#';
                break;
            case 'L': case 'l': Board[y][x]=' '; y--; Board[y][x]='#';
                break;
            case 'R': case 'r': Board[y][x]=' '; y++; Board[y][x]='#';        
                break;
            case 'E': case 'e':
                exit(0);
                break;
        }
    }
    return 0;
}


Note that I didn't put a lot of thought into the logic, I just made it work.
User is offlineProfile CardPM
+Quote Post

momentsb4autumn
RE: ARRAYS HELP
30 Dec, 2007 - 09:20 AM
Post #4

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 18


My Contributions
QUOTE(William_Wilson @ 30 Dec, 2007 - 09:14 AM) *

define weird.

What is the output and what were you expecting?



I would like to have A B C D E F G H I J in the horizontal plane as well as the vertical plane on the top and left sides, plus inside each square I want it to be an empty space and not have another square inside it?

How do I get rid of the numbers o nthe left side?


happy.gif

This post has been edited by momentsb4autumn: 30 Dec, 2007 - 09:21 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: ARRAYS HELP
30 Dec, 2007 - 09:23 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
See the above code.
User is offlineProfile CardPM
+Quote Post

momentsb4autumn
RE: ARRAYS HELP
30 Dec, 2007 - 09:34 AM
Post #6

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 18


My Contributions
QUOTE(NickDMax @ 30 Dec, 2007 - 10:23 AM) *

See the above code.


Thanks so much for your assistance, my brain is fried and it's about 1AM here...


How would I make a string of "#" such as "#####" or

#
#
#
#
#

move with all elements moving along with it when I choose a direction?


take care happy.gif

and happy New Year!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: ARRAYS HELP
30 Dec, 2007 - 03:58 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well you have a data structure that holds the board and it seems to work ok. Now make a data structure to hold a ship. Say something like:

CODE
struct ship {
int row;  //first the location
int col;
int dir;   //the orientation
int len;  //the length
}


It should not be all that hard to now place the ship somewhere on the board.

You can then maintain a collection of ships and "redraw" the board by clearing the board, then placing the ships.
User is offlineProfile CardPM
+Quote Post

chain3691
RE: ARRAYS HELP
3 Jan, 2008 - 03:55 PM
Post #8

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 1

Please give some more information on whether the output is supposed to be what's mentioned in the 'COUT' and whether it's supposed to be the 'board' or 'COUT' that's get input first.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month