Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / MFC

Solve the Pentomino puzzle with C++ and dancing links

Rate me:
Please Sign up or sign in to vote.
4.80/5 (22 votes)
2 Dec 2011CPOL11 min read 121.2K   3.1K   37  
Program to find all the solutions to a Pentomino puzzle.
#include "stdafx.h"
#include "CDibPieza.h"

#define MAXANCHO 5

BEGIN_MESSAGE_MAP(CDibPieza, CStatic)
	ON_WM_PAINT()
END_MESSAGE_MAP()

void CDibPieza::OnPaint()
{
	if (!pieza) return;

	CRect rect;
	GetClientRect(&rect);
	
	CPaintDC dc (this);
	
	int padding = 20;
	
	int anchocuad = (rect.right - padding * 2) / MAXANCHO;
	
	int xbase = (rect.right - anchocuad * pieza->Ancho) / 2;
	int ybase = (rect.bottom - anchocuad * pieza->Alto) / 2 + anchocuad * pieza->Alto;
	
	COLORREF color = RGB(255,255,255);
	CBrush fondo(color);
	dc.FillRect(rect,&fondo);

	CRect rfill;
	int leftdato, topdato;

	CBrush fondopieza(pieza->GetColor(idPInfo,CantPiezas));
	dc.SelectObject(fondopieza);
	CPen pen(PS_SOLID, 1, RGB(50,50,50));
	dc.SelectObject(pen);

	for (int i=1;i<=pieza->Alto;i++)
	{
		for (int j=1;j<=pieza->Ancho;j++)
		{
			if (pieza->GetValue(i,j))
			{
				leftdato = xbase + (j-1)*anchocuad;
				topdato = ybase - (i-1)*anchocuad;
				rfill = CRect(leftdato, topdato,leftdato + anchocuad,topdato - anchocuad);
				dc.Rectangle(rfill);
				//dc.FillRect(rfill,&fondopieza);
				
			}
		}
	}
	dc.RestoreDC(-1);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
System developer from Argentina.

Programmed in VB 5,6,.NET, C#, Java, PL-SQL, Transac-SQL, C, C++ and even some "calculator" language.

Love to build small, useful applications.
Usually building big and complicated apps based on solid, reliable components.

Hobbies: reading, photography, chess, paddle, running.

Comments and Discussions