Search This Blog

Wednesday, 27 May 2015

2048 code in JAVA without GUI

/* I have made 4 different classes as per my requirements.Here Blank square boxes defined as 0's.
 * I would suggest you to run this by copying individual class in Eclipse-IDE. Open Eclipse then 
 * define a package game and then make classes as given in Source Code.For Any query comment 
 * or message me on g+. 
 * 
 *author @ Maulik Gohil.
 *
 */

/* Class Main*/

package game;
import java.util.Random;

public class Main {

public static int[][] a=new int[][]{{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};

public static void main(String args[])
{
Show show=new Show();
boolean flag=true;

while(flag)
{
show.doPrintArray(a);
}
}
}

/*Class Show*/

package game;
import java.util.Random;
import java.util.Scanner;

public class Show {
Scanner scr=new Scanner(System.in);

public void doPrintArray(int[][] a) {

int count=0;
Random randomNum=new Random();

String s="22242224222";
        char num= s.charAt(randomNum.nextInt(s.length()));
        String numString=String.valueOf(num);
     
     
        for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
        if(a[i][j]==0)
        count++;
     
        String[] storeFreeBlocks=new String[count];
        int localcounter=0;
        for(int i=0;i<4;i++)
        {
        for(int j=0;j<4;j++)
        {
        if(a[i][j]==0)
        {
        storeFreeBlocks[localcounter]=String.valueOf(i)+","+String.valueOf(j);
        localcounter++;
        }
        }
        }
     
        String getFreeBlock=storeFreeBlocks[randomNum.nextInt(storeFreeBlocks.length)];
        String[] getRowCol=getFreeBlock.split(",");
        int p=Integer.parseInt(getRowCol[0]);
        int q=Integer.parseInt(getRowCol[1]);
        a[p][q]=Integer.parseInt(numString);
     
       
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
System.out.print(a[i][j]+"    ");
System.out.println();
}

for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i][j]==16)
{
System.out.println("you won");
System.exit(0);
}
}
}

int choice=Integer.parseInt(scr.nextLine());
Process ps=new Process(choice,a);
}

}

/*Class Process*/

package game;

public class Process {
int move;
Process(int choice, int[][] a) {
move=choice;
GetSet gs=new GetSet();
switch(move)
{
case 2:
for(int j=0;j<4;j++)
{
for(int i=3;i>=0;i--)
{
if(a[i][j]!=0)
{
int tempi=i;
while(gs.getDown(a, tempi, j)==0)
{
tempi++;
}
if(gs.getDown(a, tempi, j)!=0 && gs.getDown(a, tempi, j)==a[i][j])
{
a[tempi+1][j]+=a[i][j];
a[i][j]=0;
}
if(tempi!=i)
{
a[tempi][j]=a[i][j];
a[i][j]=0;
}
}
}
}
//app.setArray(a);
break;
case 8:
for(int j=0;j<4;j++)
{
for(int i=0;i<=3;i++)
{
if(a[i][j]!=0)
{
int tempi=i;
while(gs.getUp(a, tempi, j)==0)
{
tempi--;
}
if(gs.getUp(a, tempi, j)!=0 && gs.getUp(a, tempi, j)==a[i][j])
{
a[tempi-1][j]+=a[i][j];
a[i][j]=0;
}
if(tempi!=i)
{
a[tempi][j]=a[i][j];
a[i][j]=0;
}
}
}
}
//app.setArray(a);
break;
case 4:
for(int i=0;i<4;i++)
{
for(int j=0;j<=3;j++)
{
if(a[i][j]!=0)
{
int tempj=j;
while(gs.getLeft(a, i, tempj)==0)
{
tempj--;
}
if(gs.getLeft(a, i, tempj)!=0 && gs.getLeft(a, i, tempj)==a[i][j])
{
a[i][tempj-1]+=a[i][j];
a[i][j]=0;
}
if(tempj!=j)
{
a[i][tempj]=a[i][j];
a[i][j]=0;
}
}
}
}
//app.setArray(a);
break;
case 6:
for(int i=0;i<4;i++)
{
for(int j=3;j>=0;j--)
{
if(a[i][j]!=0)
{
int tempj=j;
while(gs.getRight(a, i, tempj)==0)
{
tempj++;
}
if(gs.getRight(a, i, tempj)!=0 && gs.getRight(a, i, tempj)==a[i][j])
{
a[i][tempj+1]+=a[i][j];
a[i][j]=0;
}
if(tempj!=j)
{
a[i][tempj]=a[i][j];
a[i][j]=0;
}
}
}
}
//app.setArray(a);
break;
default:
System.out.println("Invalid Entry");
}
}
}

/* Class GetSet*/

package game;

public class GetSet {

public int getDown(int[][] a,int i,int j) {
if(i<3)
return a[i+1][j];
else
return 1;
}
public int getUp(int[][] a,int i,int j) {
if(i>0)
return a[i-1][j];
else
return 1;
}
public int getLeft(int[][] a,int i,int j) {
if(j>0)
return a[i][j-1];
else
return 1;
}
public int getRight(int[][] a,int i,int j) {
if(j<3)
return a[i][j+1];
else
return 1;
}
}






No comments:

Post a Comment

Blog Archive