forked from codercatdev/code-challenge-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
119 lines (105 loc) · 2 KB
/
game.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
function Game(player1, player2, numOfRounds)
{
this.player1 = player1;
this.player2 = player2;
this.player1Resources = 0;
this.player2Resources = 0;
this.numOfRounds = numOfRounds;
this.costOfWorkers = 1;
this.costOfSoldiers = 2;
this.addResource = function(amount, playerNum)
{
if (playerNum == 1)
{
this.player1Resources += amount;
}
else
{
this.player2Resources += amount;
}
}
this.createWorkers = function(amount, player)
{
player.addWorker(amount);
}
this.createSoldiers = function(amount, player)
{
player.addSoldier(amount);
}
this.deductResource = function (amount, playerNum)
{
if (playerNum == 1)
{
player1Resources -= amount;
}
else if (playerNum == 2)
{
player2Resources -= amount;
}
}
this.verifyResource = function(amount, playerNum)
{
if (checkPlayer(playerNum) == 1)
{
return this.player1Resources >= amount;
}
else if (checkPlayer(playerNum == 2))
{
return this.player2Resources >= amount;
}
}
this.checkPlayer = function(playerNum)
{
if (playerNum == 1)
{
return 1;
}
else
{
return 2;
}
}
}
Game.prototype.Player1 = function()
{
return this.player1;
}
Game.prototype.Player2 = function()
{
return this.player2;
}
Game.prototype.compareSoldierCount = function()
{
return this.player1.Soldiers > this.player2.Soldiers;
}
Game.prototype.evaluateRound = function()
{
//both attack, no advantage
if (this.player1.attack && this.player2.attack)
{
return this.compareSoldierCount();
}
else
{
//p1 attacks losese advantage
if (this.player1.attack)
{
//could instead return count + 1 and not modifiy internal
this.player2.addSoldier(1);
}
else if (this.player2.attack)
{
this.player1.addSoldier(1);
}
//return this count comparison always
return this.compareSoldierCount();
}
}
Game.prototype.createWorkers = function(amount, player)
{
this.createWorkers(amount, player);
}
Game.prototype.createSoldiers = function(amount, player)
{
this.createSoldiers(amount, player);
}