How to resolve the algorithm Honeycombs step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Honeycombs step by step in the Wren programming language

Table of Contents

Problem Statement

The task is to produce a matrix of 20 hexagon shaped widgets in a honeycomb arrangement. The matrix should be arranged in such a manner that there are five columns of four hexagons. The hexagons in columns one, three and five are aligned horizontally, whereas the hexagons in columns two and four occupy a lower position within the arrangement. Each hexagon should be the same colour, and should display a unique randomly selected single capital letter on the front. The application should now wait for the user to select a hexagon, either by using a pointing device, or by pressing a key that carries a corresponding letter on a hexagon. For platforms that support pointing devices and keyboards, the application should support both methods of selection. A record of the chosen letters should be maintained and the code should be suitably commented, at the point where the the selected letter has been determined. The selected hexagon should now change colour on the display. The cycle repeats until the user has chosen all of the letters. Note that each letter can only be selected once and previously selected hexagons retain their colour after selection. The program terminates when all letters have been chosen. Optionally: output the list of selected letters and show the last selected letter, cater for a different number of columns or a different number of hexagons in each column, cater for two players, (turns alternate and the hexagons change a different colour depending on whether they were selected by player one or player two and records of both players selections are maintained.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Honeycombs step by step in the Wren programming language

Source code in the wren programming language

import "graphics" for Canvas, Color
import "dome" for Window, Process
import "math" for Math
import "font" for Font
import "input" for Mouse, Keyboard
import "random" for Random
import "./polygon" for Polygon

var Rand = Random.new()

class Hexagon is Polygon {
    static baseColor { Color.yellow }
    static selectedColor { Color.pink }

    construct new(x, y, halfWidth, letter) {
        _x = x
        _y = y
        _letter = letter
        var vertices = List.filled(6, null)
        for (i in 0..5) {
            var vx = x + halfWidth * Math.cos(i * Num.pi / 3)
            var vy = y + halfWidth * Math.sin(i * Num.pi / 3)
            vertices[i] = [vx, vy]
        }
        super(vertices, "")
        _selected = false
    }

    letter { _letter }
    selected { _selected }
    selected=(v) { _selected = v }

    draw() {
        var col = selected ? Hexagon.selectedColor : Hexagon.baseColor
        drawfill(col)
        super.draw(Color.black)
        col = selected ? Color.black : Color.red
        Canvas.print(_letter, _x - 8, _y - 8, col)
    } 
}

class Honeycombs {
    construct new(width, height) {
        Window.title = "Honeycombs"
        Window.resize(width, height)
        Canvas.resize(width, height)
        var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toList
        Rand.shuffle(letters)
        _letters = letters[0..19]
        _x1 = 150
        _y1 = 100
        _x2 = 225
        _y2 = 143
        _w  = 150
        _h  = 87
        _hs = null
        _comb = List.filled(20, null)
        Font.load("memory", "memory.ttf", 48)
        Font["memory"].antialias = true
        Canvas.font = "memory"
    }

    drawHexagons() {
         for (i in 0..._comb.count) {
            var x
            var y
            if (i < 12) {
                x = _x1 + (i % 3) * _w
                y = _y1 + (i / 3).floor * _h
            } else {
                x = _x2 + (i % 2) * _w
                y = _y2 + ((i - 12) / 2).floor * _h
            }
            _comb[i] = Hexagon.new(x, y, (_w / 3).floor, _letters[i])
            _comb[i].draw()
        }
    }

    allSelected() { _comb.all { |h| h.selected } }

    init() {
        drawHexagons()
    }

    update() {
        _hs = null
        if (Mouse.isButtonPressed("left")) {
            for (h in _comb) {
                if (h.contains(Mouse.position.x, Mouse.position.y)) {
                    _hs = h
                    break
                }
            }
        } else if (Keyboard.allPressed.count > 0) {
            for (h in _comb) {
                if (Keyboard.isKeyDown(h.letter)) {
                    _hs = h
                    break
                }
            }
        }
    }

    draw(alpha) {
        if (_hs) {
            _hs.selected = true
            _hs.draw()
            if (allSelected()) Process.exit(0)
        }
    }
}

var Game = Honeycombs.new(600, 500)


  

You may also check:How to resolve the algorithm Loops/Foreach step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Oforth programming language
You may also check:How to resolve the algorithm Empty program step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Maple programming language
You may also check:How to resolve the algorithm Scope modifiers step by step in the 11l programming language