-
Tictactoe problem
I have just started with MFC again after being away from programming for a bit. And I've encountered exactly the same problem I had the first time I tried to write tictactoe!! The area that can be clicked is seeming smaller than the area drawn, hence clicking on the right or on the bottom of the game does nothing.
This is my OnPaint code, which has the mapmode calls where I think the problem may lie.
Code:
void TTTWin::OnPaint( )
{
CPaintDC dc( this );
CRect rect;
GetClientRect( &rect );
dc.SetMapMode( MM_ANISOTROPIC );
dc.SetWindowExt( 300, 300 );
dc.SetViewportExt( rect.Width( ), rect.Height( ) );
dc.MoveTo( 100, 5 );
dc.LineTo( 100, 295 );
dc.MoveTo( 200, 5 );
dc.LineTo( 200, 295 );
dc.MoveTo( 5, 100 );
dc.LineTo( 295, 100 );
dc.MoveTo( 5, 200 );
dc.LineTo( 295, 200 );
for( int i = 0; i < 9; i++ )
{
switch( game.mark[ i ] )
{
case game.OH:
game.drawOH( dc, game.squares[ i ] );
break;
case game.EX:
game.drawEX( dc, game.squares[ i ] );
break;
case game.EMPTY:
break;
default:
MessageBox( "Cannot draw piece", ERROR );
break;
}
}
}
The TicTacToe constructore which initialises the rectangles looks like this:
Code:
TicTacToe::TicTacToe( )
{
theApp = new TTTApp( );
squares[ 0 ].SetRect( 0, 0, 100, 100 );
squares[ 1 ].SetRect( 100, 0, 200, 100 );
squares[ 2 ].SetRect( 200, 0, 300, 100 );
squares[ 3 ].SetRect( 0, 100, 100, 200 );
squares[ 4 ].SetRect( 100, 100, 200, 200 );
squares[ 5 ].SetRect( 200, 100, 300, 200 );
squares[ 6 ].SetRect( 0, 200, 100, 300 );
squares[ 7 ].SetRect( 100, 200, 200, 300 );
squares[ 8 ].SetRect( 200, 200, 300, 300 );
for( int i = 0; i < 9; i++ )
{
mark[ i ] = EMPTY;
}
lastGo = EX;
}
Really hope to get this one explained to me, cant believe I reached exactly the same problem I had 4 years ago when I did it for the first time!! :(
-
Well, I may or may not be considered qualified to reply on this since I program mostly in straight C and have never used MFC...
If you are just starting out though, why not use static (text) controls with SS_NOTIFY or buttons for the graphic portion of your tic-tac-toe game? I'm guessing simpler is better until you get a working game going. ;)
Cheers, Mac :)
-
Well graphical part itself isn't to hard, just a few lines inside rectangles. I just cant see why the window is drawn 400x400, I set the mapping mode to 300x300, yet the clicking only seems to be detected in the first 300x300 pixels. This is why I think its something to do with the mapping mode...
-
ok I've narrowed it down to the fact that in OnLButtonDown I will receive the device coordinates which may vary depending on window size, etc. How can I convert these to logical coordinates inside a 300x300 range?
I've tried using CClientDc inside OnLButtonDown and converting them but the values seem unaffected??? Am I missing something in the code?
Code:
void TTTWin::OnLButtonDown( UINT nFlags, CPoint point )
{
CClientDC dc( this );
dc.SetWindowExt( 300, 300 );
dc.DPtoLP( &point );
for( int i = 0; i < 9; i++ )
{
if( game.mark[ i ] == game.EMPTY && game.squares[ i ].PtInRect( point ) )
{
.
.
.
.