Last night salty, want to make a 2048 try, tossing to midnight is a bit of results, sharing, because too late to add comments, finally did not optimize the reconstruction, simply look at the idea of it
Let's take a little game of Jigsaw and digest it.
Import java.io.ioexception;/** * Console play jigsaw Puzzles * @author c * Yards: 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 () {/* normal sort Insert */int index = 0;for (int i = 0; i < X; i++) {for (int j = 0; J < Y; J + +) {Tar[i][j] = index++;}} /* Scrambled sort */for (int i = 0; i < tar.length; i++) {for (int j = 0; J < Tar[i].length; J + +) {int temp = Tar[i][j];int Rand OmX, 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 ("[embarrassing]"); 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 ("Congratulations win! Number of steps: "+step);} Else{system.out.priNtln ("Go on! Number of steps: "+step);}} static void Change (int dir) {switch (dir) {case 115://s under if (BX! = 0) {Tar[bx][by] = Tar[bx-1][by];tar[bx-1][by] = 0; Bx--;step++;outprint ();} Break;case 100://d Right if (by! = 0) {Tar[bx][by] = tar[bx][by-1];tar[bx][by-1] = 0; By--;step++;outprint ();} Break;case 119://w on if (BX! = X-1) {Tar[bx][by] = Tar[bx+1][by];tar[bx+1][by] = 0; Bx++;step++;outprint ();} Break;case 97://a left 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 (); and while (true) {int read = System.in.read (); Change (read);}}
Press W S a D for up and down, in fact, a two-dimensional array, in order to change the adjacent position in the direction
The following is a 2048 game, similar to the above, is to be adjacent to add together, and then remove the space (value of 0), see, Welcome to shoot Bricks
Import Java.io.ioexception;import java.util.*;/** * Java Console Development 2048 * @author c * Code farming Autumn Autumn Group->21/777/12 * */public class Gam e2048 {static final int X = 5;static final int Y = 6;static int model[][] = new Int[x][y];static int step = 0;static Boole An 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 goo n = 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;//pluggable}}}//card 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 (mod el[j][index]==0) {Model[j][index] = enumm[(int) (Math.random () *enumm.length)];break;}}} Outprint (); Break;case 100://d right int[] xp = new Int[x];for (int i = 0; i < X; i++) {Boolean goon = True;while (goon) {int [] temp = new Int[y];int Tempidex = y1;//to empty for (int j = Y-1; J >=0; j--) {if (model[i][j]!=0) {temp[tempidex--] = Model[i][j];}} boolean HV = false;//merge 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;//pluggable}}}//card list<integer> space = new arraylist<integer> (); for (int j = 0; J < Xp.le Ngth; 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) {in t[] 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;//pluggable}}}//card 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 left 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;//pluggable}}}//card 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);}}
Interested can be refactored, some methods should be available to be drawn out to the public
How to play with the Java console 2048