Skip to content

Instantly share code, notes, and snippets.

@SacredPaladin
Last active April 1, 2022 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SacredPaladin/cb593688a6896a16b5dd38e50b1c3cda to your computer and use it in GitHub Desktop.
Save SacredPaladin/cb593688a6896a16b5dd38e50b1c3cda to your computer and use it in GitHub Desktop.
라이어 다이스 함수와 최종 순환
class Character_Data //캐릭터 데이터
{
constructor()
{
this.dice = [] //주사위들
this.dice_number = 5 //주사위 최대 숫자
}
}
let current_number
const player = new Character_Data()
const ai = new Character_Data()
class Action
{
check (number_finding) //주사위 수 체크하기
{
let number_found = 0 //해당 눈의 주사위를 찾은 개수
for (let i = 0; i < player.dice_number; i++) //플레이어 주사위에서 찾기
{
if (player.dice[i] === number_finding) number_found++
}
for (let i = 0; i < ai.dice_number; i++) //그 다음, AI 주사위에서 찾기
{
if (ai.dice[i] === number_finding) number_found++
}
return number_found //찾은 수 리턴
}
roll () //주사위 굴리기
{
for (let i = 0; i < play.dice_number; i++) //플레이어 주사위 굴리기
{
player.dice[i] = (Math.floor (Math.random () * (6 - 1)) + 1)
}
for (let i = 0; i < ai.dice_number; i++) //AI 주사위 굴리기
{
ai.dice[i] = (Math.floor (Math.random () * (6 - 1)) + 1)
}
}
ran_decision (max) //랜덤 선택 함수
{
return (Math.floor (Math.random () * (max - 1)) + 1)
}
true_false (dice, dice_num, decision, player_or_ai) //참과 거짓 확인, 주사위 삭제 함수
{
if (decision === 1) //참
{
if (dice_num === this.check (dice)) //"N이 몇 개 이상"이 참인지 체크
{
if (player_or_ai === 0)
{
ai.dice[dice_number] = 0
ai.dice_number-- //플레이어가 참을 맞추면 AI가 주사위를 잃음
}
else
{
player.dice[dice_number] = 0
player.dice_number-- //AI가 참을 맞추면 플레이어가 주사위를 잃음
}
}
else //"N이 몇 개 이상"이 거짓인지 체크
{
if (player_or_ai === 0)
{
player.dice[dice_number] = 0
player.dice_number-- //플레이어가 참을 틀리면 플레이어가 주사위를 잃음
}
else
{
ai.dice[dice_number] = 0
ai.dice_number-- //AI가 참을 틀리면 AI가 주사위를 잃음
}
}
}
else //거짓
{
if (dice_num !== this.check (dice)) //"N이 몇 개 이상"이 거짓인지 체크
{
if (player_or_ai === 0)
{
ai.dice[dice_number] = 0
ai.dice_number-- //플레이어가 거짓을 맞추면 AI가 주사위를 잃음
}
else
{
player.dice[dice_number] = 0
player.dice_number-- //AI가 거짓을 맞추면 플레이어가 주사위를 잃음
}
}
else
{
if (player_or_ai === 0)
{
player.dice[dice_number] = 0
player.dice_number-- //플레이어가 거짓을 틀리면 플레이어가 주사위를 잃음
}
else
{
ai.dice[dice_number] = 0
ai.dice_number-- //AI가 거짓을 틀리면 AI가 주사위를 잃음
}
}
}
}
pro_decision (dice, dice_num, ai_turn)
{
const what_should_do = this.ran_decision (3) //참, 거짓, 선언을 각각 1/3 확률로 결정
if (what_should_do === 1 && ai_turn !== 1) this.true_false (dice, dice_num, 1, 1) //참
else if (what_should_do === 2 && ai_turn !== 1) this.true_false (dice, dice_num, 2, 1) //거짓
else //선언
{
const judice_1 = this.ran_decision (2)
if (dice_num === (ai.dice_number + player.dice_number)) //숫자가 최대치일 때 참과 거짓 결정
{
if (judice_1 === 1) this.true_false (dice, dice_num, 1, 1) //참
else this.true_false (dice, dice_num, 2, 1) //거짓
}
else //선언하기
{
if (judice_1 === 1) //자신에게 있는 숫자로 하기
{
const judice_2 = this.ran_decision (ai.dice_number) //AI의 몇 번째 주사위의 숫자로 할지 결정
const judice_3 = this.ran_decision (3) //주사위 1-3개 중 몇 개를 높여 부를지 결정
if (judice_3 === 1) { this.pronounce (ai.dice[judice_2], dice_num + 1, ++define) } //+1개
else if (judice_3 === 2) { this.pronounce (ai.dice[judice_2], dice_num + 2, ++define) } //+2개
else { this.pronounce (ai.dice[judice_2], dice_num + 3, ++define) } //+3개
}
else //아무 숫자나 하기
{
const judice_2 = this.ran_decision (6) //AI의 몇 번째 주사위의 숫자로 할지 결정
const judice_3 = this.ran_decision (3) //주사위 1-3개 중 몇 개를 높여 부를지 결정
if (judice_3 === 1) { this.pronounce (judice_2, dice_num + 1, ++define) } //+1개
else if (judice_3 === 2) { this.pronounce (judice_2, dice_num + 2, ++define) } //+2개
else { this.pronounce (judice_2, dice_num + 3, ++define)} //+3개
}
}
}
}
pronounce (dice, dice_num, define)
{
if (define % 2 === 0) //플레이어 턴
{
define++ //선언 차례 상대에게 넘길 준비
while (dice_num <= (ai.dice_number + player.dice_number))
{
dice = N //수정 필요: 주사위 눈 입력
dice_num = N //수정 필요: 주사위 수 입력
}
this.pro_decision (dice, dice_num, 0) //AI 판단 부분
}
else //AI 턴
{
define++ //선언 차례 상대에게 넘길 줌비
this.pro_decision (dice, dice_num, 1) //AI 판단 부분
const command = N //수정 필요
if (command === 1) { this.true_false (dice, dice_num, 1, 0) } //참
else if (command === 2) { this.true_false (dice, dice_num, 2, 0) } //거짓
else { this.pronounce (dice, dice_num, define) } //선언
}
}
}
function play_liardice ()
{
console.log ("라이어 다이스를 시작합니다.")
{
let who_turn = 0
while (ai.dice_number !== 0 && player.dice_number !== 0)
{
roll () //주사위 굴리기
if (who_turn % 2 === 0)
{
who_turn++ //턴 카운트
console.log (who_turn + "번째 턴입니다.")
Action.pronounce (dice, dice_num, 0) //플레이어 선언
}
else
{
who_turn++ //턴 카운트
console.log (who_turn + "번째 턴입니다.")
Action.pronounce (dice, dice_num, 1) //AI 선언
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment