Create 1+2=3 Android Game
This post will guide you to create a highly interactive 1+2=3 Game. This game is based on a simple logic. Basically you have 3 number 1,2,3 which will be shuffled each time with a randomly generated operator (+,-,*,/) placed in between.
Screenshots:
Lets go through important section of the project.
Code for fetching Highscore :
Code For Generating Expression :
Code for generating dummy answers and placing it
Code for countdown timer and operations associated with it
Alternatively you can download the complete project for ADT by clicking here.
Found Bugs? Please Report them !
Screenshots:
Lets go through important section of the project.
Code for fetching Highscore :
public int chec() { SQLiteDatabase SQLiteDb = mh.getWritableDatabase(); String [] col1 = {"_id","score"}; int ch=0; Cursor cu1 = SQLiteDb.query("scr", col1, null, null, null, null, null); while(cu1.moveToNext()) { ch=Integer.parseInt(cu1.getString(1)); } return ch; }
Code For Generating Expression :
void calc() { String st="123",op="+-*/",z="",y=""; p=0; Random rand=new Random(); for(int i=0;i<3;i++) z=z+st.charAt(rand.nextInt((2 - 0) + 1)); for(int i=0;i<2;i++) y=y+op.charAt(rand.nextInt((3 - 0) + 1)); switch(y.charAt(0)) { case '+': p=Integer.parseInt(z.charAt(0)+"")+Integer.parseInt(z.charAt(1)+""); break; case '-': p=Integer.parseInt(z.charAt(0)+"")-Integer.parseInt(z.charAt(1)+""); break; case '*': p=Integer.parseInt(z.charAt(0)+"")*Integer.parseInt(z.charAt(1)+""); break; case '/': p=Integer.parseInt(z.charAt(0)+"")/Integer.parseInt(z.charAt(1)+""); break; } switch(y.charAt(1)) { case '+': p=p+Integer.parseInt(z.charAt(2)+""); break; case '-': p=p-Integer.parseInt(z.charAt(2)+""); break; case '*': p=p*Integer.parseInt(z.charAt(2)+""); break; case '/': p=p/Integer.parseInt(z.charAt(2)+""); break; } mixz(p); t3.setText(score+""); t2.setText(z.charAt(0)+""+y.charAt(0)+""+z.charAt(1)+""+y.charAt(1)+""+z.charAt(2)+""); }
Code for generating dummy answers and placing it
void mixz(int p) { List<Integer> dataList = new ArrayList<Integer>(); dataList.add(p+1); dataList.add(p); dataList.add(p-1); Collections.shuffle(dataList); int[] num = new int[dataList.size()]; for (int i = 0; i < dataList.size(); i++) { num[i] = dataList.get(i); } b1.setText(num[0]+""); b2.setText(num[1]+""); b3.setText(num[2]+""); }
Code for countdown timer and operations associated with it
countDownTimer = new CountDownTimer(50000, 1000) { public void onTick(long millisUntilFinished) { t1.setText((millisUntilFinished / 1000)+" Secs"); bar.setProgress((int) Math.round(millisUntilFinished / 1000.0)*2); } public void onFinish() { t1.setText("done!"); bar.setProgress(0); t2.setText("Game Over, Score: "+score); b1.setEnabled(false); b2.setEnabled(false); b3.setEnabled(false); SQLiteDatabase SQLiteDb = mh.getWritableDatabase(); ContentValues cntval=new ContentValues(); cntval.put("score",score); if(chec()<score) { int num= SQLiteDb.update("scr", cntval, "chk = ?", new String []{"yes"}); } ob.show(); ot.setText(score+""); //Toast.makeText(getApplicationContext(), "Preferences Saved", Toast.LENGTH_LONG).show(); } }.start();
Alternatively you can download the complete project for ADT by clicking here.
Found Bugs? Please Report them !
Comments
Post a Comment