Permalink
Browse files

refactor interface - create a first 'draw' function

  • Loading branch information...
1 parent 729511d commit 06ca0d024eb1d44dcaff281eb1220e07e9b628e2 @mapleoin committed Apr 2, 2013
Showing with 51 additions and 36 deletions.
  1. +26 −1 resources/public/index.html
  2. +25 −35 src/cljs/goboard.cljs
@@ -1,8 +1,33 @@
<!doctype html>
<html>
<head><title>Javascript Go Board</title></head>
+ <script type="text/javascript" src="goboard.js"></script>
<body>
<canvas id="goBoard"></canvas>
- <script type="text/javascript" src="goboard.js"></script>
+ <script type="text/javascript">
+ var stones = [
+ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
+
+ goboard.goboard.draw("goBoard", stones, 18, 17);
+ </script>
+
</body>
</html>
View
@@ -1,38 +1,28 @@
(ns goboard.goboard
(:use [goboard.goban :only [draw-board draw-last-move]]))
-(def ^:const board {:lines 19
- :size 620
- :offset 20
- :space 28 ; (.floor js/Math (/ size (+ 3 board-lines)))
- :inner 504 ; (* (- board-lines 1) space-width))
- :stone-radius 11 ; space * 2/5
- :canvas (. js/document (getElementById "goBoard"))
- :context (. (. js/document (getElementById "goBoard"))
- (getContext "2d"))
- :background "#E8BD68"
- :markings "#444"
- })
-(def stones [0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
- 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
- 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1])
-
-(draw-board board stones)
-
-(draw-last-move board 18 18)
+(defn draw
+ "Create the board struct and draw it
+ - element-id - the DOM canvas id where the board will be drawn
+ - stones - a 19x19 array with stone positions where each values is either:
+ 0 - empty, 1 - black stone, 2 - white stone
+ Optional positional args:
+ (if these are not given, then the last move will not be represented)
+ - last-x - x coordonate of the last move
+ - last-y - y coordonate of the last move"
+ [element-id stones & [last-x last-y :as last-move]]
+ (def ^:const board {:lines 19
+ :size 620
+ :offset 20
+ :space 28 ; (.floor js/Math (/ size (+ 3 board-lines)))
+ :inner 504 ; (* (- board-lines 1) space-width))
+ :stone-radius 11 ; space * 2/5
+ :canvas (. js/document (getElementById element-id))
+ :context (. (. js/document (getElementById element-id))
+ (getContext "2d"))
+ :background "#E8BD68"
+ :markings "#444"
+ })
+ (draw-board board stones)
+ (if last-move
+ (draw-last-move board last-x last-y)))

0 comments on commit 06ca0d0

Please sign in to comment.