Reputation: 39
Currently setting cells in list notation using scheme. My question is how to make scheme(Dr Racket) list pair correctly using cons. My code accounts for the right location I want but creates a list by adding on the front. I am currently only working on the column = 1, 2, 3 or 4 aspect of this, and when row = 1 ONLY... Any help would be greatly appreciated.
(define KMMgame 0)
(define (KMMStartGame)
( begin
(set! KMMgame '( 1(4 9 0 0)(99 0 0 0)(88 0 0 0)(11 0 0 1)
(3 8 0 0)(77 0 0 0)(66 0 0 0)(55 0 0 2)
(2 0 0 0)(44 0 0 0)(33 0 0 0)(22 0 0 3)
(1 0 0 0)(43 0 0 0)(34 0 0 0)(87 0 0 4)))
(display "Starting Game Now!" ) (newline)
#t))
;Passes list without the one on front (That is for my player state, irrelevant to my question)
(define ( KMMmove KMMgame Plane Row Column Token)
(KMMMove (car(cdr KMMgame)) (cdr (cdr KMMgame)) Plane Row Column Token) )
;Set the cell to token.
(define (KMMMove KMMgame KMMend Plane Row Column Token)
(if (= Row 1)
(if (= Column 1)
(cons (cons Token (cdr KMMgame)) KMMend)
(cons (car KMMgame) (KMMMove (cdr KMMgame) KMMend Plane Row (- Column 1) Token))
)
;Next line accounts for rows greater than one, not working yet so please disregard. Exists to compile.
(cons (cons (car(cdr (cdr KMMgame))) (KMMMove (car KMMgame) Plane (- Row 1) Column Token)) (cdr(cdr KMMgame))
)
)
)
(KMMMove KMMgame 4 1 1 999) Plane 4, Row 1, Column 1, Token to be added instead of current number 999, will print the desired output but inside an extra set of parenthesis:
((999 9 0 0)
(99 0 0 0)
(88 0 0 0)
(11 0 0 1)
(3 8 0 0)
(77 0 0 0)
(66 0 0 0)
(55 0 0 2)
(2 0 0 0)
(44 0 0 0)
(33 0 0 0)
(22 0 0 3)
(1 0 0 0)
(43 0 0 0)
(34 0 0 0)
(87 0 0 4))
( KMMmove KMMgame 4 1 2 999) Plane 4, Row 1, Column 2, Token 999, gives the correct location for column but is pairing the front of the list outside.
(4
(999 0 0)
(99 0 0 0)
(88 0 0 0)
(11 0 0 1)
(3 8 0 0)
(77 0 0 0)
(66 0 0 0)
(55 0 0 2)
(2 0 0 0)
(44 0 0 0)
(33 0 0 0)
(22 0 0 3)
(1 0 0 0)
(43 0 0 0)
(34 0 0 0)
(87 0 0 4))
Desired output for column 2:
(4 999 0 0)
(99 0 0 0)
(88 0 0 0)
(11 0 0 1)
(3 8 0 0)
(77 0 0 0)
(66 0 0 0)
(55 0 0 2)
(2 0 0 0)
(44 0 0 0)
(33 0 0 0)
(22 0 0 3)
(1 0 0 0)
(43 0 0 0)
(34 0 0 0)
(87 0 0 4)
Upvotes: 1
Views: 186
Reputation: 50
To get a list like that, I'd suggest helper functions!
(define (newGameState KMMgame Row Column Plane Token)
(if (= Plane 4)
(steptwo KMMgame Row Column Plane Token)
(newGameState KMMgame (+ 4 Row) (+ Plane 1) Column Token)
)
)
(define (steptwo KMMgame Row Column Plane Token)
(if (= Row 1)
(cons (stepthree (car (cdr KMMgame)) Row Column Plane Token) (cdr (cdr KMMgame)))
(cons (car (cdr KMMgame))(steptwo (cdr KMMgame) (- Row 1) Column Plane Token))
)
)
(define (stepthree KMMgame Row Column Plane Token)
(if (= Column 1)
(cons Token (cdr KMMgame))
(cons (car KMMgame) (stepthree (cdr KMMgame) Row (- Column 1) Plane Token))
)
)
This should give desired output!
Upvotes: 1