Sign in to follow this  

DrScheme Tic-Tac-Toe Newb Help

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, i'm quite a newb when it comes to scheme - and all other languages for that matter. Now i've been trying to design a check for whether or not a tic-tac-toe board, in the form of a list. ie.
(list #\X #\O #\-
#\X #\- #\- 
#\X #\O #\-)
And return #t if it is a winning combination (like above) or #f if not. Now, i've worked out how to do this with 3x3, 4x4 and higher.. (board) -> like the list above. 3x3:
  (or
   (and
    (and (eq? (list-ref (board) 0) (list-ref (board) 1)) 
         (eq? (list-ref (board) 0) (list-ref (board) 2)))
    (not (eq? (list-ref (board) 0) #\-))
    )
   ; 0 1 2
   
   (and
    (and (eq? (list-ref (board) 3) (list-ref (board) 4)) 
         (eq? (list-ref (board) 3) (list-ref (board) 5)))
    (not (eq? (list-ref (board) 3) #\-))
    )
   ; 3 4 5
   
   (and
    (and (eq? (list-ref (board) 6) (list-ref (board) 7)) 
         (eq? (list-ref (board) 6) (list-ref (board) 8)))
    (not (eq? (list-ref (board) 6) #\-))
    )
   ; 6 7 8
   
   (and
    (and (eq? (list-ref (board) 0) (list-ref (board) 3)) 
         (eq? (list-ref (board) 0) (list-ref (board) 6)))
    (not (eq? (list-ref (board) 0) #\-))
    )
   ; 0 3 6
   
   (and
    (and (eq? (list-ref (board) 1) (list-ref (board) 4)) 
         (eq? (list-ref (board) 1) (list-ref (board) 7)))
    (not (eq? (list-ref (board) 1) #\-))
    )
   ; 1 4 7
   
   (and
    (and (eq? (list-ref (board) 2) (list-ref (board) 5)) 
         (eq? (list-ref (board) 2) (list-ref (board) 8)))
    (not (eq? (list-ref (board) 2) #\-))
    )
   ; 2 5 8
   
   (and
    (and (eq? (list-ref (board) 0) (list-ref (board) 4)) 
         (eq? (list-ref (board) 0) (list-ref (board) 8)))
    (not (eq? (list-ref (board) 0) #\-))
    )
   ; 0 4 8
   
   (and
    (and (eq? (list-ref (board) 2) (list-ref (board) 4)) 
         (eq? (list-ref (board) 2) (list-ref (board) 6)))
    (not (eq? (list-ref (board) 2) #\-))
    )
   ; 2 4 6
   )

4x4:
  (or
   (and
    (and (eq? (list-ref (board) 0) (list-ref (board) 1)) 
         (eq? (list-ref (board) 0) (list-ref (board) 2))
         (eq? (list-ref (board) 0) (list-ref (board) 3)))
    (not (eq? (list-ref (board) 0) #\-))
    )
   ; 0 1 2 3
   
   (and
    (and (eq? (list-ref (board) 4) (list-ref (board) 5)) 
         (eq? (list-ref (board) 4) (list-ref (board) 6))
         (eq? (list-ref (board) 4) (list-ref (board) 7)))
    (not (eq? (list-ref (board) 4) #\-))
    )
   ; 4 5 6 7
   
   (and
    (and (eq? (list-ref (board) 8) (list-ref (board) 9)) 
         (eq? (list-ref (board) 8) (list-ref (board) 10))
         (eq? (list-ref (board) 8) (list-ref (board) 11)))
    (not (eq? (list-ref (board) 8) #\-))
    )
   ; 8 9 10 11
   
   (and
    (and (eq? (list-ref (board) 12) (list-ref (board) 13)) 
         (eq? (list-ref (board) 12) (list-ref (board) 14))
         (eq? (list-ref (board) 12) (list-ref (board) 15)))
    (not (eq? (list-ref (board) 12) #\-))
    )
   ; 12 13 14 15
   
   (and
    (and (eq? (list-ref (board) 0) (list-ref (board) 4)) 
         (eq? (list-ref (board) 0) (list-ref (board) 8))
         (eq? (list-ref (board) 0) (list-ref (board) 12)))
    (not (eq? (list-ref (board) 0) #\-))
    )
   ; 0 4 8 12
   
   (and
    (and (eq? (list-ref (board) 1) (list-ref (board) 5)) 
         (eq? (list-ref (board) 1) (list-ref (board) 9))
         (eq? (list-ref (board) 1) (list-ref (board) 13)))
    (not (eq? (list-ref (board) 1) #\-))
    )
   ; 1 5 9 13
   
   (and
    (and (eq? (list-ref (board) 2) (list-ref (board) 6)) 
         (eq? (list-ref (board) 2) (list-ref (board) 10))
         (eq? (list-ref (board) 2) (list-ref (board) 14)))
    (not (eq? (list-ref (board) 2) #\-))
    )
   ; 2 6 10 14
   
   (and
    (and (eq? (list-ref (board) 3) (list-ref (board) 7)) 
         (eq? (list-ref (board) 3) (list-ref (board) 11))
         (eq? (list-ref (board) 3) (list-ref (board) 15)))
    (not (eq? (list-ref (board) 3) #\-))
    )
   ; 3 7 11 15
   
   (and
    (and (eq? (list-ref (board) 0) (list-ref (board) 5)) 
         (eq? (list-ref (board) 0) (list-ref (board) 10))
         (eq? (list-ref (board) 0) (list-ref (board) 15)))
    (not (eq? (list-ref (board) 0) #\-))
    )
   ; 0 5 10 15
   
   (and
    (and (eq? (list-ref (board) 3) (list-ref (board) 6)) 
         (eq? (list-ref (board) 3) (list-ref (board) 9))
         (eq? (list-ref (board) 3) (list-ref (board) 12)))
    (not (eq? (list-ref (board) 3) #\-))
    )
   ; 3 6 9 12   
   )

However, this is not exactly ideal and where i need help is creating a test like the above 3x3 and 4x4 for any number sized board. Any help would be greatly appreciated.. thanks!

Share this post


Link to post
Share on other sites
Some hints:

Represent the board as a list of lists, instead:


(list (list #\X #\O #\-)
(list #\X #\- #\-)
(list #\X #\O #\-))


Now checking for a win through a row is a matter of checking each sub-list for the property "are all the symbols the same?". Make a separate lambda/macro/etc. for that.

Actually ;) so that you learn your recursion, try making a function (all-eq? item alist), which checks if everything in the list matches item. You can do this recursively, by checking if the first of the list matches, and also that everything in the rest of the list matches.

To check the columns, simply transpose the list of lists first, with (zip). That turns rows into columns and columns into rows.

Diagonals will be trickier, but there are only two regardless of the board size.

Share this post


Link to post
Share on other sites
thanks Zahlman for the tips.

is it possible to split the list into smaller lists like you have done, given the starting single list?

i mean.. if i was given a list with say 64 items in it, would i be able to split these 64 into 8 lists containing 8 items? i've been playing around with the integer-sqrt to try and get row/column co-ordinates but haven't really had any success.

also, i've checked out (zip) but it doesn't want to work using the Pretty Big (includes MrEd and Advanced Student)language, found a little about it on the drscheme help desk but didn't really help :(

oh well, thanks anyway!

lodog.

Share this post


Link to post
Share on other sites

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this