#pragma once
#include "Dice.h"


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
//#include <iostream>
#include <vcclr.h>
//#include <atlstr.h>
#using <mscorlib.dll>


 



namespace DiceGame {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Diagnostics;

	/// <summary>
	/// Form1 の概要
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: ここにコンストラクター コードを追加します
			//
		}

	protected:
		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Button^  button1;




	private: System::Windows::Forms::PictureBox^  pictureDice1;
	private: System::Windows::Forms::PictureBox^  pictureDice2;
	private: System::Windows::Forms::PictureBox^  pictureDice3;
	private: System::Windows::Forms::PictureBox^  pictureDice4;
	private: System::Windows::Forms::PictureBox^  pictureDice5;
	private: System::Windows::Forms::PictureBox^  pictureDice6;
	private: System::Windows::Forms::Label^  labelResult;
	private: System::Windows::Forms::PictureBox^  pictureWin;
	private: System::Windows::Forms::PictureBox^  pictureLose;


	private: System::Windows::Forms::Timer^  timer1;
	private: System::Windows::Forms::RadioButton^  radioButtonGusu;
	private: System::Windows::Forms::RadioButton^  radioButtonKisu;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;





	private: System::ComponentModel::IContainer^  components;






	protected: 

	private:
		/// <summary>
		/// 必要なデザイナー変数です。
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディターで変更しないでください。
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->pictureDice1 = (gcnew System::Windows::Forms::PictureBox());
			this->pictureDice2 = (gcnew System::Windows::Forms::PictureBox());
			this->pictureDice3 = (gcnew System::Windows::Forms::PictureBox());
			this->pictureDice4 = (gcnew System::Windows::Forms::PictureBox());
			this->pictureDice5 = (gcnew System::Windows::Forms::PictureBox());
			this->pictureDice6 = (gcnew System::Windows::Forms::PictureBox());
			this->labelResult = (gcnew System::Windows::Forms::Label());
			this->pictureWin = (gcnew System::Windows::Forms::PictureBox());
			this->pictureLose = (gcnew System::Windows::Forms::PictureBox());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			this->radioButtonGusu = (gcnew System::Windows::Forms::RadioButton());
			this->radioButtonKisu = (gcnew System::Windows::Forms::RadioButton());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice1))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice2))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice3))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice4))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice5))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice6))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureWin))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureLose))->BeginInit();
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(10, 9);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(123, 24);
			this->label1->TabIndex = 0;
			this->label1->Text = L"ボクと勝負しよう！\r\n連続正解で点数2倍だよ";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(33, 52);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(152, 33);
			this->button1->TabIndex = 1;
			this->button1->Text = L"スタート";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// pictureDice1
			// 
			this->pictureDice1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureDice1.Image")));
			this->pictureDice1->Location = System::Drawing::Point(12, 131);
			this->pictureDice1->Name = L"pictureDice1";
			this->pictureDice1->Size = System::Drawing::Size(47, 48);
			this->pictureDice1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureDice1->TabIndex = 4;
			this->pictureDice1->TabStop = false;
			this->pictureDice1->Visible = false;
			this->pictureDice1->Click += gcnew System::EventHandler(this, &Form1::pictureBox1_Click);
			// 
			// pictureDice2
			// 
			this->pictureDice2->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureDice2.Image")));
			this->pictureDice2->Location = System::Drawing::Point(65, 131);
			this->pictureDice2->Name = L"pictureDice2";
			this->pictureDice2->Size = System::Drawing::Size(47, 48);
			this->pictureDice2->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureDice2->TabIndex = 5;
			this->pictureDice2->TabStop = false;
			this->pictureDice2->Visible = false;
			// 
			// pictureDice3
			// 
			this->pictureDice3->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureDice3.Image")));
			this->pictureDice3->Location = System::Drawing::Point(118, 131);
			this->pictureDice3->Name = L"pictureDice3";
			this->pictureDice3->Size = System::Drawing::Size(47, 48);
			this->pictureDice3->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureDice3->TabIndex = 6;
			this->pictureDice3->TabStop = false;
			this->pictureDice3->Visible = false;
			// 
			// pictureDice4
			// 
			this->pictureDice4->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureDice4.Image")));
			this->pictureDice4->Location = System::Drawing::Point(171, 131);
			this->pictureDice4->Name = L"pictureDice4";
			this->pictureDice4->Size = System::Drawing::Size(47, 48);
			this->pictureDice4->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureDice4->TabIndex = 7;
			this->pictureDice4->TabStop = false;
			this->pictureDice4->Visible = false;
			// 
			// pictureDice5
			// 
			this->pictureDice5->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureDice5.Image")));
			this->pictureDice5->Location = System::Drawing::Point(224, 131);
			this->pictureDice5->Name = L"pictureDice5";
			this->pictureDice5->Size = System::Drawing::Size(47, 48);
			this->pictureDice5->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureDice5->TabIndex = 8;
			this->pictureDice5->TabStop = false;
			this->pictureDice5->Visible = false;
			// 
			// pictureDice6
			// 
			this->pictureDice6->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureDice6.Image")));
			this->pictureDice6->Location = System::Drawing::Point(277, 131);
			this->pictureDice6->Name = L"pictureDice6";
			this->pictureDice6->Size = System::Drawing::Size(47, 48);
			this->pictureDice6->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureDice6->TabIndex = 9;
			this->pictureDice6->TabStop = false;
			this->pictureDice6->Visible = false;
			// 
			// labelResult
			// 
			this->labelResult->AutoSize = true;
			this->labelResult->Location = System::Drawing::Point(240, 215);
			this->labelResult->Name = L"labelResult";
			this->labelResult->Size = System::Drawing::Size(0, 12);
			this->labelResult->TabIndex = 10;
			// 
			// pictureWin
			// 
			this->pictureWin->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureWin.Image")));
			this->pictureWin->Location = System::Drawing::Point(12, 185);
			this->pictureWin->Name = L"pictureWin";
			this->pictureWin->Size = System::Drawing::Size(100, 74);
			this->pictureWin->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureWin->TabIndex = 11;
			this->pictureWin->TabStop = false;
			// 
			// pictureLose
			// 
			this->pictureLose->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureLose.Image")));
			this->pictureLose->Location = System::Drawing::Point(12, 185);
			this->pictureLose->Name = L"pictureLose";
			this->pictureLose->Size = System::Drawing::Size(100, 74);
			this->pictureLose->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
			this->pictureLose->TabIndex = 12;
			this->pictureLose->TabStop = false;
			this->pictureLose->Visible = false;
			// 
			// timer1
			// 
			this->timer1->Interval = 50;
			this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
			// 
			// radioButtonGusu
			// 
			this->radioButtonGusu->AutoSize = true;
			this->radioButtonGusu->Location = System::Drawing::Point(33, 92);
			this->radioButtonGusu->Name = L"radioButtonGusu";
			this->radioButtonGusu->Size = System::Drawing::Size(47, 16);
			this->radioButtonGusu->TabIndex = 13;
			this->radioButtonGusu->TabStop = true;
			this->radioButtonGusu->Text = L"偶数";
			this->radioButtonGusu->UseVisualStyleBackColor = true;
			this->radioButtonGusu->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton1_CheckedChanged);
			// 
			// radioButtonKisu
			// 
			this->radioButtonKisu->AutoSize = true;
			this->radioButtonKisu->Location = System::Drawing::Point(135, 91);
			this->radioButtonKisu->Name = L"radioButtonKisu";
			this->radioButtonKisu->Size = System::Drawing::Size(47, 16);
			this->radioButtonKisu->TabIndex = 14;
			this->radioButtonKisu->TabStop = true;
			this->radioButtonKisu->Text = L"奇数";
			this->radioButtonKisu->UseVisualStyleBackColor = true;
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(12, 293);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(405, 19);
			this->textBox1->TabIndex = 15;
			this->textBox1->Text = L":100000004466FFFFFFFFFFFFFFFFFFFFFFFFFFFF";
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(12, 335);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(405, 19);
			this->textBox2->TabIndex = 16;
			this->textBox2->TextChanged += gcnew System::EventHandler(this, &Form1::textBox2_TextChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(429, 385);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->radioButtonKisu);
			this->Controls->Add(this->radioButtonGusu);
			this->Controls->Add(this->pictureLose);
			this->Controls->Add(this->pictureWin);
			this->Controls->Add(this->labelResult);
			this->Controls->Add(this->pictureDice6);
			this->Controls->Add(this->pictureDice5);
			this->Controls->Add(this->pictureDice4);
			this->Controls->Add(this->pictureDice3);
			this->Controls->Add(this->pictureDice2);
			this->Controls->Add(this->pictureDice1);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label1);
			this->Name = L"Form1";
			this->Text = L"サイコロ予想ゲーム";
			this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing);
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice1))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice2))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice3))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice4))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice5))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureDice6))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureWin))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureLose))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private:
		//Diceクラス
		Dice^ _dice;
	private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {


			#if 0
			//page142
			//各コントロールをゲーム中の状態に変更
			ChangeControlStatus(true);
			#endif


			//char cOrg[128] = ":100000004466FFFFFFFFFFFFFFFFFFFFFFFFFFFF", *p;
			char cOrg[128],*p;
			//String^ str;
			//String^ str2;
			System::Text::StringBuilder^ str;
			System::Text::StringBuilder^ str2;


			int i;
			int iVal1,iVal2,iTotal = 0,iHosu;
			char cAns[ 128 ];


			//cOrg = textBox1->lines;
			//str = this->textBox1->Text;
			//str = this->textBox1->Text;
			str = gcnew System::Text::StringBuilder(this->textBox1->Text);
			//cOrg = L str;
			//strcpy(cOrg,this->textBox1->Text);

			//cOrg = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
			//cOrg = (char*)(void*)Marshal::StringToHGlobalAnsi(str);

			//cOrg = Char::Parse(str);

			for(i=0;i<41;i++)
			{
				cOrg[i] = str[i];

			}


			p = cOrg + 1; //":"は無視

			while(*p)
			{

				*p = toupper( *p );
 
				iVal1 = -9;


				if(('A'<= *p)&&('F'>= *p))
				{
					iVal1 = *p - 'A' + 10;
					iVal1 *= 16;
				}


				if(('0' <= *p )&&( '9' >= *p))
				{
					iVal1 = *p - '0';
					iVal1 *= 16;
				}
				//if(iVal1 < 0) return(2);
				//if(iVal1 < 0) return;
				if(iVal1 < 0) break;


				p++;

				iVal2 = -9;
	
				if(( 'A' <= *p ) && ( 'F' >= *p ))
				{
					iVal2 = *p - 'A' + 10;
				}
				if(('0'<= *p)&&('9'>= *p))
				{
					iVal2 = *p - '0';
				}
				//if( iVal2 < 0 ) return(3);
				//if( iVal2 < 0 ) return;
				if( iVal2 < 0 ) break;


				p++;
				iTotal += iVal1 + iVal2;
			}

			iHosu = ~iTotal; // １の補数
			iHosu++; // ＋１
			iHosu = iHosu & 0x00FF;
			//str[41] = iHosu;
			sprintf( cAns, "%s%02X%c", cOrg, iHosu, 0x22 );
			//sprintf( str, "%s%02X%s", str, cAns, 0x22 );
			//str2 = gcnew String(cAns,1);
			//this->textBox2->Text = str;
			str[41]=cAns[1];
			str[42]=cAns[2];
			printf( "%s\n", cAns ); //文字列結果」




		 }
#if 1
private: System::Void ShowDice(int showDiceNumber){
			//全部のサイコロを非表示
			 this->pictureDice1->Visible = false;
			 this->pictureDice2->Visible = false;
			 this->pictureDice3->Visible = false;
			 this->pictureDice4->Visible = false;
			 this->pictureDice5->Visible = false;
			 this->pictureDice6->Visible = false;
			 //サイコロを表示
			 switch(showDiceNumber){
				case 1:
					this->pictureDice1->Visible = true;
					break;
				case 2:
					this->pictureDice2->Visible = true;
					break;
				case 3:
					this->pictureDice3->Visible = true;
					break;
				case 4:
					this->pictureDice4->Visible = true;
					break;
				case 5:
					this->pictureDice5->Visible = true;
					break;
				case 6:
					this->pictureDice6->Visible = true;
					break;
				default:
					break;
			 }

		 }
#endif


//page 97
private:bool IsWinGame(int showDiceNumber){
			//偶数か奇数のどちらを選んだか保存
			String^ selectedValue;
			if(radioButtonGusu->Checked == true){
				selectedValue = "偶数";
			}else{
				selectedValue = "奇数";
			}

			//偶数か奇数のどちらの目が出たかを保存
			String^ resultValue;
			if(showDiceNumber %2 == 0){
				resultValue = "偶数";
			}else{
				resultValue = "奇数";
			}

			//予想と結果が一致するかで勝敗を判定
			if(selectedValue == resultValue){
				//勝ち
				return true;
			}else{
				//負け
				return false;
			}
		}

//page 97
private:System::Void ShowPicture(bool winGame){

			if(winGame){
				this->labelResult->Text = "あたり！";
				this->labelResult->ForeColor = Color::Red;
				this->pictureWin->Visible = true;
				this->pictureLose->Visible = false;
				this->BackColor = Color::White;

			}else{
				this->labelResult->Text = "はずれ・・・";
				this->labelResult->ForeColor = Color::Black;
				this->pictureWin->Visible = false;
				this->pictureLose->Visible = true;
				this->BackColor = Color::LightGray;
			}
		}


private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
		 
			 if(System::Windows::Forms::DialogResult::Cancel == MessageBox::Show("ゲームを終了しますか？","確認",MessageBoxButtons::OKCancel,MessageBoxIcon::Question))
			 {
				 e->Cancel = true;

			 }
		 
		 
		 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 _dice = gcnew Dice(6);
			 ShowDice(_dice->Number);
		 }
private: System::Void label2_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
			 //サイコロを振る
			 _dice->Shake();
			 //表示を反映する
			 ShowDice(_dice->Number);
		 }

private: System::Void ChangeControlStatus(bool playing){
			 //サイコロ用のタイマー
			 timer1->Enabled = playing;
			 //スタートボタン
			 this->button1->Enabled = !playing;
			 //奇数・偶数ボタン
			 this->radioButtonKisu->Enabled = playing;
			 this->radioButtonGusu->Enabled = playing;
			 if(playing){
				 this->radioButtonGusu->Checked = false;
				 this->radioButtonKisu->Checked = false;
			 }
		 }

private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
};
}

