Back to project page SeniorDesign.
The source code is released under:
GNU General Public License
If you think the Android project SeniorDesign listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package fakesetgame.seniordesign; //from w w w . j a v a 2 s . c o m import fakesetgame.seniordesign.data.OptionsHelper; import fakesetgame.seniordesign.data.PlayerDataDbHelper; import fakesetgame.seniordesign.model.Board; import fakesetgame.seniordesign.model.Game; import fakesetgame.seniordesign.model.GameOverEvent; import fakesetgame.seniordesign.model.GameOverListener; import fakesetgame.seniordesign.model.Tile; import fakesetgame.seniordesign.view.ShadedImageView; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.Timer; import java.util.TimerTask; /** * This is the Activity class for the screen on which * the game is played. */ public class GameScreen extends Activity implements View.OnClickListener, GameOverListener { private static final String TAG = "GameScreen"; private ShadedImageView[] tiles = new ShadedImageView[Board.TILES]; private ImageView[][] found; private TextView timeView = null; private final Handler handler = new Handler(); private Game game = null; private List<Tile> selectedTiles = new ArrayList<Tile>(); private final Runnable updateClock = new Runnable() { @Override public void run() { if (game.getGameType() == Game.GameType.Normal) { long elapsedSeconds = game.getElapsedTime() / 1000; timeView.setText(String.format("%d:%02d", elapsedSeconds / 60, elapsedSeconds % 60)); } if (game.getGameType() == Game.GameType.TimeAttack) { long timeRemaining = game.getTimeRemaining() / 1000; timeView.setText(String.format("%d:%02d", timeRemaining / 60, timeRemaining % 60)); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game_activity); found = new ImageView[OptionsHelper.getSetCount(this)][Game.TILES_IN_A_SET]; // Init board imageviews for (int i = 0; i < Board.TILES; i++) { tiles[i] = (ShadedImageView) findViewById(getResources().getIdentifier( "Tile" + Integer.valueOf(i + 1).toString(), "id", getPackageName())); tiles[i].setTag(R.id.TILE_INDEX, i); tiles[i].setOnClickListener(this); if (tiles[i] == null) Log.e(TAG, String.format("Failed to set tiles[%d]. Value is null.", i)); } // Init found set imageviews for (int set = 0; set < found.length; set++) { for (int tile = 0; tile < found[set].length; tile++) { found[set][tile] = (ImageView) findViewById(getResources().getIdentifier( "FoundSet" + Integer.valueOf(set + 1).toString() + "_" + Integer.valueOf(tile + 1).toString(), "id", getPackageName())); if (found[set][tile] == null) Log.e(TAG, String.format("Failed to set found[%d][%d]. Value is null.", set, tile)); } } newGame(); timeView = (TextView) findViewById(R.id.TimeView); Timer clockUpdateTimer = new Timer(); clockUpdateTimer.schedule(new TimerTask() { @Override public void run() { handler.post(updateClock); } }, 0, 200); } @Override protected void onStop() { super.onStop(); game.pauseTimer(); } @Override protected void onRestart() { super.onRestart(); game.unpauseTimer(); } /** * Selects a tile. If there are three tiles selected * after selecting the specified tile, attempts a set * using the Game class (Game.attemptSet) and handles the results of that * call accordingly. * @param tileIndex * @param selected */ private void setTileSelected(int tileIndex, boolean selected) { if (tileIndex < 0 || tileIndex > Board.TILES) throw new IllegalArgumentException("tileIndex out of bounds."); if (getTileSelected(tileIndex) == selected) return; tiles[tileIndex].setShaded(selected); Log.d(TAG, String.format("Setting tile %d shading = %s", tileIndex, Boolean.valueOf(selected).toString())); if (selected) { selectedTiles.add(game.board.getTile(tileIndex)); // If we've selected 3 tiles, check with Game class to see // if it's a good set. if (selectedTiles.size() == 3) { Tile[] tiles = selectedTiles.toArray(new Tile[3]); if(game.isFound(selectedTiles)){ messageUser(getString(R.string.foundSetAlready)); } else if (game.attemptSet(tiles[0], tiles[1], tiles[2])) { messageUser(getString(R.string.foundSet)); int set = game.getFoundSetCount() - 1; found[set][0].setImageDrawable(tiles[0].getSmallDrawable(this)); found[set][1].setImageDrawable(tiles[1].getSmallDrawable(this)); found[set][2].setImageDrawable(tiles[2].getSmallDrawable(this)); } else { messageUser(getString(R.string.badSet)); } clearTileSelection(); } } else { selectedTiles.remove(game.board.getTile(tileIndex)); } } /** * Event handler for the Game Over event. Saves * the game outcome and opens the Summary screen. * @param e */ @Override public void gameOver(GameOverEvent e) { long lastGame = PlayerDataDbHelper.saveOutcome(this, game); Intent i = new Intent(this, SummaryScreen.class); i.putExtra("lastGame", lastGame); startActivity(i); } /** * Gets the selected state of a tile, by its index. * @param tileIndex * @return */ private boolean getTileSelected(int tileIndex) { if (tileIndex < 0 || tileIndex > Board.TILES) throw new IllegalArgumentException("tileIndex out of bounds."); return tiles[tileIndex].getShaded(); } /** * Deselects all tiles. */ private void clearTileSelection() { selectedTiles.clear(); for (int i = 0; i < Board.TILES; i++) setTileSelected(i, false); } /** * Event handler for clicks on Tile images. * @param view */ public void onClick(View view) { Object o = view.getTag(R.id.TILE_INDEX); if (view instanceof ImageView && o instanceof Integer) { if (game.isGameOver()) return; int idx = (Integer) o; Log.d(TAG, "Event: onClick for Tile " + idx); setTileSelected(idx, !getTileSelected(idx)); } } /** * The event handler for the Hint button click. Selects * a tile that is part of a good set that hasn't been found. * @param view */ public void onClickHint(View view) { Set<Tile> hintTiles = game.hintProvider.getHint(); clearTileSelection(); if(hintTiles != null) { for (ShadedImageView tile : tiles) { int tileIndex = (Integer) tile.getTag(R.id.TILE_INDEX); if (hintTiles.contains(game.board.getTile(tileIndex))) setTileSelected(tileIndex, true); } } } /** * Displays a message to the user in a Toast. * @param message */ private void messageUser(String message) { Toast toast = Toast.makeText(this, message, (message.length() > 24 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT)); toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); toast.show(); } /** * Sets up a new game. */ private void newGame() { Game.GameType type = (Game.GameType) getIntent().getExtras().get("type"); game = new Game(type, OptionsHelper.getSetCount(this), OptionsHelper.getMinDiff(this), OptionsHelper.getMaxDiff(this)); game.addGameOverListener(this); clearTileSelection(); for (int i = 0; i < tiles.length; i++) { tiles[i].setImageDrawable(game.board.getTile(i).getDrawable(this)); Log.d(TAG, String.format("Set tile image for tiles[%d]", i)); } for (int set = 0; set < found.length; set++) for (int tile = 0; tile < found[set].length; tile++) found[set][tile].setImageDrawable(getResources().getDrawable(R.drawable.tile_small_blank)); } }