Tue Jun 27 01:59:00 UTC 2023 inscode

上级 9539c8d2
...@@ -6,7 +6,6 @@ import { robotPlay } from './utils/api' ...@@ -6,7 +6,6 @@ import { robotPlay } from './utils/api'
const size = ref(20) const size = ref(20)
const win_size = ref(5) const win_size = ref(5)
const lattices = ref([]) const lattices = ref([])
let nextPlay = is_black // is_white
// 5 连珠的下标 // 5 连珠的下标
let isWin = [] let isWin = []
...@@ -14,20 +13,19 @@ onMounted(confirm) ...@@ -14,20 +13,19 @@ onMounted(confirm)
function confirm() { function confirm() {
isWin = [] isWin = []
nextPlay = is_black;
lattices.value = Array(size.value).fill([]).map(() => Array(size.value).fill(is_empty)) lattices.value = Array(size.value).fill([]).map(() => Array(size.value).fill(is_empty))
} }
// 玩家下棋 // 玩家下棋
function playChess(item, row, col) { async function playChess(item, row, col) {
if (isWin.length || item !== is_empty) return if (isWin.length || item !== is_empty) return
lattices.value[row][col] = nextPlay lattices.value[row][col] = is_black
const test = checkWin({ row, col, board: lattices.value, player: nextPlay, win_size: win_size.value }) const test = checkWin({ row, col, board: lattices.value, player: is_black, win_size: win_size.value })
if (test) { if (test) {
isWin = test isWin = test
return return
} else { } else {
console.log('------------') const [x, y] = await robotPlay(lattices.value, is_white, win_size.value)
robotPlay(lattices.value, is_black, win_size.value) lattices.value[x][y] = is_white
} }
} }
......
export const apiKey = process.env.INSCODE_API_KEY; export const apiKey = process.env.INSCODE_API_KEY;
export const apiUrl = 'https://inscode-api.csdn.net/api/v1/gpt/'; export const apiUrl = 'https://inscode-api.csdn.net/api/v1/gpt/';
const regex = /"content":"([^"]*)"/; import { is_empty, checkWin, is_black, is_white } from "./index"
/** /**
* 机器人回合下棋 * 机器人回合下棋
* @param {(null|isWhite|isBlack)[][]} board 棋盘 * @param {(null|isWhite|isBlack)[][]} board 棋盘
...@@ -10,41 +10,44 @@ const regex = /"content":"([^"]*)"/; ...@@ -10,41 +10,44 @@ const regex = /"content":"([^"]*)"/;
* *
* @return {[number, number]} 返回机器人回合要落子的坐标 * @return {[number, number]} 返回机器人回合要落子的坐标
*/ */
export function robotPlay(board, player, win_size) { export async function robotPlay(board, player, win_size) {
console.log('============')
/** /**
* 发送请求,InsCode 已经集成了 GPT 能力 * 发送请求,InsCode 已经集成了 GPT 能力
* 在 vite.config.js 中已通过环境变量写入了 apiKey(该 key 是动态写入使用者的,在 IDE 中是作者,发布到社区是运行该作品的用户) * 在 vite.config.js 中已通过环境变量写入了 apiKey(该 key 是动态写入使用者的,在 IDE 中是作者,发布到社区是运行该作品的用户)
* 发布到社区后,将消耗运行者的额度 * 发布到社区后,将消耗运行者的额度
* 注意:如果部署应用,任何人通过部署后的域名访问应用时,都将消耗部署者的额度 * 注意:如果部署应用,任何人通过部署后的域名访问应用时,都将消耗部署者的额度
*/ */
const content = `这是一个五子棋游戏,${JSON.stringify(board)},下一步"${player}"要下到哪,请不要返回多余的文字,只返回数组,比如["x", "y"]。`
const body = { const body = {
messages: [ messages: [
{ {
role: 'user', role: 'user',
content: "你好" content
} }
], ],
apikey: apiKey apikey: apiKey
} }
const source = fetch(apiUrl, { return await fetch(apiUrl, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
}).then(res => { }).then(res => {
console.log(res)
return res.text() return res.text()
}).then(res => { }).then(async (res) => {
console.log(res.split('\n')) const arr = res.split('\n').filter(item => !!item && !item.includes("DONE")).map(item => JSON.parse(item.replace('data:', ''))).flatMap(item => {
console.log('++++++++') const content = item.choices[0].delta?.content
const match = res.match(regex); return content ? [content] : []
if (match && match.length == 2) { }).join('')
console.log(match[1]); console.log(arr)
let _res = []
try {
_res = JSON.parse(arr)
} catch (err) {
_res = await robotPlay(board, player, win_size)
} }
}).catch(err => { return _res
console.log(err)
}) })
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册