凑热闹-"一晚上的游戏"之WPF版

zyntl 2009-12-07 10:00:33
源代码下载http://download.csdn.net/source/1881645
用MVVM模式写成的。
感觉难点在于搜索路径,这里是自己的算法,主要是加权了一下
int SearchPath(ObservableCollection<ButtonCommandViewModel> coll, int mouseIndex)
{
int resultIndex = -1;
double resultSteps = -1;
int leftIndex = -1, topIndex = -1, rightIndex = -1, bottomIndex = -1;
double leftSteps = -1, topSteps = -1, rightSteps = -1, bottomSteps = -1;
if (coll[mouseIndex - 1].ButtonState != IsShaded)//左
{
leftIndex = mouseIndex - 1;
leftSteps = coll[mouseIndex].IndexColumn;

int j = coll[mouseIndex].IndexColumn;
double weights = 1;//权值
for (int i = j - 1; i >= 0; i--)
{
int diff = j - i;
if (coll[mouseIndex - diff].ButtonState == IsShaded)
{
weights += 1.0 / diff;
}
}

//leftSteps *= weights;
for (int n = 0; n < Rows; n++)
leftSteps *= weights;
resultIndex = leftIndex;
resultSteps = leftSteps;
}
if (coll[mouseIndex - Columns].ButtonState != IsShaded)//上
{
topIndex = mouseIndex - Columns;
topSteps = coll[mouseIndex].IndexRow;

int j = coll[mouseIndex].IndexRow;
double weights = 1;//权值
for (int i = j - 1; i >= 0; i--)
{
int diff = j - i;
if (coll[mouseIndex - diff * Rows].ButtonState == IsShaded)
{
weights += 1.0 / (double)diff;
}
}
for (int n = 0; n < Rows; n++)
topSteps *= weights;

if (resultIndex < 0)
{
resultIndex = topIndex;
resultSteps = topSteps;
}
else
{
if (resultSteps > topSteps)
{
resultIndex = topIndex;
resultSteps = topSteps;
}
}
}
if (coll[mouseIndex + 1].ButtonState != IsShaded)//右
{
rightIndex = mouseIndex + 1;
rightSteps = Columns - 1 - coll[mouseIndex].IndexColumn;

int j = coll[mouseIndex].IndexColumn;
double weights = 1;//权值
for (int i = j + 1; i < Columns; i++)
{
int diff = i - j;
if (coll[mouseIndex + diff].ButtonState == IsShaded)
{
weights += 1.0 / (double)diff;
}
}
for (int n = 0; n < Rows; n++)
rightSteps *= weights;

if (resultIndex < 0)
{
resultIndex = rightIndex;
resultSteps = rightSteps;
}
else
{
if (resultSteps > rightSteps)
{
resultIndex = rightIndex;
resultSteps = rightSteps;
}
}
}
if (coll[mouseIndex + Columns].ButtonState != IsShaded)//下
{
bottomIndex = mouseIndex + Columns;
bottomSteps = Rows - 1 - coll[mouseIndex].IndexRow;

int j = coll[mouseIndex].IndexRow;
double weights = 1;//权值
for (int i = j + 1; i < Rows; i++)
{
int diff = i - j;
if (coll[mouseIndex + diff * Columns].ButtonState == IsShaded)
{
weights += 1.0 / (double)diff;
}
}
for (int n = 0; n < Rows; n++)
bottomSteps *= weights;

if (resultIndex < 0)
{
resultIndex = bottomIndex;
resultSteps = bottomSteps;
}
else
{
if (resultSteps > bottomSteps)
{
resultIndex = bottomIndex;
resultSteps = bottomSteps;
}
}
}
return resultIndex;

}
...全文
353 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsq628 2009-12-08
  • 打赏
  • 举报
回复
强大。。必须要顶,我是来接分的,哈哈
zyntl 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 hhc123 的回复:]
再怎么说也要顶一下
顺便给你点有用的东西
http://www.cnblogs.com/alamiye010
[/Quote]
呵呵,你的这些东西看过,很有想法
whuzx 2009-12-08
  • 打赏
  • 举报
回复
强大~~~UP
hhc123 2009-12-08
  • 打赏
  • 举报
回复
再怎么说也要顶一下
顺便给你点有用的东西
http://www.cnblogs.com/alamiye010
wartim 2009-12-08
  • 打赏
  • 举报
回复
这个游戏最近很热门啊。。。期待出现F#版本的
十八道胡同 2009-12-08
  • 打赏
  • 举报
回复
支持
huangyic1986 2009-12-08
  • 打赏
  • 举报
回复
学习....Up
ljp0223 2009-12-08
  • 打赏
  • 举报
回复
强烈支持
zyntl 2009-12-08
  • 打赏
  • 举报
回复
搜索算法不够智能,谁能提供个好的算法不
kkkkkkmn 2009-12-08
  • 打赏
  • 举报
回复
kankan
chengcheng1253 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 jaylianyu 的回复:]
呵呵,昨天刚见识了Js版的,现在c#版又出来啦!
[/Quote]
C#版? 何以见得
ddsxd19 2009-12-08
  • 打赏
  • 举报
回复
膜拜
biny237 2009-12-08
  • 打赏
  • 举报
回复
学习
biny101 2009-12-08
  • 打赏
  • 举报
回复
Up 学习
长腿爸爸 2009-12-08
  • 打赏
  • 举报
回复
呵呵,昨天刚见识了Js版的,现在c#版又出来啦!
数据之巅 2009-12-08
  • 打赏
  • 举报
回复
都是牛人。。。
mbh0210 2009-12-08
  • 打赏
  • 举报
回复
mark
liherun 2009-12-08
  • 打赏
  • 举报
回复
收藏
qiuqiuye 2009-12-08
  • 打赏
  • 举报
回复
看看
abc1_2_3 2009-12-08
  • 打赏
  • 举报
回复
mark
加载更多回复(13)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧