newLISP game

For the Compleat Fan
Locked
Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

newLISP game

Post by Fanda »

Hello!
You can find a game called "Inversi" on my website:
http://www.volny.cz/fsodomka/newlisp/

Enjoy, Fanda

PS: If we had a newbie category in the newLISP contest, I would submit it :-)))

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Well done!!! Runs fine in Linux.

I like these type of games very much. Why not submit it as contribution for the contest after all?

Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Excellent, runs fine on Mac OSX too

Lutz

Ps: look into examples/tcltk.lsp in the source distribution directory it shows you how to make newLISP/Tcl-Tk programs without the newlisp-tk shell, just with Tcl/Tlk installed on your system.

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Lutz wrote:Ps: look into examples/tcltk.lsp in the source distribution directory it shows you how to make newLISP/Tcl-Tk programs without the newlisp-tk shell, just with Tcl/Tlk installed on your system.
I will check it out!

Fanda

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Using the method in examples/tcltk.lsp your applications will come up much faster, because there is a lot of overhead in newlisp-tk you don't really need in a finished applications (like all the editor/code-browser/debugger code, which is loading and initializing itself).

Interaction between newLISP and Tc/Tk is also faster because they communicate via pipes instead of Tcp/Ip. On UNIX you just have to be sure that the 'wish' tk window shell is installed, which is default on at least the popular UNIX installs like LINUX and Mac OSX and Win32 if you install both Tcl and Tk.

Lutz

ps: you may also want to checkout http://gtk-server.org/ which uses a similar way to communicate and does platform independent GUIs with a variety of scripting languages.

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

tcltk.lsp is very cool!
I just trying to do the same for subsequent newlisp:

Code: Select all

(map set '(myin nlout) (pipe))
(map set '(nlin myout) (pipe))
(process "newlisp" nlin nlout)

(set 'online true)
(write-line "(println \"Welcome...\")" myout)
(define (convers)
  (while online
    (read-buffer myin 'buf 65000)
    (print buf "\n$ ")
    (write-line (read-line) myout)))
looks working, but when I enter at the "$" prompt:

Code: Select all

$ (begin (println "start") (sleep 30) (println "end"))
I have immediate result for first println and have returned to the prompt.
If, after 30 seconds I press enter, I got the side effect of the second println, i.e.

Code: Select all

fortuna:root# newlisp l.lsp
newLISP v.8.7.0 on linux, execute 'newlisp -h' for more info.

> (convers)
Welcome...
"Welcome..."
$ (+ 3 5)
8
$ (begin (println "start") (sleep 30) (println "end"))
start

$
end
"end"
$
If subsequent newlisp will have command prompt, then I will got the ability to catch whole result.
But, as I think, the prompt is supressed because stdin/out aren't terminal line.
Can I suggest a newlisp option, that will work opposite to "-c", i.e. to turn the command prompt to "on" despite the newlisp started on the terminal or not (and to turn buffering off, of course)?
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

To suppress the side effect to:

(silent (println "start") (sleep 30) (println "end"))

and you may look into newlisp-tk.tcl to see how it done there

Lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Oh! Sorry for my english ;-)
I'm not about a side effect.
I'm about a way to capture all function output at once:
When started through pipes, underlaying newlisp instance doesn't show a prompt (">").
So, if upper-level instance request underlying one to start some time-prolonged function call, it will not be able to know is that function call already finished, or not.
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Implement some kine of protocol to recognize the end of output from newLISP. Turning on the prompt is dangerous because the line-feed '>' combination could just be part of the output, and when the context changes or in debug situations the prompt looks entirely different.

Let the piped newLISP process execute a loop i.e.:

Code: Select all

;; this loop runnning in the piped newLISP process
(while (set 'line (read-line))
    (catch (eval-string line) 'result)
    (println result "###EOT###")
)
Then the controlling program checks for "###EOT###" indicating the end of output coming back.

Lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Thanks!
I like it :-)
WBR, Dmi

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Now I got self-wrapped newlisp shell (newlisp under newlisp's control).
But to completely imitate a newlisp-shell effect, I need some feature.
I run the following loop at underlying newlisp:

Code: Select all

(while (set 'PROTOCOL:line (read-line))
   (catch (eval-string PROTOCOL:line) 'PROTOCOL:result)
   (println PROTOCOL:result "\n###EOT###"))
It represents side effects pretty good. But also I want to output a result value as newlisp does i.e.:

Code: Select all

> (set 'a "a\nb")
"a\nb"
now, when I use (println PROTOCOL:line) I got:

Code: Select all

> (set 'a "a\nb")
a
b
And so for other cases.
How can I format a symbol's contents to a newlisp-compatible source-code representation? (complement to "eval-string")
newLisp already have a function (source), that does similar, but more complex job.
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Lookup the function 'source' in the manual. It works like 'save' but instead writing to a file it returns a string:

Code: Select all

newLISP v.8.7.0 on OSX, execute 'newlisp -h' for more info.

> (define (foo x y) (+ x y))
(lambda (x y) (+ x y)) 
> (source 'foo)
"(define (foo x y)\n  (+ x y))\n\n" 
> 
lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

I just have a look at the newlisp's source code. Now I know!
I just want an exact version of a 'string' function, but with

Code: Select all

printCell(cell , TRUE, (UINT)&strStream);
that you use in REPL in newlisp.c
instead of

Code: Select all

printCell(cell , FALSE, (UINT)&strStream);
that is in original 'string' implementation in nl-string.c

This (very small) piece of code will give newLisp an ability to wrap itself in a shell' that will be undistinguished from original newlisp...
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

This program more or less behaves like the 'real' thing:

Code: Select all

; shell - simulating the newLISP shell
;
(print "newLISP shell\n\n> ")
(while (set 'line (read-line))
    (if (catch (eval-string line) 'result)
        (if (string? result)
            (println "\"" result "\"")
            (println result))
        (println result))
    (print "> "))

Code: Select all

~/newlisp> newlisp shell
newLISP shell

> (print "abc")
abc"abc"
> (+ 3 4)
7
> (abc)
invalid function in function eval-string : (abc)
> 
Lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Hehehe :-)
printCell(TRUE) calls printString, that seems to do the more complex thing:
1. [text][/text] wrapping,
2. converting a bunch of special symbols to metacharacters (\n -> \\n etc.)
3. whatsoever you have in it now or place in it later.

Yes, I can recreate this behavior in newlisp level, but it's a dumb work, I think.
Manual exporting a printCell(TRUE) to newlisp language level is much more simple (i've tested it yesterday ;)

Is there are any obstacle against that for future releases?
WBR, Dmi

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Lutz: 'tcltk.lsp' wasn't working on my Win32. Original version has:

Code: Select all

bind . <Destroy> {puts {(exit)}}
[/text])
I just added 'update idletask':

Code: Select all

bind . <Destroy> {puts {(exit)}}

update idletask

[/text])
Original wasn't responding - window didn't show up "done".

---------------

Is there any easy way how to do my favorite 'tk' function? How do I hide window from newlisp.exe?

Thank you, Fanda

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Thw 'tk' functon is just writing to the pipe 'myout' to Tcl/Tk when using tcltk.lsp

Code: Select all

(write-line "wm withdraw ." myout)    ; hide the main window
get a Tcl/Tk reference, there is one here: http://newlisp.org/downloads/TclTk/

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Its a very nice game for the contest !!

Regards, Norman.
-- (define? (Cornflakes))

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Inversi v. 1.1:
- bigger windows
- levels 2x2 and higher
- better look, faster drawing :)

Fanda

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

hahaha great game..I manage it all the time to create the most strange but semetric figures but it a hard job to get a clean square ;-) nice nice!

Regards, Norman.
-- (define? (Cornflakes))

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Very good improvement, thank you! The concept of this game is elegant, and yet, the game is difficult to solve. Very nice!!!

Peter

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Honestly... Concept of this game isn't mine. I played one that has 5x5 field. I couldn't solve it (only by accident), so I decided to make my own version where I can better see the field and can think about it more :-)

Later on, I added more levels ;-)

Fanda

PaipoJim
Posts: 20
Joined: Fri Jun 03, 2005 3:21 pm
Location: Oregon

Post by PaipoJim »

Fanda wrote:Honestly... Concept of this game isn't mine. I played one that has 5x5 field. I couldn't solve it (only by accident), so I decided to make my own version where I can better see the field and can think about it more :-)

Later on, I added more levels ;-)

Fanda
Good for you! I can't solve the 5x5 even by accident. I'm sure there is some sort of standard strategy for it but every time I get down to 2 squares I get stuck.... :-(

I guess I'll have to console myself by appropriating some of your Tcl/Tk code tricks for my current project!
-

Locked