help with making a game

This is a discussion on help with making a game within the C++ Programming forums, part of the General Programming Boards category; Ok ... so I am writing this little program to play a game. Basically what I want to do is ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    help with making a game

    Ok ... so I am writing this little program to play a game. Basically what I want to do is draw a 6 x 6 grid on the screen. Initially I want all the squares to be blank. Behind each square I would like to hide numbers in pairs from 1 to 20. I want to have the user select two squares if the two numbers match I want to color the squares grey. If they do not match I want to hide the numbers again. This is kind of like the game concentration. Below is what I have for code so far.

    Code:
    #include <lvp\gui_top.h>
    #include <lvp\matrix.h>
    #include <lvp\string.h>
    #include <lvp\bool.h>
    #include <lvp\button.h>
    
    
    //------------------------------------------------------------------------
    class GridClass {
    public:
    	GridClass();
    	void Paint();
    	void MouseClick(int x, int y);
    	void InitGrid();
    
    private:
    	const int GridDimension;  // #of Rows/colums in grid
    	// Constants for the board
    	const int FirstPick, SecondPick, Empty;
    	matrix <int> Board; //Uses above constants
    	int Row, Col;
    	bool GameOver;
    	int NumClicks;
    	int BoxSize; //Pixels per box
    	int LeftMargin; //Pixels from left
    	int TopMargin; //Pixels from top
    	void XYToRowCol(int x, int y, int &Row, int &Col);
    	void MarkBox(int Row, int Col, int BoxContents);
    	ButtonClass QuitButton;
    };
    
    //--------------------------------------------------------------------------
    
    GridClass::GridClass()
    : GridDimensions(6) //6x6 Square
    
    Empty(0), FirstPick(-1), SecondPick(1), Board(GridDimension,GridDimension,0), 
    NumClicks(0), GameOver(false),
    BoxSize(getMaxY()/2/GridDimension), //Fill half of y-dimension
    LeftMargin((GetMaxX()-BoxSize*GridDimension)/2),
    TopMargin(GetMaxY()/4),
    QuitButton("Quit"),10,10,100,40)
    
    
    //----------------------------------------------------------------------------
    
    void GridClass::InitGrid()
    /*Initialize grid for game */
    {
    	NumClicks=0;
    	GameOver=false;
    	for (int Row=0; Row<GridDimension; Row++)
    		for (int Col=0; Col<GridDimension; Col++)
    			Board[Row][Col]=Empty;
    }
    
    //---------------------------------------------------------------------------
    
    void GridClass::XYToRowCol(int x, int y, int &Row, int &Col)
    
    {
    	intDistFromLeft =x-LeftMargin;
    	Col=(DistFromLeft + BoxSize)/BoxSize -1;
    	int DistFromTop=y-TopMargin;
    	Row=(DistFromTop+BoxSize)/BoxSize-1;
    	if (Col<0 || Col>=GridDimension ||
    		Row<0 || Ro >=GridDimension){
    		Row=-1;
    		Col=-1;
    	}
    }
    
    //----------------------------------------------------------------------------
    
    void GridClass::MarkBox(int Row, int Col, int BoxContents, int FirstPick, int SecondPick)
    {
    	SetColor(BLACK); // outline
    	SetFillColor(WHITE);
    	if(FirstPick == SecondPick)
    		SetFillColor(RED);
    	FilledRectangle(Col*BoxSize+LeftMargin, Row*BoxSize+TopMargin, 
    		(Col=1)*BoxSize+LeftMargin,(Row+1)*BoxSize+TopMargion);
    }
    button.h
    Code:
    //-----------------------------------------------------------------------
    class ButtonClass {
    public:
    	ButtonClass(String Text, int X1, int Y1, int X2, int Y2);
    	void Paint();
    	bool I$$$$(int X, int y);
    
    private:
    	int MyX1, MyY1, MyX2, MyY2;
    	String MyText;
    };
    
    //------------------------------------------------------------------------
    ButtonClass::ButtonClass(String Text int X1, int Y1, int X2, int Y2)
    	:MyText(Text), MyX1(X1), MyY1(Y1), MyX2(X2), MyY2(Y2)
    {
    }
    
    //------------------------------------------------------------------------
    void ButtonClass::Paint()
    {
    	SetColor(BLACK);
    	Rectangle(MyX1, MyY1, MyX2, MyY2);
    	gotoxy((MyX1+MyX2)/2, 5+(MyY1+MyY2)/2);
    	DrawCenteredText(MtText);
    }
    
    //------------------------------------------------------------------------
    bool ButtonClass:: I$$$$(intx, int y)
    {
    	return(x>=MyX1 && x<=MyX2 && y>=MyY1 && y<=MyY2);
    }
    gui_top.h
    Code:
    #include <windows.h>
    #include <lvp\string.h>
    
    void FilledCircle(int xc,int yc,int r);
    /* Draws a filled circle with the given center and radius */
    /* Color constants that may be used in calling SetColor,
    	SetTextColor, SetFillColor */
    const COLORREF GRAY = RGB(128,128,128);
    const COLORREF BLACK = RGB(0,0,0);
    const COLORREF WHITE = RGB(255,255,255);
    const COLORREF RED = RGB(255,0,0);
    const COLORREF GREEN = RGB(0,255,0);
    const COLORREF BLUE = RGB(0,0,255);
    const COLORREF YELLOW = RGB(255,255,0);
    const COLORREF ORANGE = RGB(255,128,0);
    const COLORREF VIOLET = RGB(128,0,255);
    
    void Circle(int xc,int yc,int r);
    /* Draws a circle with the given center and radius */
    void FilledCircle(int xc,int yc,int r);
    /* Draws a filled circle with the given center and radius */
    void Line(int x1, int y1, int x2, int y2);
    /* Draws a line from the point x1,y1 to x2,y2 */
    void Rectangle(int x1, int y1, int x2, int y2);
    /* Draws a rectangle with upper left corner at x1,y1 and
    	lower right corner at x2,y2 */
    void FilledRectangle(int x1, int y1, int x2, int y2);
    /* Draws a filled rectangle with upper left corner at x1,y1
    	and lower right corner at x2,y2 */
    void SetPixel(int x, int y);
    /* Plots the indicated pixel using the current pen color */
    
    
    void SetColor(COLORREF NewColor);
    /* Sets pen to the color indicated by the R/G/B values */
    void SetFillColor(COLORREF NewColor);
    /* Sets filling to the color indicated by the R/G/B values */
    void SetColor(int R, int G, int B);
    /* Sets pen to the color indicated by the R/G/B values */
    void SetFillColor(int R, int G, int B);
    /* Sets filling to the color indicated by the R/G/B values */
    void SetThickness(int PixelWidth);
    /* Sets pen thickness to the number of pixels specified */
    void FloodFill(int x, int y);
    /* Fills all connected pixels of the same color as the one at
    	x,y with the current fill color */
    
    
    void DrawText(String S);
    //void DrawTextF(String S);
    void DrawText(int N);
    void DrawText(long N);
    void DrawText(double N);
    /* Each draws its argument as text on the graphics display
    	starting at the "output cursor" position. */
    void DrawCenteredText(String S);
    void DrawCenteredText(int N);
    void DrawCenteredText(long N);
    void DrawCenteredText(double N);
    /* Each draws its argument as text on the graphics display
    	centered at the "output cursor" position. */
    void SetTextColor(COLORREF NewColor);
    void SetTextSize(int NewFontSize);
    void SetTextFont(String FontName);
    /* Controls the output of text.  Valid font names are:
    		Arial
    		System
    		Times New Roman
    If names other than these are sent, the font is left unchanged.
    Note that the "System" font cannot be changed in size */
    
    
    void gotoxy(int x, int y);
    /* Moves the implicit "output cursor" to the indicated
    	position for subsequent text output */
    int wherex();
    int wherey();
    /* Returns the current coordinates of the output cursor */
    int GetMaxX();
    int GetMaxY();
    /* Returns the x and y dimensions of the entire screen */
    
    int MessageBox(String Text, String Title);
    /* Generates a message box with an OK button */
    int MessageBoxYN(String Text, String Title);
    /* Generates a message box with YES and NO buttons.
    	Returns 1 if user hits YES button; 0 if hits NO button */
    
    //void MessageBeep(int);  // Predefined
    /*
    -1	Produces a standard beep sound by using the computer speaker.
    MB_ICONASTERISK	Plays the sound identified by the SystemAsterisk entry
     in the [sounds] section of WIN.INI.
    MB_ICONEXCLAMATION	Plays the sound identified by the SystemExclamation entry
     in the [sounds] section of WIN.INI.
    MB_ICONHAND	Plays the sound identified by the SystemHand entry
     in the [sounds] section of WIN.INI.
    MB_ICONQUESTION	Plays the sound identified by the SystemQuestion entry
     in the [sounds] section of WIN.INI.
    MB_OK	Plays the sound identified by the SystemDefault entry
     in the [sounds] section of WIN.INI.
    */
    
    // PostQuitMessage(0);  // Predefined
    /* Terminates program execution */
    
    int GetPixel(int x, int y);  // Not documented in text
    
    /* To add more functions, put prototype here, then
    	put prototype as friend in MyGuiWindow, then put body
    	at end of gui_bot.h                       */
    matrix.h
    Code:
    #ifndef _MATRIX_H
    #define _MATRIX_H
    
    
    #include <lvp\vector.h>
    
    
    template <class itemType>
    class matrix
    {
      public:
    
      // constructors/destructor
        matrix( );                                      // default size 0 x 0
        matrix( int rows, int cols );                   // size rows x cols
        matrix( int rows, int cols,
                const itemType & fillValue );           // all entries == fillValue
        matrix( const matrix & mat );                   // copy constructor
        ~matrix( );                                     // destructor
    
      // assignment
        const matrix & operator = ( const matrix & rhs );
    
      // accessors
        int numrows( ) const;                             // number of rows
        int numcols( ) const;                             // number of columns
    
      // indexing
        const vector<itemType> & operator [ ] ( int k ) const;  // range-checked indexing
        vector<itemType> & operator [ ] ( int k );              // range-checked indexing
    
      // modifiers
        void resize( int newRows, int newCols );   // resizes matrix to newRows x newCols
                                                   // (can result in losing values)
      private:
    
        int myRows;                             // # of rows (capacity)
        int myCols;                             // # of cols (capacity)
        vector<vector<itemType> > myMatrix; // the matrix of items
    };
    
    
    #include <lvp\matrix.cpp>
    #endif
    string.h
    Code:
    #ifndef _STRING_H
    #define _STRING_H
    
    #include <iostream.h>
    
    
    extern const int npos;  // used to indicate not a position in the string
    
    class String
    {
      public:
    
      // constructors/destructor
    
    	 String( );                         // construct empty string ""
    	 String( const char * s );          // construct from string literal
    	 String( const String & str );      // copy constructor
    	 ~String( );                        // destructor
    
      // assignment
    
    	 const String & operator = ( const String & str ); // assign str
    	 const String & operator = ( const char * s );       // assign s
    	 const String & operator = ( char ch );              // assign ch
    
      // accessors
    
    	 int    length( )                  const;    // number of chars
    	 int    find( const String & str ) const;  // index of first occurrence of str
    	 int    find( char ch )            const;    // index of first occurrence of ch
    	 String substr( int pos, int len ) const;    // substring of len chars
    																// starting at pos
    	 const char * c_str( )             const;    // explicit conversion to char *
    
      // indexing
    
    	 char   operator[ ]( int k )       const;    // range-checked indexing
    	 char & operator[ ]( int k );                // range-checked indexing
    
      // modifiers
    
    	 const String & operator += ( const String & str );// append str
    	 const String & operator += ( char ch );            // append char
    
    
      private:
    		int myLength;                     // length of string (# of characters)
    		int myCapacity;                   // capacity of string
    		char * myCstring;                 // storage for characters
    };
    
    // The following free (non-member) functions operate on strings
    //
    // I/O functions
    
    ostream & operator << ( ostream & os, const String & str );
    istream & operator >> ( istream & is, String & str );
    istream & getline( istream & is, String & str );
    
    // comparison operators:
    
    bool operator == ( const String & lhs, const String & rhs );
    bool operator != ( const String & lhs, const String & rhs );
    bool operator <  ( const String & lhs, const String & rhs );
    bool operator <= ( const String & lhs, const String & rhs );
    bool operator >  ( const String & lhs, const String & rhs );
    bool operator >= ( const String & lhs, const String & rhs );
    
    // concatenation operator +
    
    String operator + ( const String & lhs, const String & rhs );
    String operator + ( char ch, const String & str );
    String operator + ( const String & str, char ch );
    
    #include <lvp\String.cpp>
    #endif
    I dont even know if I am close on any of this. TIA

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    are you encountering any specific problems or do you just want us to make your code better for you?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I am stuck. I don't even know if I am headiing in the right direction. Does any of this even look like it will work.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    53
    don't even know if I am headiing in the right direction. Does any of this even look like it will work.
    try and compile it and see if it does work...
    Its all a matter of willpower

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    It won't compile. I am getting a ton a errors.

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    copy your errors and post at least some of them

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Here are some of the errors.

    Code:
    i:\game\button.h(14) : error C2144: syntax error : missing ',' before type 'int'
    i:\game\button.h(25) : error C2065: 'MtText' : undeclared identifier
    i:\game\button.h(29) : error C2065: 'intx' : undeclared identifier
    i:\game\button.h(29) : error C2062: type 'int' unexpected
    i:\game\button.h(30) : error C2143: syntax error : missing ';' before '{'
    i:\game\button.h(30) : error C2447: missing function header (old-style formal list?)
    i:\game\concent.cpp(40) : error C2612: trailing '<Unknown>' illegal in base/member initializer list
    i:\game\concent.cpp(40) : error C2758: 'GridDimension' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(20) : see declaration of 'GridDimension'
    i:\game\concent.cpp(40) : error C2758: 'FirstPick' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(22) : see declaration of 'FirstPick'
    i:\game\concent.cpp(40) : error C2758: 'SecondPick' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(22) : see declaration of 'SecondPick'
    i:\game\concent.cpp(40) : error C2758: 'Empty' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(22) : see declaration of 'Empty'
    i:\game\concent.cpp(40) : error C2512: 'ButtonClass' : no appropriate default constructor available
    i:\game\concent.cpp(40) : error C2614: 'GridClass' : illegal member initialization: 'GridDimensions' is not a base or member
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(41) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(41) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(42) : error C2065: 'getMaxY' : undeclared identifier
    i:\game\concent.cpp(42) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(43) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(44) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(45) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(45) : error C2059: syntax error : ')'
    i:\game\concent.cpp(50) : error C2144: syntax error : missing ';' before type 'void'
    i:\game\concent.cpp(52) : error C2601: 'InitGrid' : local function definitions are illegal
    i:\game\concent.cpp(64) : error C2601: 'XYToRowCol' : local function definitions are illegal
    i:\game\concent.cpp(79) : fatal error C1903: unable to recover from previous error(s); stopping compilation

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I still can't get this thing to compile. I have tried making som changes but it just seems to get worse. Can anyone help me out?

    TIA

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Not trying to be rude, but did you try and figure out any of these errors? The first two you might not have been able to recognize. They are there because in button.h, you use the String class. You need to #include "String.h" in button.h.

    The intx error, though, I would think you should be able to see when you go to that line in the code that you accidentally typed intx instead of int x. I didn't look at the rest of the errors, since it would help you to be able to find as many as you can yourself.

    Besides, usually what I do is look for a couple of easy fixes like those two, then recompile and see what other errors go away because of the fixes. Then look at a few more errors. The only way to fix a mountain of compile errors is to go through each one.

    Also, it is generally a good idea to compile while you are writing the code. Write a little bit of code that should work by itself, then compile, then write more code, then compile. This makes the pile of errors much smaller and easier to deal with.

    If you have fixed a few of the errors, try posting your updated code and the current errors you are stuck with.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I am down to 24 errors.

    Code:
    i:\game\button.h(32) : error C2447: missing function header (old-style formal list?)
    i:\game\concent.cpp(40) : error C2612: trailing '<Unknown>' illegal in base/member initializer list
    i:\game\concent.cpp(40) : error C2758: 'GridDimension' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(20) : see declaration of 'GridDimension'
    i:\game\concent.cpp(40) : error C2758: 'FirstPick' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(22) : see declaration of 'FirstPick'
    i:\game\concent.cpp(40) : error C2758: 'SecondPick' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(22) : see declaration of 'SecondPick'
    i:\game\concent.cpp(40) : error C2758: 'Empty' : must be initialized in constructor base/member initializer list
            i:\game\concent.cpp(22) : see declaration of 'Empty'
    i:\game\concent.cpp(40) : error C2512: 'ButtonClass' : no appropriate default constructor available
    i:\game\concent.cpp(40) : error C2614: 'GridClass' : illegal member initialization: 'GridDimensions' is not a base or member
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(40) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(41) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(41) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(42) : error C2065: 'getMaxY' : undeclared identifier
    i:\game\concent.cpp(42) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(43) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(44) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(45) : error C2064: term does not evaluate to a function
    i:\game\concent.cpp(45) : error C2059: syntax error : ')'
    i:\game\concent.cpp(50) : error C2144: syntax error : missing ';' before type 'void'
    i:\game\concent.cpp(52) : error C2601: 'InitGrid' : local function definitions are illegal
    i:\game\concent.cpp(64) : error C2601: 'XYToRowCol' : local function definitions are illegal
    i:\game\concent.cpp(79) : fatal error C1903: unable to recover from previous error(s); stopping compilation

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Sorry to say, but this is yet another example of someone writing way more code than they should before pressing compile.

    Basically, find the level (ie number of lines) you can add to a program without adding any compilation errors (usually).

    Then get into the habit of pressing compile at that frequency. It's cheap and easy to do, takes just a couple of seconds, and you'll instantly know that any new errors are pretty certain to be in the 10 lines or so you've just added.

    It takes a vast amount of skill to even attempt writing an entire program without pressing compile.
    And an equal amount of skill to cope with the avalanche of errors which usually result from such folly.

    Personally, I'd create a new project from this mess and copy one class / function at a time - fixing as you go.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-25-2006, 11:14 PM
  5. Lets Play Money Making Game
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-04-2001, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21