-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_p5.html
192 lines (176 loc) · 5.47 KB
/
app_p5.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
<!doctype html>
<html class="no-js" lang="pl">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>wersja p5.js</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Umieść favicon.ico w głównym katalogu -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body background="img/tlo.png" class="menu">
<h1>Projekt Kółko i krzyżyk</h1>
<div>
<ul class="menu">
<li><a href="index.html">Strona główna</a></li>
<li><a href="app_jq.html">Wersja jQuery</a></li>
<li><a href="app_p5.html">Wersja p5js</a></li>
</ul>
</div>
<input type="button" value="Restart" onClick="location.reload();" />
<style>
#game {
width: 300px
height: 300px;
border: 1px ;
background: ;
padding: 10px;
float: center;
}
#game div {
clear: both;
}
#game button {
display: block;
float: left;
width: 50px;
height: 50px;
margin: 5px 10px;
}
#gameActionDisplay {
padding: 20px 10px;
}
</style>
<div id="game">
<div>
<button></button>
<button></button>
<button></button>
</div>
<div>
<button></button>
<button></button>
<button></button>
</div>
<div>
<button></button>
<button></button>
<button></button>
</div>
</div>
<div id="gameActionDisplay"></div>
</div>
<script>
var Display = function(displayElement) {
var display = displayElement;
function setText(message) {
display.innerText = message;
}
return {setMessage: setText};
};
function isValid(btn) {
return btn.innerText.length == 0;
}
function checkScore(btn, squares) {
console.log("click");
console.log(btn);
console.log(squares);
}
function setMark(btn, players, currentTurn) {
btn.innerText = players[currentTurn];
}
function checkForWinner(squares, players, currentTurn) {
if (squares[0].innerText == players[currentTurn] &&
squares[1].innerText == players[currentTurn] &&
squares[2].innerText == players[currentTurn])
return true;
if (squares[3].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[5].innerText == players[currentTurn])
return true;
if (squares[6].innerText == players[currentTurn] &&
squares[7].innerText == players[currentTurn] &&
squares[8].innerText == players[currentTurn])
return true;
if (squares[0].innerText == players[currentTurn] &&
squares[3].innerText == players[currentTurn] &&
squares[6].innerText == players[currentTurn])
return true;
if (squares[1].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[7].innerText == players[currentTurn])
return true;
if (squares[2].innerText == players[currentTurn] &&
squares[5].innerText == players[currentTurn] &&
squares[8].innerText == players[currentTurn])
return true;
if (squares[0].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[8].innerText == players[currentTurn])
return true;
if (squares[2].innerText == players[currentTurn] &&
squares[4].innerText == players[currentTurn] &&
squares[6].innerText == players[currentTurn])
return true;
return false;
}
function isFullBoard(squares) {
for (var i = 0, len = squares.length; i < len; i++)
if (squares[i].innerText.length == 0)
return false;
return true;
}
function isDraw(squares) {
return (!checkForWinner(squares, ["X"], 0)) && isFullBoard(squares);
}
function main() {
var squares = document.querySelectorAll("#game button");
var players = ["X", "O"];
var currentTurn = 0;
var isGameOver = false;
var display = new Display(document.querySelector("#gameActionDisplay"));
display.setMessage("Gracz 'X' zaczyna");
for (var i = 0, len = squares.length; i < len; i++)
squares[i].addEventListener("click", function(){
if (isGameOver)
return;
if (!isValid(this)) {
display.setMessage("Błędny ruch");
} else {
setMark(this, players, currentTurn);
isGameOver = checkForWinner(squares, players, currentTurn);
if (isGameOver) {
display.setMessage("Gracz '" + players[currentTurn] + "' wygrywa");
return;
}
if (isDraw(squares)) {
display.setMessage("Remis");
return;
}
currentTurn += 1;
currentTurn %= 2;
display.setMessage("Gracz '" + players[currentTurn] + "' rusza się");
}
});
}
(function(){ main();})();
</script>
<!-- skrypt umożliwiający sprawdzanie możliwośći przeglądarki -->
<script src="js/vendor/modernizr-3.5.0.min.js"></script>
<!--biblioteka jQuery-->
<!--<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>-->
<!--jeżeli nie pobrano jQuery z sieci, spróbuj załadować ją z podfolderu js -->
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.2.1.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<!--biblioteka p5js-->
<script src="js/vendor/p5.min.js"></script>
<script src="js/vendor/p5.dom.min.js"></script>
<script src="js/vendor/p5.sound.min.js"></script>
<!-- ewentualny kod JS ogólny -->
<script src="js/main.js"></script>
<!--kod aplikacji w p5js-->
<script src="js/app_p5.js"></script>
</body>
</html>