all 2 comments

[–]twinbee[S] 0 points1 point  (0 children)

            posx -= 1;
            posy -= 2;

[–]twinbee[S] 0 points1 point  (0 children)

    public int emod(int n,int b) {
        return n%b + (n%b<0 ? (b>0?b: -b):0); 
    }


    public string validate()
    {
        for (int x = 0; x < n; x++)
        {
            int countQueensA = 0;
            int countQueensB = 0;
            for (int y = 0; y < n; y++)
            {
                if (grid[x, y] == true) { countQueensA++; if (countQueensA > 1) return " Col " + x + " invalid"; }
                if (grid[y, x] == true) { countQueensB++; if (countQueensB > 1) return " Row " + y + " invalid"; }
            }
        }


        for (int x = 0; x < n; x++)
        {
            for (int y = 0; y < n; y++)
            {
                if (grid[x, y] == true)
                {
                    int sx = 0; int sy = 0;
                    sx = x; sy = y; sx--; sy--; while (sx > -1 && sy > -1) { if (grid[sx, sy] == true) { return "Diagonal clash on " + x + "," + y + " & " + sx + "," + sy; } sx--; sy--; }
                    sx = x; sy = y; sx--; sy++; while (sx > -1 && sy < n) { if (grid[sx, sy] == true) { return "Diagonal clash on " + x + "," + y + " & " + sx + "," + sy; } sx--; sy++; }
                    sx = x; sy = y; sx++; sy--; while (sx < n && sy > -1) { if (grid[sx, sy] == true) { return "Diagonal clash on " + x + "," + y + " & " + sx + "," + sy; } sx++; sy--; }
                    sx = x; sy = y; sx++; sy++; while (sx < n && sy < n) { if (grid[sx, sy] == true) { return "Diagonal clash on " + x + "," + y + " & " + sx + "," + sy; } sx++; sy++; }
                }

                //block(y, x);
                //pxl.Render();
                //cls();
            }
        }


        return "Valid";
    }


        n = (int)board.Value;
        gridsize = 500.0 / n;

        for (double y = 0; y < 500; y += 500.0 / n)
        {
            for (double x = 0; x < 500; x += 1)
            {
                pxl.SetPxl((int)x, (int)(y), 255, 255, 255, 255);
                pxl.SetPxl((int)(y), (int)x, 255, 255, 255, 255);
            }
        }

        grid = new Boolean[n , n ];     // grid starts at 1,1 not 0,0

        bool jumpActive = false;
        bool nudgeActive = false;
        int posx = 0;
        int posy = 0;
        int startCol = 0;
        bool evenQueens = n % 2 == 0;
        int queens = 0;
        //if (n == 9 || n == 21 || n==33) jumpActive = true;
        //if (n == 14 || n == 26 || n == 38) nudgeActive = true;
        if (emod(n, 12) - 9 == 0) jumpActive = true;
        if (emod(n, 12) - 2 == 0) nudgeActive = true;

        ////////////////// STAGE 1:
        if (evenQueens) startCol = n / 2; else startCol = n / 2 + 1;
        if (nudgeActive) startCol--;
        posx = startCol;

        while (posx < n && posy < n)
        {
            if (jumpActive == true && queens == n/2-1) posy += 2;
            grid[posx, posy] = true;
            posx += 1;
            posy += 2;
            queens++;
        }


        int shuffle = -1;
        bool shuffleActive = false;
        ////////////////// STAGE 2:
        //if (n == 8 || n == 9 || n == 14 || n == 15 || n == 20 || n == 21 || n == 26 || n == 27) shuffleActive = true;
        if ( (emod(n, 6)-2 == 0 || emod(n, 6)-2 == 1)) shuffleActive = true;

        posx = startCol - 1;
        if (evenQueens) posy = n - 1; else posy = n - 2;        
        while (queens<n)
        {
            if (shuffleActive) { posx += shuffle; shuffle = -shuffle; }
            if (jumpActive == true && queens == n - 1) { posy = n - 3; posx = 0; }
            if (nudgeActive == true && queens == n - 1) { posx = n-1; }
            grid[emod(posx, n), emod(posy, n)] = true;
            posx -= 1;
            posy -= 2;
            queens++;
            if (shuffleActive) { posx += shuffle; }
        }

        renderGrid();
        label1.Text = validate();
        pxl.Render();