fc2ブログ

JavaScriptっぽい。

『JavaScriptっぽい。』の更新情報やJavaScriptに関する日記。

トランプ3枚当て用スクリプト

トランプ3枚当てる携帯用懸賞サイトがある。 広告をクリックするとポイントがもらえて、そのポイントの数だけ賭けられるのだが、同時にヒントも最大5つまでもらえる。

そのヒントを満たしつつランダムにカードを予想するようなものをJavaScriptで作ってみたいと思っていたが、いざ作ろうとするとなかなかやっかいだ。 だいたいヒントの日本語もあいまいで、例えば「3枚のうち2枚は同じ数字」という場合3枚とも同じ場合も含まれるのかとか、「どれかの数字で7を使用」という場合は1枚だけという意味が含まれるのかなど、つまらないことで悩んでしまったりもする。

とはいえ、10月12日正解発表分のものを作ってみたので、今後はこれを基にしてまた作ってみようかな…


<script type="text/javascript">
/*
*1.すべて2で割り切れない
*2.すべて違うマーク
*3.3枚のうち2枚は同じ数字
*4.クラブは使っていない
*5.どれかの数字で7を使用
	1.クラブ以外のマークの配列を用意
	2.それぞれのマークの1~13までの奇数の配列を用意
	3.同じ数字が2個なので、1回目と2回目の数字が同じでない場合、1回目か2回目の数字は7でなくてはならない。
	4.1回目と2回目の数字が同じでない場合、3回目の数字は1回目の数字か2回目の数字となるため、その数字を格納する
	  配列last_arrayを用意
	5.1回目も2回目も7でない場合、3回目は7になる。
	6.1回目が7でなく、2回目の数字が1回目と一致しない場合は2回目の数字に7を入れているので、
	 必然的に1回目も2回目も7でない場合は、1回目と2回目の数字は同一となる。
*/
var Mark = ["heart","dia","spade"];		//1.
var heart = [1,3,5,7,9,11,13];
var dia = [1,3,5,7,9,11,13];
var spade = [1,3,5,7,9,11,13];			//2.
var last_array = [];		//4.
function choice_card(times){
	var random_num , first_num ;
	var first_mark = Mark [ random_num = (Math.floor ( Math.random() * Mark.length ) ) ];
	Mark.splice ( random_num , 1 );
	if ( times < 3 || last_array[0] == last_array[1] ) {
		first_num = eval ( first_mark + "[ Math.floor ( Math.random() *" + first_mark + ".length ) ]" );
		if ( times == 2 && last_array[0] != 7 && last_array[0] != first_num ) first_num = 7 ;		//3.6.
		if ( times < 3 ) last_array.push ( first_num );			//4.
	}
	else first_num = last_array [ Math.floor ( Math.random() * last_array.length )];
	if ( times == 3 ){
		if ( last_array[0] != 7 && last_array[1] != 7 )	first_num = 7 ;		//5.6.
		last_array = [];
		Mark = ["heart","dia","spade"];
	}	
	
	var first_card = first_mark + first_num;
	return first_card;
}
var choiced_card = [];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];
choiced_card[choiced_card.length] = [choice_card(1),choice_card(2),choice_card(3)];

var joint_array = choiced_card.join("<br>");
document.write ( joint_array );

</script>
スポンサーサイト



  1. 2005/10/18(火) 23:23:10|
  2. トランプ3枚当て|
  3. トラックバック:0|
  4. コメント:0
<<フレーム越しフローティングメニュー | ホーム | 関数リテラルをそのまま実行>>

コメント

コメントの投稿

管理者にだけ表示を許可する

トラックバック

トラックバックURLはこちら
http://javascript.blog3.fc2.com/tb.php/14-d0e716aa

PUREST

02 | 2024/03 | 04
Sun Mon Tue Wed Thu Fri Sat
- - - - - 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 - - - - - -

Recent Entries

Recent Comments

Recent Trackbacks

Archives

Category

Links

Search

Recommend