[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Fwd: Bug in fifteenapplet randomizeMap routine
From:       Stephan Kulow <coolo () kde ! org>
Date:       2004-04-23 7:16:15
Message-ID: 200404230916.15673.coolo () kde ! org
[Download RAW message or body]

Hi!

Is someone caring for fifeteenapplet? 
Just a warning: if I don't get a positive reply, I'll remove the applet
from kdetoys.

Greetings, Stephan

----------  Weitergeleitete Nachricht  ----------

Subject: Bug in fifteenapplet randomizeMap routine
Date: Thursday 22 April 2004 15:03
From: "Lonny Nettnay" <lnettnay@hotmail.com>
To: owner@bugs.kde.org

In the kdetoys package the fifteenapplet program has a bug in the
'randomizeMap' routine.

The routine tries to put the tiles into a random space in the puzzle until
all tiles are used. This  results in a 'parity error' problem about half the
time where two tiles are swapped. The puzzle is then unsolvable.

I think I have a solution. I changed the routine so it randomizes the puzzle
the way a person would, by sliding tiles into the empty space. I wasn't able
to test it though. I'm new to programming on Linux/KDE/C++. When I tried
'make fifteenapplet' i got a WHOLE LOT of errors. :-D If I hadn't been able
to copy the move code from the 'mousePressEvent' routine I wouldn't have
known what to do.

It should be able to break the actual move code out into a separate routine
to save duplication. Again, I'm new to this stuff so I didn't know how to do
it.

Let me know if that fixes the problem.
TIA,
Lonny Nettnay

_________________________________________________________________
Test your ‘Travel Quotient’ and get the chance to win your dream trip!
http://travel.msn.com

-------------------------------------------------------

["Attachment: 0" (text/x-c++)]

void PiecesTable::randomizeMap()
{
//here's the old routinef
/*    QMemArray<int> positions;
    positions.fill(0, 16);

    for (unsigned int i = 0; i < 16; i++) {
        while(1) {
            int r = (int) (((double)rand() / RAND_MAX) * 16);
            if(positions[r] == 0) {
                _map[i] = r;
                positions[r] = 1;
                break;
            }
        }
    }
*/
    initMap();
    int num_shifts = (int) (((double)rand() / RAND_MAX) * 240 +15); //between 15 and \
255 shifts (MIGHT BE OVERKILL)  
    for (int i = 0; i < num_shifts; i++) {
	
//copied the code here from the mousePressEvent routine
//to avoid duplication could be broken out into a separate routine
	        
		// find the free position
        int pos = _map.find(15);
        if(pos < 0) return;

        int frow = pos / numCols();
        int fcol = pos - frow * numCols();

		// valid move?
		int valid = false;
		while (!valid) {
			int row = (int) (((double)rand() / RAND_MAX) * numCols()); //random row
			int col = (int) (((double)rand() / RAND_MAX) * numCols()); //random col 
			if(row == frow || col == fcol) && !(row == frow && col == fcol) valid = true; \
//row OR col must match but NOT both  }
        // rows match -> shift pieces
        if(row == frow) {
	
            if (col < fcol) {
                for(int c = fcol; c > col; c--) {
                    _map[c + row * numCols()] = _map[ c-1 + row *numCols()];
                    updateCell(row, c, false);
                }
            }
            else if (col > fcol) {
                for(int c = fcol; c < col; c++) {
                    _map[c + row * numCols()] = _map[ c+1 + row *numCols()];
                    updateCell(row, c, false);
                }
            }
        }
        // cols match -> shift pieces
        else if (col == fcol) {

            if (row < frow) {
                for(int r = frow; r > row; r--) {
                    _map[col + r * numCols()] = _map[ col + (r-1) *numCols()];
                    updateCell(r, col, false);
                }
            }
            else if (row > frow) {
                for(int r = frow; r < row; r++) {
                    _map[col + r * numCols()] = _map[ col + (r+1) *numCols()];
                    updateCell(r, col, false);
                }
            }
        }
        // move free cell to click position
        _map[col + row * numCols()] = 15;
        updateCell(row, col, false);

	
    repaint();
    _randomized = true;
    }
}



>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic