2018年4月16日月曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(インタフェースと抽象クラス - クラスに約束を守らせる)、エクササイズ(p. 268)を取り組んでみる。

コード

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 WindowsFormsApp19
{
    public partial class Form1 : Form
    {
        Location currentLocation;
        RoomWithDoor livingRoom;
        OutsideWithDoor frontYard;
        Opponent opponent;
        int moves = 0;

        public Form1()
        {
            InitializeComponent();

            CreateObjects();
            opponent = new Opponent(frontYard);
            ResetGame(false);
        }

        private void CreateObjects()
        {
            livingRoom = new RoomWithDoor("リビングルーム",
                "アンティークカーペット",
                "真ちゅうのノブを持つオーク材のドア",
                "クローゼットの中");
            Room diningRoom = new Room("ダイニングルーム", "クリスタルのシャンデリア");
            RoomWithDoor kitchen = 
                new RoomWithDoor("台所", "ステンレス製の電化製品", "網戸", "棚の中");
            Room stairs = new Room("階段", "木の手すり");
            RoomWithHidingPlace corridor =
                new RoomWithHidingPlace("2階の廊下", "犬の写真", "クローゼット");
            RoomWithHidingPlace masterBedroom = 
                new RoomWithHidingPlace("主寝室", "大きなベッド", "ベッドの下");
            RoomWithHidingPlace secondBedroom = 
                new RoomWithHidingPlace("第2寝室", "小さなベッド", "ベッドの下");
            RoomWithHidingPlace bathroom = 
                new RoomWithHidingPlace("浴室", "洗面台とトイレ", "シャワールーム");
            frontYard = new OutsideWithDoor("前庭", false, 
                "真ちゅうのノブを持つオーク材のドア");
            OutsideWithHidingPlace garden = new OutsideWithHidingPlace("庭園", false, "小屋");
            OutsideWithDoor backYard = new OutsideWithDoor("裏庭", true, "網戸");
            OutsideWithHidingPlace driveway = 
                new OutsideWithHidingPlace("ドライブウェイ", false, "車庫");

            livingRoom.exits = new Location[] { diningRoom, stairs };
            diningRoom.exits = new Location[] { livingRoom, kitchen };
            kitchen.exits = new Location[] { diningRoom };
            stairs.exits = new Location[] { livingRoom, corridor };
            corridor.exits = new Location[] { stairs, masterBedroom, secondBedroom, bathroom };
            masterBedroom.exits = new Location[] { corridor };
            secondBedroom.exits = new Location[] { corridor };
            bathroom.exits = new Location[] { corridor };
            
            frontYard.exits = new Location[] { backYard, garden, driveway };
            garden.exits = new Location[] { frontYard, backYard };
            backYard.exits = new Location[] { frontYard, garden ,driveway};
            driveway.exits = new Location[] { frontYard, backYard };

            livingRoom.DoorLocation = frontYard;
            frontYard.DoorLocation = livingRoom;
            kitchen.DoorLocation = backYard;
            backYard.DoorLocation = kitchen;
        }

        private void MoveToANewLocation(Location loc)
        {
            moves++;
            currentLocation = loc;
            RedrawForm();
        }

        private void goHere_Click(object sender, EventArgs e)
        {
            MoveToANewLocation(currentLocation.exits[exits.SelectedIndex]);
        }

        private void goThroughTheDoor_Click(object sender, EventArgs e)
        {
            IHasExteriorDoor door = currentLocation as IHasExteriorDoor;
            MoveToANewLocation(door.DoorLocation);
        }

        private void check_Click(object sender, EventArgs e)
        {
            if (opponent.Check(currentLocation))
            {
                ResetGame(true);
            }
            else
            {
                RedrawForm();
            }
        }

        private void hide_Click(object sender, EventArgs e)
        {
            hide.Visible = false;
            for (int i = 1; i <= 10; i++)
            {
                description.Text += i + "...";
                opponent.Move();
                Application.DoEvents();
                System.Threading.Thread.Sleep(200);
            }
            description.Text = "もういいかい?いくよ";
            Application.DoEvents();
            System.Threading.Thread.Sleep(500);

            goHere.Visible = true;
            exits.Visible = true;
            MoveToANewLocation(livingRoom);
        }

        public void RedrawForm()
        {
            exits.Items.Clear();
            foreach (Location item in currentLocation.exits)
            {
                exits.Items.Add(item.Name);
            }
            exits.SelectedIndex = 0;
            description.Text = currentLocation.Description;
            IHidingPlace place = currentLocation as IHidingPlace;
            if (place != null)
            {
                check.Text = place.HidingPlace + "をチェックせよ";
                check.Visible = true;
            }
            else
            {
                check.Visible = false;
            }
            if (currentLocation is IHasExteriorDoor)
            {
                goThroughTheDoor.Visible = true;
            }
            else
            {
                goThroughTheDoor.Visible = false;
            }
        }

        public void ResetGame(bool found)
        {
            if (found)
            {
                MessageBox.Show("私は" + moves + "回で見つけられた!");
                description.Text = "";
            }
            moves = 0;
            goHere.Visible = false;
            exits.Visible = false;
            goThroughTheDoor.Visible = false;
            check.Visible = false;
            hide.Visible = true;
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp19
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    interface IHasExteriorDoor
    {
        string DoorDescription { get; }
        Location DoorLocation { get; set; }
    }

    interface IHidingPlace
    {
        string HidingPlace { get; }
    }

    public class Room : Location
    {
        private string decoration;

        public Room(string name, string decoration) :
            base(name)
        {
            this.decoration = decoration;
        }
        public string Deoraction
        {
            get { return decoration; }
        }
        public override string Description
        {
            get { return base.Description + " " + decoration + "がみえます。"; }
        }
    }

    public class Outside : Location
    {
        private bool hot;

        public Outside(string name, bool hot) : base(name)
        {
            this.hot = hot;
        }
        public bool Hot
        {
            get { return hot; }
        }
        public override string Description
        {
            get
            {
                if (hot)
                {
                    return base.Description + " ここはとても暑い。";
                }
                return base.Description;
            }
        }
    }

    public class OutsideWithHidingPlace : Outside, IHidingPlace
    {
        private string hidingPlace;

        public OutsideWithHidingPlace(string name, bool hot,string hidingPlace) : 
            base(name, hot)
        {
            this.hidingPlace = hidingPlace;
        }

        public string HidingPlace
        {
            get { return hidingPlace; }
        }
    }

    public class RoomWithHidingPlace : Room, IHidingPlace
    {
        private string hidingPlace;

        public RoomWithHidingPlace(string name, string decoration, string hidingPlace) :
            base(name, decoration)
        {
            this.hidingPlace = hidingPlace;
        }

        public string HidingPlace
        {
            get { return hidingPlace; }
        }
    }

    public class OutsideWithDoor : Outside, IHasExteriorDoor
    {
        private string doorDescription;
        private Location doorLocation;

        public OutsideWithDoor(string name, bool hot, string doorDescription) :
            base(name, hot)
        {
            this.doorDescription = doorDescription;
        }
        public string DoorDescription
        {
            get { return doorDescription; }
        }
        public Location DoorLocation
        {
            get { return doorLocation; }
            set { doorLocation = value; }
        }
    }

    public class RoomWithDoor : RoomWithHidingPlace, IHasExteriorDoor
    {

        private string doorDescription;
        private Location doorLocation;

        public RoomWithDoor(string name, string description,
            string doorDescription, string hidingPlace) :
            base(name, description, hidingPlace)
        {
            this.doorDescription = doorDescription;
        }

        public string DoorDescription
        {
            get { return doorDescription; }
        }
        public Location DoorLocation
        {
            get { return doorLocation; }
            set { doorLocation = value; }
        }
    }

    public class Opponent
    {
        private Location myLocation;
        private Random random;

        public Opponent(Location myLocation)
        {
            this.myLocation = myLocation;
            this.random = new Random();
        }

        public void Move()
        {
            IHasExteriorDoor loc = myLocation as IHasExteriorDoor;
            if (loc != null)
            {
                if (random.Next(2) == 1)
                {
                    myLocation = loc.DoorLocation;
                }
            }
            while (true)
            {
                int i = random.Next(myLocation.exits.Length);
                myLocation = myLocation.exits[i];
                if (myLocation is IHidingPlace)
                {
                    break;
                }
            }
        }
        public bool Check(Location loc)
        {
            if (loc == myLocation)
            {
                return true;
            }
            return false;
        }
    }
}

0 コメント:

コメントを投稿

関連コンテンツ