Sedat Gokcen
Sedat Gokcen

Reputation: 3270

Reaching a specific item in define-struct

I'm really stuck about somethings and I will try to tell my questions correctly, I hope you can understand. It can be a little bit long so firstly thank you to spends your time to read this.

I'm trying to create a game whose name is "Same" in Racket 5.0.2 version.

Here is explanation of the game: http://download.racket-lang.org/docs/5.0.2/html/games/same.html?q=games

I created a table with disks and draw it:

a: width
b: height
r: radius

    (define (color x)  ///for random colors
      (cond [(< (random x) 100) 'blue]
            [(< (random x) 200) 'purple]
            [(< (random x) 300) 'yellow]
            [(< (random x) 400) 'red]
            [else 'green]))

    (define-struct top (coord color))
    (define (row x y)
      (if (> x (- a r)) empty
          (cons (make-top (make-posn x y)(color 500)) (row (+ x (* 2 r)) y))))

    (define (draw-row L)
      (if (empty? L) #f
          (and
            (draw-solid-disk (top-coord (first L)) r (top-color (first L)))
            (draw-row (rest L)))))

    (define (board x y)
      (if (> y (- b r)) empty
          (cons (row x y) (board x (+ y (* 2 r))))))

    (for-each draw-row (board 20 20))

So I've 200 disks with random colors...(There are 20 disks in every row)

Here my biggest problems are:

1) To delete the disk, player will input particular line and column. Will I have conditions for every choices?

if line=1 and column=1, delete this disk and its same colored adjacent disks
if line=5 and column=7, delete that disk and its same colored adjacent disks

I hope you have some easier, alternative ways because it looks extremely challenging.

2) How can I compare disk's colors in many lists? It's hard to tell my problem but I'll try.

    (define table (board 20 20))      
    (define row1 (list-ref table 0))
    (list-ref row1 0)

It will return:

(make-top (make-posn 20 20) 'yellow)

How can I reach 'yellow in here? And if I reach, how can I compare it with other colors?

Any idea would be great! I've been thinking about these questions for 2 days and still I couldn't do anything.

I shouldn't use mutable structures

Upvotes: 2

Views: 112

Answers (1)

ben rudgers
ben rudgers

Reputation: 3669

Structs come with built in accessors:

> (define my-top (make-top (make-posn 20 20) 'yellow))
> (top-color my-top)
'yellow
> (top-coord my-top)
(make-posn 20 20)
> (top? my-top)
true

Upvotes: 3

Related Questions