Mon Jun 26 09:59:00 UTC 2023 inscode

上级 c7bfabea
<script setup>
import { onMounted, ref } from "vue"
import { is_empty, checkWin, robotPlay, is_black, is_white } from "./utils"
import { is_empty, checkWin, is_black, is_white } from "./utils"
import { robotPlay } from './utils/api'
// 配置
const size = ref(20)
const win_size = ref(5)
......@@ -18,13 +19,14 @@ function confirm() {
}
// 玩家下棋
function playChess(item, row, col) {
if (isWin.length || item) return
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 })
if (test) {
isWin = test
return
} else {
console.log('------------')
robotPlay(lattices.value, is_black, win_size.value)
}
}
......
export const apiKey = process.env.INSCODE_API_KEY;
export const apiUrl = 'https://inscode-api.csdn.net/api/v1/gpt/';
const regex = /"content":"([^"]*)"/;
/**
* 机器人回合下棋
* @param {(null|isWhite|isBlack)[][]} board 棋盘
* @param {isWhite|isBlack} player 当前下棋类型
* @param {number} win_size 多少颗棋子能赢
*
* @return {[number, number]} 返回机器人回合要落子的坐标
*/
export function robotPlay(board, player, win_size) {
console.log('============')
/**
* 发送请求,InsCode 已经集成了 GPT 能力
* 在 vite.config.js 中已通过环境变量写入了 apiKey(该 key 是动态写入使用者的,在 IDE 中是作者,发布到社区是运行该作品的用户)
* 发布到社区后,将消耗运行者的额度
* 注意:如果部署应用,任何人通过部署后的域名访问应用时,都将消耗部署者的额度
*/
const body = {
messages: [
{
role: 'user',
content: "你好"
}
],
apikey: apiKey
}
const source = 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]);
}
}).catch(err => {
console.log(err)
})
}
export const is_empty = null;
export const is_empty = 'isEmpty';
export const is_white = 'isWhite';
export const is_black = 'isBlack';
export const directions = [
......@@ -13,7 +13,7 @@ export const directions = [
* @param {object} param
* @param {number} param.row 行
* @param {number} param.col 列
* @param {(null|is_white|is_black)[]} param.board 棋盘
* @param {(isEmpty|is_white|is_black)[]} param.board 棋盘
* @param {is_white|is_black} [param.player] 当前棋子类型
* @param {number} param.win_size 需要几个棋子才赢
* @param {[]} [param.direction] 方向
......@@ -96,7 +96,7 @@ export function robotPlay(board, player, win_size) {
}
}
_board[i][j] = null; // 恢复棋盘状态
_board[i][j] = isEmpty; // 恢复棋盘状态
// 选取分数最高的空位
if (score > maxScore) {
......
......@@ -6,5 +6,10 @@ export default defineConfig({
server: {
host: true
},
plugins: [vue()]
plugins: [vue()],
define: {
'process.env': {
INSCODE_API_KEY: process.env.INSCODE_API_KEY
}
}
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册