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