On May 27, 2017, at 7:00 AM, A Mauer-Oats <mauero...@gmail.com> wrote: > I thought it would be interesting to work with 2D data in an "advanced > introductory" class. At least to the extent of being able to model classic > board games like Battleship and Checkers. > [...] > Any suggestions or tales of relevant experience would be welcome. (Even > "don't do that!" notes from people with more experience. :)
Albeit not recently, I've had students do Battleship as a project near the end of second semester (of HS intro programming using HtDP). It was all using lists of lists. For the size of the board, the performance of LOLs (vs. arrays) is perfectly adequate, and I see no reason to get into arrays just for this one project. IIRC the data defs were something like this: ;; A Cell is either ;; #f - meaning an unplayed cell ;; 'hit - meaning a cell that a previous shot hit ;; 'miss - meaning a cell where a previous shot missed ;; A Board is a Listof[Listof[Cell]]. ;; Each list in the Board represents one ;; row, and all rows are of equal length. ;; A Ship is a (make-ship String Listof[Posn]) (define-struct ship [name cells]) ;; indicating a ship's name ;; and all locations on it that aren't yet hit. ;; A Game is a (make-game Bool Listof[Ship] Board): (define-struct game [my-turn? ships shots]) ;; in which my-turn? is true iff it's the player's turn, ;; ships represents the locations of all the player's ships, and ;; shots represents shots the player has fired ;; at the enemy's ships. I did this after all students had done the problems on lists of lists (e.g., reading file input as lists of lists of words), and after the two-complex-parameters work that includes writing the equivalent of list-ref — so it isn't a huge stretch at that point for them to write functions occupied? : Nat Nat Game -> Boolean (asking "does one of my ships occupy (row,col)?") and remove-ship-posn : Nat Nat Listof[Ship] -> Listof[Ship] (used when one of the player's ships is hit) for tracking enemy shots, and functions such as record-shot : Nat Nat Game -> Game (which records a shot the player has fired at the enemy, by updating the Board) for dealing with the player's own shots. It was a fun project. The game was networked using 2htdp/universe, so the above definitions are for client side only; the server maintained each client's list of ships and a list of the shots each player had fired (plus whose turn it is, of course). I don't remember whether I provided the server code, or had one team write the server code, but having the choice is one nice thing about this game as a project: it scales to several different levels of student ability. Cheers, Jordan -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.