昨天晚上咸的,想自己做个2048试试,折腾到半夜算是有点成果,分享下,由于搞的太晚也没来得及加注释,最后也没有优化重构,就简单看下思路吧
先看个拼图的小游戏消化下
import java.io.IOException;
/**
* 控制台玩拼图游戏
* @author c
* 码农扣扣群:2177712
*/
public class Puzzle {
static final int X = 3;
static final int Y = 3;
static int tar[][] = new int[X][Y];
static int BX,BY;
static int step = 0;
static void init(){
/*正常排序插入*/
int index = 0;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
tar[i][j] = index++;
}
}
/*打乱排序*/
for (int i = 0; i < tar.length; i++) {
for (int j = 0; j < tar[i].length; j++) {
int temp = tar[i][j];
int randomX, randomY;
randomX = (int) (Math.random()*X);
randomY = (int) (Math.random()*tar[randomX].length);
tar[i][j] = tar[randomX][randomY];
tar[randomX][randomY] = temp;
}
}
}
static void outPrint(){
boolean isok = true;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
if(tar[i][j] == 0){
System.out.print(" [囧]");
BX = i;BY = j;
}else{
if(tar[i][j]!=(i*(Y) + j + 1)){
isok = false;
}
System.out.print(" ["+(tar[i][j]<10?" "+tar[i][j]:tar[i][j])+"]");
}
}
System.out.println();
}
if(isok){
System.out.println("恭喜获胜!步数: "+step);
}else{
System.out.println("go on! 步数: "+step);
}
}
static void change(int dir){
switch (dir) {
case 115://S 下
if(BX != 0){
tar[BX][BY] = tar[BX-1][BY];
tar[BX-1][BY] = 0;
BX--;
step++;
outPrint();
}
break;
case 100://D 右
if(BY != 0){
tar[BX][BY] = tar[BX][BY-1];
tar[BX][BY-1] = 0;
BY--;
step++;
outPrint();
}
break;
case 119://W 上
if(BX != X-1){
tar[BX][BY] = tar[BX+1][BY];
tar[BX+1][BY] = 0;
BX++;
step++;
outPrint();
}
break;
case 97://A 左
if(BY != Y-1){
tar[BX][BY] = tar[BX][BY+1];
tar[BX][BY+1] = 0;
BY++;
step++;
outPrint();
}
break;
default:
break;
}
}
public static void main(String[] args) throws IOException {
init();
outPrint();
while(true){
int read = System.in.read();
change(read);
}
}
}
按W S A D 为上下左右,其实就是一个二维数组,来按方向改变相邻的位置
下面这个是2048的游戏,跟上面差不多,就是要相邻的加在一起,然后去掉空格的(值为0),看看吧,欢迎拍砖
import java.io.IOException;
import java.util.*;
/**
* Java 控制台 开发2048
* @author c
* 码农秋秋群->21/777/12
*
*/
public class Game2048 {
static final int X = 5;
static final int Y = 6;
static int model[][] = new int[X][Y];
static int step = 0;
static boolean gameover = false;
static int enumM[] = {2,2,2,2,4,4,4,8};
static void outPrint(){
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
System.out.print(" [");
if(model[i][j]==0){
System.out.print(" ");
}
if(model[i][j]>0&&model[i][j]<9){
System.out.print(" "+model[i][j]+" ");
}
if(model[i][j]>9&&model[i][j]<100){
System.out.print(" "+model[i][j]+" ");
}
if(model[i][j]>99&&model[i][j]<1000){
System.out.print(" "+model[i][j]);
}
if(model[i][j]>999){
System.out.print(model[i][j]);
}
System.out.print("]");
//+(model[i][j] == 0?" ":model[i][j])+"]");
}
System.out.println();
}
}
static void change(int dir){
switch (dir) {
case 115://S 下
int[] xp4 = new int[Y];
for (int i = 0; i < Y; i++) {
boolean goon = true;
while (goon) {
int[] temp = new int[X];
int tempIdex = X-1;
for (int j = X-1; j >=0; j--) {
if(model[j][i]!=0){
temp[tempIdex--] = model[j][i];
}
}
boolean hv = false;
for (int j = X-1; j >0; j--) {
if(temp[j] == temp[j-1]&&temp[j]!=0){
temp[j] = temp[j]*2;
temp[j-1] = 0;
hv = true;
}
}
goon = hv;
int is0 = 0;
for (int j = X-1; j >=0; j--) {
model[j][i] = temp[j];
if(temp[j]==0) is0++;
}
if(is0>0){
xp4[i] = 1;//可插牌
}
}
}
//插牌
List<Integer> space4 = new ArrayList<Integer>();
for (int j = 0; j < xp4.length; j++) {
if(xp4[j]==1){
space4.add(j);
}
}
if(space4.size()==0){
gameover = true;
System.out.println("game over");
System.exit(0);
}else{
int a = (int) (Math.random()*(space4.size()));
Integer index = space4.get(a);
for (int j = X-1; j >=0 ; j--) {
if(model[j][index]==0){
model[j][index] = enumM[(int) (Math.random()*enumM.length)];
break;
}
}
}
outPrint();
break;
case 100://D 右
int[] xp = new int[X];
for (int i = 0; i < X; i++) {
boolean goon = true;
while (goon) {
int[] temp = new int[Y];
int tempIdex = Y-1;
//去空
for (int j = Y-1; j >=0 ; j--) {
if(model[i][j]!=0){
temp[tempIdex--] = model[i][j];
}
}
boolean hv = false;
//合并
for (int j = 0; j < Y-1; j++) {
if(temp[j] == temp[j+1]&&temp[j]!=0){
temp[j] = temp[j]*2;
temp[j+1] = 0;
hv = true;
}
}
goon = hv;
int is0 = 0;
for (int j = 0; j < Y; j++) {
model[i][j] = temp[j];
if(temp[j]==0) is0++;
}
if(is0>0){
xp[i] = 1;//可插牌
}
}
}
//插牌
List<Integer> space = new ArrayList<Integer>();
for (int j = 0; j < xp.length; j++) {
if(xp[j]==1){
space.add(j);
}
}
if(space.size()==0){
gameover = true;
System.out.println("game over");
System.exit(0);
}else{
int a = (int) (Math.random()*(space.size()));
Integer index = space.get(a);
for (int j = Y-1; j >=0 ; j--) {
if(model[index][j]==0){
model[index][j] = enumM[(int) (Math.random()*enumM.length)];
break;
}
}
}
outPrint();
break;
case 119://W 上
int[] xp3 = new int[Y];
for (int i = 0; i < Y; i++) {
boolean goon = true;
while (goon) {
int[] temp = new int[X];
int tempIdex = 0;
for (int j = 0; j < X; j++) {
if(model[j][i]!=0){
temp[tempIdex++] = model[j][i];
}
}
boolean hv = false;
for (int j = 0; j < X-1; j++) {
if(temp[j] == temp[j+1]&&temp[j]!=0){
temp[j] = temp[j]*2;
temp[j+1] = 0;
hv = true;
}
}
goon = hv;
int is0 = 0;
for (int j = 0; j < X; j++) {
model[j][i] = temp[j];
if(temp[j]==0) is0++;
}
if(is0>0){
xp3[i] = 1;//可插牌
}
}
}
//插牌
List<Integer> space3 = new ArrayList<Integer>();
for (int j = 0; j < xp3.length; j++) {
if(xp3[j]==1){
space3.add(j);
}
}
if(space3.size()==0){
gameover = true;
System.out.println("game over");
System.exit(0);
}else{
int a = (int) (Math.random()*(space3.size()));
Integer index = space3.get(a);
for (int j = 0; j < X ; j++) {
if(model[j][index]==0){
model[j][index] = enumM[(int) (Math.random()*enumM.length)];
break;
}
}
}
outPrint();
break;
case 97://A 左
int[] xp2 = new int[X];
for (int i = 0; i < X; i++) {
boolean goon = true;
while (goon) {
int[] temp = new int[Y];
int tempIdex = 0;
for (int j = 0; j < Y ; j++) {
if(model[i][j]!=0){
temp[tempIdex++] = model[i][j];
}
}
boolean hv = false;
for (int j = 0; j < Y-1; j++) {
if(temp[j] == temp[j+1]&&temp[j]!=0){
temp[j] = temp[j]*2;
temp[j+1] = 0;
hv = true;
}
}
goon = hv;
int is0 = 0;
for (int j = 0; j < Y; j++) {
model[i][j] = temp[j];
if(temp[j]==0) is0++;
}
if(is0>0){
xp2[i] = 1;//可插牌
}
}
}
//插牌
List<Integer> space2 = new ArrayList<Integer>();
for (int j = 0; j < xp2.length; j++) {
if(xp2[j]==1){
space2.add(j);
}
}
if(space2.size()==0){
gameover = true;
System.out.println("game over");
System.exit(0);
}else{
int a = (int) (Math.random()*(space2.size()));
Integer index = space2.get(a);
for (int j = 0; j <Y ; j++) {
if(model[index][j]==0){
model[index][j] = enumM[(int) (Math.random()*enumM.length)];
break;
}
}
}
outPrint();
break;
default:
break;
}
}
public static void main(String[] args) throws IOException {
int randomX, randomY;
randomX = (int) (Math.random()*X);
randomY = (int) (Math.random()*model[randomX].length);
model[randomX][randomY] = 2;
randomX = (int) (Math.random()*X);
randomY = (int) (Math.random()*model[randomX].length);
model[randomX][randomY] = 2;
randomX = (int) (Math.random()*X);
randomY = (int) (Math.random()*model[randomX].length);
model[randomX][randomY] = 4;
randomX = (int) (Math.random()*X);
randomY = (int) (Math.random()*model[randomX].length);
model[randomX][randomY] = 8;
outPrint();
while(!gameover){
int read = System.in.read();
change(read);
}
}
}
有兴趣的可以重构下,有些方法应该是可以抽出来公用的