package com.icystar.dicegenerator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final int MAX_COUNT_ORDER = 100;
public static final int MAX_DICE_ORDER = 100;
public enum Mode {
ENTER_COUNT,
ENTER_DICEMAX,
SHOW_RESULT
}
public static class Drop {
public Mode mode = Mode.ENTER_COUNT;
public int count = 0;
public int diceMax = 0;
public int[] dices = new int[0];
public int summ = 0;
public final static String template = "000d000 → 000000 …";
public final static String diceTemplate = "000";
public String toString(boolean small) {
String text = "";
switch(mode) {
case SHOW_RESULT:
if (!small) text = " " + Arrays.toString(dices) + text;
text = " → " + Integer.toString(summ) + text;
case ENTER_DICEMAX:
text = "d" + Integer.toString(diceMax) + text;
case ENTER_COUNT:
text = Integer.toString(count) + text;
}
return text;
}
@Override
public String toString() {
return toString(false);
}
}
public static Random random = new Random();
public static List<Drop> drops = new ArrayList<Drop>(Arrays.asList((new Drop[]{new Drop()})));
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
synchronized(drops) {
updateText();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getString(R.string.aboutUri)));
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void resetDices() {
drops.add(new Drop());
}
private void resetDices(Drop drop) {
Drop newdrop = new Drop();
newdrop.count = drop.count;
newdrop.diceMax = drop.diceMax;
newdrop.dices = new int[newdrop.count];
newdrop.summ = 0;
drops.add(newdrop);
}
private Drop getCurrent() {
return drops.get(drops.size()-1);
}
private void dropDices() {
Drop drop = getCurrent();
drop.dices = new int[drop.count];
drop.summ = 0;
for(int i = 0; i < drop.count; i++)
drop.summ += drop.dices[i] = random.nextInt(drop.diceMax) + 1;
}
private void updateText() {
Drop drop = getCurrent();
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(drop.toString());
}
public void onButton(View view) {
synchronized (drops) {
Drop drop = getCurrent();
int numButton = -1;
boolean backspaceButton = false;
boolean diceButton = false;
boolean rollButton = false;
boolean historyButton = false;
switch(view.getId()) {
case R.id.button1: numButton = 1; break;
case R.id.button2: numButton = 2; break;
case R.id.button3: numButton = 3; break;
case R.id.button4: numButton = 4; break;
case R.id.button5: numButton = 5; break;
case R.id.button6: numButton = 6; break;
case R.id.button7: numButton = 7; break;
case R.id.button8: numButton = 8; break;
case R.id.button9: numButton = 9; break;
case R.id.button10: numButton = 0; break;
case R.id.buttonBackspace: backspaceButton = true; break;
case R.id.buttonHistory: historyButton = true; break;
case R.id.buttonDice: diceButton = true; break;
case R.id.buttonRoll: rollButton = true; break;
}
if (historyButton) {
startActivity(new Intent(this, HistoryActivity.class));
} else {
switch(drop.mode) {
case ENTER_COUNT:
if (numButton >= 0) {
if (drop.count < MAX_COUNT_ORDER)
drop.count = drop.count*10 + numButton;
} else
if (backspaceButton) {
drop.count = 0;
} else
if (diceButton) {
if (drop.count == 0) drop.count = 1;
drop.mode = Mode.ENTER_DICEMAX;
} else
if (rollButton) {
if (drop.count == 0) drop.count = 1;
if (drop.diceMax == 0) drop.diceMax = 6;
dropDices();
drop.mode = Mode.SHOW_RESULT;
}
break;
case ENTER_DICEMAX:
if (numButton >= 0) {
if (drop.diceMax < MAX_DICE_ORDER)
drop.diceMax = drop.diceMax*10 + numButton;
} else
if (backspaceButton) {
drop.count = 0;
drop.diceMax = 0;
drop.mode = Mode.ENTER_COUNT;
} else
if (diceButton) {
// do nothing
} else
if (rollButton) {
if (drop.diceMax <= 1) drop.diceMax = 6;
dropDices();
drop.mode = Mode.SHOW_RESULT;
}
break;
case SHOW_RESULT:
if (numButton >= 0) {
resetDices();
getCurrent().count = numButton;
} else
if (backspaceButton) {
resetDices();
} else
if (diceButton) {
resetDices();
getCurrent().count = 1;
getCurrent().mode = Mode.ENTER_DICEMAX;
} else
if (rollButton) {
resetDices(drop);
dropDices();
getCurrent().mode = Mode.SHOW_RESULT;
}
break;
}
updateText();
}
}
}
public void onText(View view) {
if (getCurrent().mode == Mode.SHOW_RESULT) {
Intent intent = new Intent(this, DicesActivity.class);
intent.putExtra("index", drops.size()-1);
startActivity(intent);
}
}
}