Created
October 23, 2014 06:39
-
-
Save sunjay/a9c1d90a5d55a5082492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
; | |
; Program for Drawing Genius Tic-Tac-Toe Boards | |
; | |
; Author: Sunjay Varma | |
; Copyright 2014 by Sunjay Varma. All rights reserved. | |
; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(require racket/draw) | |
; Constants | |
(define PIECE_X #\x) | |
(define PIECE_O #\o) | |
(define PIECE_BLANK #\space) | |
(define TILES 3) | |
(define TILE_SIZE 50) | |
(define BOARD_SIZE (* TILE_SIZE TILES)) | |
(define BOARDS 3) | |
(define GRID_SIZE (* BOARD_SIZE BOARDS)) | |
(define FILLED_TILE_COLOR (make-color 112 200 226)) | |
(define UNFILLED_TILE_COLOR "white") | |
(define PIECE_COLOR (make-color 120 120 120)) | |
(define PIECE_LINE_THICKNESS 3) | |
(define PIECE_PADDING 0.10) | |
; Main Board Drawing Function | |
(define (draw-board board) | |
(define target (make-bitmap GRID_SIZE GRID_SIZE)) ; A 450x450 bitmap | |
(define dc (new bitmap-dc% [bitmap target])) | |
(draw-rows dc board 0 #t) | |
(draw-board-borders dc) | |
target) | |
; Draws the given board rows starting at the given vertical offset index | |
(define (draw-rows dc rows offset start-filled?) | |
(cond | |
; quit if there are no more remaining board rows | |
[(cons? rows) | |
(define row (first rows)) | |
(draw-columns dc row 0 offset start-filled?) | |
(define other-rows (rest rows)) | |
(draw-rows dc other-rows (+ offset 1) (not start-filled?))])) | |
; Draws the given row tiles starting at the given horizontal offset index | |
(define (draw-columns dc cols offset vertical-offset filled?) | |
(cond | |
[(cons? cols) | |
(define col (first cols)) | |
; Draw this column tile | |
(define top (* vertical-offset TILE_SIZE)) | |
(define left (* offset TILE_SIZE)) | |
(if filled? | |
(send dc set-brush FILLED_TILE_COLOR 'solid) | |
(send dc set-brush UNFILLED_TILE_COLOR 'solid)) | |
(send dc set-pen "black" 0 'transparent) | |
(send dc draw-rectangle left top TILE_SIZE TILE_SIZE) | |
(cond | |
[(equal? col PIECE_X) | |
(draw-piece-x dc offset vertical-offset)] | |
[(equal? col PIECE_O) | |
(draw-piece-o dc offset vertical-offset)]) | |
(define other-cols (rest cols)) | |
(draw-columns dc other-cols (+ offset 1) vertical-offset (not filled?))])) | |
; Draws the borders and separators for each individual board on the grid | |
(define (draw-board-borders dc) | |
(send dc set-pen "black" 2 'solid) | |
(define (draw-border dc counter) | |
(define line-offset (- (* BOARD_SIZE counter) 0)) | |
; Draw horizontal line | |
(send dc draw-line 0 line-offset GRID_SIZE line-offset) | |
; Draw vertical line | |
(send dc draw-line line-offset 0 line-offset GRID_SIZE) | |
(cond | |
[(>= counter 0) (draw-border dc (- counter 1))])) | |
(draw-border dc BOARDS)) | |
; Draws the X piece | |
(define (draw-piece-x dc hori-offset vert-offset) | |
(let* ([padding (* TILE_SIZE PIECE_PADDING)] | |
[top (+ (* vert-offset TILE_SIZE) padding)] | |
[left (+ (* hori-offset TILE_SIZE) padding)] | |
[bottom (- (* (+ vert-offset 1) TILE_SIZE) padding)] | |
[right (- (* (+ hori-offset 1) TILE_SIZE) padding)]) | |
(send dc set-pen PIECE_COLOR PIECE_LINE_THICKNESS 'solid) | |
(send dc set-brush "black" 'transparent) | |
(send dc draw-line left top right bottom) | |
(send dc draw-line right top left bottom))) | |
; Draws the O piece | |
(define (draw-piece-o dc hori-offset vert-offset) | |
(let* ([padding (* TILE_SIZE PIECE_PADDING)] | |
[top (+ (* vert-offset TILE_SIZE) padding)] | |
[left (+ (* hori-offset TILE_SIZE) padding)] | |
[radius (- (/ TILE_SIZE 2) padding)] | |
[size (* radius 2)]) | |
(send dc set-pen PIECE_COLOR PIECE_LINE_THICKNESS 'solid) | |
(send dc set-brush "black" 'transparent) | |
(send dc draw-rounded-rectangle left top size size radius))) | |
; Some Test Data | |
(define test-board | |
(list | |
(list PIECE_X PIECE_O PIECE_BLANK PIECE_O PIECE_X PIECE_BLANK PIECE_BLANK PIECE_O PIECE_BLANK) | |
(list PIECE_BLANK PIECE_X PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_X PIECE_O PIECE_X PIECE_X) | |
(list PIECE_X PIECE_X PIECE_BLANK PIECE_X PIECE_O PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_BLANK) | |
(list PIECE_BLANK PIECE_BLANK PIECE_O PIECE_O PIECE_X PIECE_X PIECE_O PIECE_BLANK PIECE_X) | |
(list PIECE_X PIECE_X PIECE_X PIECE_O PIECE_O PIECE_O PIECE_BLANK PIECE_O PIECE_BLANK) | |
(list PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_X PIECE_O PIECE_BLANK PIECE_X) | |
(list PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_BLANK PIECE_O PIECE_BLANK PIECE_O PIECE_BLANK PIECE_O) | |
(list PIECE_BLANK PIECE_O PIECE_BLANK PIECE_BLANK PIECE_X PIECE_BLANK PIECE_O PIECE_X PIECE_BLANK) | |
(list PIECE_BLANK PIECE_O PIECE_X PIECE_BLANK PIECE_BLANK PIECE_X PIECE_BLANK PIECE_BLANK PIECE_O))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment