Sign in to follow this  

Help a noob please!

This topic is 4811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm such a noob that I don't know how to include files the right way. =D ################################ I'm pretty new (been programming for over a week now, decided to start off with C++, and have 1 more page to go in the tutorials at gametutorials.com). So, I've been trying to program this "dot" game, where you make boxes by connecting dots, well, the rules are not really important as of right now, when I finish the game, I'll let you guys play it and see for yourselves. *Note* please, don't give me the entire code if you've programmed this before, my friend has done that for the tic-tac-toe, and I ended up not learning a lot... Well, the deal is: I'm supposed to get 2 different inputs (COORDS that I store in two x and y ints) from the mouse. I click in one coord in the screen buffer, if it's a 'dot/period (.)' (ASCII 46), it sends these integers as parameter to antoehr get input function, that will then draw the whatever if you click at another '.' (I haven't implemented it yet, but it doesn't make a difference, since it never GETS there!). Okay, I did that, as you can see in the code below, and I've tried everything I know! but the code doesn't seem to work. What I think is happening is, if you look in the main, the getInput is being called again without being "finished" the first time (that's what I think anyway), and since I really don't know how to make the while loop 'wait' for the getInput function to be completely finished, I'm pretty much screwed as of right now. Well, enough talking here is hte code (ps, if you want the header file, ill post it, I didn't think it would matter) :
(game.cpp)

#include "game.h" /* See "game.h" for more information */

/* Constructor - Only one, no need to pass parameters */
GAMEDATA::GAMEDATA() 
{
srand(GetTickCount()); /* Seed random number generator */

winner = NO_WINNER; /* Nobody has one yet */
}

/* Initiate GAMEDATA */
void GAMEDATA::init()
{
/* Initiate Grid */
for(int i = 0; i < (BOARD_WDTH * BOARD_HGT); i++)
{
screen_buff[i].Char.AsciiChar = (char)grid[i];
screen_buff[i].Attributes = FOREGROUND_BLUE; 
}

/* Could also do this 
for(int x = 0; x < BOARD_WDTH; x++)
{ 
for(int y = 0; y < BOARD_HGT; y++)
{
screen_buff[x+y*BOARD_WDTH].Char.AsciiChar = (char)grid[x+y*BOARD_WDTH];
screen_buff[x+y*BOARD_WDTH].Attributes = FOREGROUND_BLUE; 

}
} */

/* Get our standard input and output handles */
out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
in_handle = GetStdHandle(STD_INPUT_HANDLE);

/* Set the rect of where we want to draw to */
scr_rect.Left = X_START;
scr_rect.Right = X_START + (BOARD_WDTH - 1);
scr_rect.Top = Y_START;
scr_rect.Bottom = Y_START + (BOARD_HGT - 1);

SetConsoleMode(in_handle,ENABLE_MOUSE_INPUT);

turn = rand()%2; /* Randomly pick who starts */

drawScreen(); /* Draw the screen */
}

/* Draws the screen with the cool looking grid */
void GAMEDATA::drawScreen()
{ 
COORD size = {BOARD_WDTH , BOARD_HGT}; /* The size of the buffer in (width, height) format */
COORD corner = {0, 0}; /* Upper left corner to begin writing buffer from */ 

WriteConsoleOutput(out_handle, screen_buff, size, corner, &scr_rect);
}

/* Get the user input (mouse clicks) */
void GAMEDATA::getInput()
{
int x_pos = 0, y_pos = 0;
bool done = false;

INPUT_RECORD input_record; /* Keeps track of your inputs */
DWORD events; /* Necessary for events */
/* Get any input */
do{
ReadConsoleInput(in_handle, &input_record, 1, &events); /* '1' means only 1 event to keep record */

if(input_record.EventType == MOUSE_EVENT) /* Check for mouse events */
{
/* If they DID NOT hit the left mouse button, we don't care about the input */
if(input_record.Event.MouseEvent.dwButtonState != FROM_LEFT_1ST_BUTTON_PRESSED)
return;

/* Get the coordinates the clicked on */
if(screen_buff[x_pos + y_pos * BOARD_WDTH].Char.AsciiChar == 46) {
x_pos = input_record.Event.MouseEvent.dwMousePosition.X;
y_pos = input_record.Event.MouseEvent.dwMousePosition.Y;
getInput(x_pos,y_pos);
done = true;
}

}
} while(!done);

}

void GAMEDATA::getInput(int x, int y)
{ 
int x_pos = 0, y_pos = 0; 
bool done = false;

INPUT_RECORD input_record; /* Keeps track of your inputs */
DWORD events; /* Necessary for events */
/* Get any input */
ReadConsoleInput(in_handle, &input_record, 1, &events); /* '1' means only 1 event to keep record */

do{
if(input_record.EventType == MOUSE_EVENT) /* Check for mouse events */
{
/* If they DID NOT hit the left mouse button, we don't care about the input */
if(input_record.Event.MouseEvent.dwButtonState != FROM_LEFT_1ST_BUTTON_PRESSED)
return;

/* Get the coordinates the clicked on */
x_pos = input_record.Event.MouseEvent.dwMousePosition.X;
y_pos = input_record.Event.MouseEvent.dwMousePosition.Y;

if(screen_buff[x_pos + y_pos * BOARD_WDTH].Char.AsciiChar == 46) 
{ 
x_pos = input_record.Event.MouseEvent.dwMousePosition.X;
y_pos = input_record.Event.MouseEvent.dwMousePosition.Y;
fillIn(x,y,x_pos,y_pos);
done = true;
}

}
}while(!done);


}

void GAMEDATA::fillIn(int x, int y, int x_pos, int y_pos)
{ 
//if((x-2!=x_pos && x+2!=x_pos) || (y-2!=y_pos && y+2!=y_pos))
//return;

}

(main.cpp)
#include "game.h"
#include "conio.h"



int main()
{ 

GAMEDATA game;

game.init();

while(1)
{
game.getInput();
}

return EXIT_SUCCESS;
}

########################## Where can I find good C++ tutorials besides gametutorials.com? And where should I start at with graphics? [Edited by - Si0n on October 8, 2004 8:21:09 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Google Nehe and there are a lot of good tutorials.

Share this post


Link to post
Share on other sites
Check out the FAQ and find out how to post using source tags - it'll make your code much easier to read. I don't have time to actually go through your code right now to look for mistakes, and I'm not actually very good at C++ yet anyway (I learnt VB and Java first, and have just started), but you might possibly be interested in Sleep().

Share this post


Link to post
Share on other sites
1. Format you code properly by using the code box (check the "faq" link in upper right corner to see how you do this). It's a pain reading the code as it is now.

2. Have you considered buying a good C++ book? Or is that out of the question money.wise?

Share this post


Link to post
Share on other sites
Quote:
Original post by Si0n
Where can I find good C++ tutorials besides gametutorials.com?


I recommend you get a book, online tutorials are just that tutorials and nothing more you aren't going to learn anything properly with tutorials alone, besides c++ is not a language your going to learn in about a thew weeks especially with only an online tutorial for c++ someone who hasn't programmed before you'll need something like 2 years to really know it well, anyways most of the online tutorials i've seen are not that good, so don't supplement your learning with-out a good book such as:

The C++ Programming Language (Special 3rd Edition)
C++ Primer (i don't mean Primer Plus!)

Quote:
Original post by Si0n
And where should I start at with graphics?


That should be the least of your worries for now, focus on learning to program, general programming & program design, data structures & algorithms, design patterns & language idioms etc.

Share this post


Link to post
Share on other sites
Thanks for the help people, I inserted the tags now. =)
Also, about the books, no money is not exactly a problem. And I already bought two books (actually 1, the other was my dads): The complete Reference C++ and Beginning C++ Game Programming.
Are those any good?

Share this post


Link to post
Share on other sites
Quote:
Original post by Si0n
The complete Reference C++


thats a good book, not sure if its good as beginners learning guide, more of a comprehensive reference that would sit next your computer, perhaps more of a "how to program in C++" type book would be more suitable, you'll wont to write lots of console programs to practice.

Quote:
Original post by Si0n
and Beginning C++ Game Programming.


i've haven't read it but it seems to get decent public reviews.

How about libraries?

Share this post


Link to post
Share on other sites
I will definetely look into more books after I finish reading the second which is just 300 pages long, so I can do it over the weekend normally, but it has examples and stuff, so it will probably take me a week.
Then, I'll move into antoher book, and I'll write down those that you mentioned.

Starting this january, I might take a few courses at the college (I'm 17, but I can do that anyway), over C++ programming, but I'm afraid that taking intro might be a little redundant by then.
Also, this coming fall 2005, they are starting a gavming development course, which I'll go to for sure, it sounds fun. :)

Share this post


Link to post
Share on other sites
"C++: The Complete Reference" was the way I learned C++. Now, I had a good amount of background in Pascal programming beforehand, so I wasn't a n00b to programming by any means, but C/C++ is a tricky language to learn. (The C-subset is essentially a suped up assembler language.) So the way I learned was by reading the entire book (all 1000+ pages of it) three times, completely and thoroughly. Sometimes my brain wanted to claw its way out of my head, but it worked.

If you're not as much a glutton for punishment as me =) I recommend following the advice everyone else has given...Google it. Also there are some pretty good tutorials on this site (click on Articles up at the top of the page under Resources) so check those out too. Don't be afraid to ask questions. That's what we're here for.

Here's a little trick to help with files you include to keep away that nasty double-definition error shit...


// myheaderfile.h

#ifndef MYHEADERFILE_H
#define MYHEADERFILE_H

// all your code in here

#endif


You can learn more about those #command things in the back of that uber Complete Reference book you got (if it's the one I'm thinking it is, by Herbert Schildt). They're called preprocessor directives (and the # sign is called the preprocessor operator).

Good luck, my friend.

Share this post


Link to post
Share on other sites
Yes, it is the same book, I got it on wednesday, and I'm on page 88. I'm willing to read it as many times as it is necessary, but I also would like to work on stuff while reading you know. Cuz if I just read it and never implement it, I'll never learn.
That's why I'm making this game, and I still can't figure out what is going wrong, here, if you guys want to use the header file also and run it:

ps - don't mind the hardcoded array heheh =P


#ifndef GAME_H
#define GAME_H

/* Include libraries and define standard namespace */
#include <windows.h>
#include <iostream>
using namespace std;

/* Define Winners */
#define NO_WINNER 0
#define PLAYER1 1
#define PLAYER2 2
#define TIE 3

/* Define Turns */
#define P1TURN 1
#define P2TURN 2


/* Define Grid */
#define X_START 5
#define Y_START 5

#define BOARD_WDTH 19
#define BOARD_HGT 19

#define OPEN 32

/* 20 x 20 - OPEN = empty space and 46 = '.' */
const int grid[] =
{ 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN,
46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46, OPEN, 46,
};

class GAMEDATA
{
public:

/* Constructor */
GAMEDATA();

void init(); /* Init game data */

// Data Access ***

int getTurn() const { return turn; }
int getWinner() const { return winner; }

// *** End of Data Access

/* Sets a character on board */
void setScreenBuff(int x, int y, char c, bool scrCoords = true);

/* Draws grid on screen */
void drawScreen();

/* Gets user input */
void getInput();
void getInput(int x, int y);

/* Increments to the next persons turn */
void nextTurn();

/* Figures out who won the game */
void calcWinner();

/* Fills in grid */
void fillIn(int x, int y, int x1, int y1);

private:

/* The actual board */
CHAR_INFO screen_buff[BOARD_WDTH * BOARD_HGT];

HANDLE out_handle; /* Handle to the "screen" */
HANDLE in_handle; /* Handle to the "keyboard" */

SMALL_RECT scr_rect; /* This rect specifies where on the screen (where in our console
window) should we draw the tic-tac-toe board to */


int turn; /* Who's turn is it? */

int winner; /* Who has won? */
};


#endif


if you compile all 3 together they should run and show the grid, but they never get all 4 inputs like I said before.

Share this post


Link to post
Share on other sites

This topic is 4811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this