Four sides of square: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Red}}: add comment)
(Added Wren)
Line 105: Line 105:
<br>
<br>
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square]
[http://keptarhely.eu/view.php?file=20220218v00x6hugz.jpeg Four sides of square]

=={{header|Wren}}==
===Text based===
<lang ecmascript>var hollowMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
System.write((i == 0 || i == n-1 || j == 0 || j == n-1) ? "1 " : "0 ")
}
System.print()
}
}

hollowMatrix.call(9)</lang>

{{out}}
<pre>
1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1
</pre>
===Graphical===
{{libheader|DOME}}
{{libheader|Go-fonts}}
This is designed to look as close as possible to the Red entry's image so that we don't have to fill up Wikipedia Commons with similar looking images.
<lang ecmascript>import "dome" for Window
import "graphics" for Canvas, Color, Font
class Main {
construct new(n) {
var size = 60 * n + 10
Window.resize(size, size)
Canvas.resize(size, size)
Window.title = "Four sides of a square"
// see Go-fonts page
Font.load("Go-Regular20", "Go-Regular.ttf", 20)
Canvas.font = "Go-Regular20"
var beige = Color.new(245, 245, 220)
Canvas.cls(Color.lightgray)
for (x in 0...n) {
for (y in 0...n) {
var cx = x*60 + 10
var cy = y*60 + 10
if (x == 0 || x == n-1 || y == 0 || y == n-1) {
Canvas.rectfill(cx, cy, 50, 50, Color.brown)
Canvas.print("1", cx + 20, cy + 15, beige)
} else {
Canvas.rectfill(cx, cy, 50, 50, beige)
Canvas.print("0", cx + 20, cy + 15, Color.brown)
}
}
}
}

init() {}

update() {}

draw(alpha) {}
}

var Game = Main.new(9)</lang>

{{out}}
<pre>
Similar to Red entry image.
</pre>

Revision as of 10:24, 18 February 2022

Four sides of square is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


Fill with 1 four sides of square.
If you can please use GUI


Red

<lang rebol>Red[]

view-square: function [size][

   matrix: copy [
       title "Four sides of a square"
       style cell: base 50x50 font-size 20
       style one: cell brown font-color beige "1"  ; I am not an artist. Please have mercy!
       style zero: cell beige font-color brown "0"
   ]
   repeat i size [
       either any [i = 1 i = size] [
           append matrix append/dup copy [] 'one size
       ][
           row: append/dup copy [] 'zero size
           row/1: row/:size: 'one
           append matrix row
       ]
       append matrix 'return
   ]
   view matrix

]

view-square 9</lang>

Output:

https://commons.wikimedia.org/wiki/File:Hollow_matrix_gui.png

Ring

<lang ring>

  1. Project : Identity Matrix
  2. Date  : 2022/16/02
  3. Author  : Gal Zsolt (~ CalmoSoft ~)
  4. Email  : <calmosoft@gmail.com>

load "stdlib.ring" load "guilib.ring"

size = 9 C_Spacing = 1

C_ButtonBlueStyle = 'border-radius:6px;color:black; background-color: blue' C_ButtonOrangeStyle = 'border-radius:6px;color:black; background-color: orange'

Button = newlist(size,size) LayoutButtonRow = list(size)

app = new qApp {

     win = new qWidget() {

setWindowTitle('Identity Matrix') move(500,100) reSize(600,600) winheight = win.height() fontSize = 18 + (winheight / 100)

	    LayoutButtonMain = new QVBoxLayout()			

LayoutButtonMain.setSpacing(C_Spacing) LayoutButtonMain.setContentsmargins(0,0,0,0)

for Row = 1 to size LayoutButtonRow[Row] = new QHBoxLayout() { setSpacing(C_Spacing) setContentsmargins(0,0,0,0) }

        	 for Col = 1 to size

Button[Row][Col] = new QPushButton(win) {

                                       setSizePolicy(1,1)                                                

}

LayoutButtonRow[Row].AddWidget(Button[Row][Col]) next LayoutButtonMain.AddLayout(LayoutButtonRow[Row]) next

             LayoutDataRow1 = new QHBoxLayout() { setSpacing(C_Spacing) setContentsMargins(0,0,0,0) }
             LayoutButtonMain.AddLayout(LayoutDataRow1)
             setLayout(LayoutButtonMain)
             show()
  }
  pBegin()
  exec()
  }

func pBegin()

    for Row = 1 to size
        for Col = 1 to size 
            if Row = 1 or row = size or Col = 1 or Col = size
               Button[Row][Col].setStyleSheet(C_ButtonOrangeStyle)
               Button[Row][Col].settext("1")
            else
               Button[Row][Col].setStyleSheet(C_ButtonBlueStyle)
               Button[Row][Col].settext("0")
            ok

next

    next

</lang> Output image:
Four sides of square

Wren

Text based

<lang ecmascript>var hollowMatrix = Fn.new { |n|

   for (i in 0...n) {
       for (j in 0...n) {
           System.write((i == 0 || i == n-1 || j == 0 || j == n-1) ? "1 " : "0 ")
       }
       System.print()
   }

}

hollowMatrix.call(9)</lang>

Output:
1 1 1 1 1 1 1 1 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 1 1 1 1 1 1 1 1 

Graphical

Library: DOME
Library: Go-fonts

This is designed to look as close as possible to the Red entry's image so that we don't have to fill up Wikipedia Commons with similar looking images. <lang ecmascript>import "dome" for Window import "graphics" for Canvas, Color, Font class Main {

   construct new(n) {
       var size = 60 * n + 10
       Window.resize(size, size)
       Canvas.resize(size, size)
       Window.title = "Four sides of a square"
       // see Go-fonts page
       Font.load("Go-Regular20", "Go-Regular.ttf", 20)
       Canvas.font = "Go-Regular20"
       var beige = Color.new(245, 245, 220)
       Canvas.cls(Color.lightgray)
       for (x in 0...n) {
           for (y in 0...n) {
               var cx = x*60 + 10
               var cy = y*60 + 10
               if (x == 0 || x == n-1 || y == 0 || y == n-1) {
                   Canvas.rectfill(cx, cy, 50, 50, Color.brown)
                   Canvas.print("1", cx + 20, cy + 15, beige)
                } else {
                   Canvas.rectfill(cx, cy, 50, 50, beige)
                   Canvas.print("0", cx + 20, cy + 15, Color.brown)
                }
           }
       }
   }
   init() {}
   update() {}
   draw(alpha) {}

}

var Game = Main.new(9)</lang>

Output:
Similar to Red entry image.