Androidアプリ(C#)

ポートフォリオ用に
「時計学習アプリ」

MainActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Runtime;
using AndroidX.AppCompat.App;
using Android.Widget;
using Android.Graphics;
using System;
using Android.Webkit;

namespace clockExercize01
{
   [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
   public class MainActivity : Activity
   {

       protected override void OnCreate(Bundle savedInstanceState)
       {
           base.OnCreate(savedInstanceState);
           Xamarin.Essentials.Platform.Init(this, savedInstanceState);
           // Set our view from the "main" layout resource
           SetContentView(Resource.Layout.activity_main);


           ImageView button11 = FindViewById<ImageView>(Resource.Id.button11);
           button11.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice01 = new Intent(Application.Context, typeof(ActivityPractice01));
               i_practice01.PutExtra("difficulty", "easy");
               i_practice01.AddFlags(ActivityFlags.ClearTop);
               StartActivity(i_practice01);
           };

           ImageView button12 = FindViewById<ImageView>(Resource.Id.button12);
           button12.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice01 = new Intent(this, typeof(ActivityPractice01));
               i_practice01.PutExtra("difficulty", "normal");
               this.StartActivity(i_practice01);
           };

           ImageView button13 = FindViewById<ImageView>(Resource.Id.button13);
           button13.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice01 = new Intent(this, typeof(ActivityPractice01));
               i_practice01.PutExtra("difficulty", "hard");
               this.StartActivity(i_practice01);
           };

           ImageView button21 = FindViewById<ImageView>(Resource.Id.button21);
           button21.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice02 = new Intent(this, typeof(ActivityPractice02));
               i_practice02.PutExtra("difficulty", "easy");
               this.StartActivity(i_practice02);
           };

           ImageView button22 = FindViewById<ImageView>(Resource.Id.button22);
           button22.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice02 = new Intent(this, typeof(ActivityPractice02));
               i_practice02.PutExtra("difficulty", "normal");
               this.StartActivity(i_practice02);
           };

           ImageView button23 = FindViewById<ImageView>(Resource.Id.button23);
           button23.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice02 = new Intent(this, typeof(ActivityPractice02));
               i_practice02.PutExtra("difficulty", "hard");
               this.StartActivity(i_practice02);
           };

           ImageView button24 = FindViewById<ImageView>(Resource.Id.button24);
           button24.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice02 = new Intent(this, typeof(ActivityPractice02));
               i_practice02.PutExtra("difficulty", "veryhard");
               this.StartActivity(i_practice02);
           };

           ImageView button31 = FindViewById<ImageView>(Resource.Id.button31);
           button31.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice03 = new Intent(this, typeof(ActivityPractice03));
               i_practice03.PutExtra("difficulty", "easy");
               this.StartActivity(i_practice03);
           };

           ImageView button32 = FindViewById<ImageView>(Resource.Id.button32);
           button32.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice03 = new Intent(this, typeof(ActivityPractice03));
               i_practice03.PutExtra("difficulty", "normal");
               this.StartActivity(i_practice03);
           };

           ImageView button33 = FindViewById<ImageView>(Resource.Id.button33);
           button33.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice03 = new Intent(this, typeof(ActivityPractice03));
               i_practice03.PutExtra("difficulty", "hard");
               this.StartActivity(i_practice03);
           };

           ImageView button34 = FindViewById<ImageView>(Resource.Id.button34);
           button34.Click += (object sender, EventArgs e) =>
           {
               Intent i_practice03 = new Intent(this, typeof(ActivityPractice03));
               i_practice03.PutExtra("difficulty", "veryhard");
               this.StartActivity(i_practice03);
           };

       }
   }
}

ActivityPractice03.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Environment = System.Environment;

namespace clockExercize01
{
   [Activity(Label = "Activity1")]
   public class ActivityPractice03 : Activity
   {
       public static int[] handsRot;
       public static int[] preValue = new int[2] { 0, 0 };
       public static int questNumber;
       public static bool wrongFlag;
       public static int correctCount;

       protected override void OnCreate(Bundle savedInstanceState)
       {
           base.OnCreate(savedInstanceState);

           // Create your application here

           SetContentView(Resource.Layout.activity_practice_03);

           questNumber = 1;
           wrongFlag = false;
           correctCount = 0;

           NumberPicker numberPickerHours = FindViewById<NumberPicker>(Resource.Id.numberPickerHours);
           numberPickerHours.MaxValue = 11;
           numberPickerHours.MinValue = 0;

           NumberPicker numberPickerMinutes = FindViewById<NumberPicker>(Resource.Id.numberPickerMinutes);
           numberPickerMinutes.MaxValue = 59;
           numberPickerMinutes.MinValue = 00;

           ImageView longHands = FindViewById<ImageView>(Resource.Id.myClockLong);
           ImageView shortHands = FindViewById<ImageView>(Resource.Id.myClockShort);
           ImageView nextLongHands = FindViewById<ImageView>(Resource.Id.myNextClockLong);
           ImageView nextShortHands = FindViewById<ImageView>(Resource.Id.myNextClockShort);
           ImageView answerButton = FindViewById<ImageView>(Resource.Id.answerButton);
           ImageView nextButton = FindViewById<ImageView>(Resource.Id.nextButton);
           ImageView circle = FindViewById<ImageView>(Resource.Id.circle);
           TextView questText = FindViewById<TextView>(Resource.Id.questText);
           TextView questNum = FindViewById<TextView>(Resource.Id.questNum);


           var intent = this.Intent;
           if (intent != null && intent.HasExtra("difficulty"))
           {
               string difficulty = intent.GetStringExtra("difficulty");
               System.Diagnostics.Debug.WriteLine(intent.HasExtra("difficulty"));
               System.Diagnostics.Debug.WriteLine(difficulty);


               displayQuestion(longHands, shortHands, nextLongHands, nextShortHands, difficulty, questText, questNum);

               answerButton.Click += async (object sender, EventArgs e) =>
               {
                   int id;
                   int answerValue = numberPickerHours.Value * 60 + numberPickerMinutes.Value;


                   if (answerValue == handsRot[1])
                   {
                       if (wrongFlag == false)
                       {
                           correctCount++;
                       }
                       wrongFlag = false;

                       id = Resource.Raw.Answer01;
                       if (questNumber < 5)
                       {
                           circle.Visibility = ViewStates.Visible;
                           nextButton.Visibility = ViewStates.Visible;
                           answerButton.Visibility = ViewStates.Gone;
                       }
                       else
                       {
                           Intent result = new Intent(Application.Context, typeof(ActivityResult));
                           result.PutExtra("score", correctCount * 20);
                           result.PutExtra("difficulty", difficulty);
                           result.PutExtra("activity", "ActivityPractice03");
                           result.AddFlags(ActivityFlags.ClearTop);
                           StartActivity(result);
                       }
                   }
                   else
                   {
                       id = Resource.Raw.Buzzer01;

                       wrongFlag = true;
                   }
                   await PlayAsync(id);

               };

               // 再生が終了、またはエラーだったら mpを解放する
               Task<bool> PlayAsync(int rscId)
               {
                   var compSource = new TaskCompletionSource<bool>();
                   var mp = MediaPlayer.Create(this, rscId);

                   mp.Completion += (_, __) =>
                   {
                       mp.Release();
                   };

                   mp.Error += (_, __) =>
                   {
                       mp.Release();
                   };

                   mp.Start();
                   return compSource.Task;
               }

               nextButton.Click += (object sender, EventArgs e) =>
               {
                   questNumber++;

                   circle.Visibility = ViewStates.Gone;
                   nextButton.Visibility = ViewStates.Gone;
                   answerButton.Visibility = ViewStates.Visible;
                   displayQuestion(longHands, shortHands, nextLongHands, nextShortHands, difficulty,questText, questNum);
               };


           }
       }

       public static int[] createQuestion(string difficulty)
       {
           System.Diagnostics.Debug.WriteLine(difficulty);

           int seed = Environment.TickCount;
           Random r1 = new Random(seed++);
           //Random r2 = new Random(seed++);
           //Random r3 = new Random(seed++);
           //Random r4 = new Random(seed++);

           int[] ret = new int[3] ;

           switch (difficulty)
           {
               case "easy":
                   ret[0] = r1.Next(0, 12) * 60;
                   ret[1] = r1.Next(1, 12) * 60;
                   break;

               case "normal":
                   switch (r1.Next(0, 2))
                   {
                       case 0:
                           ret[0] = r1.Next(0, 12) * 60;
                           ret[1] = r1.Next(1, 720);
                           break;

                       case 1:
                           ret[0] = r1.Next(0, 144) * 5;
                           ret[1] = r1.Next(1, 12) * 60;
                           break;

                   }
                   break;

               case "hard":
                   switch (r1.Next(0, 3))
                   {
                       case 0:
                           ret[0] = r1.Next(0, 12) * 60;
                           ret[1] = r1.Next(1, 720);
                           break;

                       case 1:
                           ret[0] = r1.Next(0, 144) * 5;
                           ret[1] = r1.Next(1, 144) * 5;
                           break;

                       case 2:
                           ret[0] = r1.Next(0, 720);
                           ret[1] = r1.Next(1, 12) * 60;
                           break;
                   }
                   break;

               case "veryhard":
                   ret[0] = r1.Next(0, 720);
                   ret[1] = r1.Next(1, 720);
                   break;

           }

           if (ret[0] == preValue[0] && ret[1] == preValue[1])
           {
               createQuestion(difficulty);
           }
           else
           {
               preValue = ret;
           }

           ret[2] = r1.Next(0, 2);
           System.Diagnostics.Debug.WriteLine(ret[2]);
           return ret;
       }

       public static void displayQuestion( ImageView longHands, ImageView shortHands, ImageView nextLongHands, ImageView nextShortHands, string difficulty, TextView questText, TextView questNum)
       {
           questNum.Text = questNumber + "問目(全5問)";

           handsRot = createQuestion(difficulty);

           int rotHours = handsRot[0] / 2;
           int rotMinutes = (handsRot[0] % 60) * 6;

           int nextRot = (handsRot[0] + handsRot[1]) % 720;
           int nextRotHours = nextRot / 2;
           int nextRotMinutes = (nextRot % 60) *6;

           longHands.Rotation = rotMinutes;
           shortHands.Rotation = rotHours;

           nextLongHands.Rotation = nextRotMinutes;
           nextShortHands.Rotation = nextRotHours;

           if(difficulty == "easy")
           {
               questText.Text = "右の時計は左の時計の何時間あと?";
           }
           else
           {
               questText.Text = "右の時計は左の時計の何時間何分あと?";
           }
           

           
       }
   }

   
}

ActivityResult.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Gms.Ads;

namespace clockExercize01
{
   [Activity(Label = "Activity1")]
   public class ActivityResult : Activity
   {
       protected override void OnCreate(Bundle bundle)
       {
           base.OnCreate(bundle);

           // Create your application here
           //アプリIDを設定する
           MobileAds.Initialize(this);

           SetContentView(Resource.Layout.activity_result);

           TextView retryButton = FindViewById<TextView>(Resource.Id.retryButton);
           TextView returnButton = FindViewById<TextView>(Resource.Id.returnButton);
           TextView scoreText = FindViewById<TextView>(Resource.Id.score);
           TextView commentText = FindViewById<TextView>(Resource.Id.commentText);
           var intent = this.Intent;
           string activity = intent.GetStringExtra("activity");
           string difficulty = intent.GetStringExtra("difficulty");
           int score = intent.GetIntExtra("score",0);
           scoreText.Text = Convert.ToString(score);

           //広告用ビューの設定を行う
           AdView adView = FindViewById<AdView>(Resource.Id.adView);
           var builder = new AdRequest.Builder();
           AdRequest adRequest = builder.Build();

           adView.LoadAd(adRequest);


           System.Diagnostics.Debug.WriteLine(score);
           switch (score)
           {
               case 100:
                   commentText.Text = "すばらしい!";
                   break;

               case int i when i >= 80:
                   commentText.Text = "よくできました";
                   break;

               case int i when i >= 50:
                   commentText.Text = "もうすこし";
                   break;

               default:
                   commentText.Text = "がんばりましょう";
                   break;

           }

           retryButton.Click += (object sender, EventArgs e) =>
           {
               Intent reatry_practice = new Intent();
               switch (activity)
               {
                   case "ActivityPractice01":
                       reatry_practice = new Intent(Application.Context, typeof(ActivityPractice01));
                       break;

                   case "ActivityPractice02":
                       reatry_practice = new Intent(Application.Context, typeof(ActivityPractice02));
                       break;

                   case "ActivityPractice03":
                       reatry_practice = new Intent(Application.Context, typeof(ActivityPractice03));
                       break;

               }


               reatry_practice.PutExtra("difficulty", difficulty);
               reatry_practice.AddFlags(ActivityFlags.ClearTop);

               StartActivity(reatry_practice);
           };

           returnButton.Click += (object sender, EventArgs e) =>
           {
               Intent return_title = new Intent(Application.Context, typeof(MainActivity));

               return_title.PutExtra("difficulty", difficulty);
               return_title.AddFlags(ActivityFlags.ClearTop);

               StartActivity(return_title);
           };
       }
   }
}


この記事が気に入ったらサポートをしてみませんか?