Skip to main content
GameDev.net gamedev.net
🔒 Locked

tictac toe marks

Started by phil67rpg Apr 21, 2013 at 3:42 AM 28 replies 4.3k views
Original Post
phil67rpg
phil67rpg
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);
}
phil67rpg
phil67rpg

well after a break I am going to finish my tic tac toe game, however I still get the problem I have stated above.

alvaro
alvaro
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.
BeerNutts
BeerNutts


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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
phil67rpg
phil67rpg

yes I listen beer nutts, I am trying my best, I do need practice

Paradigm Shifter
Paradigm Shifter

I suggest you practice using your debugger to step through your code and examine your variables then. It's going to be a lot more productive.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
phil67rpg
phil67rpg

so how do I get better at coding?

Paradigm Shifter
Paradigm Shifter

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
FLeBlanc
FLeBlanc
Hi phil. Welcome back. We missed you.
phil67rpg
phil67rpg

well I have decided since I SUCK at c++ maybe I should try c# and do apps NOT games

Paradigm Shifter
Paradigm Shifter

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
phil67rpg
phil67rpg

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

BKrenz
BKrenz

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.

phil67rpg
phil67rpg

well I have decided to do a simple little calculator application, where should I start, maybe with index cards a stated above.

stitchs_login
stitchs_login

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.

alvaro
alvaro

Trust me, draw it out on a big sheet of A4!

How big do sheets of A4 get where you live? We can only get A4 in A4 size in my country... ;)
phil67rpg
phil67rpg

what is A4 paper

Trienco
Trienco

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.

f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.