-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.js
156 lines (144 loc) · 4.59 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//Horse no. and their original position
var horseProp = [
{
no: 1,
OriginTop: 68,
//left: ,
},
{
no: 2,
OriginTop: 72,
//left: ,
},
{
no: 3,
OriginTop: 76,
//left: ,
},
{
no: 4,
OriginTop: 80,
//left: ,
},
];
var bethorse, amount, funds; //Global variables
//Trigger the following when "start" button is clicked
document.getElementById('start').onclick = function () {
//Get the values from the DOM
bethorse = document.getElementById('bethorse').value;
amount = parseInt(document.getElementById('amount').value);
laps = parseInt(document.getElementById('laps').value);
funds = parseInt(document.getElementById('funds').innerText);
//Basic input validation
if (amount > funds) {
alert("You do not have enough funds.");
return 0;
} else if (laps < 1) {
alert("Lap must be greater than 0.");
return 0;
} else if (amount < 1) {
alert("Bet amount must be greater than 0.");
return 0;
}
hideResult();
var horses = document.getElementsByClassName("horse");
//Running Animation and actual movement for every horse
var horseNo = 0; //Starting from horse #0
for (const horse of horses) {
//Following two class functions are for animation
horse.classList.add("runRight");
horse.classList.remove("standRight");
horseProp[horseNo].left = 20;
horseProp[horseNo].laps = laps;
moveRight(horse, horseNo);
horseNo++;
}
};
function moveRight(horse, horseNo) {
setTimeout(() => {
horseProp[horseNo].left ++;
horse.style.left = horseProp[horseNo].left + "vw";
if (horseProp[horseNo].laps > 0) {
if (horseProp[horseNo].left < 72.5 + horseNo * 2.5) {
moveRight(horse, horseNo);
} else {
horseProp[horseNo].top = 68 + (horseNo+1)*4;
horse.classList.remove("runRight");
horse.classList.add("runUp");
moveUp(horse, horseNo);
}
} else {
if (horseProp[horseNo].left < 30) {
moveRight(horse, horseNo);
} else {
arrival(horse, horseNo);
}
}
}, 1000/(Math.random() * 20 + 12));
}
function moveUp(horse, horseNo) {
setTimeout(() => {
horseProp[horseNo].top --;
horse.style.top = horseProp[horseNo].top + "vh";
if (horseProp[horseNo].top > 4 + horseNo * 4) {
moveUp(horse, horseNo);
} else {
horse.classList.remove("runUp");
horse.classList.add("runLeft");
moveLeft(horse, horseNo);
}
}, 1000/(Math.random() * 20 + 12));
}
function moveLeft(horse, horseNo) {
setTimeout(() => {
horseProp[horseNo].left--;
horse.style.left = horseProp[horseNo].left + "vw";
if (horseProp[horseNo].left > 3 + horseNo * 2.5) {
moveLeft(horse, horseNo);
} else {
horse.classList.remove("runLeft");
horse.classList.add("runDown");
moveDown(horse, horseNo);
}
}, 1000/(Math.random() * 20 + 12));
}
function moveDown(horse, horseNo) {
setTimeout(() => {
horseProp[horseNo].top++;
horse.style.top = horseProp[horseNo].top + "vh";
if (horseProp[horseNo].top < 68 + horseNo * 4 ) {
moveDown(horse, horseNo);
} else {
horseProp[horseNo].laps--;
horse.classList.remove("runDown");
horse.classList.add("runRight");
moveRight(horse, horseNo);
}
}, 1000/(Math.random() * 20 + 12));
}
//Disable 'Start Race' button and hide results during the actual race
function hideResult() {
document.getElementById("start").disabled = true;
//
var resultIcons = document.querySelectorAll("tr td:nth-child(2)");
for (var i = 0; i < resultIcons.length; i++) {
resultIcons[i].className = "results";
}
}
function arrival(horse, horseNo) {
horse.classList.remove("runRight");
horse.classList.add("standRight");
horseProp[horseNo].top = undefined;
document.getElementById("start").disabled = false;
var resultIcons = document.getElementsByClassName("results");
horseNo++; //Arrays start with index 0, but the class names start with index 1
resultIcons[0].className = "horse"+horseNo;
if (resultIcons.length == 3){
if(bethorse == "horse" + horseNo) {
funds+=amount;
} else {
funds-=amount;
}
document.getElementById("funds").innerText = funds;
}
}