井字游戏HTML,HTML5井字棋游戏

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

'use strict';

var state = {

grid: _.map(_.range(0, 9), function(index) {

return {

index: index,

figure: -1

};

}),

myTurn: false

};

var appState = _.cloneDeep(state);

var block = Vue.component('block', {

name: 'block',

template: '#block',

props: {

figure: {

type: Number,

default: -1

}

},

computed: {

fig: function fig() {

return this.figure === 0 ? 'O' : 'X';

}

},

data: function data() {

return {

selected: false

};

},

methods: {

enter: function enter(el, done) {

TweenMax.from(el, 1, {

autoAlpha: 0,

scale: 0,

ease: Elastic.easeOut.config(1.25, 0.5),

onComplete: done

});

}

}

});

var win = Vue.component('win', {

name: 'win',

template: '#win',

props: {

clickHandler: {

type: Function,

default: null

}

}

});

var app = new Vue({

name: 'app',

el: '#app',

data: function data() {

return state;

},

components: {

block: block

},

computed: {

winner: function winner() {

var wins = ['012', '036', '345', '147', '258', '678', '048', '246'];

var grid = this.grid;

var player = this.myTurn ? 0 : 1;

var moves = _.reduce(this.grid, function(result, value, index) {

if (value.figure === player) {

result.push(index);

}

return result;

}, []);

return !!_.find(wins, function(win) {

var combination = _.map(win.split(''), function(n) {

return parseInt(n);

});

console.log('combination', combination, moves);

return _.difference(combination, moves).length === 0;

});

}

},

methods: {

select: function select(index) {

var figure = this.grid[index].figure;

if (figure > -1) {

return;

}

this.grid[index].figure = this.myTurn ? 1 : 0;

this.myTurn = !this.myTurn;

},

restart: function restart() {

this.grid = appState.grid;

this.myTurn = appState.myTurn;

},

enter: function enter(el, done) {

TweenMax.from(el, 1, {

autoAlpha: 0,

scale: 0,

ease: Elastic.easeOut.config(1.25, 0.5)

});

},

enterWin: function enterWin(el) {

TweenMax.from(el, 1, {

autoAlpha: 0,

scale: 0,

ease: Elastic.easeOut.config(1.25, 0.5)

});

}

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值