SCRIPT оформление проверка на существование объекта if document all do

  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
<SCRIPT>
//оформление, проверка на существование объекта
if (document.all||document.getElementById){
document.write('<style>.tictac{')
document.write('width:150px;height:150px;')
document.write('font: 80pt sans-serif;text-align: center;')
document.write('}</style>')
}
//Переменные для обозначения Х/О
var sqr1
var sqr2
var sqr3
var sqr4
var sqr5
var sqr6
var sqr7
var sqr8
var sqr9
//Переменные наличия
var sqr1T = 0
var sqr2T = 0
var sqr3T = 0
var sqr4T = 0
var sqr5T = 0
var sqr6T = 0
var sqr7T = 0
var sqr8T = 0
var sqr9T = 0
var moveCount = 0
var turn = 0
var mode = 1
function vari()
{
sqr1 = document.tic.sqr1.value
sqr2 = document.tic.sqr2.value
sqr3 = document.tic.sqr3.value
sqr4 = document.tic.sqr4.value
sqr5 = document.tic.sqr5.value
sqr6 = document.tic.sqr6.value
sqr7 = document.tic.sqr7.value
sqr8 = document.tic.sqr8.value
sqr9 = document.tic.sqr9.value
}
function check() //Проверка на победу Х
{
if(sqr1 == " X " && sqr2 == " X " && sqr3 == " X ") { alert("You Win!"); reset() }
else if(sqr4 == " X " && sqr5 == " X " && sqr6 == " X ") { alert("You Win!"); reset() }
else if(sqr7 == " X " && sqr8 == " X " && sqr9 == " X ") { alert("You Win!"); reset() }
else if(sqr1 == " X " && sqr5 == " X " && sqr9 == " X ") { alert("You Win!"); reset() }
else if(sqr1 == " X " && sqr4 == " X " && sqr7 == " X ") { alert("You Win!"); reset() }
else if(sqr2 == " X " && sqr5 == " X " && sqr8 == " X ") { alert("You Win!"); reset() }
else if(sqr3 == " X " && sqr6 == " X " && sqr9 == " X ") { alert("You Win!"); reset() }
else if(sqr1 == " X " && sqr5 == " X " && sqr9 == " X ") { alert("You Win!"); reset() }
else if(sqr3 == " X " && sqr5 == " X " && sqr7 == " X ") { alert("You Win!"); reset() }
else
{
winCheck()
check2()
drawCheck()
}
}
function check2() //Проверка на победу О
{
vari()
drawCheck()
if(sqr1 == " O " && sqr2 == " O " && sqr3 == " O ") { alert("You Lose!"); reset() }
else if(sqr4 == " O " && sqr5 == " O " && sqr6 == " O ") { alert("You Lose!"); reset() }
else if(sqr7 == " O " && sqr8 == " O " && sqr9 == " O ") { alert("You Lose!"); reset() }
else if(sqr1 == " O " && sqr5 == " O " && sqr9 == " O ") { alert("You Lose!"); reset() }
else if(sqr1 == " O " && sqr4 == " O " && sqr7 == " O ") { alert("You Lose!"); reset() }
else if(sqr2 == " O " && sqr5 == " O " && sqr8 == " O ") { alert("You Lose!"); reset() }
else if(sqr3 == " O " && sqr6 == " O " && sqr9 == " O ") { alert("You Lose!"); reset() }
else if(sqr1 == " O " && sqr5 == " O " && sqr9 == " O ") { alert("You Lose!"); reset() }
else if(sqr3 == " O " && sqr5 == " O " && sqr7 == " O ") { alert("You Lose!"); reset() }
}
function drawCheck() //Ничья
{
vari()
moveCount = sqr1T + sqr2T + sqr3T + sqr4T + sqr5T + sqr6T + sqr7T + sqr8T + sqr9T
if(moveCount == 9)
{
reset()
alert("Draw")
}
}
function winCheck()
{
check2()
if(sqr1 == " O " && sqr2 == " O " && sqr3T == 0 && turn == 1) { document.tic.sqr3.value = " O "; sqr3T = 1; turn = 0; }
else if(sqr2 == " O " && sqr3 == " O " && sqr1T == 0 && turn == 1) { document.tic.sqr1.value = " O "; sqr1T = 1; turn = 0; }
else if(sqr4 == " O " && sqr5 == " O " && sqr6T == 0 && turn == 1) { document.tic.sqr6.value = " O "; sqr6T = 1; turn = 0; }
else if(sqr5 == " O " && sqr6 == " O " && sqr4T == 0 && turn == 1) { document.tic.sqr4.value = " O "; sqr4T = 1; turn = 0; }
else if(sqr7 == " O " && sqr8 == " O " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr8 == " O " && sqr9 == " O " && sqr7T == 0 && turn == 1) { document.tic.sqr7.value = " O "; sqr7T = 1; turn = 0; }
else if(sqr1 == " O " && sqr5 == " O " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr5 == " O " && sqr9 == " O " && sqr1T == 0 && turn == 1) { document.tic.sqr1.value = " O "; sqr1T = 1; turn = 0; }
else if(sqr3 == " O " && sqr5 == " O " && sqr7T == 0 && turn == 1) { document.tic.sqr7.value = " O "; sqr7T = 1; turn = 0; }
else if(sqr7 == " O " && sqr5 == " O " && sqr3T == 0 && turn == 1) { document.tic.sqr3.value = " O "; sqr3T = 1; turn = 0; }
else if(sqr1 == " O " && sqr3 == " O " && sqr2T == 0 && turn == 1) { document.tic.sqr2.value = " O "; sqr2T = 1; turn = 0; }
else if(sqr4 == " O " && sqr6 == " O " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else if(sqr7 == " O " && sqr9 == " O " && sqr8T == 0 && turn == 1) { document.tic.sqr8.value = " O "; sqr8T = 1; turn = 0; }
else if(sqr1 == " O " && sqr7 == " O " && sqr4T == 0 && turn == 1) { document.tic.sqr4.value = " O "; sqr4T = 1; turn = 0; }
else if(sqr2 == " O " && sqr8 == " O " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else if(sqr3 == " O " && sqr9 == " O " && sqr6T == 0 && turn == 1) { document.tic.sqr6.value = " O "; sqr6T = 1; turn = 0; }
else if(sqr1 == " O " && sqr5 == " O " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr4 == " O " && sqr7 == " O " && sqr1T == 0 && turn == 1) { document.tic.sqr1.value = " O "; sqr1T = 1; turn = 0; }
else if(sqr5 == " O " && sqr8 == " O " && sqr2T == 0 && turn == 1) { document.tic.sqr2.value = " O "; sqr2T = 1; turn = 0; }
else if(sqr6 == " O " && sqr9 == " O " && sqr3T == 0 && turn == 1) { document.tic.sqr3.value = " O "; sqr3T = 1; turn = 0; }
else if(sqr1 == " O " && sqr4 == " O " && sqr7T == 0 && turn == 1) { document.tic.sqr7.value = " O "; sqr7T = 1; turn = 0; }
else if(sqr2 == " O " && sqr5 == " O " && sqr8T == 0 && turn == 1) { document.tic.sqr8.value = " O "; sqr8T = 1; turn = 0; }
else if(sqr3 == " O " && sqr6 == " O " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr1 == " O " && sqr9 == " O " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else if(sqr3 == " O " && sqr7 == " O " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else
{
computer()
}
check2()
}
function computer()
{
check2()
if(sqr1 == " X " && sqr2 == " X " && sqr3T == 0 && turn == 1) { document.tic.sqr3.value = " O "; sqr3T = 1; turn = 0; }
else if(sqr2 == " X " && sqr3 == " X " && sqr1T == 0 && turn == 1) { document.tic.sqr1.value = " O "; sqr1T = 1; turn = 0; }
else if(sqr4 == " X " && sqr5 == " X " && sqr6T == 0 && turn == 1) { document.tic.sqr6.value = " O "; sqr6T = 1; turn = 0; }
else if(sqr5 == " X " && sqr6 == " X " && sqr4T == 0 && turn == 1) { document.tic.sqr4.value = " O "; sqr4T = 1; turn = 0; }
else if(sqr7 == " X " && sqr8 == " X " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr8 == " X " && sqr9 == " X " && sqr7T == 0 && turn == 1) { document.tic.sqr7.value = " O "; sqr7T = 1; turn = 0; }
else if(sqr1 == " X " && sqr5 == " X " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr5 == " X " && sqr9 == " X " && sqr1T == 0 && turn == 1) { document.tic.sqr1.value = " O "; sqr1T = 1; turn = 0; }
else if(sqr3 == " X " && sqr5 == " X " && sqr7T == 0 && turn == 1) { document.tic.sqr7.value = " O "; sqr7T = 1; turn = 0; }
else if(sqr7 == " X " && sqr5 == " X " && sqr3T == 0 && turn == 1) { document.tic.sqr3.value = " O "; sqr3T = 1; turn = 0; }
else if(sqr1 == " X " && sqr3 == " X " && sqr2T == 0 && turn == 1) { document.tic.sqr2.value = " O "; sqr2T = 1; turn = 0; }
else if(sqr4 == " X " && sqr6 == " X " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else if(sqr7 == " X " && sqr9 == " X " && sqr8T == 0 && turn == 1) { document.tic.sqr8.value = " O "; sqr8T = 1; turn = 0; }
else if(sqr1 == " X " && sqr7 == " X " && sqr4T == 0 && turn == 1) { document.tic.sqr4.value = " O "; sqr4T = 1; turn = 0; }
else if(sqr2 == " X " && sqr8 == " X " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else if(sqr3 == " X " && sqr9 == " X " && sqr6T == 0 && turn == 1) { document.tic.sqr6.value = " O "; sqr6T = 1; turn = 0; }
else if(sqr1 == " X " && sqr5 == " X " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr4 == " X " && sqr7 == " X " && sqr1T == 0 && turn == 1) { document.tic.sqr1.value = " O "; sqr1T = 1; turn = 0; }
else if(sqr5 == " X " && sqr8 == " X " && sqr2T == 0 && turn == 1) { document.tic.sqr2.value = " O "; sqr2T = 1; turn = 0; }
else if(sqr6 == " X " && sqr9 == " X " && sqr3T == 0 && turn == 1) { document.tic.sqr3.value = " O "; sqr3T = 1; turn = 0; }
else if(sqr1 == " X " && sqr4 == " X " && sqr7T == 0 && turn == 1) { document.tic.sqr7.value = " O "; sqr7T = 1; turn = 0; }
else if(sqr2 == " X " && sqr5 == " X " && sqr8T == 0 && turn == 1) { document.tic.sqr8.value = " O "; sqr8T = 1; turn = 0; }
else if(sqr3 == " X " && sqr6 == " X " && sqr9T == 0 && turn == 1) { document.tic.sqr9.value = " O "; sqr9T = 1; turn = 0; }
else if(sqr1 == " X " && sqr9 == " X " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else if(sqr3 == " X " && sqr7 == " X " && sqr5T == 0 && turn == 1) { document.tic.sqr5.value = " O "; sqr5T = 1; turn = 0; }
else
{
AI()
}
check2()
}
function AI()
{
vari()
if(document.tic.sqr5.value == " " && turn == 1) { document.tic.sqr5.value = " O "; turn = 0; sqr5T = 1; }
else if(document.tic.sqr1.value == " " && turn == 1) { document.tic.sqr1.value = " O "; turn = 0; sqr1T = 1; }
else if(document.tic.sqr9.value == " " && turn == 1) { document.tic.sqr9.value = " O "; turn = 0; sqr9T = 1; }
else if(document.tic.sqr6.value == " " && turn == 1) { document.tic.sqr6.value = " O "; turn = 0; sqr6T = 1; }
else if(document.tic.sqr2.value == " " && turn == 1) { document.tic.sqr2.value = " O "; turn = 0; sqr2T = 1; }
else if(document.tic.sqr8.value == " " && turn == 1) { document.tic.sqr8.value = " O "; turn = 0; sqr8T = 1; }
else if(document.tic.sqr3.value == " " && turn == 1) { document.tic.sqr3.value = " O "; turn = 0; sqr3T = 1; }
else if(document.tic.sqr7.value == " " && turn == 1) { document.tic.sqr7.value = " O "; turn = 0; sqr7T = 1; }
else if(document.tic.sqr4.value == " " && turn == 1) { document.tic.sqr4.value = " O "; turn = 0; sqr4T = 1;
}
check2()
}
function reset()
{
document.tic.sqr1.value = " "
document.tic.sqr2.value = " "
document.tic.sqr3.value = " "
document.tic.sqr4.value = " "
document.tic.sqr5.value = " "
document.tic.sqr6.value = " "
document.tic.sqr7.value = " "
document.tic.sqr8.value = " "
document.tic.sqr9.value = " "
sqr1T = 0
sqr2T = 0
sqr3T = 0
sqr4T = 0
sqr5T = 0
sqr6T = 0
sqr7T = 0
sqr8T = 0
sqr9T = 0
vari()
turn = 0
moveCount = 0
}
function resetter()
{
reset()
}
</SCRIPT>
//прорисовка и задание начальных значений
<FORM NAME="tic" method="post">
<INPUT TYPE="button" NAME="sqr1" class="tictac" value=" " onClick="if(document.tic.sqr1.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr1.value = ' X '; sqr1T = 1; turn = 1; vari(); check();} else if(document.tic.sqr1.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr1.value = ' X '; sqr1T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr1.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr1.value = ' O '; sqr1T = 1; turn = 1; vari(); player1Check()} drawCheck()">
<INPUT TYPE="button" NAME="sqr2" class="tictac" value=" " onClick="if(document.tic.sqr2.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr2.value = ' X '; sqr2T = 1; turn = 1; vari(); check();} else if(document.tic.sqr2.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr2.value = ' X '; sqr2T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr2.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr2.value = ' O '; sqr2T = 1; turn = 1; vari(); player1Check()} drawCheck()">
<INPUT TYPE="button" NAME="sqr3" class="tictac" value=" " onClick="if(document.tic.sqr3.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr3.value = ' X '; sqr3T = 1; turn = 1; vari(); check();} else if(document.tic.sqr3.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr3.value = ' X '; sqr3T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr3.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr3.value = ' O '; sqr3T = 1; turn = 1; vari(); player1Check()} drawCheck()"><br />
<INPUT TYPE="button" NAME="sqr4" class="tictac" value=" " onClick="if(document.tic.sqr4.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr4.value = ' X '; sqr4T = 1; turn = 1; vari(); check();} else if(document.tic.sqr4.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr4.value = ' X '; sqr4T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr4.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr4.value = ' O '; sqr4T = 1; turn = 1; vari(); player1Check()} drawCheck()">
<INPUT TYPE="button" NAME="sqr5" class="tictac" value=" " onClick="if(document.tic.sqr5.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr5.value = ' X '; sqr5T = 1; turn = 1; vari(); check();} else if(document.tic.sqr5.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr5.value = ' X '; sqr5T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr5.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr5.value = ' O '; sqr5T = 1; turn = 1; vari(); player1Check()} drawCheck()">
<INPUT TYPE="button" NAME="sqr6" class="tictac" value=" " onClick="if(document.tic.sqr6.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr6.value = ' X '; sqr6T = 1; turn = 1; vari(); check();} else if(document.tic.sqr6.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr6.value = ' X '; sqr6T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr6.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr6.value = ' O '; sqr6T = 1; turn = 1; vari(); player1Check()} drawCheck()"><br />
<INPUT TYPE="button" NAME="sqr7" class="tictac" value=" " onClick="if(document.tic.sqr7.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr7.value = ' X '; sqr7T = 1; turn = 1; vari(); check();} else if(document.tic.sqr7.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr7.value = ' X '; sqr7T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr7.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr7.value = ' O '; sqr7T = 1; turn = 1; vari(); player1Check()} drawCheck()">
<INPUT TYPE="button" NAME="sqr8" class="tictac" value=" " onClick="if(document.tic.sqr8.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr8.value = ' X '; sqr8T = 1; turn = 1; vari(); check();} else if(document.tic.sqr8.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr8.value = ' X '; sqr8T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr8.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr8.value = ' O '; sqr8T = 1; turn = 1; vari(); player1Check()} drawCheck()">
<INPUT TYPE="button" NAME="sqr9" class="tictac" value=" " onClick="if(document.tic.sqr9.value == ' ' && turn == 0 && mode == 1) {document.tic.sqr9.value = ' X '; sqr9T = 1; turn = 1; vari(); check();} else if(document.tic.sqr9.value == ' ' && turn == 1 && mode == 2) {document.tic.sqr9.value = ' X '; sqr9T = 1; turn = 0; vari(); player1Check()} else if(document.tic.sqr9.value == ' ' && turn == 0 && mode == 2) {document.tic.sqr9.value = ' O '; sqr9T = 1; turn = 1; vari(); player1Check()} drawCheck()">
</form>