From 860ae2996df2aceb7574f2f89001b55dc78c96dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=84=E3=81=86=E3=81=88=E3=81=8A?= <130837816+aiueo-1234@users.noreply.github.com> Date: Sun, 18 Jun 2023 19:29:27 +0900 Subject: [PATCH] =?UTF-8?q?chatgpt=E3=81=AB=E3=83=97=E3=83=AC=E3=82=A4?= =?UTF-8?q?=E3=83=A4=E3=83=BC=E3=81=AE=E5=89=8D=E3=81=AE=E8=A1=8C=E5=8B=95?= =?UTF-8?q?=E3=82=92=E9=80=9A=E7=9F=A5=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/EnemyAI.cs | 58 ++++++++++++++------------- Assets/Scripts/SampleGameBehaviour.cs | 2 +- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Assets/Scripts/EnemyAI.cs b/Assets/Scripts/EnemyAI.cs index 569f1fc..a7ef298 100644 --- a/Assets/Scripts/EnemyAI.cs +++ b/Assets/Scripts/EnemyAI.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Cysharp.Threading.Tasks; using HackathonA.ChatGPTDatas; @@ -6,9 +7,6 @@ namespace HackathonA { public class EnemyAI { - public int DefaultPhysicalAttack { get; set; } = 20; - public int DefaultMagicAttack { get; set; } = 20; - public int DefaultRecoveryPoint { get; set; } = 15; public int DefaultActionOnError { get; set; } = 0; private const string API_URL = "https://api.openai.com/v1/chat/completions"; @@ -30,42 +28,48 @@ public EnemyAI(string apiKey) private string GenerateSystemMessage() { - return GenerateSystemMessage(DefaultPhysicalAttack, DefaultMagicAttack, DefaultRecoveryPoint); - } - - private string GenerateSystemMessage(int physicalAttack, int mamgicAttack, int recoveryPoint) - { - return @$"Please answer with an integer from 0-4 for your action + return @$"Please answer with an integer from 0 to 4 for your action. -You can take the following actions -0: Physical attack. {physicalAttack} damage -1: Magic attack. {mamgicAttack} damage -2: Recovery. {recoveryPoint} recovery +You can take the following actions. +0: Physical attack. 20 to 30 damage +1: Magic attack. 20 to 30 damage +2: Recovery. 10 to 15 recovery 3: Physical Counter. bounce back if opponen's action is Physical attack -4: Magic counter. bounce back if opponent's action is Magic attack +4: Magic Counter. bounce back if opponent's action is Magic attack -Input example: Your HP is currently 1000 and your opponent's HP is 1000 +Input example: Your HP is currently 1000 and your opponent's HP is 1000. + Choose from the options above and respond with the corresponding number. Output example: 0"; } - // 敵の設定 - public void Set(int physicalAttack, int mamgicAttack, int recoveryPoint) - { - _messages = new List() - { - new Message(){role = "system", content = GenerateSystemMessage(physicalAttack, mamgicAttack, recoveryPoint)}, - }; - } - /// /// ChatGPTとの通信を開始 /// /// プレイヤーのHP /// 敵のHP /// アクションのタイプ(0-4)。エラー時にはDefaultActionOnErrorで設定した値が返ってくる - public async UniTask GetEnemyActionAsync(int playerHP, int enemyHP) + public async UniTask GetEnemyActionAsync(int playerHP, int enemyHP, int playerAction) { - string currentMessage = $"Currently, your HP is {enemyHP} and your opponent's HP is {playerHP}.\nChoose from the options above and respond with the corresponding number."; + string currentMessage; + if (playerAction == -1) + { + currentMessage = $"Currently, your HP is {enemyHP} and your opponent's HP is {playerHP}.\nChoose from the options above and respond with the corresponding number."; + } + else + { + currentMessage = @$"Currently, your HP is {enemyHP} and your opponent's HP is {playerHP}. +Your opponent did the {playerAction switch + { + 0 => "physical attack", + 1 => "magic attack", + 2 => "recovery", + 3 => "physical counter", + 4 => "magic counter", + _ => throw new ArgumentOutOfRangeException() + }}before. +Choose from the options above and respond with the corresponding number. +Also, your response have to be only figure. You can not respond anything other than figure."; + } DebugLoger.Log(currentMessage); _messages.Add(new Message() { role = "user", content = currentMessage }); diff --git a/Assets/Scripts/SampleGameBehaviour.cs b/Assets/Scripts/SampleGameBehaviour.cs index f234139..7424261 100644 --- a/Assets/Scripts/SampleGameBehaviour.cs +++ b/Assets/Scripts/SampleGameBehaviour.cs @@ -12,7 +12,7 @@ async void Start() var config = Resources.Load("ApiKey") as TextAsset; var _apiKey = config.text.Trim(); var enemyBehaviour = new EnemyAI(_apiKey); - DebugLoger.Log($"Enemy Action: {await enemyBehaviour.GetEnemyActionAsync(PlayerHP, EnemyHP)}"); + DebugLoger.Log($"Enemy Action: {await enemyBehaviour.GetEnemyActionAsync(PlayerHP, EnemyHP, -1)}"); } } }