|
@@ -1,162 +1,171 @@
|
|
|
import { useContext, useEffect, useState } from 'react'
|
|
|
import './Checkerboard.css'
|
|
|
-import { POINT } from '../Constant';
|
|
|
+import { POINT, WIN } from '../Constant';
|
|
|
import tableData from './CheckerboardData'
|
|
|
import Navigation from '../component/Navigation'
|
|
|
import RevRoomContext from '../provider/RevRoomContext';
|
|
|
import GlobalSocketContext from '../provider/GlobalSocketContext';
|
|
|
+import Dialog from '../component/Dialog';
|
|
|
const bgline = [
|
|
|
["x", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"],
|
|
|
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"]
|
|
|
];
|
|
|
-const constant = {a:1, b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10, k:11, l:12, m:13, n:14}
|
|
|
+const constantRev = {
|
|
|
+ 0: "x",
|
|
|
+ 1: "a",
|
|
|
+ 2: "b",
|
|
|
+ 3: "c",
|
|
|
+ 4: "d",
|
|
|
+ 5: "e",
|
|
|
+ 6: "f",
|
|
|
+ 7: "g",
|
|
|
+ 8: "h",
|
|
|
+ 9: "i",
|
|
|
+ 10: "j",
|
|
|
+ 11: "k",
|
|
|
+ 12: "l",
|
|
|
+ 13: "m",
|
|
|
+ 14: "n"
|
|
|
+}
|
|
|
const Checkerboard = function () {
|
|
|
+ const [dialogData, setDialog] = useState({
|
|
|
+ title: "游戏提示",
|
|
|
+ content: "xx获得游戏胜利!!",
|
|
|
+ show: false
|
|
|
+ })
|
|
|
const { room, data } = useContext(RevRoomContext)
|
|
|
const socket = useContext(GlobalSocketContext)
|
|
|
const [table, updateTable] = useState(tableData)
|
|
|
- const [userStatus, setGameStatus] = useState(0)
|
|
|
- useEffect(()=>{
|
|
|
+ useEffect(() => {
|
|
|
updateTable(data)
|
|
|
}, [data])
|
|
|
const goBangTouchEvent = function (x, y, value) {
|
|
|
- if ( room.status === 0 ) {
|
|
|
- console.log( "棋盘未开始呢!!" )
|
|
|
+
|
|
|
+ //判断对方是否赢了
|
|
|
+ if ( room.winUsername !== "" ) {
|
|
|
+ dialogMsg( `${room.winUsername} 赢得了此轮竞技` )
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (room.status === 0) {
|
|
|
+ dialogMsg( "此轮对弈还未开始呢!" )
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
if (value === 0) {
|
|
|
data[x][y] = room.status
|
|
|
+ updateTable(data)
|
|
|
socket.send(JSON.stringify({
|
|
|
- instruct: POINT,
|
|
|
- data:data
|
|
|
+ instruct: POINT,
|
|
|
+ data: data
|
|
|
}))
|
|
|
- if ( userStatus !== 0 ) {
|
|
|
- win(x, y, table)
|
|
|
+ if (room.status !== 0) {
|
|
|
+ whoAreWin(data)
|
|
|
}
|
|
|
} else {
|
|
|
console.log("此处已经落子了")
|
|
|
}
|
|
|
}
|
|
|
- const win = function(x, y, table){
|
|
|
- //横向
|
|
|
- if( getXValue(x, y, table) ) {
|
|
|
- console.log( userStatus === 1 ? "黑方赢了" : "白方赢了" )
|
|
|
- setGameStatus(0)
|
|
|
- }
|
|
|
- //纵向
|
|
|
- if( getYValue(x, y, table) ) {
|
|
|
- console.log( userStatus === 1 ? "黑方赢了" : "白方赢了" )
|
|
|
- setGameStatus(0)
|
|
|
- }
|
|
|
- //斜向
|
|
|
- if ( getXyValue(x, y, table) ) {
|
|
|
- console.log( userStatus === 1 ? "黑方赢了" : "白方赢了" )
|
|
|
- setGameStatus(0)
|
|
|
- }
|
|
|
- }
|
|
|
- const getXValue = function(x, y, table){
|
|
|
- //向左遍历
|
|
|
- let n = 0
|
|
|
- for (let i = y; i < table[x].length; i++) {
|
|
|
- if (table[x][i] === userStatus) {
|
|
|
- n++;
|
|
|
- } else if (table[x][i] === 0){
|
|
|
- break
|
|
|
- } else {
|
|
|
- n=0
|
|
|
+ const whoAreWin = (table) => {
|
|
|
+ let blackWin = false
|
|
|
+ let whiteWin = false
|
|
|
+ //撇向
|
|
|
+ for (let x = 5; x < 14; x++) {
|
|
|
+ for (let y = 0; y <= 9; y++) {
|
|
|
+ let white = 0
|
|
|
+ let black = 0
|
|
|
+ let step = 0
|
|
|
+ while (step < 5) {
|
|
|
+ //console.log(`坐标: ${x - step} ${y + step}`)
|
|
|
+ if (table[constantRev[x - step]][y + step] !== 0) {
|
|
|
+ if (table[constantRev[x - step]][y + step] === 1) black++
|
|
|
+ if (table[constantRev[x - step]][y + step] === 2) white++
|
|
|
+ }
|
|
|
+ step++
|
|
|
+ }
|
|
|
+ if (black >= 5) blackWin = true
|
|
|
+ if (white >= 5) whiteWin = true
|
|
|
}
|
|
|
}
|
|
|
- if ( n >= 5 ) return true
|
|
|
- //向右遍历
|
|
|
- n = 0
|
|
|
- for (let i = y; i > 0; i--) {
|
|
|
- if (table[x][i] === userStatus) {
|
|
|
- n++;
|
|
|
- } else if (table[x][i] === 0){
|
|
|
- break
|
|
|
- } else {
|
|
|
- n=0
|
|
|
+ //捺向
|
|
|
+ for (let x = 10; x > 0; x--) {
|
|
|
+ for (let y = 0; y <= 9; y++) {
|
|
|
+ let white = 0
|
|
|
+ let black = 0
|
|
|
+ let step = 0
|
|
|
+ while (step < 5) {
|
|
|
+ //console.log( `坐标: ${x + step} ${y + step}` )
|
|
|
+ if (table[constantRev[x + step]][y + step] !== 0) {
|
|
|
+ if (table[constantRev[x + step]][y + step] === 1) black++
|
|
|
+ if (table[constantRev[x + step]][y + step] === 2) white++
|
|
|
+ }
|
|
|
+ step++
|
|
|
+ }
|
|
|
+ if (black >= 5) blackWin = true
|
|
|
+ if (white >= 5) whiteWin = true
|
|
|
}
|
|
|
}
|
|
|
- if ( n >= 5 ) return true
|
|
|
- return false
|
|
|
- }
|
|
|
- const getYValue = function(x, y, table){
|
|
|
- //从点击点向上遍历
|
|
|
- let n = 0
|
|
|
- const rTable = Object.keys(table).reverse();
|
|
|
- for (const key in rTable) {
|
|
|
- if ( constant[rTable[key]] <= constant[x] ) {
|
|
|
- if ( table[rTable[key]][y] === userStatus ) {
|
|
|
- n++
|
|
|
- } else if ( table[rTable[key]][y] === 0 ) {
|
|
|
- break
|
|
|
- } else {
|
|
|
- n=0
|
|
|
+ //纵向
|
|
|
+ for (let x = 1; x <= 14; x++) {
|
|
|
+ for (let y = 0; y <= 9; y++) {
|
|
|
+ let step = 0
|
|
|
+ let white = 0
|
|
|
+ let black = 0
|
|
|
+ while (step < 5) {
|
|
|
+ //console.log( `坐标: ${x} ${y + step}` )
|
|
|
+ if (table[constantRev[x]][y + step] !== 0) {
|
|
|
+ if (table[constantRev[x]][y + step] === 1) black++
|
|
|
+ if (table[constantRev[x]][y + step] === 2) white++
|
|
|
+ }
|
|
|
+ step++
|
|
|
}
|
|
|
+ if (black >= 5) blackWin = true
|
|
|
+ if (white >= 5) whiteWin = true
|
|
|
}
|
|
|
}
|
|
|
- if ( n >= 5 ) return true
|
|
|
- //从点击点向下遍历
|
|
|
- n=0
|
|
|
- for (const key in table) {
|
|
|
- if (Object.hasOwnProperty.call(table, key)) {
|
|
|
- if ( constant[key] >= constant[x] ) {
|
|
|
- if ( table[key][y] === userStatus ) {
|
|
|
- n++
|
|
|
- } else if ( table[key][y] === 0 ) {
|
|
|
- break
|
|
|
- } else {
|
|
|
- n=0
|
|
|
+ //横向
|
|
|
+ for (let y = 0; y <= 13; y++) {
|
|
|
+ for (let x = 1; x <= 10; x++) {
|
|
|
+ let step = 0
|
|
|
+ let white = 0
|
|
|
+ let black = 0
|
|
|
+ while (step < 5) {
|
|
|
+ //console.log( `坐标: ${y} ${x+step}` )
|
|
|
+ if (table[constantRev[x + step]][y] !== 0) {
|
|
|
+ if (table[constantRev[x + step]][y] === 1) black++
|
|
|
+ if (table[constantRev[x + step]][y] === 2) white++
|
|
|
}
|
|
|
+ step++
|
|
|
}
|
|
|
+ if (black >= 5) blackWin = true
|
|
|
+ if (white >= 5) whiteWin = true
|
|
|
}
|
|
|
}
|
|
|
- if ( n >= 5 ) return true
|
|
|
- return false
|
|
|
- }
|
|
|
- const getXyValue = function(x, y, table){
|
|
|
- //向左上角遍历
|
|
|
- let n = 0 , flag = false, k = y+1
|
|
|
- const tableKey = Object.keys(table).reverse();
|
|
|
- for (let i = 0; i < 14; i++) {
|
|
|
- if ( tableKey[i] === x ) {
|
|
|
- flag = true
|
|
|
- }
|
|
|
- if ( flag === false ) continue
|
|
|
- k--
|
|
|
- if ( table[tableKey[i]][k] === userStatus ) {
|
|
|
- n++
|
|
|
- } else if (table[tableKey[i]][k] === 0) {
|
|
|
- break;
|
|
|
- } else {
|
|
|
- n=0
|
|
|
+ if (blackWin === true || whiteWin === true) {
|
|
|
+ const who = blackWin === true ? room.backUsername : room.whiteUsername
|
|
|
+ if ( who ) {
|
|
|
+ socket.send(JSON.stringify({
|
|
|
+ instruct: WIN,
|
|
|
+ winUser: who
|
|
|
+ }))
|
|
|
}
|
|
|
+ dialogMsg( `${who} 赢得了此轮竞技!` )
|
|
|
}
|
|
|
- if ( n >= 5 ) return true
|
|
|
-
|
|
|
- //向左下角遍历
|
|
|
- n = 0 ; k = y-1; flag = false;
|
|
|
- const rTableKey = Object.keys(table)
|
|
|
- for (let i = 0; i < table[x].length; i++ ) {
|
|
|
- if ( rTableKey[i] === x ) {
|
|
|
- flag = true
|
|
|
- }
|
|
|
- if ( flag === false ) continue
|
|
|
- k++
|
|
|
- if ( table[rTableKey[i]][k] === userStatus ) {
|
|
|
- n++
|
|
|
- } else if (table[rTableKey[i]][k] === 0) {
|
|
|
- break;
|
|
|
- } else {
|
|
|
- n=0
|
|
|
+ }
|
|
|
+ const dialogMsg = (message) => {
|
|
|
+ setDialog({
|
|
|
+ ...dialogData,
|
|
|
+ content: message,
|
|
|
+ show: true,
|
|
|
+ confirm: () => {
|
|
|
+ setDialog({ ...dialogData, show: false })
|
|
|
}
|
|
|
- }
|
|
|
- if ( n >= 5 ) return true
|
|
|
- return false
|
|
|
+ })
|
|
|
}
|
|
|
return (
|
|
|
<div className='body'>
|
|
|
- <Navigation updateTable={updateTable} gameStatus={setGameStatus} status={userStatus} />
|
|
|
+ <Dialog {...dialogData} />
|
|
|
+ <Navigation />
|
|
|
<div className='center'>
|
|
|
<div className='checkerboard'>
|
|
|
<div className='board' >
|