tictac toe marks

Started by
28 comments, last by phil67rpg 10 years, 11 months ago
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);
}
Advertisement

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

Are you sure the mouse coordinates you get match the values of the rect?

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.

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)

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

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

so how do I get better at coding?

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
Hi phil. Welcome back. We missed you.

This topic is closed to new replies.

Advertisement