Created
April 10, 2019 19:03
-
-
Save miquelbeltran/f0b7aa96654dba42d04509b700fa2eaf to your computer and use it in GitHub Desktop.
Code I wrote in 2010 as an attempt to create a solitarie game, decompiled from a classes.dex, do not take as example of how to do things
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miquelbeltran.accordion; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.TextView; | |
public class Accordion extends Activity { | |
private static final int MENU_NEW_GAME = 1; | |
private static final int MENU_QUIT = 2; | |
MainThread main_thread; | |
private RenderView render_view; | |
TextView tv; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
this.render_view = new RenderView(this); | |
setContentView(this.render_view); | |
this.main_thread = new MainThread(); | |
this.main_thread.setRenderView(this.render_view); | |
this.main_thread.start(); | |
} | |
public boolean onCreateOptionsMenu(Menu menu) { | |
menu.add(0, 1, 0, "New Game"); | |
menu.add(0, 2, 0, "Quit"); | |
return true; | |
} | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case 1: | |
this.main_thread.NewGame(); | |
return true; | |
case 2: | |
this.main_thread.QuitGame(); | |
finish(); | |
return true; | |
default: | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miquelbeltran.accordion; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
public class Card { | |
public static final int ACE = 1; | |
public static final int CLUBS = 0; | |
public static final int DIAMONDS = 1; | |
public static final int HEARTS = 2; | |
public static final int JACK = 11; | |
public static final int KING = 13; | |
public static final int QUEEN = 12; | |
public static final int SPADES = 3; | |
private Bitmap bitmap = null; | |
private final int suit; | |
private final int value; | |
public Card(int theValue, int theSuit) { | |
this.value = theValue; | |
this.suit = theSuit; | |
} | |
public int getSuit() { | |
return this.suit; | |
} | |
public int getValue() { | |
return this.value; | |
} | |
public String getSuitAsString() { | |
switch (this.suit) { | |
case 0: | |
return "Clubs"; | |
case 1: | |
return "Diamonds"; | |
case 2: | |
return "Hearts"; | |
case 3: | |
return "Spades"; | |
default: | |
return "??"; | |
} | |
} | |
public String getValueAsString() { | |
switch (this.value) { | |
case 1: | |
return "01"; | |
case 2: | |
return "02"; | |
case 3: | |
return "03"; | |
case 4: | |
return "04"; | |
case 5: | |
return "05"; | |
case 6: | |
return "06"; | |
case 7: | |
return "07"; | |
case 8: | |
return "08"; | |
case 9: | |
return "09"; | |
case 10: | |
return "10"; | |
case JACK /*11*/: | |
return "Jack"; | |
case QUEEN /*12*/: | |
return "Queen"; | |
case KING /*13*/: | |
return "King"; | |
default: | |
return "??"; | |
} | |
} | |
public String toString() { | |
return getValueAsString() + " of " + getSuitAsString(); | |
} | |
public Bitmap getBitmap(RenderView renderView) { | |
if (this.bitmap == null) { | |
try { | |
this.bitmap = BitmapFactory.decodeResource(renderView.getResources(), (R.drawable.c01c + this.suit) + ((this.value - 1) * 4)); | |
} catch (Exception e) { | |
Exception e2 = e; | |
this.bitmap = null; | |
e2.printStackTrace(); | |
} | |
} | |
return this.bitmap; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miquelbeltran.accordion; | |
public class Deck { | |
public Card[] deck = new Card[52]; | |
public int total_cards; | |
public Deck() { | |
int cardCt = 0; | |
for (int suit = 0; suit <= 3; suit++) { | |
for (int value = 1; value <= 13; value++) { | |
this.deck[cardCt] = new Card(value, suit); | |
cardCt++; | |
} | |
} | |
this.total_cards = 52; | |
} | |
public void shuffle() { | |
for (int i = 51; i > 0; i--) { | |
int rand = (int) (Math.random() * ((double) (i + 1))); | |
Card temp = this.deck[i]; | |
this.deck[i] = this.deck[rand]; | |
this.deck[rand] = temp; | |
} | |
this.total_cards = 52; | |
} | |
public void movement(int idxPreviousSelectedCard, int idxSelectedCard) { | |
if (isValidMovement(idxPreviousSelectedCard, idxSelectedCard)) { | |
this.deck[idxSelectedCard] = this.deck[idxPreviousSelectedCard]; | |
for (int i = idxPreviousSelectedCard + 1; i < this.total_cards; i++) { | |
this.deck[i - 1] = this.deck[i]; | |
} | |
this.total_cards--; | |
} | |
} | |
private boolean isValidMovement(int idxPreviousSelectedCard, int idxSelectedCard) { | |
if (idxPreviousSelectedCard - idxSelectedCard != 1 && idxPreviousSelectedCard - idxSelectedCard != 3) { | |
return false; | |
} | |
if (this.deck[idxPreviousSelectedCard].getSuit() == this.deck[idxSelectedCard].getSuit()) { | |
return true; | |
} | |
if (this.deck[idxPreviousSelectedCard].getValue() == this.deck[idxSelectedCard].getValue()) { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miquelbeltran.accordion; | |
public class Globals { | |
public static final int LOGIC_GAME_PLAY = 2; | |
public static final int LOGIC_GAME_WIN = 3; | |
public static final int LOGIC_INTRO = 0; | |
public static final int LOGIC_NEW_GAME = 1; | |
public static Deck deck; | |
public static int idx_previous_selected_card; | |
public static int idx_selected_card; | |
public static int logic_state; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miquelbeltran.accordion; | |
public class MainThread extends Thread { | |
public int FPS = 10; | |
public Boolean exit = Boolean.valueOf(false); | |
RenderView render_view; | |
public void run() { | |
Globals.logic_state = 0; | |
while (!this.exit.booleanValue()) { | |
MainThreadRun(); | |
} | |
} | |
public void setRenderView(RenderView renderView) { | |
this.render_view = renderView; | |
} | |
public void NewGame() { | |
SetLogicState(1); | |
} | |
public void QuitGame() { | |
this.exit = Boolean.valueOf(true); | |
} | |
public void SetLogicState(int LogicState) { | |
Globals.logic_state = LogicState; | |
} | |
private void MainThreadRun() { | |
long start = System.currentTimeMillis(); | |
Logic(); | |
this.render_view.postInvalidate(); | |
long wait_time = ((long) (1000 / this.FPS)) - (System.currentTimeMillis() - start); | |
if (wait_time > 0) { | |
try { | |
Thread.sleep(wait_time); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private void Logic() { | |
switch (Globals.logic_state) { | |
case 1: | |
Globals.idx_selected_card = -1; | |
Globals.idx_previous_selected_card = -1; | |
Globals.deck = new Deck(); | |
Globals.deck.shuffle(); | |
SetLogicState(2); | |
return; | |
case 2: | |
if (Globals.idx_selected_card != -1 && Globals.idx_previous_selected_card != -1) { | |
Globals.deck.movement(Globals.idx_previous_selected_card, Globals.idx_selected_card); | |
Globals.idx_selected_card = -1; | |
Globals.idx_previous_selected_card = -1; | |
return; | |
} | |
return; | |
default: | |
return; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.miquelbeltran.accordion; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Paint.Style; | |
import android.graphics.Point; | |
import android.graphics.Rect; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
public class RenderView extends View implements OnTouchListener { | |
Paint mTextPaint = new Paint(); | |
public Handler render_handler = new Handler() { | |
public void handleMessage(Message msg) { | |
} | |
}; | |
Paint selected_paint; | |
public RenderView(Context context) { | |
super(context); | |
this.mTextPaint.setAntiAlias(true); | |
this.mTextPaint.setTextSize(16.0f); | |
this.mTextPaint.setStyle(Style.FILL); | |
this.mTextPaint.setColor(-16711936); | |
this.selected_paint = new Paint(); | |
this.selected_paint.setAntiAlias(true); | |
this.selected_paint.setStyle(Style.FILL); | |
this.selected_paint.setColor(-16711936); | |
this.selected_paint.setAlpha(125); | |
setLongClickable(true); | |
} | |
/* Access modifiers changed, original: protected */ | |
public void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
Render(canvas); | |
} | |
private void Render(Canvas canvas) { | |
switch (Globals.logic_state) { | |
case 2: | |
for (int _idx_card = 0; _idx_card < Globals.deck.total_cards; _idx_card++) { | |
Bitmap bitmapOrg = Globals.deck.deck[_idx_card].getBitmap(this); | |
int x = ((_idx_card % 13) * 36) + 2; | |
int y = ((_idx_card / 13) * 65) + 2; | |
canvas.drawBitmap(bitmapOrg, (float) x, (float) y, this.mTextPaint); | |
if (Globals.idx_selected_card == _idx_card) { | |
canvas.drawRect(new Rect(x, y, bitmapOrg.getWidth() + x, bitmapOrg.getHeight() + y), this.selected_paint); | |
} | |
} | |
return; | |
default: | |
return; | |
} | |
} | |
public boolean onTouchEvent(MotionEvent event) { | |
if (event.getAction() != 0) { | |
return super.onTouchEvent(event); | |
} | |
Point point = new Point(); | |
point.x = (int) event.getX(); | |
point.y = (int) event.getY(); | |
int _idx = ((point.x - 2) / 36) + (((point.y - 2) / 65) * 13); | |
if (_idx != Globals.idx_selected_card) { | |
Globals.idx_previous_selected_card = Globals.idx_selected_card; | |
Globals.idx_selected_card = _idx; | |
} | |
invalidate(); | |
return true; | |
} | |
public boolean onTouch(View arg0, MotionEvent arg1) { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment