2048不多说,直接上代码
特别鸣谢王跃坤大佬,不到三分钟直接就解决了重复三次的问题,牛逼牛逼
package my2048;
import java.util.*;
public class this2048 {
public static void main(String[] args) throws Exception{
int a[][] = new int [4][4];
int b[][] = new int [4][4];
int bool1 = 0,bool = 0,booll = 0;
Scanner sc = new Scanner(System.in);
a[1][1] = 1;
a[2][1] = 1;
a[1][2] = 1;
a[2][2] = 1;
a[1][3] = 1;
a[2][3] = 1;
a[3][0] = 3;
//起初地图创建,可以的话,能弄个随机生成的
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
b[i][j] = a[i][j];
}
}
//隐藏地图,用于做检索是否无法继续游戏用
char oper;
for (int r = 0; r < 99999; r++) {
bool = 0;
bool1 = 0;
System.out.println("输入 方向 w s a d");
// oper = (char)System.in.read();
oper=sc.nextLine().charAt(0);
System.out.println("次数为" + r);
switch (oper) {
case 'a':
for (int n = 0; n < 3; n++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[i][j] == 0) {
a[i][j] = a[i][j+1];
a[i][j+1] = 0;
}else if (a[i][j] == a[i][j+1]) {
a[i][j] ++;
a[i][j+1] = 0;
}
}
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 2; j < a.length; j++) {
if (r % 2 == 0) {
if (a[1][3] == 0 ) {
a[1][3]++;
}
if (a[3][2] == 0) {
a[3][2]++;
}
}
if (r % 3 == 0) {
if (a[2][2] == 0) {
a[2][2] = 2;
}
}
if (r > 30) {
if (a[i][3] == 0) {
a[i][3]++;
}
}
}
}
break;
//向左移动时的加成算法与生成算法,下面都是同理,全是这两部分构成
case 's':
for (int n = 0; n < 3; n++) {
for (int i = 3; i > 0; i--) {
for (int j = 0; j < a.length; j++) {
if (a[i][j] == 0) {
a[i][j] = a[i-1][j];
a[i-1][j] = 0;
}else if (a[i][j] == a[i-1][j]) {
a[i][j] ++;
a[i-1][j] = 0;
}
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < a.length; j++) {
if (r % 2 == 0) {
if (a[0][0] == 0) {
a[0][0]++;
}
if (a[0][3] == 0) {
a[0][3]++;
}
}
if (r % 4 == 0) {
if (a[1][2] == 0) {
a[1][2]++;
}
}
if (r > 40) {
if (a[0][j] == 0) {
a[0][j] = 2;
}
}
}
}
break;
case 'w':
for (int n = 0; n < 3; n++) {
for (int i = 0; i < a.length -1; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i][j] == 0) {
a[i][j] = a[i+1][j];
a[i+1][j] = 0;
}else if(a[i][j] == a[i+1][j]) {
a[i][j] ++;
a[i+1][j] = 0;
}
}
}
}
for (int i = 2; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (r % 2 == 0) {
if (a[2][2] == 0) {
a[2][2]++;
}
if (a[3][0] == 0) {
a[3][0]++;
}
}
if (r % 4 == 0) {
if (a[3][2] == 0) {
a[3][2] = 2;
}
}
if (r > 25) {
if (a[3][j] == 0) {
a[3][j]++;
}
}
}
}
break;
case 'd':
for (int n = 0; n < 3; n++) {
for (int i = 0; i < a.length; i++) {
for (int j = 3; j > 0; j--) {
if (a[i][j] == 0) {
a[i][j] = a[i][j-1];
a[i][j-1] = 0;
}else if(a[i][j] == a[i][j-1]) {
a[i][j] ++;
a[i][j-1] = 0;
}
}
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 3; j > 1; j--) {
if (r % 2 == 0) {
if (a[0][1] == 0) {
a[0][1]++;
}
}
if (r % 4 == 0) {
if (a[1][3] == 0) {
a[1][3] = 2;
}
}
}
}
break;
default:
continue;
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
switch (a[i][j]) {
case 0:
System.out.print("---- ");
break;
case 1:
System.out.print("---2 ");
break;
case 2:
System.out.print("---4 ");
break;
case 3:
System.out.print("---8 ");
break;
case 4:
System.out.print("--16 ");
break;
case 5:
System.out.print("--32");
break;
case 6:
System.out.print("--64");
break;
case 7:
System.out.print("-128");
break;
case 8:
System.out.print("-256");
break;
case 9:
System.out.print("-512");
break;
case 10:
System.out.print("1024");
break;
case 11:
System.out.print("2048");
booll++;
break;
default:
break;
}
}
System.out.println("\n\n\n");
}
//生成地图,并且检索是否游戏胜利
if (booll >= 1) {
System.out.println("达到2048,游戏成功");
r = r + 99999;
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i][j] == 0) {
bool1++;
}
}
}
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (a[i][j] != b[i][j]) {
bool++;
}
}
}
if (bool == 0 && bool1 == 0) {
System.out.println("动不了了,游戏失败");
r = r + 99999;
}else {
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
b[i][j] = a[i][j];
}
}
}
//游戏继续下一个循环
}
}
}