thecoderenroute

Latest

May 14th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.72 KB | None | 0 0
  1. package com.example.s.braintest;
  2.  
  3. import android.content.Intent;
  4. import android.icu.text.DecimalFormat;
  5. import android.os.CountDownTimer;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.text.Layout;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.RelativeLayout;
  13. import android.widget.TextView;
  14.  
  15. import java.lang.*;
  16.  
  17. import org.w3c.dom.Text;
  18.  
  19. import java.util.ArrayList;
  20.  
  21. import static com.example.s.braintest.GetData.lim;
  22. import static com.example.s.braintest.GetData.noq;
  23. import static com.example.s.braintest.MainActivity.scores;
  24.  
  25. public class Test extends AppCompatActivity {
  26.  
  27.     RelativeLayout game;
  28.     RelativeLayout over;
  29.     TextView question;
  30.     TextView questionNo;
  31.     TextView tscore;
  32.     TextView time;
  33.     TextView finalScore;
  34.     Button opt0;
  35.     Button opt1;
  36.     Button opt2;
  37.     Button opt3;
  38.     Button gameexit;
  39.     ArrayList<Double> options;
  40.     DecimalFormat df;
  41.     int score;
  42.     int fscore;
  43.  
  44.     char sign;
  45.     int signId;
  46.     int locationOfAnswer;
  47.     int q1;
  48.  
  49.  
  50.     public void startGame() {
  51.         game.setVisibility(View.VISIBLE);
  52.         score = 0;
  53.         generateQ(0);
  54.     }
  55.  
  56.     public void generateQ(int q) {
  57.         q1 = q;
  58.         if (q1 < noq) {
  59.             options = new ArrayList<>();
  60.             double answer;
  61.  
  62.             df = new DecimalFormat();
  63.             StringBuilder str = new StringBuilder();
  64.  
  65.             signId = (int) (Math.random() * 4);
  66.             switch (signId) {
  67.                 case 0:
  68.                     sign = '+';
  69.  
  70.                     break;
  71.                 case 1:
  72.                     sign = '-';
  73.                     break;
  74.                 case 2:
  75.                     sign = '*';
  76.  
  77.                     break;
  78.                 case 3:
  79.                     sign = '/';
  80.  
  81.                     break;
  82.                 default:
  83.                     sign = '+';
  84.             }
  85.  
  86.             if(signId<2){
  87.                 double a,b;
  88.                 a = Math.random() * lim;
  89.                 b = Math.random() * lim;
  90.                 if(signId == 0){
  91.                     answer = a+b;
  92.                 }
  93.                 else{
  94.                     if(b>a){
  95.                         double t = a;
  96.                         a=b;
  97.                         b=t;
  98.                     }
  99.                     answer = a-b;
  100.                 }
  101.  
  102.                 question.setText(String.valueOf(df.format(a) + sign + df.format(b)));
  103.             }
  104.             else{
  105.                 double a,b;
  106.                 a = Math.round(Math.random()*lim);
  107.                 b = Math.round(Math.random()*lim);
  108.                 if(signId == 2){
  109.                     answer = (a*b);
  110.                 }
  111.                 else{
  112.                     if(b>a){
  113.                         double t = a;
  114.                         a=b;
  115.                         b=t;
  116.                     }
  117.                     answer = (a/b);
  118.                 }
  119.                 str.append(a);
  120.                 str.append(sign);
  121.                 str.append(b);
  122.                 Log.i("str",str.toString());
  123.                 question.setText(str);
  124.             }
  125.  
  126.             String qnos = q1 + "/" + noq;
  127.             questionNo.setText(qnos);
  128.             String scorS = score + "/" + q1;
  129.             tscore.setText(scorS);
  130.             locationOfAnswer = (int) (Math.random() * 4);
  131.  
  132.             for (int i = 0; i < 4; i++) {
  133.                 if (i == locationOfAnswer) {
  134.                     //Log.i("Options test", "adding to " + locationOfAnswer + " the value " + answer);
  135.                     options.add(answer);
  136.                     // Log.i("Options after adding: ", options.toString());
  137.                 } else {
  138.                     double temp = Math.random() * (answer * 2);
  139.                     options.add(temp);
  140.                     //Log.i("Options after adding: ", options.toString());
  141.                 }
  142.             }
  143.  
  144.             opt0.setText(String.valueOf(df.format(options.get(0))));
  145.             opt1.setText(String.valueOf(df.format(options.get(1))));
  146.             opt2.setText(String.valueOf(df.format(options.get(2))));
  147.             opt3.setText(String.valueOf(df.format(options.get(3))));
  148. /*
  149.             Log.i("Options: ", options.toString());
  150.             Log.i("Option 1:", opt0.getText().toString());
  151.             Log.i("Option 2:", opt1.getText().toString());
  152.             Log.i("Option 3:", opt2.getText().toString());
  153.             Log.i("Option 4:", opt3.getText().toString());
  154. */
  155.  
  156.             final CountDownTimer ct = new CountDownTimer(15000, 1000) {
  157.                 @Override
  158.                 public void onTick(long millisUntilFinished) {
  159.                     time.setText(String.valueOf((int) (millisUntilFinished / 1000)));
  160.                 }
  161.  
  162.                 @Override
  163.                 public void onFinish() {
  164.                     generateQ(++q1);
  165.                 }
  166.             }.start();
  167.             opt0.setOnClickListener(new View.OnClickListener() {
  168.                 @Override
  169.                 public void onClick(View v) {
  170.                     // selected = 0;
  171.                     ct.cancel();
  172.                     scoreCheck(0);
  173.                     generateQ(++q1);
  174.                 }
  175.             });
  176.             opt1.setOnClickListener(new View.OnClickListener() {
  177.                 @Override
  178.                 public void onClick(View v) {
  179.                     // selected = 1;
  180.                     ct.cancel();
  181.                     scoreCheck(1);
  182.                     generateQ(++q1);
  183.                 }
  184.             });
  185.             opt2.setOnClickListener(new View.OnClickListener() {
  186.                 @Override
  187.                 public void onClick(View v) {
  188.                     // selected = 2;
  189.                     ct.cancel();
  190.                     scoreCheck(2);
  191.                     generateQ(++q1);
  192.                 }
  193.             });
  194.             opt3.setOnClickListener(new View.OnClickListener() {
  195.                 @Override
  196.                 public void onClick(View v) {
  197.                     // selected = 3;
  198.                     ct.cancel();
  199.                     scoreCheck(3);
  200.                     generateQ(++q1);
  201.                 }
  202.             });
  203.  
  204.         } else {
  205.             Log.i("Info", "GameEnded");
  206.             endGame();
  207.         }
  208.     }
  209.  
  210.     public void scoreCheck(int sel) {
  211.         if (sel == locationOfAnswer) {
  212.             score++;
  213.         }
  214.     }
  215.  
  216.     public int getInteger(double d) {
  217.         return (int) Math.round(d);
  218.     }
  219.  
  220.     public void setQuestion(double d1, double d2) {
  221.         df.setMaximumFractionDigits(2);
  222.         question.setText(df.format(d1) + "" + sign + "" + df.format(d2));
  223.  
  224.  
  225.     }
  226.  
  227.     public void setQuestion(int i1, int i2) {
  228.         question.setText(i1 + "" + sign + "" + i2);
  229.     }
  230.  
  231.     public void gameExit(View view) {
  232.         Intent i = new Intent(this, MainActivity.class);
  233.         startActivity(i);
  234.     }
  235.  
  236.     public void endGame() {
  237.         over.setVisibility(View.VISIBLE);
  238.         game.setVisibility(View.INVISIBLE);
  239.         fscore = score;
  240.         finalScore.setText(String.valueOf("SCORE: " + fscore));
  241.         scores.add(String.valueOf(fscore) + "/" + String.valueOf(noq));
  242.     }
  243.  
  244.     @Override
  245.     protected void onCreate(Bundle savedInstanceState) {
  246.         super.onCreate(savedInstanceState);
  247.         setContentView(R.layout.activity_test);
  248.         game = (RelativeLayout) findViewById(R.id.game);
  249.         over = (RelativeLayout) findViewById(R.id.ender);
  250.         over.setVisibility(View.INVISIBLE);
  251.         game.setVisibility(View.INVISIBLE);
  252.         gameexit = (Button) findViewById(R.id.exiter);
  253.         //gameexit.setVisibility(View.INVISIBLE);
  254.         question = (TextView) findViewById(R.id.question);
  255.         questionNo = (TextView) findViewById(R.id.qno);
  256.         tscore = (TextView) findViewById(R.id.score);
  257.         finalScore = (TextView) findViewById(R.id.fScore);
  258.         //finalScore.setVisibility(View.INVISIBLE);
  259.         time = (TextView) findViewById(R.id.Timer);
  260.         opt0 = (Button) findViewById(R.id.option0);
  261.         opt1 = (Button) findViewById(R.id.option1);
  262.         opt2 = (Button) findViewById(R.id.option2);
  263.         opt3 = (Button) findViewById(R.id.option3);
  264.  
  265.         final TextView starter = (TextView) findViewById(R.id.starter);
  266.         new CountDownTimer(5000, 1000) {
  267.             @Override
  268.             public void onTick(long millisUntilFinished) {
  269.                 starter.setText(String.valueOf((int) (millisUntilFinished / 1000)));
  270.             }
  271.  
  272.             @Override
  273.             public void onFinish() {
  274.                 starter.setText(String.valueOf("Begin"));
  275.                 starter.setVisibility(View.INVISIBLE);
  276.                 startGame();
  277.             }
  278.         }.start();
  279.     }
  280. }
Add Comment
Please, Sign In to add comment