-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (120 loc) · 3.83 KB
/
script.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
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function start_game(){
max_move_size = parseInt(document.getElementById("moveInput").value)
total_sticks = parseInt(document.getElementById("sticksInput").value)
if(max_move_size < 1 || total_sticks < 1){
return
}
$('#moveInput')[0].disabled = true
$('#sticksInput')[0].disabled = true
$('#startGameButton')[0].style.visibility = "hidden"
show_game()
add_row("Human", "?")
}
function hide_game(){
$('#moveTable')[0].style.visibility = "hidden"
$('#moveButton')[0].style.visibility = "hidden"
$('#playerInput')[0].style.visibility = "hidden"
$('#inputLabel3')[0].style.visibility = "hidden"
}
function show_game(){
$('#moveTable')[0].style.visibility = "visible"
$('#moveButton')[0].style.visibility = "visible"
$('#playerInput')[0].style.visibility = "visible"
$('#inputLabel3')[0].style.visibility = "visible"
}
function add_row(player, move){
human_color = "LightGray"
computer_color = "WhiteSmoke"
var newRow = $("<tr>");
var cols = "";
cols += '<th scope="row">' + move_counter + '</th>';
cols += '<td>' + player + '</td>'
cols += '<td>' + total_sticks + '</td>'
cols += '<td>' + move + '</td>'
newRow.append(cols);
$("#moveTable").prepend(newRow)
row_human = true
if(player == "Computer"){
row_human = false
}
table_rows = $("#moveTable")[0].rows
for (i = 1; i < table_rows.length; i++){
if(row_human){
table_rows[i].style.backgroundColor = human_color
}
else{
table_rows[i].style.backgroundColor = computer_color
}
row_human = !row_human
}
}
function calculate_optimal_move(){
ideal_step = (total_sticks-1)%(max_move_size+1)
if(ideal_step == 0){
ideal_step = Math.floor(Math.random()*max_move_size + 1)
}
return Math.min(ideal_step, total_sticks)
}
function check_for_win(){
if(total_sticks <= 0){
game_over = true
if(player_turn){
message = "You lost, better luck next time"
$('#alert_placeholder').html('<div class="alert alert-danger"><span>'+message+'</span></div>')
}
else{
message = "Well done, you won!"
$('#alert_placeholder').html('<div class="alert alert-success"><span>'+message+'</span></div>')
}
}
}
// take turn button
function take_human_turn() {
if(!game_over){
player_move = parseInt(document.getElementById("playerInput").value)
// validate that option in turn is good, else print error message
if(player_move > max_move_size || player_move < 1 || player_move > total_sticks){
return
}
$('#moveButton')[0].disabled = true
// subtract the user input from number of sticks
total_sticks -= player_move
$("#moveTable")[0].rows[1].cells[3].innerHTML = player_move
console.log("You removed " + player_move + " sticks, current total = " + total_sticks)
move_counter += 1
}
check_for_win()
if (!game_over) {
player_turn = false;
setTimeout(take_computer_turn, 500)
}
}
function take_computer_turn(){
if(!game_over){
computer_move = calculate_optimal_move()
add_row("Computer", computer_move)
total_sticks -= computer_move
console.log("I removed " + computer_move + " sticks, current total = " + total_sticks)
move_counter += 1
}
check_for_win()
player_turn=true;
if(!game_over){
setTimeout(add_human_prompt, 500)
}
}
function add_human_prompt(){
if(!game_over){
add_row("Human", "?")
$('#moveButton')[0].disabled = false
}
}
var player_turn = true;
var max_move_size = -1;
var total_sticks = -1;
var move_counter = 1;
var game_over = false
hide_game()