Skip to content

Commit

Permalink
Merge pull request #20 from OUCC/#4-ChatGPT
Browse files Browse the repository at this point in the history
大丈夫です。
  • Loading branch information
watanabeRRR authored Jun 18, 2023
2 parents b0538bb + 860ae29 commit c1b15f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
58 changes: 31 additions & 27 deletions Assets/Scripts/EnemyAI.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using HackathonA.ChatGPTDatas;

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";
Expand All @@ -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<Message>()
{
new Message(){role = "system", content = GenerateSystemMessage(physicalAttack, mamgicAttack, recoveryPoint)},
};
}

/// <summary>
/// ChatGPTとの通信を開始
/// </summary>
/// <param name="playerHP">プレイヤーのHP</param>
/// <param name="enemyHP">敵のHP</param>
/// <returns>アクションのタイプ(0-4)。エラー時にはDefaultActionOnErrorで設定した値が返ってくる</returns>
public async UniTask<int> GetEnemyActionAsync(int playerHP, int enemyHP)
public async UniTask<int> 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 });

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/SampleGameBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)}");
}
}
}

0 comments on commit c1b15f6

Please sign in to comment.