-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoints_tracker.js
122 lines (101 loc) · 2.93 KB
/
points_tracker.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
var players=[];
function load()
{
initial();
}
function initial()
{
var content = document.getElementById("content");
content.style.backgroundImage = 'url("images/background2.jpg")';
// HEADER - title = Add Players
document.getElementById("title").innerHTML = "ADD PLAYERS";
// SECTION -create 6 input
document.getElementsByTagName("section")[0].innerHTML = "<input id='firstinp'><br><input><br><input><br><input><br><input><br><input><br>";
// FOOTER - Add start game - button (on click call start_game())
document.getElementsByTagName("footer")[0].innerHTML = '<button onclick="start_game()">START</button>';
}
function start_game()
{
var number_of_players = 0;
var input = document.getElementsByTagName("input");
// counted the number of plyers entered
for(i=0; i < input.length; i++)
{
if(input[i].value.trim() != '')
{
players[number_of_players]= [input[i].value.trim(),0];
number_of_players ++;
}
}
if(number_of_players > 1 && number_of_players % 2 == 0)
{
playing_game();
}
else
{
show_alert("Add a new player");
players=[];
}
}
function playing_game()
{
var content = document.getElementById("content");
content.style.background = 'black';
// HEADER - title = Playing....
document.getElementById("title").innerHTML = "Playing...";
// SECTION - build player's table
document.getElementsByTagName("section")[0].innerHTML = create_table();
// FOOTER - add refresh_points - button (on click call update_points())
document.getElementsByTagName("footer")[0].innerHTML = '<button onclick="update_game()">UPDATE</button><button onclick="new_game()">NEW</button>';
}
function create_table()
{
var th = "";
var td = "";
for (var i = 0; i < players.length; i++)
{
th += "<th>"+players[i][0]+"</th>";
td += '<td><input placeholder="Add points"></td>';
}
return "<table><tr>"+th+"</tr>"+td+"</table>";
}
function update_game()
{
var is_valid = true;
for (var i = 0; is_valid && i < document.getElementsByTagName("input").length; i++)
{
var input = parseInt(document.getElementsByTagName("input")[i].value.trim());
is_valid = Number.isInteger(input);
}
if(!is_valid)
{
show_alert("please enter all points.");
}
else
{
var table = document.getElementsByTagName("table")[0];
var row = table.insertRow(-1);
for (var i = 0; i < players.length; i++)
{
var player_points = players[i][1];
var points = document.getElementsByTagName("input")[i].value;
player_points += parseInt(points);
players[i][1] = player_points;
var cel = row.insertCell(i);
cel.innerHTML = player_points;
document.getElementsByTagName("input")[i].value = '';
}
}
}
function show_alert(msg)
{
alert(msg);
}
function new_game()
{
if(confirm("Do you want to start a new game?"))
{
initial();
}
}
document.addEventListener("DOMContentLoaded",load);