2018年5月3日木曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(イベントとデリゲート - 見ていないときのコードの動作)、自分で考えてみよう(p. 408)を取り組んでみる。

コード

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 WindowsFormsApp24
{
    
    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, 1000);
            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();
            }
            timer1.Interval = random.Next(500, 1000);
            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, 1000);
            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;
        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("popUpCallBackはnullになることはできません");
            }
            this.random = random;
            this.popUpCallback = popUpCallback;
            hidden = true;
        }
        public bool Hidden { get { return hidden; } }
        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 コメント:

コメントを投稿