How to resolve the algorithm Peaceful chess queen armies step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Peaceful chess queen armies step by step in the Julia programming language

Table of Contents

Problem Statement

In chess, a queen attacks positions from where it is, in straight lines up-down and left-right as well as on both its diagonals. It attacks only pieces not of its own colour.

The goal of Peaceful chess queen armies is to arrange m black queens and m white queens on an n-by-n square grid, (the board), so that no queen attacks another of a different colour.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Peaceful chess queen armies step by step in the Julia programming language

The code you provided defines a function, place!, that attempts to place queens on a chessboard such that no two queens attack each other. The function takes four arguments:

  • numeach: The number of queens to place on each side of the board.
  • bsize: The size of the board.
  • bqueens: A vector of positions occupied by black queens.
  • wqueens: A vector of positions occupied by white queens.

The function works by iterating over all possible positions on the board and checking if each position is open (i.e., not already occupied by a queen). If a position is open, the function checks if it is safe to place a queen there (i.e., no other queen attacks it). If it is safe, the function places a queen there and updates the bqueens or wqueens vector accordingly.

The function then calls itself recursively to place the remaining queens. If the function is able to place all of the queens without conflict, it returns true. Otherwise, it returns false.

The code also defines a function, peacefulqueenapp, that creates a graphical user interface (GUI) for the place! function. The GUI allows the user to select the size of the board and the number of queens to place on each side. The user can then click a button to solve the puzzle.

If the puzzle is solved, the GUI displays the solution on the chessboard. If the puzzle cannot be solved, the GUI displays an error message.

Here is a breakdown of the key parts of the code:

  • The struct Position defines a type that represents a position on the chessboard.
  • The isattack function checks if two positions attack each other.
  • The noattack function checks if a position is not attacked by any other queens.
  • The positionopen function checks if a position is open (i.e., not already occupied by a queen).
  • The place! function attempts to place queens on a chessboard such that no two queens attack each other.
  • The peacefulqueenapp function creates a GUI for the place! function.

The code is written in the Julia programming language.

Source code in the julia programming language

using Gtk

struct Position
    row::Int
    col::Int
end

function place!(numeach, bsize, bqueens, wqueens)
    isattack(q, pos) = (q.row == pos.row || q.col == pos.col ||
                        abs(q.row - pos.row) == abs(q.col - pos.col))
    noattack(qs, pos) = !any(x -> isattack(x, pos), qs)
    positionopen(bqs, wqs, p) = !any(x -> x == p, bqs) && !any(x -> x == p, wqs)

    placingbqueens = true
    if numeach < 1
        return true
    end
    for i in 1:bsize, j in 1:bsize
        bpos = Position(i, j)
        if positionopen(bqueens, wqueens, bpos)
            if placingbqueens && noattack(wqueens, bpos)
                push!(bqueens, bpos)
                placingbqueens = false
            elseif !placingbqueens && noattack(bqueens, bpos)
                push!(wqueens, bpos)
                if place!(numeach - 1, bsize, bqueens, wqueens)
                    return true
                end
                pop!(bqueens)
                pop!(wqueens)
                placingbqueens = true
            end
        end
    end
    if !placingbqueens
        pop!(bqueens)
    end
    false
end

function peacefulqueenapp()
    win = GtkWindow("Peaceful Chess Queen Armies", 800, 800) |> (GtkFrame() |> (box = GtkBox(:v)))
    boardsize = 5
    numqueenseach = 4
    hbox = GtkBox(:h)
    boardscale = GtkScale(false, 2:16)
    set_gtk_property!(boardscale, :hexpand, true)
    blabel = GtkLabel("Choose Board Size")
    nqueenscale = GtkScale(false, 1:24)
    set_gtk_property!(nqueenscale, :hexpand, true)
    qlabel = GtkLabel("Choose Number of Queens Per Side")
    solveit = GtkButton("Solve")
    set_gtk_property!(solveit, :label, "   Solve   ")
    solvequeens(wid) = (boardsize = Int(GAccessor.value(boardscale));
        numqueenseach = Int(GAccessor.value(nqueenscale)); update!())
    signal_connect(solvequeens, solveit, :clicked)
    map(w->push!(hbox, w),[blabel, boardscale, qlabel, nqueenscale, solveit])
    scrwin = GtkScrolledWindow()
    grid = GtkGrid()
    push!(scrwin, grid)
    map(w -> push!(box, w),[hbox, scrwin])
    piece = (white = "\u2655", black = "\u265B", blank = "   ")
    stylist = GtkStyleProvider(Gtk.CssProviderLeaf(data="""
        label {background-image: image(cornsilk); font-size: 48px;}
        button {background-image: image(tan); font-size: 48px;}"""))

    function update!()
        bqueens, wqueens = Vector{Position}(), Vector{Position}()
        place!(numqueenseach, boardsize, bqueens, wqueens)
        if length(bqueens) == 0
            warn_dialog("No solution for board size $boardsize and $numqueenseach queens each.", win)
            return
        end
        empty!(grid)
        labels = Array{Gtk.GtkLabelLeaf, 2}(undef, (boardsize, boardsize))
        buttons = Array{GtkButtonLeaf, 2}(undef, (boardsize, boardsize))
        for i in 1:boardsize, j in 1:boardsize
            if isodd(i + j)
                grid[i, j] = buttons[i, j] = GtkButton(piece.blank)
                set_gtk_property!(buttons[i, j], :expand, true)
                push!(Gtk.GAccessor.style_context(buttons[i, j]), stylist, 600)
            else
                grid[i, j] = labels[i, j] = GtkLabel(piece.blank)
                set_gtk_property!(labels[i, j], :expand, true)
                push!(Gtk.GAccessor.style_context(labels[i, j]), stylist, 600)
            end
            pos = Position(i, j)
            if pos in bqueens
                set_gtk_property!(grid[i, j], :label, piece.black)
            elseif pos in wqueens
                set_gtk_property!(grid[i, j], :label, piece.white)
            end
        end
        showall(win)
    end

    update!()
    cond = Condition()
    endit(w) = notify(cond)
    signal_connect(endit, win, :destroy)
    showall(win)
    wait(cond)
end

peacefulqueenapp()


  

You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Scala programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Yorick programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the Ada programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the gecho programming language
You may also check:How to resolve the algorithm Erdös-Selfridge categorization of primes step by step in the Java programming language