27 Feb 2013, 20:17
Generic-user-small

Miller Ridgeway (13 posts)

Hi again. I have a slight problem with the way my sudoku board looks. It is slightly longer than the 9 by 9 grid that it should be. It is extended down what appears to be another length. I have been digging around in the onDraw for a while, but i’m not 100% sure which part sets up the actual grid. Any help would be appreciated. Thanks

28 Feb 2013, 08:33
Generic-user-small

Anthony Shaw (132 posts)

It sounds like you may be looking in the right place by looking in the onDraw method. Post the code here and I will see if I can spot where the problem is. You might also want to check the onSizeChanged method, that is where the size of the tiles is set so the problem could be in there.

01 Mar 2013, 20:07
Generic-user-small

Miller Ridgeway (13 posts)

Alright. Thanks for the advice. My onDraw is below

protected void onDraw(Canvas canvas) { Paint background = new Paint(); background.setColor(getResources().getColor(R.color.puzzle_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background);

	Paint dark = new Paint();
	dark.setColor(getResources().getColor(R.color.puzzle_dark));
	
	Paint hilite = new Paint();
	hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
	
	Paint light = new Paint();
	light.setColor(getResources().getColor(R.color.puzzle_light));
	
	//Minor Lines
	for(int i = 0; i<9; i++)
	{
		canvas.drawLine(0, i*height, getWidth(), i*height, light);
		canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilite); 
		canvas.drawLine(i*width, 0, i*width, getHeight(),light); 
		canvas.drawLine(i*width+1, 0, i*width + 1, getHeight(), hilite);
	}//forMinor
	
	//Major Lines
	for(int i=0; i<9; i++)
	{
		if(i%3 != 0)
			continue;
		canvas.drawLine(0, i*height, getWidth(), i*height, dark);
		canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilite);
		canvas.drawLine(i*width, 0, i*width, getHeight(), dark);
		canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hilite);
	}//forMajor
	
	Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
	foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
	foreground.setStyle(Style.FILL);
	foreground.setTextSize(height*0.75f);
	foreground.setTextScaleX(width/height);
	foreground.setTextAlign(Paint.Align.CENTER);
	
	FontMetrics fm = foreground.getFontMetrics();
	
	float x =width / 2;
	float y = height/2 -(fm.ascent + fm.descent)/2;
	
	for(int i=0; i<9; i++)
	{
		for(int j =0; j<9; j++)
		{
			canvas.drawText(this.game.getTileString(i,j),i*width + x, j*height + y, foreground); 
		}//for
	}//for
	
	Paint hint = new Paint();
	
	int c[] = 
	{ 
			getResources().getColor(R.color.puzzle_hint_0),
			getResources().getColor(R.color.puzzle_hint_1),
			getResources().getColor(R.color.puzzle_hint_2),
	};//C
	
	Rect r = new Rect();
	for(int i = 0; i<9; i++)
	{	
		for(int j = 0; j<9; j++)
		{
			int movesleft = 9-game.getUsedTiles(i,j).length;
			
			if(movesleft < c.length)
			{
				getRect(i,j,r);
				hint.setColor(c[movesleft]);
				canvas.drawRect(r, hint);
			}//if
		}//for
	}//for
	
	Log.d(TAG, "selRect="+selRect);
	Paint selected = new Paint();
	selected.setColor(getResources().getColor(R.color.puzzle_selected));
	canvas.drawRect(selRect, selected);
	
	
	
}//onDraw

I’m aware that I need to clean up a few object allocations, but I really want to solve this error first :p

02 Mar 2013, 11:42
Generic-user-small

Anthony Shaw (132 posts)

The first thing that jumps is your use of iheight and iwidth, they should really be defined as height and width (as on page 70) and the code should be ‘i * height’ and ‘i * width’ rather than ‘iheight’ and ‘iwidth’. You should be multiplying the loop variable ‘i’ by the height and width of each individual box. I have not checked the code any further than that so there may be other problems in it that come to light when this is fixed.

As a matter of style, I would never use ‘i’ as a loop variable in the real world because it is too easy to confuse with 1. I would never use single character variable names at all, in fact. If I were writing the code above from scratch I would use ‘ndx’ for the loop variables rather than ‘i’, it gives a big hint of what the variables is being used for and is not easy to confuse with other variables.

Having said that, when you are learning from a book it is best to follow exactly what is there then you don’t have to remember to change every single usage of the variables as you type them.

04 Mar 2013, 20:05
Generic-user-small

Miller Ridgeway (13 posts)

Well, this is strange. In my code, I do have the “*” character added. However, it did not paste into the forums. I am really stumped here, especially since I am not the best with graphics in any programming languages. Also, as a side note, I only use “i” because the first person that taught me C++ always used it, and I never thought otherwise. Your idea seems much better ^^

05 Mar 2013, 10:52
Generic-user-small

Anthony Shaw (132 posts)

I think I know what happened to code when you posted it, you should have put the pre and code html tags aroung it as it says under formatting. posting the code as you did switched parts of it into bold.

I have looked through the code and it seems OK, so the next place to look is in the onSizeChanged method from page 70. Have a check to make sure that is OK, and post it up here if you want me to have a look.

07 Mar 2013, 19:45
Generic-user-small

Miller Ridgeway (13 posts)

Okay, thanks for the tip. I have been through and through with the onSizeChanged method, I hope it wouldn’t be in there. My code is below

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
	width = w/9f;
	height =w/9f;
	getRect(selX, selY, selRect);
	Log.d(TAG, "onSizeChanged: width"+width+",height"+height);
	super.onSizeChanged(w, h, oldw, oldh);
}//onSizeChanged
08 Mar 2013, 15:06
Generic-user-small

Anthony Shaw (132 posts)

I think I can see what the problem is, the fifth line should be:

height = h / 9f;

You used ‘w’ rather than ‘h’. Yet again another good reason not to use single character variable names :-)

08 Mar 2013, 20:02
Generic-user-small

Miller Ridgeway (13 posts)

Anthony, I literally just cried inside. Thanks a lot for the help. ^

09 Mar 2013, 13:38
Generic-user-small

Anthony Shaw (132 posts)

It happens to the best of us, you know the sort of things to look out for next time around. Best of luck.

14 Apr 2013, 02:45
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Nice detective work, Anthony.

I got in the habit of short variable names a long time ago and never completely grew out of it. On the opposite end of the spectrum I know a guy who uses variable names like “zeroBasedHorizontalColumnIndex”.

  You must be logged in to comment