Tic Tac Toe小游戏

目前实现的功能:

  • 初始化3×3的子组件网格
  • 绑定网格id
  • 点击网格,向父组件传递id值
  • 父组件接收到id,会对其进行计数,初始为0接受到一次id,计数加一(第一次接受id,count =1)
  • 判断逻辑,如果count为奇数,且clickstate为1,修改其player值为1为偶数就修改为2,clicktate值置0
  • 判断逻辑,如果点击过该网格(clickstate == 0),此次点击次数作废,player数值不变(以为是div不是button的,要是button就更好办了,直接设置disabled就好了)
  • 每次点击网格,记录玩家1,2点击过的地方(具体是通过遍历父组件中的player属性,将其存放到数组中,因为之前存放的player属性的值都是string类型的,所以后面出了一个小错误,我还以为是逻辑设计错误,查了半天错,(´;ω;`))
  • 最后就是判断胜利的玩家了,提前在父组件中定义好一个lines数组(存放胜利条件,以id为准,如id为123那么就算成功),将上一步的数组与lines数组进行比较,如果玩家路线数组中包含了胜利条件数组,那么游戏就结束,弹框打印胜利玩家,游戏终止,此时点击网格的任何格子都没有反应。
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1,
.box2,
.box3 {
background: #fff;
margin: 0 auto;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
float: left;
border-left: 1px solid black;
border-bottom: 1px solid black;
}

.body {
width: 303px;
height: 100px;
margin: 0 auto;
display: block;
border-right: 1px solid black;
border-top: 1px solid black;

}
</style>
</head>

<body>
<div id="app">
<block1 @listen-child-id='getChildId'></block1>
<block2 @listen-child-id='getChildId'></block2>
<block3 @listen-child-id='getChildId'></block3>

</div>
<template id="block1">
<div class="body">
<div class="box1" v-for="item in total_stats1" @click="sendId(item)">{{item.player}}</div>
</div>
</template>
<template id="block2">
<div class="body">
<div class="box2" v-for="item in total_stats2" @click="sendId(item)">{{item.player}}</div>
</div>
</template>
<template id="block3">
<div class="body">
<div class="box3" v-for="item in total_stats3" @click="sendId(item)">{{item.player}}</div>
</div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const block1 = {
template: '#block1',
data() {
return {
total_stats1: [
{ id: '1', click_state: '2', player: '' },
{ id: '2', click_state: '3', player: '' },
{ id: '3', click_state: '4', player: '' },

],
clickCount: 0,
}
},
methods: {
changeState(item) {
console.log(item.id);
},
sendId(item) {
this.$emit("listen-child-id", item.id, item);

}
},
props: ['cplayer'],
}
const block2 = {
template: '#block2',
data() {
return {
total_stats2: [

{ id: '4', click_state: '5', player: '' },
{ id: '5', click_state: '6', player: '' },
{ id: '6', click_state: '7', player: '' },

],
clickCount: 0,
}
},
methods: {
changeState(item) {
console.log(item.id);
},
sendId(item) {
// 把div换成按钮,点击按钮之后,disabled属性给他设置成禁用就可以了
this.$emit("listen-child-id", item.id, item);

}
},
props: ['cplayer'],
}
const block3 = {
template: '#block3',
data() {
return {
total_stats3: [
{ id: '7', click_state: '8', player: '' },
{ id: '8', click_state: '9', player: '' },
{ id: '9', click_state: '10', player: '' },
],
clickCount: 0,
}
},
methods: {
changeState(item) {
console.log(item.id);
},
sendId(item) {
this.$emit("listen-child-id", item.id, item);
}
},
props: ['cplayer'],
}
const app = new Vue({
el: '#app',
data: {
// 记录点击次数,奇数为玩家一,偶数为玩家二
clickCount: 0,
// 记录子组件的状态
childstats: [
{ id: '1', click_state: '1', player: '1' },
{ id: '2', click_state: '1', player: '' },
{ id: '3', click_state: '1', player: '' },
{ id: '4', click_state: '1', player: '' },
{ id: '5', click_state: '1', player: '' },
{ id: '6', click_state: '1', player: '' },
{ id: '7', click_state: '1', player: '' },
{ id: '8', click_state: '1', player: '' },
{ id: '9', click_state: '1', player: '' },
],
newarr: [],
newarr2: [],
lines: [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7],
],
game_state: 1,
},
components: {
block1,
block2,
block3,
},
methods: {
// 存放id,用于判断玩家都走了哪几步
storageId() {
// this.newarr = this.childstats.filter((item, index) => {
// return item.player === "1";
// })
// console.log(this.newarr);
// this.newarr2 = this.childstats.filter((item, index) => {
// return item.player === "2";
// })
for (let item of this.childstats) {
// console.log(item.id);
if (item.player === "1") {
this.newarr.splice(-1, 0, parseInt(item.id));

}
if (item.player === "2") {
this.newarr2.splice(-1, 0, parseInt(item.id));

}
}
// if (this.newarr instanceof Array) {
// alert("这个是个数组")
// } else {
// alert('这个是个对象')
// }
console.log(this.newarr);
console.log(this.newarr2);
},
// 检查一个数组中是否包含另一个数组
checkId(arr1, arr2) {
if (arr2) {
for (var i = arr2.length - 1; i >= 0; i--) {
if (!arr1.includes(arr2[i])) {
return false;
}
}
}
return true;

},
findWinner() {
if (this.lines.length) {
for (let i = 0; i < this.lines.length; i++) {
if (this.checkId(this.newarr, this.lines[i])) {
alert('玩家1胜');
this.game_state = 0;
} else if (this.checkId(this.newarr2, this.lines[i])) {
alert('玩家2胜');
this.game_state = 0;
}
}
}
},

// 获取从子组件传过来的id
getChildId(id, item1) {
console.log(item1);
if (this.game_state) {
console.log('这个是id');
console.log(id);
// console.log(typeof id);
this.clickCount++;
// console.log(this.clickCount);
// 奇数为玩家一,偶数为玩家二
for (let item of this.childstats) {
// console.log(item.player);
if (item.id == id) {
// console.log('查询成功');
// 如果查询到已经点击过了,此次点击次数作废
if (item.click_state == '0') {
this.clickCount--;
}
// 奇数为玩家一
if (this.clickCount % 2 == 1 && item.click_state == '1') {
item.player = '1';
item1.player = 'O';
// 将点击状态置0,表示已经点击过了
item.click_state = '0';
// console.log('赋值成功');
// 偶数为玩家二
} else if (this.clickCount % 2 == 0 && item.click_state == '1') {
item.player = '2';
item.click_state = '0';
item1.player = 'X';
}

}
}
// for (let item of this.childstats) {
// console.log(item.player);
// }
this.storageId();
// if (this.checkId(this.newarr, this.arr1)) {
// alert('1');
// }
this.findWinner()
}
},


}
})
</script>
</body>

</html>