청기백기 명령문 generator
글쓴이: highwind / 작성시간: 월, 2009/05/18 - 2:52오후
교회에서 소풍을가서 추억의 게임 청기백기를 하기로 하였습니다.
미리 올려 내려 명령을 손으로 쓰기도 그렇고 즉흥적으로 명령하기도 모하고 해서
프로그램을 짜서 명령문을 랜덤으로 만들게 해보았습니다.
Python과 ruby로 짜보았습니다.
Python 3.0:
import random
data = [["청기", "백기", "둘다"], ["올", "내"], ["려","려","리지마"]]
for i in range(50):
print(*[random.choice(d) for d in data], sep='')Ruby:
data = [["청기", "백기", "둘다"], ["올", "내"], ["려","려","리지마"]]
50.times {puts data.collect {|d| d[rand(d.length)]}.join}"려"가 두번 들어간 이유는 하지마라는 명령보다 하라는 명령을 더 많이 출력하고 싶어서 입니다.
ps.
강좌에 올리긴 좀 그런가??
Forums:


괜춘하네요
심플하면서도 괜찮은듯
이터레이션 엘리먼트 d 가 중복사용되어서 list generator에서 2초정도 헷갈렸다능 ㅎㅎ
------------------------------
모든것은 모든것에 잇닿아 있다.
------------------------------
모든것은 모든것에 잇닿아 있다.
앗 오타네요.. 수정
앗 오타네요.. 수정 하였습니다.
=====================================
http://timothylive.net
=====================================
http://timothylive.net
Perl 버젼
.
.
.
.
.
Perl 5
use 5.010; my @data = ([qw/청기 백기 둘다/],[qw/올 내/],[qw/려 려 리지마/]); say map { @$_[rand @$_] } @data for 1..50;Perl 6
my @data = [<청기 백기 둘다>],[<올 내>],[<려 려 리지마>]; say map {$_.pick(1)}, @data for 1..50;조금 길게 짜 보았습니다.
출력할 때 약간의 지연 현상을 줘서 긴장감을 살짝 더 유발하도록 고쳐봤습니다. :-)
여기에 올라온 다른 것들도 테스트 해보고 싶은데 제 컴퓨터에는 python2.6과 perl밖에 없네요.
#!/usr/bin/env perl use Time::HiRes qw(usleep); use strict; package DelayWord; use base qw(Class::Accessor::Fast); __PACKAGE__->mk_accessors( qw(word) ); sub new { my $class = shift; return bless { 'word' => shift }, $class; } sub print { my ( $this, $surfix ) = @_; local $| = 1; $surfix = ' ' unless defined $surfix; print $this->word.$surfix; ::usleep( ( 30 + rand( 70 ) ) * 10e2 ); } package DelayWord::Direction; use base 'DelayWord'; sub print { $_[0]->SUPER::print( '' ); } package DelayWord::Command; use base 'DelayWord'; __PACKAGE__->mk_accessors( qw(continue) ); sub new { my $class = shift; return bless { 'word' => shift, 'continue' => shift, }, $class; } sub print { my $this = shift; $this->SUPER::print( ( $this->continue ) ? " " : "\n" ); ::usleep( ( 21 + rand(170) ) * 10e3 ); } package main; use warnings; use strict; my @data; for my $args ( ( map { [ 0, "DelayWord", $_ ] } qw(청기 백기 둘다) ), ( map { [ 1, "DelayWord::Direction", $_ ] } qw(올 내) ), ( map { [ 2, "DelayWord::Command", @$_ ] } ( ( ["려", ] ) x 3, ( ["리지마", ] ) x 2, ["리지 말고", "continue" ] ) ) ) { push @{ $data[ shift @$args ] }, (shift @$args)->new( @$args ); } for ( 1..50 ) { for my $part ( @data ) { @$part[ rand scalar( @$part) ]->print; } }just for fun 인 거 아시죠? ^^
$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}
실제 게임하는 기분이네요.
실행해보니 실제 게임하는 기분이네요
근데 사용하신 Class::Accessor::Fast 모듈은 따로 new 메소드를 재정의 하지 않아도 됩니다.
이렇게 하셨으면 객체를 생성할 때
DelayWord->new( { word => $word } );
와 같이 mk_accessors에 정의한 파라메터들을 담은 해시를 익명해시 형태로 넘겨주면 됩니다.
이건 Class::Accessor 모듈의 다음과 같은 new 메소드 정의를 보시면 이해가 가실 듯..
sub new { my($proto, $fields) = @_; my($class) = ref $proto || $proto; $fields = {} unless defined $fields; # make a copy of $fields. bless {%$fields}, $class; }아 그렇군요. 하나
아 그렇군요. 하나 배우고 갑니다. ^^
수정한 버전입니다. 그리고 위에서 "리고" 명령어를 빼먹었네요. ^^;
#!/usr/bin/env perl use strict; use Time::HiRes qw(usleep); package DelayWord; use base qw(Class::Accessor::Fast); __PACKAGE__->mk_accessors( qw(word) ); sub print { my ( $this, $surfix ) = @_; local $| = 1; $surfix = ' ' unless defined $surfix; print $this->word.$surfix; ::usleep( ( 30 + rand( 70 ) ) * 10e2 ); } package DelayWord::Direction; use base 'DelayWord'; sub print { $_[0]->SUPER::print( '' ); } package DelayWord::Command; use base 'DelayWord'; __PACKAGE__->mk_accessors( qw(continue) ); sub print { my $this = shift; $this->SUPER::print( ( $this->continue ) ? " " : "\n" ); ::usleep( ( 21 + rand(170) ) * 10e3 ); } package main; use warnings; use strict; my @data; for my $args ( ( map { [ 0, 'DelayWord', 'word' => $_ ] } qw(청기 백기 둘다) ), ( map { [ 1, 'DelayWord::Direction', 'word' => $_ ] } qw(올 내) ), ( map { [ 2, 'DelayWord::Command', 'word', @$_ ] } ( ( ["려", ] ) x 3, ( ["리지마", ] ) x 2, ["리고", "continue" => "t", ], ["리지 말고", "continue" => "t" ] ) ) ) { push @{ $data[ shift @$args ] }, (shift @$args)->new( { @$args } ); } for ( 1..50 ) { for my $part ( @data ) { @$part[ rand scalar( @$part) ]->print(); } }$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}
xvidcap을 처음
xvidcap을 처음 써보는군요. youtube에 한 번 올려봤습니다.
$Myoungjin_JEON=@@=qw^rekcaH lreP rehtonA tsuJ^;$|++;{$i=$like=pop@@;unshift@@,$i;$~=18-length$i;print"\r[","~"x abs,(scalar reverse$i),"~"x($~-abs),"]"and select$good,$day,$mate,1/$~for 0..$~,-$~+1..-1;redo}
Haskell 버전
Haskell 버전입니다.
--------------------Signature--------------------
Light a candle before cursing the darkness.
파이썬에서
join 메쏘드에는 대괄호를 쓰지 않아도 됩니다. generator expression.
좀더 다이나믹하게~
이렇게
이렇게 update했습니다.
.
.
.
data = [["청기", "백기", "둘다"], ["올", "내"], ["려","려","리지마"]] for i in range(50): print(*[random.choice(d) for d in data], sep='')=====================================
http://timothylive.net
=====================================
http://timothylive.net
Ruby...
data = [["청기", "백기", "둘다"], ["올", "내"], ["려","려","리지마"]] 50.times{puts data.map{|d| d.sort_by{rand}[0]}*''}data = [["청기", "백기",
data = [["청기", "백기", "둘다"], ["올", "내"], ["려","려","리지마"]] ARGV[0].to_i.times{puts data.map{|d| d.sort_by{rand}[0]}*''}실행시 수를 지정할 수 있도록 수정했습니다. :)
- Why don't you come in OpenSolaris? I hope you come together.
--
I think to myself...what a emerging world.
그럼 기본값도 필요하죠. 뭐..
data = [["청기", "백기", "둘다"], ["올", "내"], ["려","려","리지마"]] (ARGV[0]||10).to_i.times{puts data.map{|d| d.sort_by{rand}[0]}*''}감사합니다! :) - Why
감사합니다! :)
- Why don't you come in OpenSolaris? I hope you come together.
--
I think to myself...what a emerging world.
이런
는 되는데
같은건 안되네요
로 하셔야겠네요.. 주절..
C버젼으로 만들어 봤어요
char* data1[] = { (char*)"청기",(char*)"백기",(char*)"둘다"}; char* data2[] = { (char*)"올",(char*)"내"}; char* data3[] = { (char*)"려",(char*)"려",(char*)"리지마"}; int i,d1,d2,d3; srand(time(NULL)); for(i=0;i<10;i++) { d1 = rand()%3; d2 = rand()%2; d3 = rand()%3; //printf("d1=%d d2=%d d3=%d\n",d1,d2,d3); printf("%s %s %s \n",data1[d1],data2[d2],data3[d3]); sleep(1); }방가워요
은근히 재미있네요. :)
class Flags { public static void main(String[] args) { String[] data = { "청기", "백기", "둘다", "올", "내", "려", "려", "리지마" }; java.util.Random rand = new java.util.Random(); for (int i = 0; i < 50; i++) { int d1 = rand.nextInt(3); int d2 = rand.nextInt(2) + 3; int d3 = rand.nextInt(3) + 5; System.out.println(data[d1] + data[d2] + data[d3]); } } }- Why don't you come in OpenSolaris? I hope you come together. class Flags {
--
I think to myself...what a emerging world.
댓글 달기