File tree 8 files changed +171
-3
lines changed
8 files changed +171
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { Component } from 'angular2/core' ;
2
+ import { minesweeper } from './minesweeper' ;
2
3
3
4
@Component ( {
4
5
selector : 'my-app' ,
5
- template : '<h1>My First Angular 2 App</h1>'
6
+ template : '<minesweeper></minesweeper>' ,
7
+ directives : [ minesweeper ]
6
8
} )
7
- export class AppComponent { }
9
+ export class AppComponent { }
Original file line number Diff line number Diff line change
1
+ export class cell {
2
+ private bomb : boolean ;
3
+ private clicked : boolean = false ;
4
+ private x : number ;
5
+ private y : number ;
6
+ private field : Array < Array < cell > > ;
7
+
8
+ constructor ( x : number , y : number , field : Array < Array < cell > > ) {
9
+ this . bomb = Math . round ( ( Math . random ( ) * 4 ) ) == 0 ;
10
+ this . x = x ;
11
+ this . y = y ;
12
+ this . field = field ;
13
+
14
+ if ( this . bomb ) {
15
+ console . log ( "is Bomb on " + this . x + " and " + this . y ) ;
16
+ } else {
17
+ console . log ( "is not a Bomb" ) ;
18
+ }
19
+ }
20
+
21
+ public isBomb ( ) : boolean {
22
+ return this . bomb ;
23
+ }
24
+
25
+ public click ( ) {
26
+ this . clicked = true ;
27
+ }
28
+
29
+ public isClicked ( ) {
30
+ return this . clicked ;
31
+ }
32
+
33
+ public getNumNeighborBombs ( ) : number {
34
+ var num = 0 ;
35
+ if ( this . x > 1 && this . field [ this . y ] [ this . x - 1 ] . isBomb ( ) ) {
36
+ num ++ ;
37
+ }
38
+ if ( this . y > 1 && this . field [ this . y - 1 ] [ this . x ] . isBomb ( ) ) {
39
+ num ++ ;
40
+ }
41
+ return num ;
42
+ }
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ < div class ="cell ">
2
+ < div class ="innerCell " [ngClass] ="{bomb:showBomb(), noBomb:noBomb()} " (click) ="click() ">
3
+ < span >
4
+ {{getDisplayValue()}}
5
+ </ span >
6
+ </ div >
7
+ </ div >
Original file line number Diff line number Diff line change
1
+ import { Component , Input } from 'angular2/core' ;
2
+ import { cell } from './cell' ;
3
+
4
+ @Component ( {
5
+ selector : 'cellrenderer' ,
6
+ templateUrl : 'app/cellRenderer.html' ,
7
+ } )
8
+ export class cellrenderer {
9
+ @Input ( ) cellParameter : cell ;
10
+ private clicked : boolean = false ;
11
+
12
+ constructor ( ) {
13
+
14
+ }
15
+
16
+ public isBomb ( ) : boolean {
17
+ return this . cellParameter . isBomb ( ) ;
18
+ }
19
+
20
+ public showBomb ( ) {
21
+ return this . cellParameter . isBomb ( ) && this . isClicked ( ) ;
22
+ }
23
+
24
+ public noBomb ( ) {
25
+ return ! this . cellParameter . isBomb ( ) && this . isClicked ( ) ;
26
+ }
27
+
28
+ public isClicked ( ) {
29
+ return this . cellParameter . isClicked ( ) ;
30
+ }
31
+
32
+
33
+ public click ( ) {
34
+ this . cellParameter . click ( ) ;
35
+ }
36
+
37
+ public getDisplayValue ( ) : string {
38
+ if ( this . isClicked ( ) ) {
39
+ if ( this . isBomb ( ) ) {
40
+ return "B" ;
41
+ } else {
42
+ return this . cellParameter . getNumNeighborBombs ( ) + "" ;
43
+ }
44
+ } else {
45
+ return "-"
46
+ }
47
+ }
48
+
49
+
50
+
51
+
52
+ }
Original file line number Diff line number Diff line change
1
+ Ich bin das Minesweeper feld.
2
+ < br />
3
+ < span *ngFor ="#row of getField(); #r = index ">
4
+ < span *ngFor ="#mycell of row; #c = index " >
5
+ < cellrenderer [cellParameter] ="mycell "> </ cellrenderer >
6
+ </ span >
7
+ < br />
8
+ < br />
9
+ </ span >
Original file line number Diff line number Diff line change
1
+ import { Component } from 'angular2/core' ;
2
+ import { cell } from './cell' ;
3
+ import { cellrenderer } from './cellrenderer' ;
4
+
5
+ @Component ( {
6
+ selector : 'minesweeper' ,
7
+ templateUrl : 'app/minesweeper.html' ,
8
+ directives :[ cellrenderer ]
9
+ } )
10
+ export class minesweeper {
11
+ private field : Array < Array < cell > > ;
12
+
13
+ constructor ( ) {
14
+ this . initField ( ) ;
15
+ }
16
+
17
+ public initField ( ) {
18
+ console . log ( "init field" ) ;
19
+ var length = 10 ;
20
+ this . field = new Array < Array < cell > > ( length ) ;
21
+ for ( var r = 0 ; r < length ; r ++ ) {
22
+ this . field [ r ] = new Array < cell > ( length ) ;
23
+ for ( var c = 0 ; c < 10 ; c ++ ) {
24
+ this . field [ r ] [ c ] = new cell ( c , r , this . field ) ;
25
+ }
26
+ }
27
+ }
28
+
29
+ public getField ( ) : Array < Array < cell > > {
30
+ return this . field ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ .cell {
2
+ padding : 10px ;
3
+ display : inline;
4
+ width : 10px ;
5
+ }
6
+
7
+ .innerCell {
8
+ padding : 10px ;
9
+ border : 1px solid black;
10
+ display : inline;
11
+ }
12
+
13
+
14
+ .bomb {
15
+ background-color : red;
16
+ }
17
+
18
+ .noBomb {
19
+ background-color : green;
20
+ }
Original file line number Diff line number Diff line change 4
4
< title > Angular 2 QuickStart</ title >
5
5
6
6
< script src ="assets/js/jquery-2.2.0.min.js "> </ script >
7
-
7
+
8
8
<!-- 1. Load libraries -->
9
9
<!-- IE required polyfills, in this exact order -->
10
10
< script src ="node_modules/es6-shim/es6-shim.min.js "> </ script >
18
18
19
19
20
20
< link rel ="stylesheet " href ="assets/css/bootstrap.min.css ">
21
+ < link rel ="stylesheet " href ="css/app.css ">
22
+
21
23
<!-- Optional theme -->
22
24
< link rel ="stylesheet " href ="assets/css/bootstrap-theme.min.css " >
23
25
<!-- Latest compiled and minified JavaScript -->
You can’t perform that action at this time.
0 commit comments