mirror of
				https://github.com/bspeice/itcs4180
				synced 2025-10-31 09:20:32 -04:00 
			
		
		
		
	Functionality for HW3. Tried to seperate Tile class and MainActivity.
Has 2 issues. First is that a completed row has the Tile ImageViews change size and second is that finish()does not properly close the entire application.
This commit is contained in:
		| @ -194,7 +194,8 @@ | |||||||
|             android:layout_width="0dp" |             android:layout_width="0dp" | ||||||
|             android:layout_height="40dp" |             android:layout_height="40dp" | ||||||
|             android:layout_weight="1" |             android:layout_weight="1" | ||||||
|             android:text="@string/btnNewGame" /> |             android:text="@string/btnNewGame"  | ||||||
|  |             android:onClick="newGame"/> | ||||||
|  |  | ||||||
|         <Button |         <Button | ||||||
|             android:id="@+id/btnUnCover" |             android:id="@+id/btnUnCover" | ||||||
| @ -202,7 +203,8 @@ | |||||||
|             android:layout_width="0dp" |             android:layout_width="0dp" | ||||||
|             android:layout_height="40dp" |             android:layout_height="40dp" | ||||||
|             android:layout_weight="1" |             android:layout_weight="1" | ||||||
|             android:text="@string/btnUnCover" /> |             android:text="@string/btnUnCover"  | ||||||
|  |             android:onClick="uncover" /> | ||||||
|     </LinearLayout> |     </LinearLayout> | ||||||
|  |  | ||||||
| </LinearLayout> | </LinearLayout> | ||||||
| @ -3,5 +3,7 @@ | |||||||
|     <!-- Default screen margins, per the Android Design guidelines. --> |     <!-- Default screen margins, per the Android Design guidelines. --> | ||||||
|     <dimen name="activity_horizontal_margin">16dp</dimen> |     <dimen name="activity_horizontal_margin">16dp</dimen> | ||||||
|     <dimen name="activity_vertical_margin">16dp</dimen> |     <dimen name="activity_vertical_margin">16dp</dimen> | ||||||
|  |     <dimen name="imageHeight">80dp</dimen> | ||||||
|  |     <dimen name="imageWidth">80dp</dimen> | ||||||
|  |  | ||||||
| </resources> | </resources> | ||||||
|  | |||||||
| @ -7,16 +7,39 @@ package com.uncc.hw3; | |||||||
|  * MainActivity.java |  * MainActivity.java | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Collections; | ||||||
|  |  | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
|  | import android.os.Handler; | ||||||
| import android.app.Activity; | import android.app.Activity; | ||||||
| import android.view.Menu; | import android.view.Menu; | ||||||
|  | import android.view.View; | ||||||
|  | import android.widget.ImageView; | ||||||
|  |  | ||||||
|  | public class MainActivity extends Activity | ||||||
|  | { | ||||||
|  | 	 | ||||||
|  | 	ImageView iv; | ||||||
|  | 	public int [] imageViews = {R.id.imgTile1_1, R.id.imgTile1_2, R.id.imgTile1_3, R.id.imgTile1_4, | ||||||
|  | 								R.id.imgTile2_1, R.id.imgTile2_2, R.id.imgTile2_3, R.id.imgTile2_4, | ||||||
|  | 								R.id.imgTile3_1, R.id.imgTile3_2, R.id.imgTile3_3, R.id.imgTile3_4, | ||||||
|  | 								R.id.imgTile4_1, R.id.imgTile4_2, R.id.imgTile4_3, R.id.imgTile4_4,}; | ||||||
|  | 	public int[] iconIds = {R.drawable.diamond, R.drawable.garnet, R.drawable.gem, R.drawable.pearl, R.drawable.ruby, R.drawable.sapphire, R.drawable.swarovski, R.drawable.toppaz}; | ||||||
|  | 	static ArrayList<Tile> tiles; | ||||||
|  | 	static ArrayList<Tile> focusImages; | ||||||
|  | 	Handler handler = new Handler(); | ||||||
|  | 	static int focusIndex; | ||||||
|  | 	static long startTime, endTime; | ||||||
| 	 | 	 | ||||||
| public class MainActivity extends Activity { |  | ||||||
| 	 | 	 | ||||||
| 	@Override | 	@Override | ||||||
| 	protected void onCreate(Bundle savedInstanceState) { | 	protected void onCreate(Bundle savedInstanceState) { | ||||||
| 		super.onCreate(savedInstanceState); | 		super.onCreate(savedInstanceState); | ||||||
| 		setContentView(R.layout.activity_main); | 		setContentView(R.layout.activity_main); | ||||||
|  | 		 | ||||||
|  | 		newGame((View)findViewById(R.id.LinearLayout1)); | ||||||
|  | 		focusImages.get(focusIndex).show(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| @ -26,4 +49,64 @@ public class MainActivity extends Activity { | |||||||
| 		return true; | 		return true; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	public void newGame(View v) | ||||||
|  | 	{ | ||||||
|  | 		tiles = new ArrayList<Tile>(); | ||||||
|  | 		focusImages = new ArrayList<Tile>(); | ||||||
|  | 		ArrayList<Tile> focusImagesTemp = new ArrayList<Tile>(); | ||||||
|  | 		 | ||||||
|  | 		//Create list of board tiles and tiles to find | ||||||
|  | 		for(int x: iconIds) | ||||||
|  | 		{ | ||||||
|  | 			tiles.add(new Tile(null, x)); | ||||||
|  | 			tiles.add(new Tile(null, x)); | ||||||
|  | 			focusImagesTemp.add(new Tile((ImageView)findViewById(R.id.imgResult), x)); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		//Shuffle  Tiles | ||||||
|  | 		Collections.shuffle(tiles); | ||||||
|  | 		Collections.shuffle(focusImagesTemp); | ||||||
|  | 		 | ||||||
|  | 		//Set focusImages list | ||||||
|  | 		for(int x=0; x<focusImagesTemp.size(); x++) | ||||||
|  | 		{ | ||||||
|  | 			focusImages.add(focusImagesTemp.get(x)); | ||||||
|  | 			focusImages.add(focusImagesTemp.get(x)); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		//Assign ImageViews id to the tiles and assign listener to all imageViews | ||||||
|  | 		for(int x=0; x<imageViews.length; x++) | ||||||
|  | 		{ | ||||||
|  | 			tiles.get(x).setImageView((ImageView)findViewById(imageViews[x])); | ||||||
|  | 			tiles.get(x).reset(); | ||||||
|  | 			((ImageView)findViewById(imageViews[x])).setOnClickListener(tiles.get(x)); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		//reset focus images and show | ||||||
|  | 		focusIndex = 0; | ||||||
|  | 		focusImages.get(focusIndex).show(); | ||||||
|  | 		 | ||||||
|  | 		startTime = System.currentTimeMillis(); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void uncover(View v) | ||||||
|  | 	{ | ||||||
|  | 		//Show all of the tiles | ||||||
|  | 		for(int x=0; x<imageViews.length; x++) | ||||||
|  | 		{ | ||||||
|  | 			tiles.get(x).show(); | ||||||
|  | 		} | ||||||
|  | 		handler.postDelayed(new Runnable() { | ||||||
|  | 			public void run() { | ||||||
|  | 				for(int x=0; x<imageViews.length; x++) | ||||||
|  | 				{ | ||||||
|  | 					//Cover the tiles that aren't already found | ||||||
|  | 					if(!tiles.get(x).getMatched()) | ||||||
|  | 					{ | ||||||
|  | 						tiles.get(x).cover(); | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		}, 1000); | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -49,8 +49,12 @@ public class ResultActivity extends Activity { | |||||||
| 	 | 	 | ||||||
| 	public void onClick(View v) { | 	public void onClick(View v) { | ||||||
| 		// Figure out if the Try Again or Exit button was clicked | 		// Figure out if the Try Again or Exit button was clicked | ||||||
|  | 		Intent intent = new Intent(this, MainActivity.class); | ||||||
| 		if (v.getId() == R.id.btnTryAgain) | 		if (v.getId() == R.id.btnTryAgain) | ||||||
| 			startActivity(new Intent(this, MainActivity.class)); | 		{ | ||||||
|  | 			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | ||||||
|  | 			startActivity(intent); | ||||||
|  | 		} | ||||||
| 		else if (v.getId() == R.id.btnExit) | 		else if (v.getId() == R.id.btnExit) | ||||||
| 			finish(); | 			finish(); | ||||||
| 	} | 	} | ||||||
|  | |||||||
							
								
								
									
										126
									
								
								HW3/src/com/uncc/hw3/Tile.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								HW3/src/com/uncc/hw3/Tile.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,126 @@ | |||||||
|  | package com.uncc.hw3; | ||||||
|  |  | ||||||
|  | import android.app.Activity; | ||||||
|  | import android.content.Intent; | ||||||
|  | import android.os.Handler; | ||||||
|  | import android.view.View; | ||||||
|  | import android.widget.ImageView; | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Bradlee Speice, Brandon Rodenmayer | ||||||
|  |  * ITIS 4180 | ||||||
|  |  * Homework 3 | ||||||
|  |  * ResultActivity.java | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | public class Tile implements View.OnClickListener | ||||||
|  | { | ||||||
|  | 	private ImageView iv; | ||||||
|  | 	private int id; | ||||||
|  | 	private boolean show; | ||||||
|  | 	private boolean matched; | ||||||
|  | 	private boolean touchEnabled; | ||||||
|  | 	Handler handler= new Handler(); | ||||||
|  | 	 | ||||||
|  | 	public Tile(ImageView iv, int id)  | ||||||
|  | 	{ | ||||||
|  | 		this.iv = iv; | ||||||
|  | 		this.id = id; | ||||||
|  | 		show = false; | ||||||
|  | 		matched = false; | ||||||
|  | 		touchEnabled = true; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public int getId() | ||||||
|  | 	{ | ||||||
|  | 		return id; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void setId(int id) | ||||||
|  | 	{ | ||||||
|  | 		this.id = id; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public boolean getShow() | ||||||
|  | 	{ | ||||||
|  | 		return show; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void show() | ||||||
|  | 	{ | ||||||
|  | 		show = true; | ||||||
|  | 		touchEnabled = false; | ||||||
|  | 		iv.setImageResource(id); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void cover() | ||||||
|  | 	{ | ||||||
|  | 		show = false; | ||||||
|  | 		touchEnabled = true; | ||||||
|  | 		iv.setImageResource(R.drawable.cover); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void reset() | ||||||
|  | 	{ | ||||||
|  | 		show = false; | ||||||
|  | 		touchEnabled = true; | ||||||
|  | 		matched = false; | ||||||
|  | 		iv.setImageResource(R.drawable.cover); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public boolean touchEnabled() | ||||||
|  | 	{ | ||||||
|  | 		return touchEnabled; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public boolean getMatched() | ||||||
|  | 	{ | ||||||
|  | 		return matched; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void setMatched() | ||||||
|  | 	{ | ||||||
|  | 		matched = true; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void setImageView(ImageView iv) | ||||||
|  | 	{ | ||||||
|  | 		this.iv = iv; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public void onClick(View v) | ||||||
|  | 	{ | ||||||
|  | 		if(!matched) | ||||||
|  | 		{ | ||||||
|  | 			if(touchEnabled) | ||||||
|  | 			{ | ||||||
|  | 				show(); | ||||||
|  | 				if(id==MainActivity.focusImages.get(MainActivity.focusIndex).getId()) | ||||||
|  | 				{ | ||||||
|  | 					setMatched(); | ||||||
|  | 					try | ||||||
|  | 					{ | ||||||
|  | 						MainActivity.focusIndex++; | ||||||
|  | 						MainActivity.focusImages.get(MainActivity.focusIndex).show(); | ||||||
|  | 					} | ||||||
|  | 					catch(Exception e) | ||||||
|  | 					{ | ||||||
|  | 						Intent intent = new Intent(iv.getContext(), ResultActivity.class); | ||||||
|  | 						MainActivity.endTime = System.currentTimeMillis(); | ||||||
|  | 						intent.putExtra("ELAPSED_TIME", (float)((MainActivity.endTime - MainActivity.startTime)/1000.0)); | ||||||
|  | 						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | ||||||
|  | 						iv.getContext().startActivity(intent); | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 				else | ||||||
|  | 				{ | ||||||
|  | 					handler.postDelayed(new Runnable() { | ||||||
|  | 						public void run() { | ||||||
|  | 							cover(); | ||||||
|  | 						} | ||||||
|  | 					}, 1000); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 tokugawa
					tokugawa