Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
aa152147
五子棋
提交
24f5dfe5
五
五子棋
项目概览
aa152147
/
五子棋
与 Fork 源项目一致
Fork自
inscode / VueJS
通知
1
Star
43
Fork
23
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
五
五子棋
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
24f5dfe5
编写于
6月 25, 2023
作者:
6
63db3122f0950a2aef64df95
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add: 添加胜利棋数的配置
上级
dde5b676
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
59 addition
and
128 deletion
+59
-128
src/App.vue
src/App.vue
+18
-30
src/assets/main.css
src/assets/main.css
+34
-6
src/utils/index.js
src/utils/index.js
+7
-92
未找到文件。
src/App.vue
浏览文件 @
24f5dfe5
<
script
setup
>
<
script
setup
>
import
{
onMounted
,
re
active
,
re
f
}
from
"
vue
"
import
{
onMounted
,
ref
}
from
"
vue
"
import
{
checkWin
,
findBestMove
}
from
"
./utils
"
import
{
checkWin
}
from
"
./utils
"
// 配置
// 配置
const
con
=
reactive
({
const
size
=
ref
(
20
)
width
:
20
,
const
winSize
=
ref
(
5
)
height
:
20
})
const
lattices
=
ref
([])
const
lattices
=
ref
([])
//
let nextPlay = 'isBlack' // isWhite
let
nextPlay
=
'
isBlack
'
// isWhite
// 5 连珠的下标
// 5 连珠的下标
let
isWin
=
[]
let
isWin
=
[]
onMounted
(
confirm
)
onMounted
(
confirm
)
function
confirm
()
{
function
confirm
()
{
const
{
width
,
height
}
=
con
isWin
=
[]
isWin
=
[]
lattices
.
value
=
Array
(
height
).
fill
([]).
map
(
item
=>
Array
(
width
).
fill
(
null
))
nextPlay
=
'
isBlack
'
;
lattices
.
value
=
Array
(
size
.
value
).
fill
([]).
map
(()
=>
Array
(
size
.
value
).
fill
(
null
))
}
}
// 玩家下棋
// 玩家下棋
function
playChess
(
item
,
r_i
,
c_i
)
{
function
playChess
(
item
,
r_i
,
c_i
)
{
if
(
isWin
.
length
||
item
)
return
if
(
isWin
.
length
||
item
)
return
lattices
.
value
[
r_i
][
c_i
]
=
'
isBlack
'
lattices
.
value
[
r_i
][
c_i
]
=
nextPlay
const
test
=
checkWin
(
r_i
,
c_i
,
lattices
.
value
)
const
test
=
checkWin
(
{
row
:
r_i
,
col
:
c_i
,
board
:
lattices
.
value
,
player
:
nextPlay
,
winSize
:
winSize
.
value
}
)
if
(
test
)
{
if
(
test
)
{
isWin
=
test
isWin
=
test
}
else
{
return
robotMove
()
}
}
}
nextPlay
=
nextPlay
==
'
isWhite
'
?
'
isBlack
'
:
'
isWhite
'
function
robotMove
()
{
const
[
bestRow
,
bestCol
]
=
findBestMove
(
lattices
.
value
,
"
isWhite
"
);
console
.
log
(
bestRow
,
bestCol
)
// lattices.value[bestRow][bestCol] = "isWhite";
/* const testRobot = checkWin(lattices.value)
if (testRobot) {
isWin = testRobot
} */
}
}
...
@@ -50,14 +38,14 @@ function getStyle(item, r_i, c_i) {
...
@@ -50,14 +38,14 @@ function getStyle(item, r_i, c_i) {
</
script
>
</
script
>
<
template
>
<
template
>
<div
class=
"content flex-
between
"
>
<div
class=
"content flex-
start
"
>
<p
class=
"flex-between"
>
<p
class=
"flex-between
mr-6px
"
>
<label>
高
:
</label>
<label>
棋盘大小
:
</label>
<input
v-model=
"
con.height
"
type=
"number"
>
<input
v-model=
"
size
"
type=
"number"
>
</p>
</p>
<p
class=
"flex-between"
>
<p
class=
"flex-between
mr-6px
"
>
<label>
宽
:
</label>
<label>
胜利棋数
:
</label>
<input
v-model=
"
con.width
"
type=
"number"
>
<input
v-model=
"
winSize
"
type=
"number"
>
</p>
</p>
<button
@
click=
"confirm"
>
重新开始
</button>
<button
@
click=
"confirm"
>
重新开始
</button>
</div>
</div>
...
...
src/assets/main.css
浏览文件 @
24f5dfe5
...
@@ -23,6 +23,16 @@
...
@@ -23,6 +23,16 @@
align-items
:
center
;
align-items
:
center
;
}
}
.flex-start
{
display
:
flex
;
justify-content
:
start
;
align-items
:
center
;
}
.mr-6px
{
margin-right
:
6px
;
}
input
{
input
{
width
:
60px
;
width
:
60px
;
}
}
...
@@ -59,6 +69,7 @@ input {
...
@@ -59,6 +69,7 @@ input {
position
:
absolute
;
position
:
absolute
;
top
:
calc
(
50%
-
1px
);
top
:
calc
(
50%
-
1px
);
left
:
0
;
left
:
0
;
font-size
:
20px
;
}
}
.col
::before
{
.col
::before
{
...
@@ -71,17 +82,34 @@ input {
...
@@ -71,17 +82,34 @@ input {
top
:
0
;
top
:
0
;
}
}
.isWhite
::after
,
.isBlack
::after
,
.isWin
::after
{
border-radius
:
50%
;
width
:
90%
;
height
:
90%
;
top
:
calc
((
20px
-
20px
*
0.9
)
*
0.5
);
left
:
calc
((
20px
-
20px
*
0.9
)
*
0.5
);
border
:
1px
solid
#999
;
}
/* 竖线隐藏 */
.isWhite
::before
,
.isBlack
::before
,
.isWin
::before
{
display
:
none
;
}
.isWhite
::after
{
.isWhite
::after
{
content
:
'O'
;
background-color
:
white
;
color
:
brown
;
}
}
.isBlack
::after
{
.isBlack
::after
{
content
:
'O'
;
background-color
:
black
;
color
:
black
;
border
:
1px
solid
black
;
}
}
.isWin
::after
{
.isWin
::after
{
content
:
'O'
;
background-color
:
red
;
color
:
red
;
border
:
1px
solid
red
;
}
}
\ No newline at end of file
src/utils/index.js
浏览文件 @
24f5dfe5
export
function
checkWin
(
row
,
col
,
board
)
{
export
function
checkWin
(
{
row
,
col
,
board
,
player
,
winSize
}
)
{
const
_row
=
board
.
length
;
const
_row
=
board
.
length
;
const
_col
=
board
[
0
].
length
const
_col
=
board
[
0
].
length
const
pieceType
=
board
[
row
][
col
]
const
pieceType
=
player
??
board
[
row
][
col
]
const
directions
=
[
const
directions
=
[
[
1
,
0
],
// 水平方向
[
1
,
0
],
// 水平方向
...
@@ -12,14 +12,14 @@ export function checkWin(row, col, board) {
...
@@ -12,14 +12,14 @@ export function checkWin(row, col, board) {
];
];
for
(
let
i
=
0
;
i
<
directions
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
directions
.
length
;
i
++
)
{
const
res
=
[[
row
,
col
]];
const
[
dx
,
dy
]
=
directions
[
i
];
const
[
dx
,
dy
]
=
directions
[
i
];
let
count
=
1
;
// 当前位置算一个
let
x
=
row
+
dx
;
let
x
=
row
+
dx
;
let
y
=
col
+
dy
;
let
y
=
col
+
dy
;
// 向正反两个方向扩展,检查是否有连续的五个相同棋子
// 向正反两个方向扩展,检查是否有连续的五个相同棋子
while
(
x
>=
0
&&
x
<
_row
&&
y
>=
0
&&
y
<
_col
&&
board
[
x
][
y
]
===
pieceType
)
{
while
(
x
>=
0
&&
x
<
_row
&&
y
>=
0
&&
y
<
_col
&&
board
[
x
][
y
]
===
pieceType
)
{
count
++
;
res
.
push
([
x
,
y
])
x
+=
dx
;
x
+=
dx
;
y
+=
dy
;
y
+=
dy
;
}
}
...
@@ -27,100 +27,15 @@ export function checkWin(row, col, board) {
...
@@ -27,100 +27,15 @@ export function checkWin(row, col, board) {
x
=
row
-
dx
;
x
=
row
-
dx
;
y
=
col
-
dy
;
y
=
col
-
dy
;
while
(
x
>=
0
&&
x
<
_row
&&
y
>=
0
&&
y
<
_col
&&
board
[
x
][
y
]
===
pieceType
)
{
while
(
x
>=
0
&&
x
<
_row
&&
y
>=
0
&&
y
<
_col
&&
board
[
x
][
y
]
===
pieceType
)
{
count
++
;
res
.
push
([
x
,
y
])
x
-=
dx
;
x
-=
dx
;
y
-=
dy
;
y
-=
dy
;
}
}
if
(
count
>=
5
)
{
if
(
res
.
length
>=
winSize
)
{
return
true
;
// 出现五连珠,返回胜利
return
res
;
// 出现五连珠,返回胜利
}
}
}
}
return
false
;
// 未出现五连珠
return
false
;
// 未出现五连珠
}
}
export
const
findBestMove
=
(
boardSize
,
piece
)
=>
{
let
bestScore
=
-
Infinity
;
let
bestRow
=
-
1
;
let
bestCol
=
-
1
;
const
_row
=
boardSize
.
length
const
_col
=
boardSize
[
0
].
length
for
(
let
row
=
0
;
row
<
_row
;
row
++
)
{
for
(
let
col
=
0
;
col
<
_col
;
col
++
)
{
if
(
boardSize
[
row
][
col
]
===
0
)
{
boardSize
[
row
][
col
]
=
piece
;
const
score
=
minimax
(
boardSize
,
0
,
false
,
-
Infinity
,
Infinity
);
boardSize
[
row
][
col
]
=
0
;
if
(
score
>
bestScore
)
{
bestScore
=
score
;
bestRow
=
row
;
bestCol
=
col
;
}
}
}
}
return
[
bestRow
,
bestCol
];
}
// 最大值搜索算法
function
minimax
(
boardSize
,
depth
,
isMaximizingPlayer
,
alpha
,
beta
)
{
const
_row
=
boardSize
.
length
const
_col
=
boardSize
[
0
].
length
if
(
checkWin
(
-
1
,
-
1
,
playerPiece
))
{
return
-
1
;
// 玩家获胜
}
else
if
(
checkWin
(
-
1
,
-
1
,
robotPiece
))
{
return
1
;
// 机器人获胜
}
else
if
(
isGameOver
())
{
return
0
;
// 平局
}
if
(
isMaximizingPlayer
)
{
let
maxScore
=
-
Infinity
;
for
(
let
row
=
0
;
row
<
_row
;
row
++
)
{
for
(
let
col
=
0
;
col
<
_col
;
col
++
)
{
if
(
boardSize
[
row
][
col
]
===
0
)
{
boardSize
[
row
][
col
]
=
robotPiece
;
const
score
=
minimax
(
depth
+
1
,
false
,
alpha
,
beta
);
boardSize
[
row
][
col
]
=
0
;
maxScore
=
Math
.
max
(
maxScore
,
score
);
alpha
=
Math
.
max
(
alpha
,
score
);
if
(
alpha
>=
beta
)
{
break
;
}
}
}
}
return
maxScore
;
}
else
{
let
minScore
=
Infinity
;
for
(
let
row
=
0
;
row
<
_row
;
row
++
)
{
for
(
let
col
=
0
;
col
<
_col
;
col
++
)
{
if
(
boardSize
[
row
][
col
]
===
0
)
{
boardSize
[
row
][
col
]
=
playerPiece
;
const
score
=
minimax
(
depth
+
1
,
true
,
alpha
,
beta
);
boardSize
[
row
][
col
]
=
0
;
minScore
=
Math
.
min
(
minScore
,
score
);
beta
=
Math
.
min
(
beta
,
score
);
if
(
alpha
>=
beta
)
{
break
;
}
}
}
}
return
minScore
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录