Skip to content

Commit 0035468

Browse files
committed
Zwischenstand Minesweeper
1 parent 54c3ebd commit 0035468

File tree

8 files changed

+171
-3
lines changed

8 files changed

+171
-3
lines changed

minesweeper-kep/app/app.component.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {Component} from 'angular2/core';
2+
import {minesweeper} from './minesweeper';
23

34
@Component({
45
selector: 'my-app',
5-
template: '<h1>My First Angular 2 App</h1>'
6+
template: '<minesweeper></minesweeper>',
7+
directives: [minesweeper]
68
})
7-
export class AppComponent { }
9+
export class AppComponent { }

minesweeper-kep/app/cell.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

minesweeper-kep/app/cellRenderer.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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>

minesweeper-kep/app/cellrenderer.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

minesweeper-kep/app/minesweeper.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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>

minesweeper-kep/app/minesweeper.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

minesweeper-kep/css/app.css

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

minesweeper-kep/index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>Angular 2 QuickStart</title>
55

66
<script src="assets/js/jquery-2.2.0.min.js"></script>
7-
7+
88
<!-- 1. Load libraries -->
99
<!-- IE required polyfills, in this exact order -->
1010
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
@@ -18,6 +18,8 @@
1818

1919

2020
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
21+
<link rel="stylesheet" href="css/app.css">
22+
2123
<!-- Optional theme -->
2224
<link rel="stylesheet" href="assets/css/bootstrap-theme.min.css" >
2325
<!-- Latest compiled and minified JavaScript -->

0 commit comments

Comments
 (0)