首页 > 代码库 > 基于C#开发的2048

基于C#开发的2048

昨天花了一晚上的时间写了一个过气的小游戏-2048,一直到晚上十二点多才把键盘响应事件写好,感觉自己的脑子不行,还要再补补。

先来一波运行的效果:

技术分享

  1 主要算法,可以实现不同边长的2048
  2    private void Up()
  3         {
  4             int i, j;
  5             for (j = 0; j < gameHeight; j++)
  6             {//每一列   
  7                 for (i = 0; i < gameWidth - 1; i++)
  8                 {
  9                     if (game[i, j].Value =http://www.mamicode.com/= game[i + 1, j].Value)
 10                     {
 11                         game[i, j].Value = http://www.mamicode.com/game[i, j].Value + game[i + 1, j].Value;
 12                         game[i + 1, j].Value = http://www.mamicode.com/0;
 13                         //移除空格  
 14                         Up_Remove_Blank();
 15                     }
 16 
 17                 }
 18             }
 19         }
 20         private void Up_Remove_Blank()
 21         {
 22             int i, j, k;
 23             for (j = 0; j < gameHeight; j++)
 24             {
 25                 for (i = 1; i < gameWidth; i++)
 26                 {
 27                     k = i;
 28                     while (k - 1 >= 0 && game[k - 1, j].Value =http://www.mamicode.com/= 0)
 29                     {//上面的那个为空   
 30                         int temp;
 31                         temp = game[k, j].Value;
 32                         game[k, j].Value = http://www.mamicode.com/game[k - 1, j].Value;
 33                         game[k - 1, j].Value =http://www.mamicode.com/ temp;
 34                         k--;
 35 
 36                     }
 37                 }
 38             }
 39         }
 40         private void Down()
 41         {
 42             int i, j;
 43             for (j = 0; j < gameHeight; j++)
 44             {//每一列   
 45                 for (i = gameWidth - 1; i >= 1; i--)
 46                 {
 47                     if (game[i, j].Value =http://www.mamicode.com/= game[i - 1, j].Value)
 48                     {
 49                         game[i, j].Value = http://www.mamicode.com/game[i, j].Value + game[i - 1, j].Value;
 50                         game[i - 1, j].Value = http://www.mamicode.com/0;
 51                         //移除空格  
 52                         Down_Remove_Blank();
 53                     }
 54 
 55                 }
 56             }
 57         }
 58         private void Down_Remove_Blank()
 59         {
 60             int i, j, k;
 61             for (j = 0; j < gameHeight; j++)
 62             {
 63                 for (i = gameWidth - 2; i >= 0; i--)
 64                 {
 65                     k = i;
 66                     while (k + 1 <= gameWidth - 1 && game[k + 1, j].Value =http://www.mamicode.com/= 0)
 67                     {//上面的那个为空   
 68                         int temp;
 69                         temp = game[k, j].Value;
 70                         game[k, j].Value = http://www.mamicode.com/game[k + 1, j].Value;
 71                         game[k + 1, j].Value =http://www.mamicode.com/ temp;
 72                         k++;
 73                     }
 74                 }
 75             }
 76         }
 77         private void Left()
 78         {
 79             int i, j;
 80             for (i = 0; i < gameWidth; i++)
 81             {
 82                 for (j = 0; j < gameHeight - 1; j++)
 83                 {
 84                     if (game[i, j].Value =http://www.mamicode.com/= game[i, j + 1].Value)
 85                     {
 86                         game[i, j].Value += game[i, j + 1].Value;
 87                         game[i, j + 1].Value = http://www.mamicode.com/0;
 88                         Left_Remove_Blank();
 89                     }
 90 
 91                 }
 92             }
 93         }
 94         public void Left_Remove_Blank()
 95         {
 96             int i, j, k;
 97             for (i = 0; i < gameWidth; i++)
 98             {
 99                 for (j = 1; j < gameHeight; j++)
100                 {
101                     k = j;
102                     while (k - 1 >= 0 && game[i, k - 1].Value =http://www.mamicode.com/= 0)
103                     {//上面的那个为空   
104                         int temp;
105                         temp = game[i, k].Value;
106                         game[i, k].Value = http://www.mamicode.com/game[i, k - 1].Value;
107                         game[i, k - 1].Value =http://www.mamicode.com/ temp;
108                         k--;
109                     }
110                 }
111             }
112         }
113         private void Right()
114         {
115             int i, j;
116             for (i = 0; i < gameWidth; i++)
117             {
118                 for (j = gameHeight - 1; j >= 1; j--)
119                 {
120                     if (game[i, j].Value =http://www.mamicode.com/= game[i, j - 1].Value)
121                     {
122                         game[i, j].Value += game[i, j - 1].Value;
123                         game[i, j - 1].Value = http://www.mamicode.com/0;
124                         Right_Remove_Blank();
125                     }
126                 }
127             }
128         }
129 
130         private void Right_Remove_Blank()
131         {
132             int i, j, k;
133             for (i = 0; i < gameWidth; i++)
134             {
135                 for (j = gameHeight - 2; j >= 0; j--)
136                 {
137                     k = j;
138                     while (k + 1 <= gameHeight - 1 && game[i, k + 1].Value =http://www.mamicode.com/= 0)
139                     {//上面的那个为空  
140                         int temp;
141                         temp = game[i, k].Value;
142                         game[i, k].Value = http://www.mamicode.com/game[i, k + 1].Value;
143                         game[i, k + 1].Value =http://www.mamicode.com/ temp;
144                         k++;
145                     }
146                 }
147             }
148         }

附上源码:链接:http://pan.baidu.com/s/1skOKdR3 密码:2p3h

基于C#开发的2048