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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with <div>. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()
