조조하사의 Gaming Nexus☆

[180121 R Programming] 2. Functions and Dice Game




전에 마무리지었던 곳부터 시작했습니다! 전에는 아마 내 수행과정을 찍지는 않았었던 것 같지만, 아무래도 어떤 생각의 흐름을 따라서 왔는지 이후에도 확인하기 위해 찍는게 낫다고 생각해서 수행과정을 찍게 되었습니다. 헤헤

I started from previous end. Although the first post has passed capturing all process, from this post I'll capture my process to check my flow of idea. :)



우선 전에 끝냈던 과정까지의 복습을 위해 주사위 세팅과 수열(seq) 함수, 표본추출(sample) 함수를 사용해보았습니다. 아래는 그 과정.

At first, I tried to use Sequence, Sample, Dice setting. Below is the process.



눈이 1에서 6까지 있는 수열(주사위)을 세팅한 것과,
수를 특정 규칙에 따라 나열하는데 있어서는 seq 함수가 유용하겠다고 생각한 것,
sample 함수는 벡터, 추출횟수, 복원/비복원 여부로 작동가능하다는 것 세 가지를 보여주고 있습니다.


아래는 주사위를 굴리는 함수를 만들어가는 과정.



It shows all process about setting a dice from point 1 to point 6,

and 'seq' function is useful than 'c' function when I arrange numbers
and 'sample' function operates with a Vector, Sample Size, Replacement.


Below is my process of rolling the dice.



저번에 얼핏 뒤쪽을 본 건 있어갖고 함수를 만드는 과정을 어느 정도 흉내내 보았지만...첫 도전 실패.
두 번째는 책을 보고 해서 성공했습니다.
그런데 또 함수 작동시키는법을 몰라서 roll만 쳤다가 보고 싶지 않은 함수 본체를 한 번 마주하게 됨 ㅠㅠ
괄호까지 쳐야 함수가 잘 작동하더군요. 그래서 괄호 없이 함수를 정의하려다 실패했습니다.


그런데 문득 두 가지 의문이 들었습니다. 첫 번째는 괄호의 활용성, '1에서 6까지'의 수열이 입력된 객체 'dice' 외의 다른 수열로 함수가 작동하게 할 수 없을까? 두 번째는, 함수가 잘 작동하는게 맞는걸까...? 어떤 수들이 뽑혔길래 합이 저렇게 나왔을까?

그래서 roll 함수를 문제를 해결할 수 있도록 약간 조작해 보기로 했습니다. 함수가 어떤 원리로 정의되는지 알 수 있었기에 다행히도 책은 필요 없었습니다. 아래는 그 과정.



Since I've seen today's study before(little bit), I mimicked it, and it failed.
Thanks for the book, the second trial was successful.
But I have input 'roll' instead of 'roll()', so I encountered the core of function T.T
I learned that bracket is needed to cause the function. Moreover I tried to define a function without bracket. And it is failed.


By the way, I picked a question mark up with two things. The first is about the use of bracket, [is 'roll' function operates with other dices?]. The second is about the validity of the result. So I changed 'roll()' function to check them. Below is my trial.



쓸데없이 길지만 roll과 sample로 점철된 절반은 테스트입니다 테스트. 위쪽이 핵심이죠.
처음 부분에서는 'roll' 함수를 괄호 안에 주사위(라는 이름의 수열) 객체를 넣어야 작동하도록 만들었습니다. 안 될 줄 알았는데 잘 되어서 흥분해서 다른 주사위를 넣어볼 생각도 못하고 다음으로 넘어감.

두번째 수정 과정에서는, a라는 객체에 두 주사위의 눈을 입력, b라는 객체에 두 눈의 합을 입력하여 a와 b를 동시에 출력하게끔 함수를 조정했습니다.

그리고 테스트를 통해 함수가 잘 작동하고 있구나 하는걸 확인했죠!


The shot is quite long, but all of lower half part consists of test. The core is upper part.
At first, I changed 'roll' function to act on the name of dice. Because it seems success at once, I've excited and so passed a test with another dice.

Next, I changed 'roll' function with two inner-object 'a=each result of two dices' and 'b=sum of two results'. Now I have all output I needed!

At last, I checked all functions are well-defined with sampling tests!



위는 제가 바꾼 'roll'함수에서 두 눈의 합을 더하는 과정이 잘 작동하는지 확인하기 위해 똑같은 메커니즘을 가진, 그러나 4개의 주사위를 던져 결과를 확인하는 'roll2' 함수를 만들어 테스트하는 과정입니다. 그리고 책을 따라가면서 입력값이 없는 객체 'bund'를 넣은 경우 어떤 일이 일어나는지 확인해 보았지요.

아래는 그래서 책 내용에 따라 새 객체 'bund'를 'roll2'에 귀속된 객체로서 인정해주는 과정입니다. 지금 생각해보면 사실 위에서 'roll'함수를 정의할 때 썼던 'x1'이라는 객체랑 그 지위가 다를게 없네요; 괜히 한 듯.


Above shot contains a process to define 'roll2' function, which has same mechanism with 'roll' but uses 4 dices. I made 'roll2' function to check the summation process in 'roll' is either well-functional or not.

In below shot, I've used 'bund' object as factor of 'roll2' function. But now, I guess that 'bund' in 'roll2' has same role with 'x1' in 'roll'. It was not worth..



아래에서는 두 가지 작업을 했는데요, 첫 번째는 'roll2' 함수를 'roll'과 같이 주사위의 눈까지도 출력하도록 바꿔준 거예요.

두 번째는 함수를 정의하면서 기본값까지 설정해 주는 과정입니다. 함수 정의 이후 괄호 안에 아무것도 넣지 않아도 자동으로 1에서 6까지의 눈이 있는 주사위에서 두 값을 뽑아 합을 출력하는걸 확인하실 수 있을 겁니다. 헤헤


I did 2 tasks in below shot. The first is changing 'roll2' function to output all result of two dices.(Of course, with the sum of results) The second is putting initial value in the factor of 'roll' function. You can see 'roll' works well with any input. :P



이 내용 이외에 다른 끄적끄적해만한 것들이 있다면 있다면 다음과 같습니다.

미션 : 텍스트문 안에 객체를 잘 작동하는 상태로 넣어보자!!!(아직 해결 안 됨)

함수 : 'data.frame' 함수의 사용 방법, 'data.frame' 함수로 만들어진 행렬의 행 및 열 출력, seq 함수의 'length.out='인수


오늘은 여기까지! 이거 쓰는 동안 벌써 22일로 접어들었네요 ㅠㅜ
궁금한 점이 있으시면 댓글로 달아주신 뒤에, 스스로 한 번 해보시기 바랍니다.(본격 아이디어 해킹) 헤헷ㅎ
이상! 조조하사였습니다.


Outline of other knowledges :
Mission : Put an object into text with functional(Not solved yet)
Function : using 'data.frame', output of row or column of a matrix generated by 'data.frame', factor 'length.out=' of 'seq' function


It is all for today! Now 22th, Jan. already.
If you have enlightened question mark, then reply to this post and do it yourself(lol it's such a idea hacking).
Then! It was Vjjo!