display a array into a grid

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

display a array into a grid

Roelof
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ?

I know I can display items with each but not how to display 4 do a line
break, display 4 and so on.

Roelof

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

jWarrior
On 4/14/2014 10:15 AM, Roelof Wobben wrote:
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ?

| array |

array := ( Array new)  addAll: #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16).
array doWithIndex: [:each :index |
    Transcript show : each printString, ' '. 
    index \\ 4 ifTrue: [Transcript cr]].


I know I can display items with each but not how to display 4 do a line break, display 4 and so on.

Roelof

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc



-- 
Donald [|]

Joel Cairo: "You always have a very smooth explanation."
Sam Spade: "What do you want me to do? Learn to stutter?"
- The Maltese Falcon

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

jWarrior
Whoops!
| array |

array := ( Array new)  addAll: #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16).
array doWithIndex: [:each :index |
    Transcript show : each printString, ' '. 
    (index \\ 4) = 0 ifTrue: [Transcript cr]].


I know I can display items with each but not how to display 4 do a line break, display 4 and so on.

Roelof

_



-- 
Donald [|]

Joel Cairo: "You always have a very smooth explanation."
Sam Spade: "What do you want me to do? Learn to stutter?"
- The Maltese Falcon

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Paul Baumann
In reply to this post by Roelof
Here is one way:

| items s |
items := (1 to: 16).
s := WriteStream on: String new.
items doWithIndex: [:ea :i |
        s print: ea.
        i \\ 4 = 0 ifTrue: [s cr] ifFalse: [s tab].
].
s contents

'1      2       3       4
5       6       7       8
9       10      11      12
13      14      15      16
'


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Roelof Wobben
Sent: Monday, April 14, 2014 10:16
To: [hidden email]
Subject: [vwnc] display a array into a grid

Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ?

I know I can display items with each but not how to display 4 do a line break, display 4 and so on.

Roelof

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Steven Kelly
In reply to this post by Roelof
Roelof Wobben wrote:
> I have a array of 16 items.
> Now I want to display them into a array of 4 X 4.
>
> How can I do this ?

I'd use a TwoDList in a Table widget. See GUIDevGuide.pdf, Configuring
Widgets | Tables.

HTH,
Steve

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
jas
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

jas
In reply to this post by Roelof
On 4/14/2014 7:15 AM, Roelof Wobben wrote:
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ? What I try to figure out is the next step.
How can I represent the 4 x 4 grid with on the two positions choosen by the random function a "2" and all the others empty.

Hi Roelof,

If I'm understanding you correctly,
the following should get you started.

Cheers,

-cstb


"verify "

| random |

random := Random new.

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r < 1] )
   ].

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r * 16 < (1 * 16)] )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next *16) truncate..
     self assert: ( (0 to: 15) includes: r )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next * 16) truncate + 1.
     self assert: ( (1 to: 16) includes: r )
   ].


" define "

| size at atput random randomX randomY grid |

size := 4@4.
at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
atput := [:coordinate :value| grid at: (coordinate y - 1 * size x) + coordinate x put: value].
random := Random new.
randomX := [(random next * (size x)) truncate + 1].    " 1..size x "
randomY := [(random next * (size y)) truncate + 1].    " 1..size y "" create "


"create "

grid := OrderedCollection new: (size x * size y) withAll: 0.


" fill "

first := randomX value @ randomY value.
second := randomX value @ randomY value.

atput value: first value: 2.
atput value: second value: 2.


" show "

1 to: size y do:
    [:y|
    1 to: size x do:
        [:x |
        Transcript tab; nextPut: (at value: x@y) displayString
        ]
    Transcript cr
    ]
       

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Arden-8
In reply to this post by Roelof
Hi  Roelof, et al;

It sounds like you may be doing exactly what I just did - implementing
"2048".
If not you still may find it of interest.  
It displays a 4x4 grid, and uses Random for determining which empty space to
add a new value.

I published a basic version of this on the Cincom Public Repository.

It is deliberately basic so you can easily enhance, improve and modify it,
or just see how to do this work in VW.

Check out:
http://ardenssmalltalkblog.wordpress.com/

hth

Arden Thomas
Cincom Smalltalk Product Manager
[hidden email]

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf
Of Roelof Wobben
Sent: Monday, April 14, 2014 10:16 AM
To: [hidden email]
Subject: [vwnc] display a array into a grid

Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ?

I know I can display items with each but not how to display 4 do a line
break, display 4 and so on.

Roelof

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Roelof
In reply to this post by jas
Thanks,

The code gets more and more difficult.
I have a few questions so I can understand what the code does,

Why three times 1000timesrepeat ?

Where does coordinate come from and how does the x and y gets a value

I try to understand the code so I can change it if it's needed otherwise it's copy /past and then i do not learn anything.

Roelof


jas schreef op 15-4-2014 2:28:
On 4/14/2014 7:15 AM, Roelof Wobben wrote:
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ? What I try to figure out is the next step.
How can I represent the 4 x 4 grid with on the two positions choosen by the random function a "2" and all the others empty.

Hi Roelof,

If I'm understanding you correctly,
the following should get you started.

Cheers,

-cstb


"verify "

| random |

random := Random new.

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r < 1] )
   ].

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r * 16 < (1 * 16)] )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next *16) truncate..
     self assert: ( (0 to: 15) includes: r )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next * 16) truncate + 1.
     self assert: ( (1 to: 16) includes: r )
   ].


" define "

| size at atput random randomX randomY grid |

size := 4@4.
at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
atput := [:coordinate :value| grid at: (coordinate y - 1 * size x) + coordinate x put: value].
random := Random new.
randomX := [(random next * (size x)) truncate + 1].    " 1..size x "
randomY := [(random next * (size y)) truncate + 1].    " 1..size y "" create "


"create "

grid := OrderedCollection new: (size x * size y) withAll: 0.


" fill "

first := randomX value @ randomY value.
second := randomX value @ randomY value.

atput value: first value: 2.
atput value: second value: 2.


" show "

1 to: size y do:
    [:y|
    1 to: size x do:
        [:x |
        Transcript tab; nextPut: (at value: x@y) displayString
        ]
    Transcript cr
    ]
       


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Steven Kelly
Roelof Wobben wrote Fri 18/04/2014 09:53:
> Why three times 1000timesrepeat ?
They're just tests at the start, example code where he verifies his assumption that the Smalltalk Random number stream gives a number >=0 and <1. They're not part of the actual code, they're intended to be written in a unit test. If you're not up to speed with a unit test framework like SUnit yet, you can replace the lines like
   self assert: ( r positive and: [r < 1] )
with code that opens a debugger if the assertion isn't true:
                   ( r positive and: [r < 1] ) ifFalse: [r halt].

>Where does coordinate come from
The line:
   at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
defines a block, which is like a little inline function (or a lambda if you know functional programming). ":coordinate |" at the start declares the formal parameters of the block, in this case a single parameter, coordinate, which gets a value from the argument with which the block is called, e.g. in the innermost loop at the end:
  at value: x@y
which runs the block with coordinate set to the Point made from the current values, x @ y.
 
> and how does the x and y gets a value 
The x and y in the block aren't variables, they're messages sent to "size" and "coordinate". "coordinate" is a formal parameter of the block, and "size" has been defined in the previous line as 4 @ 4. "size x" thus means "get the x co-ordinate of the Point 4 @ 4", and returns 4.
 
The x and y in the last few lines are of the pattern:
   1 to: size y do: [:y| ... ]
where "size y" is a message #y sent to a variable "size", which has the value 4 @ 4. "size y" thus returns 4, the y co-ordinate of the Point 4 @ 4.
:y defines an iterator variable, a formal parameter of the block that iterates over 1..4. In BASIC that would be "FOR y=1 TO 4 ... NEXT y"
 
If you find the difference between "size x" and "x" confusing (rather likely!), you might want to change the first line from "size := 4 @ 4" to "maxX := maxY := 4", and replace each "size x" with "maxX" and "size y" with "maxY". The last loops are then:
 
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x@y) displayString]].
That's just my personal style of indentation. I also moved the "Transcript cr" before the rest of the output to the Transcript, to follow the normal VW approach of starting a new Transcript line before outputting, rather than adding one after outputting. I think nextPut: (which expects a Character, not a String, as argument) was a typo for nextPutAll:. I corrected it to show:, which expects a String like nextPutAll: and also makes sure the Transcript display is updated.
 
All the best,
Steve

jas schreef op 15-4-2014 2:28:
On 4/14/2014 7:15 AM, Roelof Wobben wrote:
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ? What I try to figure out is the next step.
How can I represent the 4 x 4 grid with on the two positions choosen by the random function a "2" and all the others empty.

Hi Roelof,

If I'm understanding you correctly,
the following should get you started.

Cheers,

-cstb


"verify "

| random |

random := Random new.

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r < 1] )
   ].

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r * 16 < (1 * 16)] )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next *16) truncate..
     self assert: ( (0 to: 15) includes: r )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next * 16) truncate + 1.
     self assert: ( (1 to: 16) includes: r )
   ].


" define "

| size at atput random randomX randomY grid |

size := 4@4.
at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
atput := [:coordinate :value| grid at: (coordinate y - 1 * size x) + coordinate x put: value].
random := Random new.
randomX := [(random next * (size x)) truncate + 1].    " 1..size x "
randomY := [(random next * (size y)) truncate + 1].    " 1..size y "" create "


"create "

grid := OrderedCollection new: (size x * size y) withAll: 0.


" fill "

first := randomX value @ randomY value.
second := randomX value @ randomY value.

atput value: first value: 2.
atput value: second value: 2.


" show "

1 to: size y do:
    [:y|
    1 to: size x do:
        [:x |
        Transcript tab; nextPut: (at value: x@y) displayString
        ]
    Transcript cr
    ]
       


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Roelof
Oke,

So to look how it works I can run the create,fill and show part in a workspace ?
or do I have to put everything in the class browser ?

Roelof

Steven Kelly schreef op 18-4-2014 16:26:
Roelof Wobben wrote Fri 18/04/2014 09:53:
> Why three times 1000timesrepeat ?
They're just tests at the start, example code where he verifies his assumption that the Smalltalk Random number stream gives a number >=0 and <1. They're not part of the actual code, they're intended to be written in a unit test. If you're not up to speed with a unit test framework like SUnit yet, you can replace the lines like
   self assert: ( r positive and: [r < 1] )
with code that opens a debugger if the assertion isn't true:
                   ( r positive and: [r < 1] ) ifFalse: [r halt].

>Where does coordinate come from
The line:
   at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
defines a block, which is like a little inline function (or a lambda if you know functional programming). ":coordinate |" at the start declares the formal parameters of the block, in this case a single parameter, coordinate, which gets a value from the argument with which the block is called, e.g. in the innermost loop at the end:
  at value: x@y
which runs the block with coordinate set to the Point made from the current values, x @ y.
 
> and how does the x and y gets a value 
The x and y in the block aren't variables, they're messages sent to "size" and "coordinate". "coordinate" is a formal parameter of the block, and "size" has been defined in the previous line as 4 @ 4. "size x" thus means "get the x co-ordinate of the Point 4 @ 4", and returns 4.
 
The x and y in the last few lines are of the pattern:
   1 to: size y do: [:y| ... ]
where "size y" is a message #y sent to a variable "size", which has the value 4 @ 4. "size y" thus returns 4, the y co-ordinate of the Point 4 @ 4.
:y defines an iterator variable, a formal parameter of the block that iterates over 1..4. In BASIC that would be "FOR y=1 TO 4 ... NEXT y"
 
If you find the difference between "size x" and "x" confusing (rather likely!), you might want to change the first line from "size := 4 @ 4" to "maxX := maxY := 4", and replace each "size x" with "maxX" and "size y" with "maxY". The last loops are then:
 
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x@y) displayString]].
That's just my personal style of indentation. I also moved the "Transcript cr" before the rest of the output to the Transcript, to follow the normal VW approach of starting a new Transcript line before outputting, rather than adding one after outputting. I think nextPut: (which expects a Character, not a String, as argument) was a typo for nextPutAll:. I corrected it to show:, which expects a String like nextPutAll: and also makes sure the Transcript display is updated.
 
All the best,
Steve

jas schreef op 15-4-2014 2:28:
On 4/14/2014 7:15 AM, Roelof Wobben wrote:
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ? What I try to figure out is the next step.
How can I represent the 4 x 4 grid with on the two positions choosen by the random function a "2" and all the others empty.

Hi Roelof,

If I'm understanding you correctly,
the following should get you started.

Cheers,

-cstb


"verify "

| random |

random := Random new.

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r < 1] )
   ].

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r * 16 < (1 * 16)] )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next *16) truncate..
     self assert: ( (0 to: 15) includes: r )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next * 16) truncate + 1.
     self assert: ( (1 to: 16) includes: r )
   ].


" define "

| size at atput random randomX randomY grid |

size := 4@4.
at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
atput := [:coordinate :value| grid at: (coordinate y - 1 * size x) + coordinate x put: value].
random := Random new.
randomX := [(random next * (size x)) truncate + 1].    " 1..size x "
randomY := [(random next * (size y)) truncate + 1].    " 1..size y "" create "


"create "

grid := OrderedCollection new: (size x * size y) withAll: 0.


" fill "

first := randomX value @ randomY value.
second := randomX value @ randomY value.

atput value: first value: 2.
atput value: second value: 2.


" show "

1 to: size y do:
    [:y|
    1 to: size x do:
        [:x |
        Transcript tab; nextPut: (at value: x@y) displayString
        ]
    Transcript cr
    ]
       



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Steven Kelly
There were a couple more minor typos or bugs in the original code (truncate->truncated, OrderedCollection doesn't implement new:withAll: in my VW so use Array instead), so here's the code that works in a VW 7.7.1 workspace:
 
| maxX maxY at atput random randomX randomY grid |
" create "
maxX := maxY := 4.
grid := Array new: (maxX * maxY) withAll: 0.
 
" define helpers "
at := [:coordinate | grid at: (coordinate y - 1) * maxX + coordinate x].
atput := [:coordinate :value | grid at: (coordinate y - 1) * maxX + coordinate x put: value].
random := Random new.
randomX := [(random next * maxX) truncated + 1].    " 1..maxX "
randomY := [(random next * maxY) truncated + 1].    " 1..maxY "
 
" fill "
first := randomX value @ randomY value.
second := randomX value @ randomY value.
atput value: first value: 2.
atput value: second value: 2.
 
" show "
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x @ y) displayString]]
Writing this much code in a workspace is a bit of an anti-pattern. Putting it in a class would let you use more natural, easy-to-read and maintainable Smalltalk, e.g. methods instead of blocks (at, atput, randomX, randomY), and separating instance variables (maxX, maxY, grid, random?) from temporary variables.
 
All the best,
Steve


From: [hidden email] on behalf of Roelof Wobben
Sent: Fri 18/04/2014 18:49
To: [hidden email]
Subject: Re: [vwnc] display a array into a grid

Oke,

So to look how it works I can run the create,fill and show part in a workspace ?
or do I have to put everything in the class browser ?

Roelof

Steven Kelly schreef op 18-4-2014 16:26:
Roelof Wobben wrote Fri 18/04/2014 09:53:
> Why three times 1000timesrepeat ?
They're just tests at the start, example code where he verifies his assumption that the Smalltalk Random number stream gives a number >=0 and <1. They're not part of the actual code, they're intended to be written in a unit test. If you're not up to speed with a unit test framework like SUnit yet, you can replace the lines like
   self assert: ( r positive and: [r < 1] )
with code that opens a debugger if the assertion isn't true:
                   ( r positive and: [r < 1] ) ifFalse: [r halt].

>Where does coordinate come from
The line:
   at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
defines a block, which is like a little inline function (or a lambda if you know functional programming). ":coordinate |" at the start declares the formal parameters of the block, in this case a single parameter, coordinate, which gets a value from the argument with which the block is called, e.g. in the innermost loop at the end:
  at value: x@y
which runs the block with coordinate set to the Point made from the current values, x @ y.
 
> and how does the x and y gets a value 
The x and y in the block aren't variables, they're messages sent to "size" and "coordinate". "coordinate" is a formal parameter of the block, and "size" has been defined in the previous line as 4 @ 4. "size x" thus means "get the x co-ordinate of the Point 4 @ 4", and returns 4.
 
The x and y in the last few lines are of the pattern:
   1 to: size y do: [:y| ... ]
where "size y" is a message #y sent to a variable "size", which has the value 4 @ 4. "size y" thus returns 4, the y co-ordinate of the Point 4 @ 4.
:y defines an iterator variable, a formal parameter of the block that iterates over 1..4. In BASIC that would be "FOR y=1 TO 4 ... NEXT y"
 
If you find the difference between "size x" and "x" confusing (rather likely!), you might want to change the first line from "size := 4 @ 4" to "maxX := maxY := 4", and replace each "size x" with "maxX" and "size y" with "maxY". The last loops are then:
 
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x@y) displayString]].
That's just my personal style of indentation. I also moved the "Transcript cr" before the rest of the output to the Transcript, to follow the normal VW approach of starting a new Transcript line before outputting, rather than adding one after outputting. I think nextPut: (which expects a Character, not a String, as argument) was a typo for nextPutAll:. I corrected it to show:, which expects a String like nextPutAll: and also makes sure the Transcript display is updated.
 
All the best,
Steve

jas schreef op 15-4-2014 2:28:
On 4/14/2014 7:15 AM, Roelof Wobben wrote:
Hello,

I have a array of 16 items.
Now I want to display them into a array of 4 X 4.

How can I do this ? What I try to figure out is the next step.
How can I represent the 4 x 4 grid with on the two positions choosen by the random function a "2" and all the others empty.

Hi Roelof,

If I'm understanding you correctly,
the following should get you started.

Cheers,

-cstb


"verify "

| random |

random := Random new.

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r < 1] )
   ].

1000 timesRepeat:
    [| r |
     r :=  random next.
     self assert: ( r positive and: [r * 16 < (1 * 16)] )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next *16) truncate..
     self assert: ( (0 to: 15) includes: r )
   ].

1000 timesRepeat:
    [| r |
     r :=  (random next * 16) truncate + 1.
     self assert: ( (1 to: 16) includes: r )
   ].


" define "

| size at atput random randomX randomY grid |

size := 4@4.
at := [:coordinate| grid at: (coordinate y - 1 * size x) + coordinate x].
atput := [:coordinate :value| grid at: (coordinate y - 1 * size x) + coordinate x put: value].
random := Random new.
randomX := [(random next * (size x)) truncate + 1].    " 1..size x "
randomY := [(random next * (size y)) truncate + 1].    " 1..size y "" create "


"create "

grid := OrderedCollection new: (size x * size y) withAll: 0.


" fill "

first := randomX value @ randomY value.
second := randomX value @ randomY value.

atput value: first value: 2.
atput value: second value: 2.


" show "

1 to: size y do:
    [:y|
    1 to: size x do:
        [:x |
        Transcript tab; nextPut: (at value: x@y) displayString
        ]
    Transcript cr
    ]
       



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Roelof
Steven Kelly schreef op 18-4-2014 19:01:
There were a couple more minor typos or bugs in the original code (truncate->truncated, OrderedCollection doesn't implement new:withAll: in my VW so use Array instead), so here's the code that works in a VW 7.7.1 workspace:
 
| maxX maxY at atput random randomX randomY grid |
" create "
maxX := maxY := 4.
grid := Array new: (maxX * maxY) withAll: 0.
 
" define helpers "
at := [:coordinate | grid at: (coordinate y - 1) * maxX + coordinate x].
atput := [:coordinate :value | grid at: (coordinate y - 1) * maxX + coordinate x put: value].
random := Random new.
randomX := [(random next * maxX) truncated + 1].    " 1..maxX "
randomY := [(random next * maxY) truncated + 1].    " 1..maxY "
 
" fill "
first := randomX value @ randomY value.
second := randomX value @ randomY value.
atput value: first value: 2.
atput value: second value: 2.
 
" show "
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x @ y) displayString]]
Writing this much code in a workspace is a bit of an anti-pattern. Putting it in a class would let you use more natural, easy-to-read and maintainable Smalltalk, e.g. methods instead of blocks (at, atput, randomX, randomY), and separating instance variables (maxX, maxY, grid, random?) from temporary variables.
 
All the best,
Steve


I know,
For me it's a test if this is doing what i expect.

But I saw something wierd. When I copy this into a workspace and choose print it or do it. I expect that a transscript window is opened. But on my system I do not see a transscript window so I cannot check the outcome.


Roelof


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Steven Kelly
The Transcript is in the main VisualWorks launcher window - press F12 to go there if it's buried.


From: [hidden email] on behalf of Roelof Wobben
Sent: Fri 18/04/2014 20:32
To: [hidden email]
Subject: Re: [vwnc] display a array into a grid

Steven Kelly schreef op 18-4-2014 19:01:
There were a couple more minor typos or bugs in the original code (truncate->truncated, OrderedCollection doesn't implement new:withAll: in my VW so use Array instead), so here's the code that works in a VW 7.7.1 workspace:
 
| maxX maxY at atput random randomX randomY grid |
" create "
maxX := maxY := 4.
grid := Array new: (maxX * maxY) withAll: 0.
 
" define helpers "
at := [:coordinate | grid at: (coordinate y - 1) * maxX + coordinate x].
atput := [:coordinate :value | grid at: (coordinate y - 1) * maxX + coordinate x put: value].
random := Random new.
randomX := [(random next * maxX) truncated + 1].    " 1..maxX "
randomY := [(random next * maxY) truncated + 1].    " 1..maxY "
 
" fill "
first := randomX value @ randomY value.
second := randomX value @ randomY value.
atput value: first value: 2.
atput value: second value: 2.
 
" show "
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x @ y) displayString]]
Writing this much code in a workspace is a bit of an anti-pattern. Putting it in a class would let you use more natural, easy-to-read and maintainable Smalltalk, e.g. methods instead of blocks (at, atput, randomX, randomY), and separating instance variables (maxX, maxY, grid, random?) from temporary variables.
 
All the best,
Steve


I know,
For me it's a test if this is doing what i expect.

But I saw something wierd. When I copy this into a workspace and choose print it or do it. I expect that a transscript window is opened. But on my system I do not see a transscript window so I cannot check the outcome.


Roelof


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: display a array into a grid

Roelof
Thanks,

The script does exactly what I expected.
If I understand it right if I want to change a number I have to change the array grid.
And for printing (displaying) the grid I can use the show function.

Roelof




Steven Kelly schreef op 18-4-2014 19:57:
The Transcript is in the main VisualWorks launcher window - press F12 to go there if it's buried.


From: [hidden email] on behalf of Roelof Wobben
Sent: Fri 18/04/2014 20:32
To: [hidden email]
Subject: Re: [vwnc] display a array into a grid

Steven Kelly schreef op 18-4-2014 19:01:
There were a couple more minor typos or bugs in the original code (truncate->truncated, OrderedCollection doesn't implement new:withAll: in my VW so use Array instead), so here's the code that works in a VW 7.7.1 workspace:
 
| maxX maxY at atput random randomX randomY grid |
" create "
maxX := maxY := 4.
grid := Array new: (maxX * maxY) withAll: 0.
 
" define helpers "
at := [:coordinate | grid at: (coordinate y - 1) * maxX + coordinate x].
atput := [:coordinate :value | grid at: (coordinate y - 1) * maxX + coordinate x put: value].
random := Random new.
randomX := [(random next * maxX) truncated + 1].    " 1..maxX "
randomY := [(random next * maxY) truncated + 1].    " 1..maxY "
 
" fill "
first := randomX value @ randomY value.
second := randomX value @ randomY value.
atput value: first value: 2.
atput value: second value: 2.
 
" show "
1 to: maxY do: [:y |
    Transcript cr.
    1 to: maxX do: [:x |
        Transcript tab; show: (at value: x @ y) displayString]]
Writing this much code in a workspace is a bit of an anti-pattern. Putting it in a class would let you use more natural, easy-to-read and maintainable Smalltalk, e.g. methods instead of blocks (at, atput, randomX, randomY), and separating instance variables (maxX, maxY, grid, random?) from temporary variables.
 
All the best,
Steve


I know,
For me it's a test if this is doing what i expect.

But I saw something wierd. When I copy this into a workspace and choose print it or do it. I expect that a transscript window is opened. But on my system I do not see a transscript window so I cannot check the outcome.


Roelof



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc