当前位置: 移动技术网 > IT编程>开发语言>.net > .NET实现魔方游戏(一)之任意阶魔方的表示

.NET实现魔方游戏(一)之任意阶魔方的表示

2017年12月12日  | 移动技术网IT编程  | 我要评论

宋祖德近况,女子无殇19楼,北京杂谈

第一节 魔方的简单表示

  对于任意n阶的魔方均有六个面(surface),每个面有n*n个方块。在面向对象的程序设计里,我们可以把魔方(cube)、魔方的面(surface)和面的方块(block)均视作为对象。

  魔方的定义:六个面存储在一个数组

''' <summary>
''' 表示一个指定阶的魔方
''' </summary>
public class cubeclass
''' <summary>
''' 魔方阶数
''' </summary>
public cuberank as integer
''' <summary>
''' 魔方的六个表面
''' </summary>
public surfacearray(5) as cubesurfaceclass
end class 

  魔方的面定义:方块存储为n*n的二维数组

''' <summary>
''' 表示一个魔方的面
''' </summary>
public class cubesurfaceclass
''' <summary>
''' 魔方表层的块数据
''' </summary>
public blockdata(,) as cubeblockclass
end class

  魔方的块定义:每个块具有独立的颜色

''' <summary>
''' 表示魔方面上的一个方块
''' </summary>
public class cubeblockclass
''' <summary>
''' 当前块的颜色
''' </summary>
public blockcolor as color
public x as integer '所在列数
public y as integer '所在行数
end class

  上述,我们完成了魔方类的简单定义,并且确立了之间的从属关系。这里有一点需要注意,方块(blockclass)是指魔方的单个颜色块。

  所以n阶魔方应有6*n*n个颜色块,以三阶魔方为例,它应有54个颜色块(blockclass)。

--------------------------------------------------------------------------------

第二节 面之间的空间关系

  魔方的六个面之间并非独立的,而是存在一定的空间关系。前面在cubeclass中定义了surfacearray()表示魔方的六个面,现在索引0~5分别指示魔方的顶层,底层,左侧,右侧,前方,后方六个面。

  surfacearray(0):顶层

  surfacearray(1):底层

  surfacearray(2):左侧

  surfacearray(3):右侧

  surfacearray(4):前方

  surfacearray(5):后方

图2.1 魔方六面的空间位置

  由此确定了各个面的空间方位,给surfaceclass添加如下定义:

''' <summary>
''' 当前表层的相邻表层(顶、底、左、右、前和后)
''' </summary>

public neiboursurface(5) as cubesurfaceclass

  neiboursurface()的索引从0~5依次指示当前面的顶、底、左、右、前和后。以图2.1“右面”为例,它的neiboursurface()

应该为

  neiboursurface(0):顶层

  neiboursurface(1):底层

  neiboursurface(2):前方

  neiboursurface(3):后方

  neiboursurface(4):右侧,每一个面的“前”就是它自身

  neiboursurface(5):左侧

  但上述是默认为"右面"的"上方"就是顶层。所以我们还需要

对每一个面的“上方”进行严格的定义:


图2.2 魔方六面的空间位置_平面展开


图2.3 魔方六面"上方"的方向

  由上,我们可以确定各面之间的空间关系:

dim temparray(,) as integer = {{2, 3, 4, 5, 0, 1},
{3, 2, 4, 5, 1, 0},
{1, 0, 4, 5, 2, 3},
{0, 1, 4, 5, 3, 2},
{0, 1, 2, 3, 4, 5},
{0, 1, 3, 2, 5, 4}} '空间相邻关系矩阵 

  该矩阵行数值指某个面在surfacearray()中的索引,列数表示这个面相邻的面在surfacearray()的索引。前面提到每一个面的“前”就是它自身,矩阵的第五列从0依次到5。

  在cubeclass类中添加如下方法,并在构造函数中调用:

''' <summary>
'''初始化各个表层间的空间相邻关系
''' </summary>
public sub initsurface()
dim temparray(,) as integer = {{2, 3, 4, 5, 0, 1},
{3, 2, 4, 5, 1, 0},
{1, 0, 4, 5, 2, 3},
{0, 1, 4, 5, 3, 2},
{0, 1, 2, 3, 4, 5},
{0, 1, 3, 2, 5, 4}} '空间相邻关系矩阵
for i = 0 to 5
for j = 0 to 5
surfacearray(i).neiboursurface(j) = surfacearray(temparray(i, j))
next
next
end sub 

--------------------------------------------------------------------------------

第三节 魔方的初始化

  魔方(cubeclass)的构造函数:六面的颜色标准:顶-白色,底-黄色,左-橙色,右-红色,前-绿色,后-蓝色

''' <summary>
''' 新建一个指定阶的魔方
''' </summary>
''' <param name="nrank">指定的阶数</param>
public sub new(byval nrank as integer)
dim colorarr() as color = {color.white, color.yellow, color.orange, color.red, color.green, color.blue}
for i = 0 to 5
surfacearray(i) = new cubesurfaceclass(nrank, i, colorarr(i))
next
cuberank = nrank
initsurface()
end sub 

  魔方面(cubesurfaceclass)的构造函数:

''' <summary>
''' 当前魔方的阶数
''' </summary>
public cuberank as integer
''' <summary>
''' 魔方表层的数据
''' </summary>
public blockdata(,) as cubeblockclass
''' <summary>
''' 当前表层的相邻表层(顶、底、左、右、前和后)
''' </summary>
public neiboursurface(5) as cubesurfaceclass
public index as integer
public sub new(nrank as integer, nindex as integer, ncolor as color)
redim blockdata(nrank - 1, nrank - 1)
cuberank = nrank
index = nindex
dim rnd as new random
for i = 0 to nrank - 1
for j = 0 to nrank - 1
blockdata(i, j) = new cubeblockclass(i, j)
blockdata(i, j).parentindex = index
blockdata(i, j).blockcolor = ncolor
next
next
end sub 

  魔方块(cubeblockclass)的构造函数:

public parentindex as integer
''' <summary>
''' 当前块的颜色
''' </summary>
public blockcolor as color
public x as integer '所在列数
public y as integer '所在行数
public sub new(nx as integer, ny as integer)
x = nx
y = ny
end sub 

  至此,一个任意阶的魔方已可以被表示,并且在此基础上将可以实现魔方的扭动逻辑。

以上所述是针对.net实现魔方游戏(一)之任意阶魔方的表示,希望对大家有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网