基本信息
源码名称:wpf 拼图游戏源码
源码大小:0.76M
文件格式:.rar
开发语言:C#
更新时间:2017-06-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace NineKong
{
    enum Direction
    {
        D_Up = 0,
        D_Down,
        D_Left,
        D_Right
    }
    //拼图面板VM
    class PicManager : INotifyPropertyChanged
    {
        public DataConverter converter { set; get; }
        public ObservableCollection<PicViewMod> Pics { set; get; }
        public event PropertyChangedEventHandler PropertyChanged;
        int _step;
        int _pos;//空白块当前位置
        int _colum, _row;//行列数
        public int Step
        {
            set
            {
                if (_step != value)
                {
                    _step = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Step"));
                }
            }

            get
            {
                return _step;
            }
        }
        public PicViewMod Ori { set; get; }//原图
        public PicManager(string path, int col, int row)
        {
            Pics = new ObservableCollection<PicViewMod>();
            _colum = col;
            _row = row;
            _pos = 0;
            _step = 0;
            Ori = new PicViewMod(col * row);
            for (int x = 0; x < _colum; x  )
            {
                for (int y = 0; y < _row; y  )
                {
                    Pics.Add(new PicViewMod(x * _row   y));
                }
            }
        }

        void ClipPic(string path)
        {
            //using (var fs = new FileStream(path, FileMode.Open))
            //{
            //    var image = new BitmapImage();
            //    image.BeginInit();
            //    image.CacheOption = BitmapCacheOption.OnLoad;
            //    image.StreamSource = fs;
            //    image.EndInit();
            //}
            //var pic = new BitmapImage(new Uri(path));
            //BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            //encoder.Frames.Add(BitmapFrame.Create(pic));
            //encoder.Save(new FileStream());

            Pics.Clear();
            for (int x = 0; x < _colum; x  )
            {
                for (int y = 0; y < _row; y  )
                {
                    Pics.Add(new PicViewMod(x * _row   y));
                }
            }
        }

        public void MovePic(Direction dir)
        {
            switch (dir)
            {
                case Direction.D_Up://键盘的向上,实际就是空白块的向下
                    {
                        if (_pos / _colum < _row - 1)
                        {
                            int id = Pics[_pos   _colum].ID;
                            Pics[_pos   _colum].ID = Pics[_pos].ID;
                            Pics[_pos].ID = id;
                            _pos  = _colum;
                            Step  ;
                        }
                    }
                    break;
                case Direction.D_Down:
                    {
                        if (_pos / _colum > 0)
                        {
                            int id = Pics[_pos - _colum].ID;
                            Pics[_pos - _colum].ID = Pics[_pos].ID;
                            Pics[_pos].ID = id;
                            _pos -= _colum;
                            Step  ;
                        }
                    }
                    break;
                case Direction.D_Left:
                    {
                        if (_pos % _colum < _colum - 1)
                        {
                            int id = Pics[_pos   1].ID;
                            Pics[_pos   1].ID = Pics[_pos].ID;
                            Pics[_pos].ID = id;
                            _pos  = 1;
                            Step  ;
                        }
                    }
                    break;
                case Direction.D_Right:
                    {
                        if (_pos % _colum > 0)
                        {
                            int id = Pics[_pos - 1].ID;
                            Pics[_pos - 1].ID = Pics[_pos].ID;
                            Pics[_pos].ID = id;
                            _pos -= 1;
                            Step  ;
                        }
                    }
                    break;
            }

        }
        public bool CheckOVER()
        {
            for (int i = 0; i < _row * _colum; i  )
            {
                if (Pics[i].ID != i)
                    return false;
            }
            return true;
        }
        public void Random(int count)
        {
            System.Random rand = new System.Random((int)DateTime.Now.Ticks);
            for (int i = 0; i < count; i  )
            {
                int d = rand.Next(0, 4);
                MovePic((Direction)d);
            }
            Step = 0;
        }
    }

}