The
Photographer
The
Techincal Artist
Maths Game
package org.example.mathgame;
import android.os.Bundle;
import android.app.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import java.util.Random;
public class GameActivity extends Activity implements OnClickListener{
private Button gameButton;
private Button continueButton;
private Button aboutButton;
private Button exitButton;
private TextView expressionTxt;
private TextView displayTxt;
private TextView answer;
private Button button_0;
private Button button_1;
private Button button_2;
private Button button_3;
private Button button_4;
private Button button_5;
private Button button_6;
private Button button_7;
private Button button_8;
private Button button_9;
private Button button_del;
private Button button_check;
private Button button_minus;
private static final int OPERATOR_MINUS = 0;
private static final int OPERATOR_ADD = 1;
private static final int OPERATOR_DIVIDE = 2;
private static final int OPERATOR_MULTIPLY = 3;
private String[] operators = {"+", "-", "x", "/"};
private Random rand;
private int level = 0;
private String CheckAnswer;
private int TenQuestions = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
expressionTxt = (TextView)findViewById(R.id.expression);
displayTxt = (TextView)findViewById(R.id.display);
answer = (TextView)findViewById(R.id.response);
button_0 = (Button)findViewById(R.id.digit_0);
button_1 = (Button)findViewById(R.id.digit_1);
button_2 = (Button)findViewById(R.id.digit_2);
button_3 = (Button)findViewById(R.id.digit_3);
button_4 = (Button)findViewById(R.id.digit_4);
button_5 = (Button)findViewById(R.id.digit_5);
button_6 = (Button)findViewById(R.id.digit_6);
button_7 = (Button)findViewById(R.id.digit_7);
button_8 = (Button)findViewById(R.id.digit_8);
button_9 = (Button)findViewById(R.id.digit_9);
button_del = (Button)findViewById(R.id.del_button);
button_check = (Button)findViewById(R.id.check_button);
button_minus = (Button)findViewById(R.id.minus_button);
button_minus.setOnClickListener(this);
button_del.setOnClickListener(this);
button_check.setOnClickListener(this);
button_0.setOnClickListener(this);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
button_6.setOnClickListener(this);
button_7.setOnClickListener(this);
button_8.setOnClickListener(this);
button_9.setOnClickListener(this);
Bundle playIntent = getIntent().getExtras();
if (playIntent != null) {
int passedLevel = playIntent.getInt("difficulty", -1);
if (passedLevel == 0)
generateN();
if (passedLevel == 1)
generateE();
if (passedLevel == 2)
generateM();
if (passedLevel == 3)
generateG();
}
}
@Override
public void onClick(View view)
{
if(view.getId()==R.id.check_button){
displayTxt.setVisibility(View.VISIBLE);
if(answer.getText().toString().equals(CheckAnswer))
{
displayTxt.setText("CORRECT");
displayTxt.setTextColor(0xff00ff00);
TenQuestions++;
}
else if(answer.getText().toString().endsWith(""))
{
displayTxt.setText("WRONG Correct Answer:" + CheckAnswer);
displayTxt.setTextColor(0xffff0000);
TenQuestions++;
}
if (TenQuestions <=10){
Bundle playIntent = getIntent().getExtras();
answer.setText(" ?");
if (playIntent != null) {
int passedLevel = playIntent.getInt("difficulty", -1);
if (passedLevel == 0)
generateN();
if (passedLevel == 1)
generateE();
if (passedLevel == 2)
generateM();
if (passedLevel == 3)
generateG();
}
if (TenQuestions == 10) {
displayTxt.setText("Finished");
}
}
}
if(view.getId()==R.id.del_button){
int length = answer.length();
if(length > 0) {
answer.setText(answer.getText().toString().substring(0,length-1));
}
}
if(view.getId() == R.id.minus_button){
if((answer.getText().equals(" ?")) || (answer.length() == 0)){
answer.setText("-");
}
}
if(view.getId()==R.id.digit_0){
if(answer.getText().equals(" ?")){
answer.setText("0");
}
else {
answer.setText(answer.getText() + "0");
}
}
if(view.getId() == R.id.digit_1) {
if (answer.getText().equals(" ?")) {
answer.setText("1");
} else {
answer.setText(answer.getText() + "1");
}
}
if(view.getId() == R.id.digit_2) {
if (answer.getText().equals(" ?")) {
answer.setText("2");
} else {
answer.setText(answer.getText() + "2");
}
}
if(view.getId() == R.id.digit_3) {
if (answer.getText().equals(" ?")) {
answer.setText("3");
} else {
answer.setText(answer.getText() + "3");
}
}
if(view.getId() == R.id.digit_4) {
if (answer.getText().equals(" ?")) {
answer.setText("4");
} else {
answer.setText(answer.getText() + "4");
}
}
if(view.getId() == R.id.digit_5) {
if (answer.getText().equals(" ?")) {
answer.setText("5");
} else {
answer.setText(answer.getText() + "5");
}
}
if(view.getId() == R.id.digit_6) {
if (answer.getText().equals(" ?")) {
answer.setText("6");
} else {
answer.setText(answer.getText() + "6");
}
}
if(view.getId() == R.id.digit_7) {
if (answer.getText().equals(" ?")) {
answer.setText("7");
} else {
answer.setText(answer.getText() + "7");
}
}
if(view.getId() == R.id.digit_8) {
if (answer.getText().equals(" ?")) {
answer.setText("8");
} else {
answer.setText(answer.getText() + "8");
}
}
if(view.getId() == R.id.digit_9) {
if (answer.getText().equals(" ?")) {
answer.setText("9");
} else {
answer.setText(answer.getText() + "9");
}
}
}
public void generateN(){
rand = new Random();
int randOperator = rand.nextInt(operators.length);
String randSOperator= "x";
if (randOperator == 0)
{
randSOperator = "+";
}
if (randOperator == 1)
{
randSOperator = "-";
}
if (randOperator == 2)
{
randSOperator = "x";
}
if (randOperator == 3)
{
randSOperator = "/";
}
int randNum = rand.nextInt(10);
int randNum2 = rand.nextInt(10);
int Answer;
expressionTxt.setText(randNum + randSOperator + randNum2 + " = ");
if (randOperator == 0)
{
Answer =(randNum + randNum2);
CheckAnswer = new Integer(Answer).toString();
}
if (randOperator == 1)
{
Answer =(randNum - randNum2);
CheckAnswer = new Integer(Answer).toString();
}
if (randOperator == 2)
{
Answer =(randNum * randNum2);
CheckAnswer = new Integer(Answer).toString();
}
if (randOperator == 3)
{
Answer=(randNum / randNum2);
CheckAnswer = new Integer(Answer).toString();
}
}
public void generateE(){
rand = new Random();
int randOperator = rand.nextInt(operators.length);
int randOperator2 = rand.nextInt(operators.length);
String randSOperator= "x";
String randSOperator2= "x";
if (randOperator == 0)
{
randSOperator = "+";
}
if (randOperator == 1)
{
randSOperator = "-";
}
if (randOperator == 2)
{
randSOperator = "x";
}
if (randOperator == 3)
{
randSOperator = "/";
}
if (randOperator2 == 0)
{
randSOperator2 = "+";
}
if (randOperator2 == 1)
{
randSOperator2 = "-";
}
if (randOperator2 == 2)
{
randSOperator2 = "x";
}
if (randOperator2 == 3)
{
randSOperator2 = "/";
}
int randNum = rand.nextInt(10);
int randNum2 = rand.nextInt(10);
int randNum3 = rand.nextInt(10);
int Answer = 0;
int TotalAnswer;
expressionTxt.setText(randNum + randSOperator + randNum2 + randSOperator2 + randNum3 + " = ");
if (randOperator == 0) {
Answer = (randNum + randNum2);
}
if (randOperator == 1) {
Answer = (randNum - randNum2);
}
if (randOperator == 2) {
Answer = (randNum * randNum2);
}
if (randOperator == 3) {
Answer = (randNum / randNum2);
}
if (randOperator2 == 0) {
TotalAnswer = (Answer + randNum3);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator2 == 1) {
TotalAnswer = (Answer - randNum3);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator2 == 2) {
TotalAnswer = (Answer * randNum3);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator2 == 3) {
TotalAnswer = (Answer / randNum3);
CheckAnswer = new Integer(TotalAnswer).toString();
}
}
public void generateM(){
rand = new Random();
int randOperator = rand.nextInt(operators.length);
int randOperator2 = rand.nextInt(operators.length);
int randOperator3 = rand.nextInt(operators.length);
String randSOperator= "x";
String randSOperator2= "x";
String randSOperator3= "x";
if (randOperator == 0)
{
randSOperator = "+";
}
if (randOperator == 1)
{
randSOperator = "-";
}
if (randOperator == 2)
{
randSOperator = "x";
}
if (randOperator == 3)
{
randSOperator = "/";
}
if (randOperator2 == 0)
{
randSOperator2 = "+";
}
if (randOperator2 == 1)
{
randSOperator2 = "-";
}
if (randOperator2 == 2)
{
randSOperator2 = "x";
}
if (randOperator2 == 3)
{
randSOperator2 = "/";
}
if (randOperator3 == 0)
{
randSOperator3 = "+";
}
if (randOperator3 == 1)
{
randSOperator3 = "-";
}
if (randOperator3 == 2)
{
randSOperator3 = "x";
}
if (randOperator3 == 3)
{
randSOperator3 = "/";
}
int randNum = rand.nextInt(100);
int randNum2 = rand.nextInt(100);
int randNum3 = rand.nextInt(100);
int randNum4 = rand.nextInt(100);
int Answer = 0;
int Answer2 = 0;
int TotalAnswer;
expressionTxt.setText(randNum + randSOperator + randNum2 + randSOperator2 + randNum3 + randSOperator3 + randNum4 + " = ");
if (randOperator == 0) {
Answer = (randNum + randNum2);
}
if (randOperator == 1) {
Answer = (randNum - randNum2);
}
if (randOperator == 2) {
Answer = (randNum * randNum2);
}
if (randOperator == 3) {
Answer = (randNum / randNum2);
}
if (randOperator2 == 0) {
Answer2 = (Answer + randNum3);
}
if (randOperator2 == 1) {
Answer2 = (Answer - randNum3);
}
if (randOperator2 == 2) {
Answer2 = (Answer * randNum3);
}
if (randOperator2 == 3) {
Answer2 = (Answer / randNum3);
}
if (randOperator3 == 0) {
TotalAnswer = (Answer2 + randNum4);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator3 == 1) {
TotalAnswer = (Answer2 - randNum4);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator3 == 2) {
TotalAnswer = (Answer2 * randNum4);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator3 == 3) {
TotalAnswer = (Answer2 / randNum4);
CheckAnswer = new Integer(TotalAnswer).toString();
}
}
public void generateG(){
rand = new Random();
int randOperator = rand.nextInt(operators.length);
int randOperator2 = rand.nextInt(operators.length);
int randOperator3 = rand.nextInt(operators.length);
int randOperator4 = rand.nextInt(operators.length);
String randSOperator= "x";
String randSOperator2= "x";
String randSOperator3= "x";
String randSOperator4= "x";
if (randOperator == 0)
{
randSOperator = "+";
}
if (randOperator == 1)
{
randSOperator = "-";
}
if (randOperator == 2)
{
randSOperator = "x";
}
if (randOperator == 3)
{
randSOperator = "/";
}
if (randOperator2 == 0)
{
randSOperator2 = "+";
}
if (randOperator2 == 1)
{
randSOperator2 = "-";
}
if (randOperator2 == 2)
{
randSOperator2 = "x";
}
if (randOperator2 == 3)
{
randSOperator2 = "/";
}
if (randOperator3 == 0)
{
randSOperator3 = "+";
}
if (randOperator3 == 1)
{
randSOperator3 = "-";
}
if (randOperator3 == 2)
{
randSOperator3 = "x";
}
if (randOperator3 == 3)
{
randSOperator3 = "/";
}
if (randOperator4 == 0)
{
randSOperator4 = "+";
}
if (randOperator4 == 1)
{
randSOperator4 = "-";
}
if (randOperator4 == 2)
{
randSOperator4 = "x";
}
if (randOperator4 == 3)
{
randSOperator3 = "/";
}
int randNum = rand.nextInt(100);
int randNum2 = rand.nextInt(100);
int randNum3 = rand.nextInt(100);
int randNum4 = rand.nextInt(100);
int randNum5 = rand.nextInt(100);
int Answer = 0;
int Answer2 = 0;
int Answer3 = 0;
int TotalAnswer;
expressionTxt.setText(randNum + randSOperator + randNum2 + randSOperator2 + randNum3 + randSOperator3 + randNum4 + randSOperator4 + randNum5 +" = ");
if (randOperator == 0) {
Answer = (randNum + randNum2);
}
if (randOperator == 1) {
Answer = (randNum - randNum2);
}
if (randOperator == 2) {
Answer = (randNum * randNum2);
}
if (randOperator == 3) {
Answer = (randNum / randNum2);
}
if (randOperator2 == 0) {
Answer2 = (Answer + randNum3);
}
if (randOperator2 == 1) {
Answer2 = (Answer - randNum3);
}
if (randOperator2 == 2) {
Answer2 = (Answer * randNum3);
}
if (randOperator2 == 3) {
Answer2 = (Answer / randNum3);
}
if (randOperator3 == 0) {
Answer3 = (Answer2 + randNum4);
}
if (randOperator3 == 1) {
Answer3 = (Answer2 - randNum4);
}
if (randOperator3 == 2) {
Answer3 = (Answer2 * randNum4);
}
if (randOperator3 == 3) {
Answer3 = (Answer2 / randNum4);
}
if (randOperator4 == 0) {
TotalAnswer = (Answer3 + randNum5);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator4 == 1) {
TotalAnswer = (Answer3 - randNum5);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator4 == 2) {
TotalAnswer = (Answer3 * randNum5);
CheckAnswer = new Integer(TotalAnswer).toString();
}
if (randOperator4 == 3) {
TotalAnswer = (Answer3 / randNum5);
CheckAnswer = new Integer(TotalAnswer).toString();
}
}
}
//Main Activity.java///
package org.example.mathgame;
import android.app.*;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private Button gameButton;
private Button continueButton;
private Button aboutButton;
private Button exitButton;
private AlertDialog aDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gameButton = (Button)findViewById(R.id.game_button);
gameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("Select Difficulty")
.setItems(R.array.difficulty_level, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startPlay(which);
dialog.dismiss();
}
});
aDialog = builder.show();
}
});
continueButton = (Button)findViewById(R.id.continue_button);
continueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("Select Difficulty")
.setItems(R.array.difficulty_level, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startPlay(which);
dialog.dismiss();
}
});
aDialog = builder.show();
}
});
aboutButton = (Button)findViewById(R.id.about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("About Math Game");
builder.setMessage("This is a game for people who want to test out their maths ability. " +
"Solve the questions above the buttons in time to score points. " +
"Created by Jaime M. Da Cunha");
builder.setCancelable(false);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
aDialog = builder.show();
}
});
exitButton = (Button)findViewById(R.id.exit_button);
exitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
public void startPlay(int difficulty){
Intent playIntent = new Intent(this, GameActivity.class);
playIntent.putExtra("difficulty", difficulty);
this.startActivity(playIntent);
}
}