宝石迷阵位主板采用重力 [英] Bejeweled bit board applying gravity

查看:125
本文介绍了宝石迷阵位主板采用重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个宝石迷阵级联模拟器位板。到目前为止,我已经能够检测和清除的比赛,但现在我需要有珠宝掉下来。我的状态被重新由位板,一个用于每个类型的宝石的列表psented $ P $。我有所有被删除的珠宝的面具。

是否有可能使用一些位魔法来做到这一点?

两个初始位板(让我们姑且认为只有两种类型的宝石,它是一个4×4板,而不是8×8)为例。第一位是左下方,第四位为左上角,而最后一位是右上

  0 0 1 1 1 1 0 0
1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0
0 0 1 0 1 1 0 1
 

在删除匹配:

  0 0 1 1 1 1 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 1 0 1 1 0 1
 

中使用的面具是:

  0 0 0 0
0 1 1 1
1 1 1 1
0 0 0 0
 

和严重性后,它应该是这样的:

  0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
1 0 1 1 0 1 0 0
0 0 1 0 1 1 0 1
 

这是与整数实现,具体的步骤如下:

  [43814,21721]#初始状态
[35076,4249],比赛已被删除,屏蔽用来清除匹配26210#国后
[8962,4149]重力之后#国已应用
 

解决方案

让我们把左边的宝石板, A ;右, B ;和电路板的物理重新presentation, AB
该清除后,我们有:

  0 0 1 1 1 1 0 0 1111
AB = A | B = 1 0 0 0或者0 0 0 0 = 1000
               0 0 0 0 0 0 0 0 0000
               0 0 1 0 1 1 0 1 1111
 

算法:

 对于每一行(R,临时变量)的最低一行清除上面:
  对于每种宝石类型:
    开始清除发生时的最低行(AB_row)
    而r是不为零
      使AB_row的临时副本(AB_row_copy)
      为jewel_type新行:=(行| AB_row)^ same_row_for_other_jewel_types
      R:= R&安培; AB_row_copy
      上升到AB的下一行
 

例如:

更新最下面一行与清除上面的第一行:

 #AB_row最低行从棋盘清除,结合了所有
#宝石类型; R_A是从A副本棋盘上方AB_row的第一行
R_A = 1 0 0 0,AB_row = 0 0 0 0

  #使AB_row副本
  AB_row_copy = 0 0 0 0

  #计算宝石A型新行
  #为jewel_type新行:=(行| AB_row)^ same_row_for_other_jewel_types
  (| 0 0 0 0 1 0 0 0)^ 0 0 0 0 = 1 0 0 0,A =新行

  #更新堕落位来自R_A
  #R:= R&安培; AB_row_copy
  R_A = 1 0 0 0&安培; 0 0 0 0 = 0 0 0 0

#r_B在此行是零,没有任何关系。
r_B = 0 0 0 0
 

更新最下面一行与清除上面的第二行:

 #行A具有相同的过程同上
R_A = 0 0 1 1,AB_row = 1 0 0 0 // AB_row是最低的行与清除
  AB_row_copy = 1 0 0 0
  (| 1 0 0 0 0 0 1 1)^ 0 0 0 0 = 1 0 1 1 A =新行
  R_A = 0 0 1 1&安培; 1 0 0 0 = 0 0 0 0


#AB_row最低行从棋盘清除,结合了所有
#宝石类型; r_B是从上方AB_row第二行中的B位棋盘副本
r_B = 1 1 0 0,AB_row = 1 0 1 1

  #使AB_row副本
  AB_row_copy = 1 0 1 1

  #计算宝石B型的新行
  #为jewel_type新行:=(行| AB_row)^ same_row_for_other_jewel_types
  为乙新行=(1 1 0 0 | 1 0 1 1)^ 1 0 1 1 = 0 1 0 0

  #更新堕落位来自r_B
  #R:= R&安培; AB_row_copy
  r_B = 1 1 0 0&安培; 1 0 1 1 = 1 0 0 0

#因为还有剩余r_B设置位取出倒下后
#位,我们将继续与r_B,进入到下一行了。

#AB_row现在是下一行从最下面一行与清除,再从
#在棋盘结合所有的宝石类型; r_B是同一个变量,现在
#一组少一点(一个堕落位)
r_B = 1 0 0 0,AB_row = 0 0 0 0

  #使AB_row副本
  AB_row_copy = 0 0 0 0

  #计算宝石B型的新行
  #为jewel_type新行:=(行| AB_row)^ same_row_for_other_jewel_types
  对B新行=(1 0 0 0 | 0 0 0 0)^ 0 0 0 0 = 1 0 0 0

  #更新堕落位来自r_B
  r_B = 1 0 0 0&安培; 0 0 0 0 = 0 0 0 0

  #r_B现在零,因此while循环被终止
 

I am attempting to make a Bejeweled cascade simulator with bit boards. So far, I have been able to detect and remove the matches, but now I need to have the jewels fall down. My state is represented by a list of bit boards, one for each type of jewel. I have a mask of all the jewels that are removed.

Is it possible to use some bitwise magic to do this?

Example of two initial bit boards (let's just assume that there are only two types of jewels and that it's a 4x4 board instead of 8x8). The first bit is the bottom left, the fourth bit is top left, and the last bit is top right.

0 0 1 1    1 1 0 0
1 0 0 0    0 1 1 1
1 1 1 1    0 0 0 0
0 0 1 0    1 1 0 1

After removing matches:

0 0 1 1    1 1 0 0
1 0 0 0    0 0 0 0
0 0 0 0    0 0 0 0
0 0 1 0    1 1 0 1

The mask used is:

0 0 0 0
0 1 1 1
1 1 1 1
0 0 0 0

And after gravity it should look like:

0 0 0 0    0 0 0 0
0 0 0 0    1 0 0 0
1 0 1 1    0 1 0 0
0 0 1 0    1 1 0 1

This is implemented with integers, and the steps would look like:

[43814, 21721]       # Initial state
[35076, 4249], 26210 # State after matches have been removed, mask used to remove matches
[8962, 4149]         # State after gravity has been applied

解决方案

Let's call the left jewel board, A; the right, B; and the physical representation of the board, AB.
After the removals, we have:

               0 0 1 1      1 1 0 0     1111
AB =  A | B =  1 0 0 0  or  0 0 0 0  =  1000
               0 0 0 0      0 0 0 0     0000
               0 0 1 0      1 1 0 1     1111

Algorithm:

For each row (r, a temporary variable) above the lowest row with removals:
  For each jewel type:
    starting with the lowest row where removals occurred (AB_row)
    While r is not zero
      make a temporary copy of AB_row (AB_row_copy)
      new row for jewel_type := (row | AB_row) ^ same_row_for_other_jewel_types
      r := r & AB_row_copy
      ascend to next row of AB

Example:

Update the first row above the lowest row with removals:

# AB_row is the lowest row with removals from the bitboard that combines all
# jewel types; r_A is a copy from the A bitboard of the first row above AB_row 
r_A = 1 0 0 0, AB_row = 0 0 0 0 

  # make a copy of AB_row
  AB_row_copy = 0 0 0 0

  # calculate the new row for jewel type A
  # new row for jewel_type := (row | AB_row) ^ same_row_for_other_jewel_types
  new row for A = (1 0 0 0 | 0 0 0 0) ^ 0 0 0 0 = 1 0 0 0

  # update the fallen bits from r_A
  # r := r & AB_row_copy
  r_A = 1 0 0 0 & 0 0 0 0 = 0 0 0 0

# r_B at this row is zero, nothing to do.
r_B = 0 0 0 0

Update the second row above the lowest row with removals:

# row for A has the same process same as above
r_A = 0 0 1 1, AB_row = 1 0 0 0 // AB_row is the lowest row with removals
  AB_row_copy = 1 0 0 0
  new row for A = (0 0 1 1 | 1 0 0 0) ^ 0 0 0 0 = 1 0 1 1
  r_A = 0 0 1 1 & 1 0 0 0 = 0 0 0 0


# AB_row is the lowest row with removals from the bitboard that combines all
# jewel types; r_B is a copy from the B bitboard of the second row above AB_row
r_B = 1 1 0 0, AB_row = 1 0 1 1

  # make a copy of AB_row
  AB_row_copy = 1 0 1 1

  # calculate the new row for jewel type B
  # new row for jewel_type := (row | AB_row) ^ same_row_for_other_jewel_types
  new row for B = (1 1 0 0 | 1 0 1 1) ^ 1 0 1 1 = 0 1 0 0

  # update the fallen bits from r_B
  # r := r & AB_row_copy
  r_B = 1 1 0 0 & 1 0 1 1 = 1 0 0 0

# since there are still set bits remaining in r_B after removing the fallen 
# bit, we continue with r_B, proceeding to the next row up.

# AB_row now is the next row up from the lowest row with removals, again from
# the bitboard combining all jewel types; r_B is the same variable, now with 
# one set bit less (one "fallen" bit)
r_B = 1 0 0 0, AB_row = 0 0 0 0

  # make a copy of AB_row
  AB_row_copy = 0 0 0 0

  # calculate the new row for jewel type B
  # new row for jewel_type := (row | AB_row) ^ same_row_for_other_jewel_types
  new row for B = (1 0 0 0 | 0 0 0 0) ^ 0 0 0 0 = 1 0 0 0

  # update the fallen bits from r_B
  r_B = 1 0 0 0 & 0 0 0 0 = 0 0 0 0

  #r_B is now zero so the while loop is terminated

这篇关于宝石迷阵位主板采用重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆