/*----------------------
	JSブラックジャック
----------------------*/

// トランプ（Cardオブジェクト）の配列
var nCard = 54;
var aCard = new Array(nCard);

// プレーヤー :: [0]CPU, [1]プレーヤー
var aPlayer = new Array(2);

// Card クラス
function Card() {
	this.image = null;		// イメージ
	this.figure = 0;		// 数字
	this.selected = false;	// 選択済みかどうか
}

// プレーヤークラス
function Player() {
	this.money = 10000;	// 持ち金
	this.stand = true;		// standコール
	this.score = 0;		// 手札の点数
	this.count = 0;		// 手札の枚数
	this.images = new Array(5);	// 表示イメージ
	this.indices = new Array(5);	// 手札のインデックス
	this.textnode = null;	// textnodeオブジェクト(表示文字列用)
	this.tdstatus = null;	// tdオブジェクト（文字列表示用）
	this.tdimage = null;	// tdオブジェクト（イメージ表示用）
}

/*----------------------------------------------------*/

// プリロード
function PreLoad() {
	var strDir = "image/";
	var strExt = ".gif";
	var aFile = new Array(
		"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13",
		"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13",
		"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13",
		"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13",
		"Rev6","XV");
	var i;
	for(i = 0; i < nCard; i++) {
		aCard[i] = new Card();
		with (aCard[i]) {
			image = new Image();
			image.src = strDir + aFile[i] + strExt;
			index = i;
			figure =i % 13 + 1;
			selected = false;
		}
	}
}

// テーブルの作成
// ... 引数 index :: 0=CPU用, 1=プレーヤー用
// ... 戻り値 :: 作成したテーブルオブジェクト
function CreateTable(index) {
	// ゲーム領域の追加
	var tBJ = document.createElement("table");
	var tbodyElem = document.createElement("tbody");
	var trElem1 = document.createElement("tr");
	var trElem2 = document.createElement("tr");
	aPlayer[index].tdstatus =  document.createElement("td");
	aPlayer[index].tdimage = document.createElement("td");

	// テーブルの設定
	with (tBJ) {
		style.width = "170px";
		style.fontSize = "9pt";
		style.border = "1px solid white";
		style.margin = "8px";
	}

	// ステータス表示領域の追加
	with (aPlayer[index].tdstatus) {
		style.color = "white";
		style.backgroundColor = "green";
		style.borderWidth = "0 0 1px 0";
		style.borderStyle = "solid";
		style.borderColor = "white";
		style.padding = "1pt 2pt";
	}

	// カード表示領域の追加
	with (aPlayer[index]) {
		tdimage.style.height = "40px";
		for (i=0; i<5; i++) {
			images[i] = new Image();
			images[i].src = aCard[53].image.src;
			tdimage.appendChild(aPlayer[index].images[i]);
		}
	}

	// 各オブジェクトの追加
	aPlayer[index].textnode = index == 0?
		document.createTextNode("ブラックジャックゲーム"):
		document.createTextNode("Playボタンを押してください");
	aPlayer[index].tdstatus.appendChild(aPlayer[index].textnode);
	trElem1.appendChild(aPlayer[index].tdstatus);
	trElem2.appendChild(aPlayer[index].tdimage);
	tbodyElem.appendChild(trElem1);
	tbodyElem.appendChild(trElem2);
	tBJ.appendChild(tbodyElem);

	return tBJ;
}

// ボタンの追加
function AddButtons()
{
	var sTag = "<input type='button'";
	var sStyle = "style='width:43px; margin:1px;'>";
	document.write("<form style='width:182px'>");
	document.write(sTag, " value='Play' ", "onclick='Play_Click()' ", sStyle);
	document.write(sTag, " value='Hit' ", "onclick='Hit_Click()' ", sStyle);
	document.write(sTag, " value='Stand' ", "onclick='Stand_Click()' ", sStyle);
	document.write(sTag, " value='Help' ", "onclick='Help_Click()' ", sStyle);
	document.write("</form>");
}

// プレーヤーの登録
function RegistPlayer() 
{
	aPlayer[0] = new Player();	// CPU用
	aPlayer[1] = new Player();	// ユーザー用
}

// 初期化
function Init(parent)
{
	if (!document.createElement) { 
		document.write("ゲームを起動することができませんでした。");
		return;
	}

	// トランプ画像のプリロード
	PreLoad();	

	// プレーヤーの登録
	RegistPlayer();

	// ゲーム領域の追加
	with (parent) {
		style.backgroundColor="green";	// 親オブジェクトの背景色
		appendChild(CreateTable(0));		// CPU側テーブル
		appendChild(CreateTable(1));		// ユーザー側テーブル
	}
	
	// ボタンの追加
	AddButtons();
}

/*----------------------------------------------------*/
// イベント

// [Play]ボタンのクリックイベント
function Play_Click()
{
	var i, j;

	if (!aPlayer[1].stand) {
		alert("まだゲームが終わっていません。\nHitボタンかStandボタンを押してください。");
		return;
	}

	// プレーヤーの初期化
	var bMinus = (aPlayer[0].money < 0 || aPlayer[1].money < 0);
	for (i=0; i<2; i++)  {
		with (aPlayer[i]) {
			for (j=0; j<5; j++) {
				if (indices[j] >= 0) aCard[indices[j]].selected = false;
				images[j].src = aCard[53].image.src;
				indices[j] = -1;
			}
			stand = false;
			score = 0;
			count = 0;
			if (bMinus) money = 10000;
		}
	}
	
	// シャッフル
	Shuffle();

	// プレーヤーに二枚づつ配る
	for (i=0; i<2; i++) {
		for (j=0; j<2; j++)	DrawCard(i);
	}

	ShowStatus();		// ステータス文字列の表示
}

// [Hit]ボタンのクリックイベント
function Hit_Click()
{
	if (aPlayer[1].stand) {
		alert("ゲームは終了しています。\nPlayボタンを押してください。");
		return;
	}

	// プレーヤー側
	if (aPlayer[1].count < 5)	DrawCard(1);	
	else alert("引けるカードは５枚までです。");

	// CPU側
	if (!aPlayer[0].stand) DrawCard(0);
	
	ShowStatus();		// ステータス文字列の表示
}

// [Stand]ボタンのクリックイベント
function Stand_Click()
{
	if (aPlayer[1].stand) {
		alert("ゲームは終了しています。\nPlayボタンを押してください。");
		return;
	}

	// プレーヤーのstand宣言
	aPlayer[1].stand = true;
	while (!aPlayer[0].stand) {
		DrawCard(0);
	}
	// CPUの一枚目を表にする
	aPlayer[0].images[0].src = aCard[aPlayer[0].indices[0]].image.src;

	ShowStatus();		// ステータス文字列の表示
}

// [Help]ボタンのクリックイベント
function Help_Click()
{
	window.open("bjhelp.html", "HelpWindow", "toolbar=no, resizable=yes, width=640, height=480");
}

/*----------------------------------------------------*/

// シャッフル
function Shuffle() {
	var i, j;
	for (i=0; i<52; i++) {
		j = Math.floor(Math.random() * 100) % 52;
		var oCard = aCard[i];
		aCard[i] = aCard[j];
		aCard[j] = oCard;
	}
}

// 一枚引く
function DrawCard(nPlayer) {
	// カードを選択
	var nIndex;
	while (1) {
		nIndex = Math.floor(Math.random() * 100) % 52;
		if (aCard[nIndex].selected == false) {
			aCard[nIndex].selected = true;
			break;
		}
	}

	// カードを配る
	with (aPlayer[nPlayer]) {
		if (nPlayer == 0 && count == 0) 
			images[count].src = aCard[52].image.src;	// CPUの一枚目を裏に
		else
			images[count].src = aCard[nIndex].image.src;
		indices[count] = nIndex;
		count++;
	}

	// 点数の計算
	var bAce = false;	// エースがあるか
	var i;
	aPlayer[nPlayer].score = 0;
	for (i=0; i<5; i++) {
		var nCard = aPlayer[nPlayer].indices[i];
		if (nCard != -1) {
			if (aCard[nCard].figure >= 10) aPlayer[nPlayer].score += 10;
			else aPlayer[nPlayer].score += aCard[nCard].figure;
			if (aCard[nCard].figure == 1) bAce = true;
		}
	}
	// エースがあるときは再計算
	if (bAce && aPlayer[nPlayer].score+10 <= 21) 
		aPlayer[nPlayer].score += 10;

	// CPUのときはstandの判断
	if (nPlayer == 0) {
		var nNum;
		with (aPlayer[0]) {
			if (count == 5 || score >= 18) {	/* 手札5枚か18以上でstand */
				stand = true;
			} else if (score == 17) {		/* 17→9/10の確率でstand */
				nNum = Math.random();
				if (nNum > 0.1) stand = true;
			} else if (score == 16) {		/* 16→1/5の確率でstand */
				nNum = Math.random();
				if (nNum < 0.2) stand = true;
			} else if (score == 15) {		/* 15→1/20の確率でstand */
				nNum = Math.random();
				if (nNum < 0.05) stand = true;
			}
		}
	}
}

// ステータスの表示
function ShowStatus() {
	var i;
	var nScore;		// 点数
	var sResult;		// 表示文字列
	var nWin, nLoose;	//勝った方のインデックス、負けた方のインデックス

	// ゲーム終了 :: 0:継続、1:終了、2:破産終了
	var nOver = (aPlayer[0].stand && aPlayer[1].stand)? 1: 0;
	if (nOver) {
		if (aPlayer[0].score > 21) {
			if (aPlayer[1].score <= 21) nWin = 1;
			else nWin = -1;
		} else {
			if (aPlayer[1].score > 21) nWin = 0;
			else if (aPlayer[0].score > aPlayer[1].score) nWin = 0;
			else if (aPlayer[0].score < aPlayer[1].score) nWin = 1;
			else nWin = -1;
		}
		if (nWin != -1) {
			nLoose = nWin==0? 1: 0;
			if (aPlayer[nWin].score == 21) {	/* ブラックジャックは二倍 */
				if (aPlayer[nWin].count == 2) {
					aPlayer[nWin].money += 2000;
					aPlayer[nLoose].money -= 2000;
				} else {				/* 21のときは1.5倍 */
					aPlayer[nWin].money += 1500;
					aPlayer[nLoose].money -= 1500;
				}
			} else {					/* 通常は1000点 */
				aPlayer[nWin].money += 1000;
				aPlayer[nLoose].money -= 1000;
			}
			// 破産終了のチェック
			if (aPlayer[nWin].money < 0 || aPlayer[nLoose].money < 0) nOver = 2; 
		}
	}

	// ステータス文字列
	for (i=0; i<2; i++) {
		with (aPlayer[i]) {
			// CPUの一枚目は"??"を表示
			nScore = (i==0 && nOver==0)? "??": aPlayer[i].score;
			if (nOver == 2) {			/* 破産終了時 */
				sResult = money < 0? "　　>>> 破産 <<<": "　　<<< 優勝 >>>";
			} else if (nOver == 1) {	/* 一回分の終了 */
				if (nWin == -1) sResult = "　　= 引き分け =";
				else if (nWin == i) sResult = "　　+++ 勝ち +++";
				else sResult = "　　--- 負け ---";
			} else if (i == 1 && count == 2 && score == 21) {	/* ブラックジャック成立時 */
				sResult = "　　BlackJack !";
			} else {				/* ゲーム継続時 */
				sResult = "　　$" + aPlayer[i].money;
			}
			// ステータス文字列の削除と追加
			tdstatus.removeChild(textnode);
			textnode = i == 0?
				document.createTextNode("CPU : " + nScore + "点" + sResult):
				document.createTextNode("YOU : " + nScore + "点" + sResult);
			tdstatus.appendChild(textnode);
		}
	}
}


