当前位置: 编程技术>移动开发
本页文章导读:
▪请问一个五子棋电脑算法 请教一个五子棋电脑算法
第一篇文章,希望高人指点一二。 最近看一些五子棋的经典算法,发现以前自己写的算法算是最低效的了,从来没有想到用最后一个落盘的棋子为基准.........
▪ 银幕切换 屏幕切换
android切屏生命周期:http://marshal.easymorse.com/archives/2056禁止切屏时调用oncreate,onstop,onresume如果在androidmanifest.xml中加入配置android:configChanges="orientation|keyboardHidden|navigation当屏幕翻.........
▪ 替View绘制阴影 为View绘制阴影
关键代码预览:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 225.0)];
view.center = self.view.center;
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFl.........
[1]请问一个五子棋电脑算法
来源: 互联网 发布时间: 2014-02-18
请教一个五子棋电脑算法
第一篇文章,希望高人指点一二。
最近看一些五子棋的经典算法,发现以前自己写的算法算是最低效的了,从来没有想到用最后一个落盘的棋子为基准点来判断当前形势,实在是让我茅塞顿开,很佩服这些作者。
小弟是个新人,平时很喜欢javaeye上的一些前辈的文章,本人做开发小一年,都是一些很基本的项目,没什么高深的东西,这段时间闲来无事,分析了一下五子棋的电脑落子的算法。基本思想如下:
每个落子的点都是一个Point对象,对象属性包括X坐标,Y坐标。现在再添加两个属性(位置和权重),来表示该点上的形势,例如若是在这个点落子,可能在横向形成活三形势,那么位置属性就设置成横,权重就设置成活三的权重例如15,如果还能在竖向形成冲四,那么,这个点对象位置属性设置成 横竖,权重设置成 15+10(活三+冲四),依次来遍历一定范围内的所有空白点,分别为其设置位置属性和权重,最后在根据每个点的权重值来确定落点位置。
问题就出现了,权重值的设置应该考虑很多情况,在这里我能想到的如下(权重从高到低)
己方胜利
己方活三冲四
己方双四
己方双活三
对手冲四
对手活三
己方活三
己方活二
对手活二
还有什么双活二什么的就不考虑的。
这是个最基本的雏形,往后还要考虑猜测的方法,猜测如下:如果己方把棋子放到一个位置形成了活三,那么对手可能会去堵截,然后再根据堵截后的棋盘形势来判断下一个落子的地方,如果出现权重比以上分析的权重都大,例如出现己方胜利,己方活三冲四等,那么就在该点落子,若是没有,就可以按上述方法继续猜测,但不能过多的猜测,可能会出现反应迟钝。
基本就是这样了。
希望各位前辈给予指导批评。
第一篇文章,希望高人指点一二。
最近看一些五子棋的经典算法,发现以前自己写的算法算是最低效的了,从来没有想到用最后一个落盘的棋子为基准点来判断当前形势,实在是让我茅塞顿开,很佩服这些作者。
小弟是个新人,平时很喜欢javaeye上的一些前辈的文章,本人做开发小一年,都是一些很基本的项目,没什么高深的东西,这段时间闲来无事,分析了一下五子棋的电脑落子的算法。基本思想如下:
每个落子的点都是一个Point对象,对象属性包括X坐标,Y坐标。现在再添加两个属性(位置和权重),来表示该点上的形势,例如若是在这个点落子,可能在横向形成活三形势,那么位置属性就设置成横,权重就设置成活三的权重例如15,如果还能在竖向形成冲四,那么,这个点对象位置属性设置成 横竖,权重设置成 15+10(活三+冲四),依次来遍历一定范围内的所有空白点,分别为其设置位置属性和权重,最后在根据每个点的权重值来确定落点位置。
问题就出现了,权重值的设置应该考虑很多情况,在这里我能想到的如下(权重从高到低)
己方胜利
己方活三冲四
己方双四
己方双活三
对手冲四
对手活三
己方活三
己方活二
对手活二
还有什么双活二什么的就不考虑的。
这是个最基本的雏形,往后还要考虑猜测的方法,猜测如下:如果己方把棋子放到一个位置形成了活三,那么对手可能会去堵截,然后再根据堵截后的棋盘形势来判断下一个落子的地方,如果出现权重比以上分析的权重都大,例如出现己方胜利,己方活三冲四等,那么就在该点落子,若是没有,就可以按上述方法继续猜测,但不能过多的猜测,可能会出现反应迟钝。
基本就是这样了。
希望各位前辈给予指导批评。
[2] 银幕切换
来源: 互联网 发布时间: 2014-02-18
屏幕切换
android切屏生命周期:
http://marshal.easymorse.com/archives/2056
禁止切屏时调用oncreate,onstop,onresume
如果在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
当屏幕翻转时,Activity就不会重复的调用onCreate()、onPause()和onResume().
而是调用onConfigurationChanged(Configuration newConfig)
http://stulog.com/?post=214
在android官方文档中指出另一种保存数据的方法:
The Activity class has a special method called onRetainNonConfigurationInstance(). This method can be used to pass an arbitrary object your future self and Android is smart enough to call this method only when needed. In the case of Photostream, the application used this method to pass the downloaded images to the future activity on orientation change. The implementation can be summarized like so:
In the new activity, in onCreate(), all you have to do to get your object back is to call getLastNonConfigurationInstance(). In Photostream, this method is invoked and if the returned value is not null, the grid is loaded with the list of photos from the previous activity:
Be very careful with the object you pass through onRetainNonConfigurationChange(), though. If the object you pass is for some reason tied to the Activity/Context, you will leak all the views and resources of the activity. This means you should never pass a View, a Drawable, an Adapter, etc. Photostream for instance extracts the bitmaps from the drawables and pass the bitmaps only, not the drawables. Finally, remember that onRetainNonConfigurationChange() should be used only to retain data that is expensive to load. Otherwise, keep it simple and let Android do everything.
android切屏生命周期:
http://marshal.easymorse.com/archives/2056
禁止切屏时调用oncreate,onstop,onresume
如果在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
当屏幕翻转时,Activity就不会重复的调用onCreate()、onPause()和onResume().
而是调用onConfigurationChanged(Configuration newConfig)
http://stulog.com/?post=214
在android官方文档中指出另一种保存数据的方法:
The Activity class has a special method called onRetainNonConfigurationInstance(). This method can be used to pass an arbitrary object your future self and Android is smart enough to call this method only when needed. In the case of Photostream, the application used this method to pass the downloaded images to the future activity on orientation change. The implementation can be summarized like so:
@Override public Object onRetainNonConfigurationInstance() { final LoadedPhoto[] list = new LoadedPhoto[numberOfPhotos]; keepPhotos(list); return list; }
In the new activity, in onCreate(), all you have to do to get your object back is to call getLastNonConfigurationInstance(). In Photostream, this method is invoked and if the returned value is not null, the grid is loaded with the list of photos from the previous activity:
private void loadPhotos() { final Object data = getLastNonConfigurationInstance(); // The activity is starting for the first time, load the photos from Flickr if (data == null) { mTask = new GetPhotoListTask().execute(mCurrentPage); } else { // The activity was destroyed/created automatically, populate the grid // of photos with the images loaded by the previous activity final LoadedPhoto[] photos = (LoadedPhoto[]) data; for (LoadedPhoto photo : photos) { addPhoto(photo); } } }
Be very careful with the object you pass through onRetainNonConfigurationChange(), though. If the object you pass is for some reason tied to the Activity/Context, you will leak all the views and resources of the activity. This means you should never pass a View, a Drawable, an Adapter, etc. Photostream for instance extracts the bitmaps from the drawables and pass the bitmaps only, not the drawables. Finally, remember that onRetainNonConfigurationChange() should be used only to retain data that is expensive to load. Otherwise, keep it simple and let Android do everything.
[3] 替View绘制阴影
来源: 互联网 发布时间: 2014-02-18
为View绘制阴影
关键代码预览:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 225.0)]; view.center = self.view.center; view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; view.layer.contents = (id)[UIImage imageNamed:@"photo.jpeg"].CGImage; view.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor; view.layer.borderWidth = 5.0; view.layer.shadowOffset = CGSizeMake(0, 3); view.layer.shadowOpacity = 0.7; view.layer.shouldRasterize = YES; // shadow UIBezierPath *path = [UIBezierPath bezierPath]; CGPoint topLeft = view.bounds.origin; CGPoint bottomLeft = CGPointMake(0.0, CGRectGetHeight(view.bounds) + 10); CGPoint bottomMiddle = CGPointMake(CGRectGetWidth(view.bounds) / 2, CGRectGetHeight(view.bounds) - 5); CGPoint bottomRight = CGPointMake(CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds) + 10); CGPoint topRight = CGPointMake(CGRectGetWidth(view.bounds), 0.0); [path moveToPoint:topLeft]; [path addLineToPoint:bottomLeft]; [path addQuadCurveToPoint:bottomRight controlPoint:bottomMiddle]; [path addLineToPoint:topRight]; [path addLineToPoint:topLeft]; [path closePath]; view.layer.shadowPath = path.CGPath; [self.view addSubview:view];
最新技术文章: