Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(イベントとデリゲート: 見てないときのコードの動作)、自分で考えてみよう(p.408)を解いてみる。
自分で考えてみよう(p.408)
コード
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Mole mole;
Random random = new Random();
public Form1()
{
InitializeComponent();
mole = new Mole(random, new Mole.PopUp(MoleCallBack));
timer1.Interval = random.Next(500, 1500);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
ToggleMole();
}
private void ToggleMole()
{
if(mole.Hidden == true)
{
mole.Show();
}
else
{
mole.HideAgain();
}
Console.WriteLine(mole.Hidden);
timer1.Interval = random.Next(500, 1500);
timer1.Start();
}
private void MoleCallBack(int MoleNumber, bool Show)
{
if (MoleNumber <0)
{
timer1.Stop();
return;
}
Button button;
switch (MoleNumber)
{
case 0: button = button1; break;
case 1: button = button2; break;
case 2: button = button3; break;
case 3: button = button4; break;
default: button = button5; break;
}
if (Show == true)
{
button.Text = "ここだ!";
button.BackColor = Color.Red;
}
else
{
button.Text = "";
button.BackColor = SystemColors.Control;
}
timer1.Interval = random.Next(500, 1500);
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
mole.Smacked(0);
}
private void button2_Click(object sender, EventArgs e)
{
mole.Smacked(1);
}
private void button3_Click(object sender, EventArgs e)
{
mole.Smacked(2);
}
private void button4_Click(object sender, EventArgs e)
{
mole.Smacked(3);
}
private void button5_Click(object sender, EventArgs e)
{
mole.Smacked(4);
}
}
public class Mole
{
public delegate void PopUp(int hole, bool show);
private PopUp popUpCallback;
private bool hidden;
public bool Hidden { get { return hidden; } }
private int timesHit = 0;
private int timesShown = 0;
private int hole = 0;
Random random;
public Mole(Random random, PopUp popUpCallback)
{
if (popUpCallback == null)
{
throw new ArgumentException("popUpCallBakはnullになることはできません");
}
this.random = random;
this.popUpCallback = popUpCallback;
hidden = true;
}
public void Show()
{
timesShown++;
hidden = false;
hole = random.Next(5);
popUpCallback(hole, true);
}
public void HideAgain()
{
hidden = true;
popUpCallback(hole, false);
CheckForGameOver();
}
public void Smacked(int holeSmacked)
{
if(holeSmacked == hole)
{
timesHit++;
hidden = true;
CheckForGameOver();
popUpCallback(hole, false);
}
}
private void CheckForGameOver()
{
if (timesShown >= 10)
{
popUpCallback(-1, false);
MessageBox.Show("スコア: " + timesHit + "点", "ゲームオーバー");
Application.Exit();
}
}
}
}
0 コメント:
コメントを投稿