-
Notifications
You must be signed in to change notification settings - Fork 0
/
fillerang.html
140 lines (122 loc) · 4.12 KB
/
fillerang.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Filler Game</title>
<link rel="stylesheet" href="main.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
</head>
<body>
<div ng-app="fillergame">
<div ng-controller="fillerCtrl">
<div id="gamefield" data-ng-init="fillfield()"><table>
<tr data-ng-repeat="points in field">
<td data-ng-repeat="z in points track by $index" style="background: {{colours[z]}}">
</td>
</tr>
</table>
</div>
<div id="gameselect">
<table><tr>
<td ng-repeat="colour in colours" style="background: {{colour}}" ng-click="clicked($index);"></td>
</tr><table>
</div>
</div>
<div id="player1">Your score - {{ player1score }} </div>
<div id="player2">PC score - {{ player2score }} </div>
</div>
</body>
</html>
<script>
(function(angular) {
'use strict';
var app = angular.module('fillergame', []);
var tempfield = [];
var width = 60;
var height = 40;
var movescore = 0;
var unusedcolour = 255;
var winningamount = 0.2;
app.controller('fillerCtrl', ['$scope', function($scope) {
$scope.player1score = 0;
$scope.player2score = 0;
$scope.field = [];
$scope.colours = ["Black", "Blue", "Red", "Green", "Yellow", "Maggenta", "Grey"];
//making copy of field
function makefieldcopy(){
tempfield = [];
for(var i = 0; i < height; i++) {
tempfield[i] = [];
for(var j = 0; j < width; j++){
tempfield[i][j] = $scope.field[i][j];
}
}
};
//full action on player move, including filling the field and points counting
function fullmove (x,y,currentcolour, newcolour, currentfield){
playermove(x,y,currentcolour,newcolour, currentfield);
//next 3 lines are a bit controversial solution, but other one would require making clone of array, and $scope is is pretty long in JS.
playermove(x,y,newcolour,unusedcolour, currentfield);
movescore=0;
playermove(x,y,unusedcolour,newcolour, currentfield);
return movescore;
};
//filling field according player move
function playermove (x,y,currentcolour,newcolour,currentfield){
if (currentfield[x][y]==currentcolour){
currentfield[x][y]=newcolour;
movescore++;
if (x-1>=0){playermove(x-1,y,currentcolour,newcolour, currentfield);}
if (x+1<height){playermove(x+1,y,currentcolour,newcolour, currentfield);}
if (y+1<width){playermove(x,y+1,currentcolour,newcolour, currentfield);}
if (y-1>=0){playermove(x,y-1,currentcolour,newcolour, currentfield);}
}
};
//shoosing and making best move for 2nd player
function aimove() {
var bestmovecolour=0;
var bestscore=0;
for (var currentcolour = 0; currentcolour < $scope.colours.length; currentcolour++){
if ($scope.field[0][width-1] != currentcolour){
makefieldcopy();
$scope.player2score=fullmove(0,width-1,$scope.field[0][width-1],currentcolour,tempfield);
}
if ($scope.player2score>bestscore){
bestmovecolour=currentcolour;
bestscore=$scope.player2score;
}
}
return (fullmove(0,width-1,$scope.field[0][width-1],bestmovecolour,$scope.field));
};
//checking who is winner
function checkwinner(){
var winningscore=width*height*winningamount;
if (winningscore<=$scope.player1score || winningscore<=$scope.player2score){
if ($scope.player1score>=$scope.player2score){
window.alert("Game is ended and winner is Player 1");
}
else{
window.alert("Game is ended and winner is Player 2");
}
window.location.reload(true);
}
};
//creating random field
$scope.fillfield = function() {
for(var i = 0; i < height; i++) {
$scope.field[i] = [];
for(var j = 0; j < width; j++){
$scope.field[i][j]= Math.floor(Math.random()*$scope.colours.length);
}
}
};
//action if colour selected
$scope.clicked = function(x){
if (x != $scope.field[height-1][0]){
$scope.player1score=fullmove(height-1,0,$scope.field[height-1][0],x,$scope.field);
$scope.player2score=aimove();
checkwinner();
}
};
}]);
})(window.angular);
</script>