0

I have organized it a bunch using a switch statement. as for the edges i have thought of using something like this but im not sure if its really going to work im too tired to finish tonight but heres what i have.

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


class cMakeBoard
{
    private:
        int iSide;
        int iSize;
        int **cSpace;
        int playerX; // x coordinate
        int playerY; // y coordinate
    
    public:
        cMakeBoard(int x) // constructor makes dynamic array for given size
        {
            iSide = x;
            cSpace = new int *[iSide]; //create rows
            for(int i = 0 ; i < iSide ; i++ )
            {
                cSpace[i] = new int[iSide]; //create columns
            }
            int iXPos;
            int iYPos;
    
            // initialize board
            for(iXPos = 0; iXPos < iSide; iXPos++)
            {
                for(iYPos = 0; iYPos < iSide; iYPos++)
                {
                    cSpace[iXPos][iYPos] = 9; // initial integer for empty spot
                }
            }
        }
        
        ~cMakeBoard()   // destructor deletes dynamic array for given size
        {
            //delete dynamic arrays
            for( int i = 0 ; i < iSide ; i++ )
            {
                delete [] cSpace[i] ;
            }
            delete [] cSpace;            
        }
        
        void displayBoard()
        {    
            // display board
            for(int iXPos = 0; iXPos < iSide; iXPos++)
            {
                for(int iYPos = 0; iYPos < iSide; iYPos++)
                {
                    cout << cSpace[iXPos][iYPos] << " "; 
                }
                cout << endl;
            }
        }
        
        void getHero()
        {
            char cCell;
            playerX = -1;
            playerY = -1;
            for(int iXPos = 0; iXPos < iSide; iXPos++)
            {
                for(int iYPos = 0; iYPos < iSide; iYPos++)
                {
                    cCell = cSpace[iXPos][iYPos];
                    if(cCell == 0)
                    {
                        playerX = iXPos;
                        playerY = iYPos;
                    }
                }
            }
            cout << playerX << ", " << playerY << endl;
        }        
        
        int iCreate();

        char getViewerSpot(int NumKey)
        {
            int  iTemp = -1;
            char cTemp = ' ';
            int iXSpot;
            int iYSpot;
            switch(NumKey)
            {
                case 1:
                    iXSpot = playerX-1;
                    if(iXSpot <0 )
                    {
                        iTemp = '|';
                    }
                    iYSpot = playerY+1;
                    if(iYSpot = iSide)
                    {
                        iTemp = '=';
                    }
                    iTemp = cSpace[iXSpot][iYSpot]; // 1 - down & left of player
                    break;
                case 2:
                    iTemp = cSpace[playerX+0][playerY+1]; // 2 - down from player
                    break;
                case 3:
                    iTemp = cSpace[playerX+1][playerY+1]; // 3 - down & right of player
                    break;
                case 4:
                    iTemp = cSpace[playerX-1][playerY+0]; // 4 - left of player
                    break;
                case 5:
                    iTemp = cSpace[playerX+0][playerY+0]; // 5 - player
                    break;
                case 6:
                    iTemp = cSpace[playerX+1][playerY+0]; // 6 - right of player
                    break;
                case 7:
                    iTemp = cSpace[playerX-1][playerY-1]; // 7 - up & left of player

                    break;
                case 8:
                    iTemp = cSpace[playerX+0][playerY-1]; // 8 - up from player
                    break;
                case 9:
                    iTemp = cSpace[playerX+1][playerY-1]; // 9 - up & right of player
                    break;
                default:
                    break;
            }
                    if(iTemp == 0)
                    {
                        cTemp = '~'; // get display char for player
                    }
            return cTemp;
        }
};

cMakeBoard::iCreate()
{

    
    // spawn player 0
    srand ( time(NULL) );
    cSpace[rand() % iSide][rand() % iSide] = 0; // then create character object
    // class object(0, '~', 500, 30); // set attributes in constructor
    
    // spawn player 1
    cSpace[rand() % iSide][rand() % iSide] = 1;
    // class object(1, 'M', 100, 60); // set attributes in constructor

    // spawn player 2
    cSpace[rand() % iSide][rand() % iSide] = 2;
    // class object(1, 'D', 100, 60); // set attributes in constructor

    // spawn player 3
    cSpace[rand() % iSide][rand() % iSide] = 3;
    // class object(1, 'B', 100, 60); // set attributes in constructor



    displayBoard();
    getHero();



    cout << getViewerSpot(7);
    cout << getViewerSpot(8);
    cout << getViewerSpot(9);
    cout << endl;
    cout << getViewerSpot(4);
    cout << getViewerSpot(5);
    cout << getViewerSpot(6);
    cout << endl;
    cout << getViewerSpot(1);
    cout << getViewerSpot(2);
    cout << getViewerSpot(3);
    cout << endl;
    
    return 0;
}

class cViewer
{
    private:
       static const int iMAX = 3;
       char cViewer[iMAX][iMAX];
    public:
        void InitViewer()
        {
            for(int x = 0; x < iMAX; x++)
            {
                for(int y = 0; y < iMAX; y++)
                {
                    cViewer[x][y] = ' ';
                }
            }
        }
        void DisplayViewer()
        {
            for(int x = 0; x < iMAX; x++)
            {
                for(int y = 0; y < iMAX; y++)
                {
                    cout << cViewer[x][y];
                }
                cout << endl;
            }            
        }
};

int main ()
{
    int iHold;
    srand ( time(NULL) );
    iHold = rand() % 12 + 9;
    cMakeBoard oBoard(iHold);
    cViewer    oViewer;
    oViewer.InitViewer();
    oViewer.DisplayViewer();
    oBoard.iCreate();
   
    cin >> iHold;
  return 0;
}
0

Also just in case your wondering here is my assignment in case youd like to help me any more lol.


Scenario: You are in a dangerous cavern and need to find your way out. You have to pass through 10 levels before you die. There are numerous creatures wandering about and you will each be harmed if you collide. When the game starts you are in a random place on the first level and must find the randomly located passage to the next level. This process will continue until you die or find the 10th exit in the 10th level.

Assignment:
1. You need to have at least 3 classes that will include: board, viewer & character. This is an individual assignment but you may discuss ideas with other students and help one another use specific code concepts. The main function must be lean and show high-level program flow like the TTT code example I posted on Moodle.

2. Your board class must contain private data to control a dynamic array. (Please refer to the class “clsFish” in the “Const & Dest” example as found in Moodle.) The size of the array must be input to the constructor function and may not be the same for any level. Your main function must generate a random number greater than 9 and pass it in when you create an object. The board must also track the location of the characters. The dynamic array must be deleted by the destructor function.

3. Your viewer class must display the 3X3 grid around your position. This will include any walls (array boundaries) or characters within this grid. Your character must always show up in the middle of this grid no matter where it physically resides on the board. The viewer must be updated before you are asked to move.

4. Your character class must have at least the following attributes: id number (unique object number), display char (what will show up in the viewer), life points (how much damage the character can still take), and hit points (how much damage this character inflicts upon other characters in a collision). You must have at least 4 different character types beyond the character you play. You must control the movement of your character with a number from 1-9. (This will allow you to use the numeric keypad.) You need to use modulus and integer division to translate the number into the direction [x+/-][y+/-] to move. (Ask Mr. Terry or his son James how to do this.) The other characters must move randomly (from -1 to +1) along one or both coordinates (after your move). The starting number of characters in each level must be about 10% of the area.

5. You need to handle collisions.
5.1. A wall collision occurs if a character tries to move to a position outside the array.
5.2. A character collision occurs if a character tries to move to a position occupied by another character.
5.2.1. Both characters suffer loss of life points equal to the hit points of the other character.
5.2.2. When any character has no life points left then it dies and is removed from the board. (If this is you the game is over.)
5.2.3. If the stationary character survives then the movement is not allowed.
5.2.4. If the stationary character dies then the movement is allowed.

0

I think the switch is a good idea. You then simply have to disallow movements to the left when you are on the first column, disallow movements to the up and left when you're in the corner, etc.

That's a fairly involved assignment, so you should test some of the functions separately and then integrate it all together.

0

yea thats a good idea instead of confusing myself ill just create functions for the remaining things and then put them in after they are working. thanks =D if i need help on any specific part ill post it.

0

Your ideas are solid jonsca. As for my findings: I haven't got any! Like I wrote, I ran it, and ran it and ran it. I'd managed to set up a break but stupidly it was in the wrong place. I could see the output but couldn't trace back to the root of the problem. The IDE gave me: "Access violation at address 00401340. Read of address 0000005D". I'll give it another go though.

0

I think I've spotted where the biggest problem lies. I believe that the coordinate system has been interchanged. The clue is in line 78 with the output of playerX and playerY. eg. the output here is (1, 5);

x x x x x x . . .
x x x x x 0 . . .
. . . . . . . . .

So playerX is the north-south movement, which means that the case statements need to be looked into again.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.