-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
229 lines (199 loc) · 7 KB
/
index.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prolog XO</title>
<!-- Bootstrap 4.5 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<!-- Vue.js -->
<script src="https://unpkg.com/vue@next"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="body">
<div id="header">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Prolog XO</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">The Game<span class="sr-only">(current)</span></a>
</li>
</ul>
</div>
</nav>
</div>
<div id="content" class="container">
<!-- Game info text -->
<div id="game_info" class="bodyBlock">
<h3>Prolog XO</h3>
<p>
This is a classic tic-tac-toe game where the logic is implemented in Prolog (swiprolog specifically)<br>
Choose your game options and play!<br>
</p>
</div>
<!-- Games options -->
<div id="gameOptions" class="bodyBlock">
<form class="form-inline">
<div class="form-group">
<div class="btn-group" role="group">
<button type="button" class="btn btn-primary" :disabled="playerSymbol === 'X'" @click="setSymbol('X')">X</button>
<button type="button" class="btn btn-primary":disabled="playerSymbol === 'O'" @click="setSymbol('O')">O</button>
</div>
</div>
<div class="form-group">
<label for="difficultySlider">Difficulty Level (minimax depth)</label>
<input type="range" class="form-control-range" min="1" v-bind:max="maxDifficultyLevel" v-model="difficultyLevel" id="difficultySlider">
</div>
<div class="form-group">
<label for="boardSizeInput" class="sr-only">Board Size</label>
<input type="text" class="form-control" id="boardSizeInput" placeholder="Board size" v-model="boardSize" @input="resizeBoard" />
</div>
<button type="button" class="btn btn-warning" @click="resizeBoard">Reset</button>
</form>
</div>
<!-- Tic-tac-toe board -->
<div id="theGame" class="bodyBlock">
<table id="gameBoard">
<tr v-for="(row, rowIndex) in board" class="">
<td v-for="(cell, cellIndex) in row" class="boardCell" @click="makeMove(rowIndex, cellIndex)">
{{ cell }}
</td>
</tr>
</table>
</div>
</div>
<!-- Info Modal -->
<div class="modal fade" id="infoModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ infoModalText }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" @click="resizeBoard">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<!-- Frontend Logic -->
<script>
const App = {
data() {
return {
board: null,
boardSize: 3,
playerSymbol: "X",
difficultyLevel: 0,
maxDifficultyLevel: 9,
infoModalText: null,
}
},
mounted() {
this.difficultyLevel = this.boardSize * this.boardSize;
this.resizeBoard();
},
methods: {
createBoard(size) {
var newBoard = []
for (var i = 0; i < size; i++) {
newBoard[i] = [];
for (var j = 0; j < size; j++) {
newBoard[i][j] = "";
}
}
return newBoard;
},
resizeBoard() {
this.board = this.createBoard(this.boardSize);
this.isWinner = null;
},
postJsonData() {
return {
board: this.board,
difficultyLevel: this.difficultyLevel,
symbol: this.playerSymbol
}
},
makeMove(rowIndex, cellIndex) {
if (this.board[rowIndex][cellIndex] !== "") {
alert("Invalid move!");
} else {
this.board[rowIndex][cellIndex] = this.playerSymbol;
axios.post("/api/make_move", this.postJsonData()).then(response => {
this.board = response.data.board;
this.getIsWinner();
}).catch(() => {
this.showInfoModal("It's too damm hard! please lower difficulty");
});
}
},
getIsWinner() {
axios.post("/api/is_winner", this.postJsonData()).then(response => {
isWinner = response.data.result;
if (isWinner === true) {
this.showInfoModal("You win!");
} else if (isWinner === false) {
this.showInfoModal("You lose!");
} else if (this.checkIsDraw()) {
this.showInfoModal("Game reached a draw");
}
});
},
checkIsDraw() {
for (var i = 0; i < this.board.length; i++) {
for (var j = 0; j < this.board[i].length; j++) {
if (this.board[i][j] === "") {
return false;
}
}
}
return true;
},
setSymbol(symbol) {
this.playerSymbol = symbol;
this.resizeBoard();
},
showInfoModal(text) {
this.infoModalText = text;
$("#infoModal").modal("show");
},
}
}
const app = Vue.createApp(App).mount("#body")
</script>
<!-- Frontend Styling -->
<style>
.bodyBlock {
margin-top: 25px;
float: none;
}
.boardCell {
height: 100px;
width: 100px;
text-align: center;
vertical-align: middle;
font-size: 65px;
border: 1px solid black;
cursor: pointer
}
.form-group {
padding: 0 10px;
}
#boardSizeInput {
width: 65px;
text-align: center;
}
</style>