Permalink
Browse files

Merge branch 'master' of github.com:trifork/dart-jigsaw

  • Loading branch information...
2 parents c9a3754 + 53dadab commit 1657d5b1e0ce43130f3dba8fecb61c69d57d2843 @krestenkrab krestenkrab committed Nov 10, 2011
View
@@ -2,3 +2,4 @@ dlib
dunit
dwm
out
+.idea
View
@@ -1,10 +1,23 @@
#library('jigsaw.client.dart');
+//Dart libs
#import('../dlib/html/html.dart');
#import('../dlib/base/base.dart');
+//dlibs
+#import('../dlib/events_base.dart', prefix: "e");
+#import('../dlib/pprint.dart', prefix: "pprint");
+#import('../dlib/utils.dart');
+
+//model
+#import('shared/model.dart');
+
#source('client/Jigsaw.dart');
+#source('client/view/JigsawView.dart');
+#source('client/view/PuzzleView.dart');
+#source('client/controller/JigsawController.dart');
+
main() {
Jigsaw.main();
View
@@ -7,30 +7,32 @@
*/
class Jigsaw {
- _handleGo(e) {
- print(e);
- }
+ static final Jigsaw APP = const Jigsaw();
+ static final int JIGSAW_SIZE = 3;
+ const Jigsaw();
+
+ JigsawModel _model;
+ JigsawView _view;
+ JigsawController _controller;
void ready() {
- document.query("#go").on.click.add(_handleGo);
- // document.query("#status").innerHTML = "Slider Menu Sample App";
- // sliderMenu = new SliderMenu(menuItems, (selectedText) {
- // document.query("#message").innerHTML = "Selected '${selectedText}'";
- // });
- // document.query("#menu").nodes.add(sliderMenu.node);
-
- // document.query('#next').on.click.add((e) {
- // sliderMenu.selectNext(true);
- // });
-
- // document.query('#prev').on.click.add((e) {
- // sliderMenu.selectPrevious(true);
- // });
-
- // sliderMenu.enterDocument();
+ //document.query("#go").on.click.add(_handleGo);
+ _model = new JigsawModel(JIGSAW_SIZE, "images/porche.png");
+ _view = new JigsawView(this.model);
+ _controller = new JigsawController(this.model,this.view);
+
+ _controller.init();
+ _view.init();
+ _model.init();
}
+ JigsawModel get model() => _model;
+ JigsawView get view() => _view;
+ JigsawController get ctrl() => _controller;
+
+
static void main() {
- Dom.ready( () { new Jigsaw().ready();} );
+
+ Dom.ready(Jigsaw.APP.ready);
}
}
@@ -0,0 +1,66 @@
+class JigsawController extends e.EventTarget {
+
+ AbstractStrategy currentStrategy;
+ JigsawModel model;
+ JigsawView view;
+ ReceivePort currentPort;
+
+ List _moves;
+
+ JigsawController(this.model, this.view);
+
+ void init() {
+
+ this._moves = [];
+ window.setInterval(() {
+ if (this._moves.length == 0) return;
+ Pos p = this._moves.removeLast();
+ if (p === null) _kill();
+ else {
+ if (!this.model.move(p)) _kill();
+ }
+ }, 1000);
+
+ view.on["move"].add((e) {
+ var pos = e.payload['loc'];
+ model.move(pos);
+ });
+ view.on["shuffle"].add((e)=> model.shuffle());
+
+
+ view.on["back"].add((e) => model.stepback());
+ view.on["reset"].add((e) => model.reset());
+
+ view.on['start'].add((e) {
+ currentStrategy = new RandomAIStrategy();
+ initStrategy();
+ currentStrategy.spawn().then((sp) {
+ var x = serialize(model.board);
+ sp.send(x,currentPort.toSendPort());
+ });
+ });
+ }
+
+ void initStrategy() {
+ _kill();
+ currentPort = new ReceivePort();
+ currentPort.receive(_receive);
+
+ }
+
+
+ _kill() {
+ if (currentPort !== null) {
+ currentPort.close();
+ currentPort = null;
+ }
+
+ }
+
+ _receive(Map msg,_) {
+ Pos p = new Pos(msg['row'], msg['col']);
+ _moves.add(p);
+
+ }
+
+}
@@ -0,0 +1,37 @@
+class JigsawView extends e.EventTarget {
+ JigsawModel model;
+
+ PuzzleView puzzleView;
+ /*TimerView*/ var timerView;
+
+ JigsawView(this.model): super(null, new e.EventTargetEvents()),
+ this.puzzleView = new PuzzleView(this.model, document.query("#jigsaw")),
+ this.timerView = null {
+ this.puzzleView.parent = this;
+ }
+
+ init() {
+ model.on.change.add(_handleModelChange);
+ this.puzzleView.init();
+
+ document.query("#shuffle").
+ on.click.add((e) => this.dispatch(new JEvent("shuffle",null)));
+
+ document.query("#reset").
+ on.click.add((e) => this.dispatch(new JEvent("reset",null)));
+
+
+ document.query("#back").
+ on.click.add((e) => this.dispatch(new JEvent("back",null)));
+
+ document.query("#start").
+ on.click.add((e) => this.dispatch(new JEvent("start",null)));
+ }
+
+ _handleModelChange(e) {
+ var data = e.payload;
+ if (data['old'] !== null) {//this is a move
+ puzzleView.swap(data['old'], data['new']);
+ }
+ }
+}
@@ -0,0 +1,45 @@
+class PuzzleView extends e.EventTarget {
+ JigsawModel model;
+ Element _element;
+ PuzzleView(this.model, this._element):super(null, new e.EventTargetEvents());
+
+ init() {
+ this._element.on.click.add(_clickHandler);
+ }
+
+ _locFilter(s) => s.indexOf("loc") >= 0;
+
+ _clickHandler(e) {
+ Element t = e.target;
+ String loc = first(t.classes.filter(_locFilter));
+
+ this.dispatch(new JEvent("move", {"loc":_class2pos(loc)}));
+ }
+
+ swap(Pos old, Pos n) {
+ String old_loc = "loc${old.row}${old.col}";
+ String new_loc = "loc${n.row}${n.col}";
+ Element old_piece = document.query(".$old_loc");
+ Element new_piece = document.query(".$new_loc");
+
+ old_piece.classes.remove(old_loc);
+ old_piece.classes.add(new_loc);
+
+ new_piece.classes.remove(new_loc);
+ new_piece.classes.add(old_loc);
+
+ }
+
+ Pos _class2pos(String s) {//"loc01"
+ int r = Math.parseInt(s.substring(3,4));
+ int c = Math.parseInt(s.substring(4,5));
+ return new Pos(r,c);
+ }
+
+}
+
+
+
+class JEvent extends e.EventImpl implements e.Event {
+ JEvent(String type, p): super(type,p);
+}
View
@@ -34,52 +34,52 @@
#jigsaw div.clear { float: none; display: block;}
-#jigsaw div.hole { background-color: white; background-image: none;}
+#jigsaw div.hole { background-image: none;}
-.move00 {
+.loc00 {
position: relative;
left: 0px;
top: 0px;
}
-.move01 {
+.loc01 {
position: relative;
left: 150px;
top: 0px;
}
-.move02 {
+.loc02 {
position: relative;
left: 300px;
top: 0px;
}
-.move10 {
+.loc10 {
position: relative;
left: 0px;
top: 150px;
}
-.move11 {
+.loc11 {
position: relative;
left: 150px;
top: 150px;
}
-.move12 {
+.loc12 {
position: relative;
left: 300px;
top: 150px;
}
-.move20 {
+.loc20 {
position: relative;
left: 0px;
top: 300px;
}
-.move21 {
+.loc21 {
position: relative;
left: 150px;
top: 300px;
}
-.move22 {
+.loc22 {
position: relative;
left: 300px;
top: 300px;
View
@@ -7,28 +7,31 @@
</head>
<body>
<h1>Welcome to Jigsaw</h1>
- <div id="menu" >
+ <div id="menu" class="inactive hidden" >
<p>Please enter your username</p>
<input type="text" class="username">
<button id="go">Go!</button>
</div>
- <div id="buttons" class="inactive">
- <button id="shuffle" >Shuffle!</button>
- <button id="start">Start!</button>
+ <div id="buttons" class="active">
+ <button id="start">Start</button>
+ <button id="reset" >Reset</button>
+ <button id="back" >Go back</button>
+ <button id="shuffle" >Shuffle</button>
+
</div>
- <div id="jigsaw" class="inactive hidden">
- <div id="sq00" class="move00 hole"></div>
- <div id="sq01" class="move01"></div>
- <div id="sq02" class="move02"></div>
- <div id="sq10" class="move10"></div>
- <div id="sq11" class="move11"></div>
- <div id="sq12" class="move12"></div>
- <div id="sq20" class="move20"></div>
- <div id="sq21" class="move21"></div>
- <div id="sq22" class="move22"></div>
+ <div id="jigsaw" class="active">
+ <div id="sq00" class="loc00 hole"></div>
+ <div id="sq01" class="loc01"></div>
+ <div id="sq02" class="loc02"></div>
+ <div id="sq10" class="loc10"></div>
+ <div id="sq11" class="loc11"></div>
+ <div id="sq12" class="loc12"></div>
+ <div id="sq20" class="loc20"></div>
+ <div id="sq21" class="loc21"></div>
+ <div id="sq22" class="loc22"></div>
</div>
- <!--<script type="text/javascript" src="out/Jigsaw.dart.app.js"></script>-->
- <script type="text/javascript" src="tmp/jigsaw.js"></script>
+ <script type="text/javascript" src="out/Jigsaw.dart.app.js"></script>
+ <!--<script type="text/javascript" src="tmp/jigsaw.js"></script>-->
</body>
</html>
View
@@ -5,5 +5,12 @@
#import("../../dlib/events_base.dart");
#source("model/Pos.dart");
-#source("model/Jigsaw.dart");
+#source("model/Board.dart");
+#source("model/JigsawModel.dart");
#source("model/JigsawModelEvents.dart");
+
+#source("model/Strategy.dart");
+#source("model/AbstractStrategy.dart");
+#source("model/AIStrategy.dart");
+#source("model/RandomAIStrategy.dart");
+#source("model/Serializer.dart");
@@ -0,0 +1,20 @@
+class AIStrategy extends AbstractStrategy {
+ AIStrategy(String name): super(name);
+
+ void init(){}
+
+ void run(SendPort sp) {
+ while (!this.board.isSolved()) {
+ Pos pos = nextMove(this.board);
+ if (pos !== null) {
+ sp.send({"row":pos.row, "col":pos.col});
+ this.board.move(pos);
+ } else {
+ return;
+ }
+ }
+ }
+
+ abstract Pos nextMove(Board board);
+
+}
@@ -0,0 +1,20 @@
+class AbstractStrategy extends Isolate implements Strategy {
+ final String _name;
+
+ Board board;
+
+ AbstractStrategy(this._name):super();
+
+ String get name() => _name;
+
+ main() {
+ this.port.receive((m, SendPort sp) {
+ this.board = new Board.from(deserialize(m));
+ init();
+ run(sp);
+ });
+ }
+
+ abstract init();
+ abstract run(SendPort p);
+}
Oops, something went wrong.

0 comments on commit 1657d5b

Please sign in to comment.