Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.AlertDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.graphics.drawable.Drawable;
  6. import android.support.v7.app.ActionBarActivity;
  7. import android.support.v7.app.ActionBar;
  8. import android.support.v4.app.Fragment;
  9. import android.os.Bundle;
  10. import android.util.AttributeSet;
  11. import android.view.LayoutInflater;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.os.Build;
  17. import android.widget.Button;
  18. import android.widget.ImageButton;
  19.  
  20. import java.util.HashMap;
  21. import java.util.Random;
  22.  
  23. class MyCustomImageButton extends ImageButton
  24. {
  25. protected int holdingImage;
  26.  
  27. public MyCustomImageButton(Context context) {
  28. super(context);
  29. this.holdingImage = -1;
  30. }
  31. public MyCustomImageButton(Context context, AttributeSet attrs) {
  32. super(context, attrs);
  33. this.holdingImage = -1;
  34. }
  35. public MyCustomImageButton(Context context, AttributeSet attrs, int defStyle) {
  36. super(context, attrs, defStyle);
  37. this.holdingImage = -1;
  38. }
  39. }
  40. //public class MainActivity extends ActionBarActivity {
  41. public class MainActivity extends Activity {
  42. Random myR = new Random ();
  43. MyCustomImageButton[] myImageButtons;
  44. int[] myImages={R.drawable.one,R.drawable.two,R.drawable.five,R.drawable.seven,R.drawable.eight,R.drawable.six,R.drawable.three,R.drawable.four};
  45. int[] myImageButtonArray = {R.id.imageButton0, R.id.imageButton1, R.id.imageButton2, R.id.imageButton3, R.id.imageButton4,
  46. R.id.imageButton5, R.id.imageButton6, R.id.imageButton7, R.id.imageButton8, R.id.imageButton9,
  47. R.id.imageButton10, R.id.imageButton11, R.id.imageButton12, R.id.imageButton13, R.id.imageButton14,
  48. R.id.imageButton15};
  49.  
  50. // The following four lies for holding temp data while checking the match
  51. int myFirstButtonImage = R.drawable.ic_launcher;
  52. int mySecondButtonImage = R.drawable.ic_launcher;
  53. ImageButton myFirstButton = null;
  54. ImageButton mySecondButton = null;
  55.  
  56. // for message box
  57. int myNoOfClick = 0;
  58. int myNoOfCompletedCells = 0;
  59.  
  60. Button myNewgame;
  61. AlertDialog.Builder alertDialog; // = new AlertDialog.Builder(MainActivity.this);
  62.  
  63. protected void initializeGrid()
  64. {
  65. for (int i=0; i < 16; ++i)
  66. {
  67. myImageButtons[i].setBackgroundResource(R.drawable.ic_launcher);
  68. myImageButtons[i].setClickable(true);
  69. myImageButtons[i].holdingImage = -1;
  70. }
  71. myNoOfClick = 0;
  72. myNoOfCompletedCells = 0;
  73. }
  74.  
  75. protected void createGrid()
  76. {
  77. myImageButtons = new MyCustomImageButton[16];
  78. for (int i=0; i < 16; ++i)
  79. {
  80. myImageButtons[i] = (MyCustomImageButton) findViewById(myImageButtonArray[i]);
  81.  
  82. }
  83. }
  84.  
  85. protected void loadImages()
  86. {
  87. int myNextInt;
  88. for (int i=0; i < 8; ++i)
  89. {
  90. for(int j=0; j < 2; ++j)
  91. {
  92. myNextInt = myR.nextInt(16);
  93. while (myImageButtons[myNextInt].holdingImage != -1)
  94. {
  95. myNextInt = myR.nextInt(16);
  96. }
  97. if(myImageButtons[myNextInt].holdingImage == -1)
  98. {
  99. myImageButtons[myNextInt].setBackgroundResource(R.drawable.ic_launcher);
  100. myImageButtons[myNextInt].holdingImage = myImages[i];
  101. //myGridValues[myNextInt] = i+1;
  102. // myMap.put(myImageButtonArray[myNextInt],myImages[i]);
  103. }
  104. }
  105.  
  106. }
  107. }
  108.  
  109.  
  110. protected void showMessage()
  111. {
  112. alertDialog = new AlertDialog.Builder(MainActivity.this);
  113. //alertDialog = new AlertDialog.Builder(MainActivity.this).create();
  114. alertDialog.setTitle("Score");
  115. alertDialog.setMessage("No. Of clicks are: " + myNoOfClick +" !");
  116. alertDialog.setInverseBackgroundForced(true);
  117. alertDialog.setCancelable(true);
  118. alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
  119.  
  120. public void onClick(DialogInterface dialog,
  121. int which) {
  122. dialog.dismiss();
  123. }
  124. });
  125. alertDialog.show();
  126. }
  127.  
  128.  
  129. @Override
  130. protected void onCreate(Bundle savedInstanceState) {
  131. super.onCreate(savedInstanceState);
  132. setContentView(R.layout.activity_main);
  133. createGrid();
  134. initializeGrid();
  135. loadImages();;
  136.  
  137. myNewgame = (Button) findViewById(R.id.newGame);
  138. myNewgame.setOnClickListener(new View.OnClickListener() {
  139. @Override
  140. public void onClick(View view) {
  141. initializeGrid();
  142. loadImages();
  143. }
  144. });
  145.  
  146.  
  147. for(int i=0; i<myImageButtons.length; ++i)
  148. {
  149. final MyCustomImageButton myTempImageButton = myImageButtons[i];
  150. //final int myIBId = myTempImageButton.getId();
  151. myTempImageButton.setOnClickListener(new View.OnClickListener() {
  152. //@Override
  153. public void onClick(View view) {
  154. ++myNoOfClick;
  155. myTempImageButton.setBackgroundResource(myTempImageButton.holdingImage);
  156. if (myFirstButtonImage == R.drawable.ic_launcher)
  157. {
  158. if(mySecondButtonImage == R.drawable.ic_launcher)
  159. {
  160. myFirstButtonImage = myTempImageButton.holdingImage;
  161. myFirstButton = myTempImageButton;
  162. myTempImageButton.setClickable(false);
  163. myNoOfCompletedCells += 2;
  164. if (myNoOfCompletedCells >= 16)
  165. {
  166. showMessage();
  167. }
  168. }
  169. else
  170. {
  171. myFirstButton.setClickable(true);
  172. mySecondButton.setClickable(true);
  173. myFirstButton.setBackgroundResource(R.drawable.ic_launcher);
  174. mySecondButton.setBackgroundResource(R.drawable.ic_launcher);
  175.  
  176. myFirstButton = myTempImageButton;
  177. myFirstButtonImage = myTempImageButton.holdingImage;
  178. myTempImageButton.setClickable(false);
  179. }
  180. }
  181. else
  182. {
  183. myTempImageButton.setClickable(false);
  184. if (myFirstButtonImage == myTempImageButton.holdingImage) // For Success full Match
  185. {
  186. myFirstButton.setClickable(false);
  187. myFirstButtonImage = R.drawable.ic_launcher;
  188. mySecondButtonImage = R.drawable.ic_launcher;
  189. }
  190. else
  191. {
  192. mySecondButton = myTempImageButton;
  193. mySecondButtonImage = myTempImageButton.holdingImage;
  194. myFirstButtonImage = R.drawable.ic_launcher;
  195. }
  196. }
  197. }
  198. });
  199. }
  200.  
  201. }
  202.  
  203.  
  204. }
  205.  
  206.  
  207.  
  208.  
  209. The above is the source file
  210.  
  211. <?xml version="1.0" encoding="utf-8"?>
  212. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  213. package="com.example.myimagematchgamev10"
  214. android:versionCode="1"
  215. android:versionName="1.0" >
  216.  
  217. <uses-sdk
  218. android:minSdkVersion="7"
  219. android:targetSdkVersion="10" />
  220.  
  221. <application
  222. android:allowBackup="true"
  223. android:icon="@drawable/ic_launcher"
  224. android:label="@string/app_name"
  225. android:theme="@style/AppTheme" >
  226. <activity
  227. android:name="com.example.myimagematchgamev10.MainActivity"
  228. android:label="@string/app_name" >
  229. <intent-filter>
  230. <action android:name="android.intent.action.MAIN" />
  231.  
  232. <category android:name="android.intent.category.LAUNCHER" />
  233. </intent-filter>
  234. </activity>
  235. </application>
  236.  
  237. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement