Tue Jun 27 01:59:00 UTC 2023 inscode

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