摘要:預(yù)覽效果源碼核心代碼記錄是否走過所有贏法數(shù)量贏法數(shù)組我方贏法的統(tǒng)計數(shù)組計算機(jī)贏法的統(tǒng)計數(shù)組初始化填充數(shù)組是否走過贏法數(shù)組橫豎斜反斜贏法的統(tǒng)計數(shù)組繪制水印繪制棋盤落子實(shí)現(xiàn)我方落子統(tǒng)計贏法你贏了計算機(jī)落子最大分?jǐn)?shù)所在坐標(biāo)遍歷棋盤遍
預(yù)覽效果
github源碼
核心代碼
export default { data () { return { chess: {}, context: {}, chessBoard: [], // 記錄是否走過 me: true, count: 0, // 所有贏法數(shù)量 wins: [], // 贏法數(shù)組 myWin: [], // 我方贏法的統(tǒng)計數(shù)組 computerWin: [], // 計算機(jī)贏法的統(tǒng)計數(shù)組 over: false, } }, mounted () { setTimeout(_ => { this.init() }) }, methods: { // 初始化 init () { this.chess = this.$refs.canvas this.context = this.chess.getContext("2d") this.drawImage( _ => { this.drawChessBoard() }) this.fillArray() }, // 填充數(shù)組 fillArray () { // 是否走過 for (let i = 0; i < 15; i++) { this.chessBoard[i] = [] for (let j = 0; j < 15; j++) { this.chessBoard[i][j] = 0 } } // 贏法數(shù)組 for (let i = 0; i < 15; i++) { this.wins[i] = [] for (let j = 0; j < 15; j++) { this.wins[i][j] = [] } } // 橫 for (let i = 0; i < 15; i++) { for (let j = 0; j < 11; j++) { for (let k = 0; k < 5; k++) { this.wins[i][j+k][this.count] = true } this.count++ } } // 豎 for (let i = 0; i < 15; i++) { for (let j = 0; j < 11; j++) { for (let k = 0; k < 5; k++) { this.wins[j+k][i][this.count] = true } this.count++ } } // 斜 for (let i = 0; i < 11; i++) { for (let j = 0; j < 11; j++) { for (let k = 0; k < 5; k++) { this.wins[i+k][j+k][this.count] = true } this.count++ } } // 反斜 for (let i = 0; i < 11; i++) { for (let j = 14; j > 3; j--) { for (let k = 0; k < 5; k++) { this.wins[i+k][j-k][this.count] = true } this.count++ } } // 贏法的統(tǒng)計數(shù)組 for (let i = 0; i < this.count; i++) { this.myWin[i] = 0 this.computerWin[i] = 0 } }, // 繪制水印 drawImage (callback) { const { context } = this const img = new Image() img.src = "/images/logo.svg" img.onload = _ => { context.drawImage(img, (450 - img.width)/2, (450 - img.height)/2, img.width, img.height) callback() } }, // 繪制棋盤 drawChessBoard () { const { context } = this context.strokeStyle = "#bfbfbf" for (let i = 0; i < 15; i++) { context.moveTo(15 + i * 30, 15) context.lineTo(15 + i * 30, 435) context.stroke() context.moveTo(15, 15 + i * 30) context.lineTo(435, 15 + i * 30) context.stroke() } }, // 落子實(shí)現(xiàn) onStep (x, y, me) { const { context } = this context.beginPath() context.arc(15 + x * 30, 15 + y * 30, 13, 0, 2 * Math.PI) context.closePath() const gradient = context.createRadialGradient(15 + x * 30 + 2, 15 + y * 30 - 2, 13, 15 + x * 30 + 2, 15 + y * 30 - 2, 0) if (me) { gradient.addColorStop(0, "#0a0a0a") gradient.addColorStop(1, "#636766") } else { gradient.addColorStop(0, "#d1d1d1") gradient.addColorStop(1, "#f9f9f9") } context.fillStyle = gradient context.fill() }, // 我方落子 chessClick (e) { if (this.over) { return } if (!this.me) { return } const ox = e.offsetX const oy = e.offsetY const x = Math.floor(ox/30) const y = Math.floor(oy/30) if (this.chessBoard[x][y] === 0) { this.chessBoard[x][y] = 1 this.onStep(x, y, this.me) // 統(tǒng)計贏法 for (let k = 0; k < this.count; k++) { if (this.wins[x][y][k]) { this.myWin[k]++ this.computerWin[k] = 6 if (this.myWin[k] === 5) { alert("你贏了") this.over = true } } } if (!this.over) { this.me = !this.me this.computerAI() } } }, // 計算機(jī)落子 computerAI () { const myScore = [] const computerScore = [] let max = 0 // 最大分?jǐn)?shù) let u = 0, v = 0 // 所在坐標(biāo) for (let i = 0; i < 15; i++) { myScore[i] = [] computerScore[i] = [] for (let j = 0; j < 15; j++) { myScore[i][j] = 0 computerScore[i][j] = 0 } } // 遍歷棋盤 for (let i = 0; i < 15; i++) { for (let j = 0; j < 15; j++) { if (this.chessBoard[i][j] === 0) { // 遍歷所有贏法 for (let k = 0; k < this.count; k++) { if (this.wins[i][j][k]) { // 我方 if (this.myWin[k] === 1) { myScore[i][j] += 200 } else if (this.myWin[k] === 2) { myScore[i][j] += 400 } else if (this.myWin[k] === 3) { myScore[i][j] += 2000 } else if (this.myWin[k] === 4) { myScore[i][j] += 10000 } // 計算機(jī) if (this.computerWin[k] === 1) { computerScore[i][j] += 220 } else if (this.computerWin[k] === 2) { computerScore[i][j] += 420 } else if (this.computerWin[k] === 3) { computerScore[i][j] += 2100 } else if (this.computerWin[k] === 4) { computerScore[i][j] += 20000 } } } // 比較分?jǐn)?shù) if (myScore[i][j] > max) { max = myScore[i][j] u = i v = j } else if (myScore[i][j] === max) { if (computerScore[i][j] > computerScore[u][v]) { u = i v = j } } if (computerScore[i][j] > max) { max = computerScore[i][j] u = i v = j } else if (computerScore[i][j] === max) { if (myScore[i][j] > myScore[u][v]) { u = i v = j } } } } } this.onStep(u, v, false) this.chessBoard[u][v] = 2 // 統(tǒng)計贏法 for (let k = 0; k < this.count; k++) { if (this.wins[u][v][k]) { this.computerWin[k]++ this.myWin[k] = 6 if (this.computerWin[k] === 5) { alert("計算機(jī)贏了") this.over = true } } } if (!this.over) { this.me = !this.me } } } }
原文地址:https://unnue.com/article/50
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/99973.html
摘要:預(yù)覽效果源碼核心代碼記錄是否走過所有贏法數(shù)量贏法數(shù)組我方贏法的統(tǒng)計數(shù)組計算機(jī)贏法的統(tǒng)計數(shù)組初始化填充數(shù)組是否走過贏法數(shù)組橫豎斜反斜贏法的統(tǒng)計數(shù)組繪制水印繪制棋盤落子實(shí)現(xiàn)我方落子統(tǒng)計贏法你贏了計算機(jī)落子最大分?jǐn)?shù)所在坐標(biāo)遍歷棋盤遍 預(yù)覽效果 github源碼 showImg(https://segmentfault.com/img/bVbk02l?w=450&h=450); 核心代碼 ...
摘要:五子棋人機(jī)大戰(zhàn)創(chuàng)建實(shí)例是否結(jié)束我電腦所有棋子已經(jīng)落下的棋子贏法總數(shù)所有贏法統(tǒng)計我的贏法統(tǒng)計電腦贏法統(tǒng)計初始化初始化生成棋盤棋盤初始化鼠標(biāo)移動聚焦功能實(shí)現(xiàn)算法初始化落子功能實(shí)現(xiàn)生成棋盤初始化生成不是的倍數(shù)棋盤列初始化棋盤棋盤初始化畫棋盤畫 JS+canvas五子棋人機(jī)大戰(zhàn) 1. 創(chuàng)建實(shí)例 function Gobang () { this.over = false; // 是否結(jié)...
摘要:由于二維數(shù)組中代表黑棋,代表白棋,,的數(shù)字集合即可代表棋盤上的棋子情況,可以對整個棋盤數(shù)組進(jìn)行遍歷,得到每一個可以下的位置的八個方向的棋子情況。 java基本入門之后,迎來第一個挑戰(zhàn)——五子棋設(shè)計 寒假的時候,靠著看java手冊,實(shí)現(xiàn)了雙人對戰(zhàn)并判斷輸贏的功能。但是一直卡在了人機(jī)對戰(zhàn)上面。 之后隨著學(xué)習(xí)的深入,終于實(shí)現(xiàn)。 以下詳細(xì)的敘述一下整體的設(shè)計過程: 首先是五子棋窗口界面的設(shè)計,...
閱讀 2289·2021-11-24 09:38
閱讀 1986·2021-11-22 14:44
閱讀 1150·2021-07-29 13:48
閱讀 2615·2019-08-29 13:20
閱讀 1115·2019-08-29 11:08
閱讀 2046·2019-08-26 10:58
閱讀 1264·2019-08-26 10:55
閱讀 3149·2019-08-26 10:39