Sign in to follow this  
phil67rpg

tictac toe marks

Recommended Posts

phil67rpg    443
Well I am still working on my tic tac toe game using dx9 and c++. My problem is that when I click on the second row that it puts a mark in the first row and when I click on the third row it puts a mark in the second row. The x and y variables are the mouse position.
here is the code I am using.
void display_text_x(int x,int y)
{
	d3ddev->BeginScene();

	RECT font_rect;
	
	if(x>=300 && x <=350 && y>=225 && y <=275)
	{
	font_rect.left=310;
	font_rect.top=235;
	font_rect.right=330;
	font_rect.bottom=255;
	board_x[0][0]=true;
	}

	if(x>=350 && x <=400 && y>=225 && y <=275)
	{
	font_rect.left=360;
	font_rect.top=235;
	font_rect.right=380;
	font_rect.bottom=255;
	board_x[0][1]=true;
	}

	if(x>=400 && x <=450 && y>=225 && y <=275)
	{
	font_rect.left=415;
	font_rect.top=235;
	font_rect.right=435;
	font_rect.bottom=255;
	board_x[0][2]=true;
	}

	if(x>=300 && x <=350 && y>=275 && y <=325)
	{
	font_rect.left=310;
	font_rect.top=290;
	font_rect.right=330;
	font_rect.bottom=310;
	board_x[1][0]=true;
	}

	if(x>=350 && x <=400 && y>=275 && y <=325)
	{
	font_rect.left=360;
	font_rect.top=290;
	font_rect.right=380;
	font_rect.bottom=310;
	board_x[1][1]=true;
	}

	if(x>=400 && x <=450 && y>=275 && y <=325)
	{
	font_rect.left=415;
	font_rect.top=290;
	font_rect.right=435;
	font_rect.bottom=310;
	board_x[1][2]=true;
	}

	if(x>=300 && x <=350 && y>=325 && y <=375)
	{
	font_rect.left=310;
	font_rect.top=350;
	font_rect.right=330;
	font_rect.bottom=370;
	board_x[2][0]=true;
	}

	if(x>=350 && x <=400 && y>=325 && y <=375)
	{
	font_rect.left=360;
	font_rect.top=350;
	font_rect.right=380;
	font_rect.bottom=370;
	board_x[2][1]=true;
	}

	if(x>=400 && x <=450 && y>=325 && y <=375)
	{
	font_rect.left=415;
	font_rect.top=350;
	font_rect.right=435;
	font_rect.bottom=370;
	board_x[2][2]=true;
	}

	HRESULT hr=D3DXCreateFontA(d3ddev,
				  20,
				  10,
				  FW_NORMAL,
				  1,
				  false,
				  DEFAULT_CHARSET,
				  OUT_DEFAULT_PRECIS,
				  ANTIALIASED_QUALITY,
				  DEFAULT_PITCH|FF_DONTCARE,
				  "Arial",
				  &g_font);
			 g_font->DrawTextA(NULL,"X",-1,&font_rect,DT_CENTER,0xFFFFFFFF);

	 d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

Share this post


Link to post
Share on other sites
BeerNutts    4400

Looking at that code makes me cringe.

The function name is `display_text_x', but it does all sorts of things that are not displaying text. I certainly wouldn't expect a that function with that name could change the board (I imagine that's what `board_x' is, although I don't know what either of the `_x's means).

Then there is the block of 9 separate conditions for the cells on the board. You can convert your (x,y) coordinates to board coordinates with code like this:

int board_x = (x - left_edge_of_the_board) / width_of_a_cell;
int board_y = (y - top_edge_of_the_board) / height_of_a_cell;
You can similarly compute the coordinates where the text goes with simple arithmetic instead of exhaustive case-by-case code.

By the way, you should give constants names instead of having your code full of hard-to-interpret numbers all over the place.

Alvaro, he'll post the same code without any of your suggestions in about a week and ask why it puts an X in the wrong spot.  I hope he listens to you this time; he never listens to me.

Share this post


Link to post
Share on other sites

Coding is a small part of programming. You're going to spend most of your time debugging things. Learning to use the debugger is going to be faster than posting threads asking what is wrong with your code in the long run.

Share this post


Link to post
Share on other sites

Games are apps.

 

Three steps to heaven:

 

1) Make a plan

2) Write code

3) Debug code

 

I think you are doing too much (2) and not enough (1) and (3). Moving to a different language isn't going to be a magic bullet. Most apps are MUCH more complicated than tic-tac-toe.

Share this post


Link to post
Share on other sites
BKrenz    353

With my latest game plans in the planning stage, I find myself adding onto the game design document slowly. While sitting here, I have notecards and a pen beside me where I can write a note on, then add it to a pile based on whether its a gameplay feature or a technical implementation detail. Obviously, the features pile grows faster, but it still helps me keep thoughts together. I just figured out that a linked list may be the best way to handle a specific data set in my game. Written down and added to the technical implementation pile.

 

If it's something that will take more than a week to program, it's worth spending at least a few weeks gathering ideas, writing them down, figuring out some implementation details, and researching what you don't know. Keeping notes is great.

Share this post


Link to post
Share on other sites
stitchs_login    1361

so I should plan things out very carefully more, should I use pseudocode or flowcharts or uml

 

A combination of all 3, depending on how big your application is. It is best to visualise everything before you start coding. The amount of times I code something, have difficulty understanding it (drawing with your finger in the air only goes so far!) and then map out everything (coordinates, the next positions, grid squares, calculations) really helps me understand what is going on. Trust me, draw it out on a big sheet of A4!

 

Back to the original question, I really think you should have taken on repeated advice by now of eliminating magic numbers from you code. This:

 

if(x>=300 && x <=350 && y>=225 && y <=275)
{
font_rect.left=310;
font_rect.top=235;
font_rect.right=330;
font_rect.bottom=255;
board_x[0][0]=true;
}

if(x>=350 && x <=400 && y>=225 && y <=275)
{
font_rect.left=360;
font_rect.top=235;
font_rect.right=380;
font_rect.bottom=255;
board_x[0][1]=true;
}

 

I can only assume means that you are checking for a collision in a top-left quadrant, or top-right quadrant, and then setting font_rect at a new position. I also guess that board_x[][] = true means that you are setting that grid box to no longer accept shapes. I can only assume because it is impossible to tell with properly named variables to represent these coordinates. I am unable to help to the fullest extent if you are unable to provide enough information.

 

On a side-note, a calculator application might not be as simple as you might think depending on what you want it to do. Alas, it won't work without proper variables..

 

Stitchs.

Share this post


Link to post
Share on other sites
Trienco    2555

Sorry, I don't see any of these attempts of an "easy way out" working. Looking at this code and the breakout code, no amount of switching language, UML drawings or doing apps instead of games will allow avoiding to take a huge step back and learn some basic and fundamental programming first. That means understanding and knowing how and when to apply them, not "research" as in "I skimmed over a tutorial and compiled the code".

 

The code is an insane mess of copy/paste, arrays are used, but then stuff is just copied and hard coded for every field in the array, showing a complete lack of understanding WHY an array is used in the first place. 

 

Data that should be grouped in a class or struct is instead spread over a bunch of different arrays, functions are just doing all kinds of stuff. 

 

There's endless if/else-copy/paste replacing a simple division. 

 

Despite making the same mistake in the breakout thread, this function is again presenting an entire frame when it should just set a value (so I doubt that it was even understood what those magical D3D functions are actually doing). 

 

There is also no recognizable understanding of the difference between game logic and graphics. Graphical representation isn't the game, it's what you put ON TOP of the game, so a player can see what he's doing. The "core" of the game doesn't care about graphics, sprites, 2D/3D, keyboards or mice. Inputs are used to trigger functions of the game and graphics are used to display the state of the game.

 

You can't buy a Spanish dictionary, learn how to pronounce two or three words and then try to write an entire novel. You need to learn the grammar and rules, have a decent vocabulary and know common phrases and idioms.

 

In this case: you need to learn programming. The language here isn't C++ (or C#, Basic or any other specific language), it's basic programming concepts. Not just knowing what a loop looks like, but how to use them. Forget about GUI and 3D (or even 2D). Running requires knowing how to walk first. Otherwise you're not getting far and keep hurting yourself.

 

Planning for TicTacToe or a calculator by using ULM and flow charts is overkill. Maybe a nice practice, but if you need a flow chart to understand the game logic behind TicTacToe (of which 95% should be covered by a standard game loop), you are clearly overcomplicating a very simple thing. Frankly, at this point it seems more like avoiding the real issue rather than tackling it. Like constantly "taking a break from programming" it won't magically make you write better code.

Share this post


Link to post
Share on other sites
Khatharr    8812

Sorry, I don't see any of these attempts of an "easy way out" working. Looking at this code and the breakout code, no amount of switching language, UML drawings or doing apps instead of games will allow avoiding to take a huge step back and learn some basic and fundamental programming first. That means understanding and knowing how and when to apply them, not "research" as in "I skimmed over a tutorial and compiled the code".
 
The code is an insane mess of copy/paste, arrays are used, but then stuff is just copied and hard coded for every field in the array, showing a complete lack of understanding WHY an array is used in the first place. 
 
Data that should be grouped in a class or struct is instead spread over a bunch of different arrays, functions are just doing all kinds of stuff. 
 
There's endless if/else-copy/paste replacing a simple division. 
 
Despite making the same mistake in the breakout thread, this function is again presenting an entire frame when it should just set a value (so I doubt that it was even understood what those magical D3D functions are actually doing). 
 
There is also no recognizable understanding of the difference between game logic and graphics. Graphical representation isn't the game, it's what you put ON TOP of the game, so a player can see what he's doing. The "core" of the game doesn't care about graphics, sprites, 2D/3D, keyboards or mice. Inputs are used to trigger functions of the game and graphics are used to display the state of the game.
 
You can't buy a Spanish dictionary, learn how to pronounce two or three words and then try to write an entire novel. You need to learn the grammar and rules, have a decent vocabulary and know common phrases and idioms.
 
In this case: you need to learn programming. The language here isn't C++ (or C#, Basic or any other specific language), it's basic programming concepts. Not just knowing what a loop looks like, but how to use them. Forget about GUI and 3D (or even 2D). Running requires knowing how to walk first. Otherwise you're not getting far and keep hurting yourself.
 
Planning for TicTacToe or a calculator by using ULM and flow charts is overkill. Maybe a nice practice, but if you need a flow chart to understand the game logic behind TicTacToe (of which 95% should be covered by a standard game loop), you are clearly overcomplicating a very simple thing. Frankly, at this point it seems more like avoiding the real issue rather than tackling it. Like constantly "taking a break from programming" it won't magically make you write better code.

Shhhh....

Share this post


Link to post
Share on other sites
Trienco    2555

Generally by giving them names and making them constants, but that only applies to numbers that _must_ be defined in the program and won't change at runtime. The resolution or window size for a PC application should not be a magic number or a constant, because you don't know what hardware people are using.

 

In your case: by learning how to use structs, arrays, loops and functions.

 

Just doing a find/replace and creating thousands of constants like "block1TopLeftX" and "block3BottomRightY" might get rid of magic numbers, but is also insanely silly, because a sane person would never want to hard code everything for every single block in every single level of a game like Breakout (which will be thousands or even a lot more). Always tell yourself "there will probably be a bug in this code, I don't want to fix the same bug in 10000 copies of this". In fact, writing the same snippet more than twice (at most) is usually a bad sign.

 

Force yourself to keep things flexible. Plan for change, write code that will work no matter if your game board is 3x3, 5x5 or any other size (somewhat limited by screen size) and don't assume you will always need 3-in-a-row. Make board and required row size a variable and don't use magic numbers or constants. Remove the option to just hard code everything and force yourself to think in algorithms instead of a huge list of if-elses.

Share this post


Link to post
Share on other sites

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