From 9323b899d930107f9f4779c707e17e5dc43c5de4 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Sun, 5 Nov 2023 16:21:12 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D1=8B=20=D0=BD=D0=B0=20?= =?UTF-8?q?racket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + racket/chat.rkt | 35 ++++++++++++++++++ racket/gol.rkt | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 racket/chat.rkt create mode 100644 racket/gol.rkt diff --git a/.gitignore b/.gitignore index 8d98f9d..9ea0f13 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .* +!.gitignore diff --git a/racket/chat.rkt b/racket/chat.rkt new file mode 100644 index 0000000..012fe62 --- /dev/null +++ b/racket/chat.rkt @@ -0,0 +1,35 @@ +#lang racket +(require racket/tcp) + +(define outs '()) + +(define (write-all data out) + (for-each + (lambda (o) + (cond [(not (eq? o out)) (displayln data o)])) + outs)) + +(define (worker in out) + (define (loop msg) + (if (equal? msg eof) + (begin (close-input-port in) + (close-output-port out) + (set! outs (remove out outs))) + (let ([data (read-line in)]) + (displayln data) + (write-all (string-append ">>> " data) out) + (loop data)))) + (loop 'y)) + +(define (listener sock) + (define-values (in-conn out-conn) (tcp-accept sock)) + (file-stream-buffer-mode in-conn 'none) + (file-stream-buffer-mode out-conn 'none) + (thread (lambda () (worker in-conn out-conn))) + (set! outs (cons out-conn outs)) + (writeln (length outs)) + (listener sock)) + +(define sock (tcp-listen 1678 4 #t)) +(listener sock) +(tcp-close sock) \ No newline at end of file diff --git a/racket/gol.rkt b/racket/gol.rkt new file mode 100644 index 0000000..ad5bbf6 --- /dev/null +++ b/racket/gol.rkt @@ -0,0 +1,111 @@ +#lang racket +(require graphics/graphics) +(require racket/bool) + +(define screen-width 600) +(define screen-height screen-width) + +(define board-width 15) +(define board-height 15) + +(define cell-vertical-size (/ screen-width board-width)) +(define cell-horizontal-size (/ screen-height board-height)) + +(define line-color (make-rgb 0.8 0.8 0.8)) +(define cell-color (make-rgb 0.95 0.95 0)) + +(define starting-board (set '(1 0) + '(2 1) + '(0 2) + '(1 2) + '(2 2))) + +(open-graphics) + +(define w (open-viewport "Game of Life" + screen-width + screen-height)) + +(define (draw-vertical-lines coord step) + (cond + [(< coord screen-width) (begin + ((draw-line w) (make-posn coord 0) + (make-posn coord screen-height) + line-color) + (draw-vertical-lines (+ coord step) step))])) + +(define (draw-horizontal-lines coord step) + (cond + [(< coord screen-width) (begin + ((draw-line w) (make-posn 0 coord) + (make-posn screen-height coord) + line-color) + (draw-horizontal-lines (+ coord step) step))])) + +(define (draw-lines width height) + (draw-vertical-lines cell-vertical-size cell-vertical-size) + (draw-horizontal-lines cell-horizontal-size cell-horizontal-size)) + +(define (draw-cell cell) + ((draw-solid-ellipse w) (make-posn (* cell-horizontal-size (car cell)) + (* cell-horizontal-size (car (cdr cell)))) + cell-vertical-size + cell-horizontal-size + cell-color)) + +(define (draw-board board) + (for ([cell board]) + (draw-cell cell))) + +(define (count-alive-neighbors orig-cell board) + (let* ([orig-x (car orig-cell)] + [orig-y (car (cdr orig-cell))]) + (define (iterate y sum) + (define (iterate-row x row-sum) + (let ([cell (list x y)]) + (if (<= x (+ orig-x 1)) + (iterate-row (+ x 1) (if (set-member? board cell) + (+ row-sum 1) + row-sum)) + row-sum))) + (if (<= y (+ orig-y 1)) (iterate (+ y 1) + (+ sum + (iterate-row (- orig-x 1) 0))) + sum)) + + (let ([neighbors (iterate (- orig-y 1) 0)]) + (if (set-member? board orig-cell) + (- neighbors 1) + neighbors)))) + +(define (will-be-alive cell board) + (let* ([alive-neighbors (count-alive-neighbors cell board)] + [is-alive (set-member? board cell)]) + (or + (and is-alive (or (eq? alive-neighbors 2) (eq? alive-neighbors 3))) + (and (not is-alive) (eq? alive-neighbors 3))))) + +(define (next-generation current-board) + (define new-board (mutable-set)) + + (define (iterate y) + (define (iterate-row x) + (let ([cell (list x y)]) + (cond [(will-be-alive cell current-board) (set-add! new-board cell)]) + (cond [(< x board-width) (iterate-row (+ x 1))]))) + (iterate-row 0) + (cond [(< y board-height) (iterate (+ y 1))])) + + (iterate 0) + new-board) + +(define (play board) + ((clear-viewport w)) + (draw-lines board-width board-height) + (draw-board board) + (sleep 0.5) + (play (next-generation board))) + +(play starting-board) + +(close-graphics) \ No newline at end of file -- cgit v1.2.3