I will take you step by step to develop a backgammon game using Vue!
This article will help you use Vue basic grammar to write a backgammon game. I hope it will be helpful to everyone!
In the previous article, I wrote a backgammon game using the basic syntax of JS. Today I will write one using the basic syntax of Vue to feel the difference between the two. . As for how to judge the outcome, I copied and pasted the method from the previous article. If you want to understand this logic, you can read my previous article. (Learning video sharing: vuejs video tutorial)
1. Draw the game area and game elements
Before you start writing code, you must remember to package the Vue file. The chessboard is still rendered using a two-dimensional array. You can use the Array(15).fill(0).map(()=>Array(15).fill(0))
method to quickly generate an array.
//创建Vue实例 let vm = new Vue({ //挂载到对应的盒子上 el: '#app', data: { //快速生成用来渲染棋盘的数组,15*15,默认值是0 list: Array(15).fill(0).map(()=>Array(15).fill(0)) }, })
After the array is generated, you can use the v-for method to render Html. The first-level loop generates tr tags, and the second-level loop generates td tags. Then pass the two parameters index and index02 into the click event function, and use vue style binding to bind the black and white styles in td.
<div id="app"> <table> <!-- 渲染tr --> <tr v-for="(item,index) in list"> <!-- 渲染td,绑定点击事件,并把参数传递到事件中 --> <td v-for="(item02,index02) in item" @click="doClick(index,index02)" :class="{'bgc1':item02==1,'bgc2':item02==2}"></td> </tr> </table> <!-- 悔棋按钮 --> <button @click="withdraw">悔棋</button> </div>
Attach CSS style
<style> * { margin: 0; padding: 0; box-sizing: border-box; list-style: none; } table { position: relative; width: 730px; height: 730px; margin: 0 auto; border: 5px solid black; background: url(./src=http___pic45.nipic.com_20140804_2372131_155038114014_2.jpg&refer=http___pic45.nipic.webp) no-repeat; background-size: 100%; background-position: center; padding: 24px 12px; } td { width: 35px; height: 35px; border-radius: 50%; margin-right: 13px; margin-bottom: 11px; cursor: pointer; } .bgc1 { background-color: black; } .bgc2 { background-color: white; } button { position: absolute; width: 200px; height: 100px; bottom: 100px; right: 200px; text-align: center; line-height: 100px; font-size: 25px; } </style>
2. Click event
First use the flag variable to determine the order of Othello. The default values in the array are all 0. The principle of clicking to play chess is to change this value. In the td style binding above, if the value changes to 1, the bgc1 style is rendered, which is black. A value of 2 renders white. In this event, after the value of the array changes, the page will not be re-rendered, so you need to use the this.$set()
method to force v-for to update. Because we are using Vue syntax, this event function needs to be written into methods.
//所有黑棋数组 let blackArr = [] //所有白棋数组 let whiteArr = [] //下棋顺序变量 let flag = true //创建Vue实例 let vm = new Vue({ //挂载到对应的盒子上 el: '#app', data: { //用来渲染棋盘的数组,15*15 list: Array(15).fill(0).map(()=>Array(15).fill(0)) }, methods: { //点击事件,参数a,b对应td里的index,index02 doClick(a, b) { //判断是黑棋还是白棋 if (flag) { //判断格子内是否已经有棋子 if (this.list[a][b] == 0) { //改变点击的td对应的数组元素的值,并且强制更新数组渲染页面 this.$set(this.list[a], b, 1) flag = !flag //将对应的棋子坐标添加至总数组中,后面判断胜负需要用 blackArr.push([a, b]) } } else { this.$set(this.list[a], b, 2) flag = !flag whiteArr.push([a, b]) } }, }, })
3. Regret chess function
The principle of regret chess is to change the value of the last chess piece to 0. How to know which chess piece is the last one? Didn't two global arrays be declared above? The last element in the array is the last chess piece. After the value changes to 0, this element must be deleted from the global array, because this array is not only used when regretting the move, but also used to determine the outcome later. If it is not deleted, it will interfere with the determination of outcome. The function of the regret move event must also be written in methods.
//悔棋事件 withdraw() { //判断前面一步下的是黑棋还是白棋 if (!flag) { //获取最后一颗棋子的位置 const a = blackArr[blackArr.length - 1][0] const b = blackArr[blackArr.length - 1][1] //将最后一刻棋子对应的数组元素的值改为0,并且强制更新数组渲染页面 this.$set(this.list[a], b, 0) //把这个棋子从总数组里面删除,否则会影响到输赢判断 blackArr.splice(blackArr.length - 1, 1) flag = !flag } else { const a = whiteArr[whiteArr.length - 1][0] const b = whiteArr[whiteArr.length - 1][1] this.$set(this.list[a], b, 0) whiteArr.splice(whiteArr.length - 1, 1) flag = !flag } }
4. Judging the outcome
I have already written about the logic of determining the outcome in the previous article, so I won’t go into details here. If you are interested, you can read the previous article on how to write backgammon with native JS. Just take the method here and call it in the click event. Remember to pass the parameters in
//横轴获胜逻辑 function XWin(a, b) { //当前X轴的所有棋子集合数组 let xAllArr = [] //判断横轴胜负逻辑的X轴棋子数组 let xWinArr = [] //判断下的是黑棋还是白棋 if (!flag) { blackArr.map(item => { if (item[0] == a) { //将当前排的所有棋子加入对应数组 xAllArr.push(item[1]) } }) } else { whiteArr.map(item => { if (item[0] == a) { xAllArr.push(item[1]) } }) } //把横排总数组排序,方便比较 xAllArr.sort((a, b) => a - b) for (let i = 1; i < xAllArr.length; i++) { if (xAllArr[i] == (+xAllArr[i - 1] + 1)) { //如果相邻的两个棋子数量相差1,就将其添加至胜负逻辑数组 xWinArr.push(xAllArr[i]) } else { //否则得清空 xWinArr = [] } } //获胜条件 if (xWinArr.length == 4) { //这里要用定时器将弹框变成异步任务,否则第五颗棋子渲染不出来就提示获胜了 if (!flag) { setTimeout(function () { alert('黑棋获胜!') location.reload() }, 100) } else { setTimeout(function () { alert('白棋获胜!') location.reload() }, 100) } } } //竖轴获胜逻辑 function YWin(a, b) { let yAllArr = [] let yWinArr = [] if (!flag) { blackArr.map(item => { if (item[1] == b) { yAllArr.push(item[0]) } }) } else { whiteArr.map(item => { if (item[1] == b) { yAllArr.push(item[0]) } }) } yAllArr.sort((a, b) => a - b) for (let i = 1; i < yAllArr.length; i++) { if (yAllArr[i] == (+yAllArr[i - 1] + 1)) { yWinArr.push(yAllArr[i]) } else { yWinArr = [] } } if (yWinArr.length == 4) { if (!flag) { setTimeout(function () { alert('黑棋获胜!') location.reload() }, 100) } else { setTimeout(function () { alert('白棋获胜!') location.reload() }, 100) } } } //正斜轴获胜逻辑 function X_YWin(a, b) { let x_yAllArr = [] let x_yWinArr = [] if (!flag) { blackArr.map(item => { if ((item[0] - a) == (item[1] - b)) { x_yAllArr.push(item[1]) } }) } else { whiteArr.map(item => { if ((item[0] - a) == (item[1] - b)) { x_yAllArr.push(item[1]) } }) } x_yAllArr.sort((a, b) => a - b) for (let i = 1; i < x_yAllArr.length; i++) { if (x_yAllArr[i] == (+x_yAllArr[i - 1] + 1)) { x_yWinArr.push(x_yAllArr[i]) } else { x_yWinArr = [] } } if (x_yWinArr.length == 4) { if (!flag) { setTimeout(function () { alert('黑棋获胜!') location.reload() }, 100) } else { setTimeout(function () { alert('白棋获胜!') location.reload() }, 100) } } } //反斜轴获胜逻辑 function Y_XWin(a, b) { let y_xAllArr = [] let y_xWinArr = [] if (!flag) { blackArr.map(item => { if (0 - (item[0] - a) == (item[1] - b)) { y_xAllArr.push(item[1]) } }) } else { whiteArr.map(item => { if (0 - (item[0] - a) == (item[1] - b)) { y_xAllArr.push(item[1]) } }) } y_xAllArr.sort((a, b) => a - b) for (let i = 1; i < y_xAllArr.length; i++) { if (y_xAllArr[i] == (+y_xAllArr[i - 1] + 1)) { y_xWinArr.push(y_xAllArr[i]) } else { y_xWinArr = [] } } if (y_xWinArr.length == 4) { if (!flag) { setTimeout(function () { alert('黑棋获胜!') location.reload() }, 100) } else { setTimeout(function () { alert('白棋获胜!') location.reload() }, 100) } } }
Write it at the end
The function of backgammon has been written here. Vue’s basic syntax is much more convenient than the native DOM syntax. Regarding the logic of judging the outcome, I still copied the previous article. If you are interested, you can read my previous article:
"How to use native JS to quickly write a backgammon game"
https://juejin.cn/post/7107172938062757925?share_token=83f7dc0c-3038-4310-b9b5-ba8a7c914af4
【Related video tutorial recommendations :webfrontend】
The above is the detailed content of I will take you step by step to develop a backgammon game using Vue!. For more information, please follow other related articles on the PHP Chinese website!

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

Vue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

There are the following methods to implement component jump in Vue: use router-link and <router-view> components to perform hyperlink jump, and specify the :to attribute as the target path. Use the <router-view> component directly to display the currently routed rendered components. Use the router.push() and router.replace() methods for programmatic navigation. The former saves history and the latter replaces the current route without leaving records.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

There are two main ways to pass data in Vue: props: one-way data binding, passing data from the parent component to the child component. Events: Pass data between components using events and custom events.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use