-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.js
132 lines (112 loc) · 3.41 KB
/
match.js
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
var gene_size = 20;
var A = 'A'; // Attack
var D = 'D'; // Defense
var R = 'R'; // Reproduction
var geneList = [ A, D, R ]
function genMonster() {
var gene = [];
for(var i = 0; i < gene_size; i++) {
var g = Math.floor(Math.random()*geneList.length);
gene.push(geneList[g]);
}
return makeMonster(gene);
}
function makeMonster(gene) {
var monster = { attack:[], defense:[], reproduction: 0 };
monster.seed = 0.5 + Math.random()*2;
monster.gene = gene;
monster.name = gene.join('');
monster.fightsWon = 0;
var lastAttack = 0;
var lastDefense = 0;
gene.forEach(function(e) {
if (e == A) {
if (lastAttack == 0) {
monster.attack.push(0);
}
lastAttack += 1;
monster.attack[monster.attack.length-1] = lastAttack;
lastDefense = 0;
} else if (e == D) {
if (lastDefense == 0) {
monster.defense.push(0);
}
lastDefense += 1;
monster.defense[monster.defense.length-1] = lastDefense;
lastAttack = 0;
} else if (e == R) {
lastAttack = 0;
lastDefense = 0;
monster.reproduction += 1;
}
});
return monster;
}
function cross(m1, m2) {
var crossPoint = Math.random() * m1.gene.length;
var crossA = [], crossB = [];
for(var i in m1.gene)
{
crossA.push(i < crossPoint ? m1.gene[i] : m2.gene[i]);
crossB.push(i < crossPoint ? m2.gene[i] : m1.gene[i]);
}
crossA = mutate(crossA);
crossB = mutate(crossB);
return [makeMonster(crossA), makeMonster(crossB)];
}
function mutate(genes) {
var pos = Math.floor(Math.random() * genes.length);
var oldGeneIndex = geneList.indexOf(genes[pos]);
var mixFactor = Math.floor(Math.random() * 2) + 1;
var newGeneIndex = (oldGeneIndex + mixFactor) % geneList.length;
genes[pos] = geneList[newGeneIndex];
return genes;
}
function fight(m1, m2) {
var lst;
m1.score = 0;
m2.score = 0;
lst = [ { monster:m1, attack:m1.attack.slice(0), defense:m1.defense.slice(0) },
{ monster:m2, attack:m2.attack.slice(0), defense:m2.defense.slice(0) } ]
//console.log(m1.name);
//console.log(m2.name);
var results = [];
var i = 0;
while(true) {
i++;
//console.log("round "+i);
if (lst[0].attack.length === 0 && lst[0].defense.length === 0 &&
lst[1].attack.length === 0 && lst[1].defense.length === 0) {
break;
}
var a1 = lst[0].attack.shift();
var d1 = lst[1].defense.shift();
if (a1 === undefined) {
a1 = 0;
}
if (d1 === undefined) {
d1 = 0;
}
//console.log(a1+" -> "+d1);
if (a1 > d1) {
lst[0].monster.score += 1;
//console.log(lst[0].name + " attacks and win: +1 point");
results.push({attacker: lst[0].monster.name, defender: lst[1].monster.name,
winner:lst[0].monster.name, attackV: a1, defenseV: d1});
} else if (a1 < d1) {
lst[1].monster.score += 1;
//console.log(lst[0].name + " attacks and loose, +1 point to opponant");
results.push({attacker: lst[0].monster.name, defender: lst[1].monster.name,
winner:lst[1].monster.name, attackV: a1, defenseV: d1});
//console.log(lst[0].gene + " attacks and is esquived, 0");
} else {
//console.log(lst[0].name + " attacks and is esquived, 0");
results.push({attacker: lst[0].monster.name, defender: lst[1].monster.name,
attackV: a1, defenseV: d1});
}
lst = [ lst[1], lst[0] ];
}
//console.log(m1.name + " : "+m1.score);
//console.log(m2.name + " : "+m2.score);
return results;
}