社内勉強会の近況
先週から会社に新たなプログラマが加わり、毎日のように勉強会に明け暮れてます。
なので、最近はバックエンドに関する記事が書けない状態ですw
彼はイタリア出身の方で、幼い頃からPascalを触ったりゲームをしたりと、かなりプログラマとしての適性を備えていますw
最近は僕がRailsで作ったアプリケーションと連携できるモバイルアプリケーションを作ってもらうために、XamarinでC#のアプリケーションを作る練習をしてもらっています。
昨日まではXamarinのInterface APIsとC#のデータ構造を勉強するために、世界時計と数字当てゲームを作成したので、作成したコードをGithubとGISTに公開しています。
全然このブログの方針から外れている気もしますが、とりあえずコードのURLをのっけときますw
Number Suggestion
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using System.Collections; // The program generates 3 random numbers and the user must guess them. Guess the number isn't enough though! // The user must also find out the right order in which order they are. namespace GuessTheNumber { [Activity (Label = "GuessTheNumber", MainLauncher = true)] public class MainActivity : Activity { int first,second,third; int numberOfGuess = 0; Random r = new Random(); EditText guess1,guess2,guess3; String guessString1,guessString2,guessString3; TextView matchLabel,orderLabel,scoreLabel; Button guessButton; int answerArray = new int[3]; int guessArray = new int[3]; ArrayList answerArrayList = new ArrayList(); protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); //generates 3 random numbers first = r.Next(10)+1; second = r.Next(10)+1; third = r.Next(10)+1; // puts them into an array answerArray [0] = first; answerArray [1] = second; answerArray [2] = third; Console.WriteLine ("The random numbers are " + first + " " + second + " " + third); //Initializes the widget variables matchLabel = FindViewById<TextView> (Resource.Id.matchNumber); orderLabel= FindViewById<TextView> (Resource.Id.orderNumber); scoreLabel= FindViewById<TextView> (Resource.Id.score); guess1 = FindViewById<EditText> (Resource.Id.editText1); guess2 = FindViewById<EditText> (Resource.Id.editText2); guess3 = FindViewById<EditText> (Resource.Id.editText3); guessButton = FindViewById<Button> (Resource.Id.button1); //sets up a listener for the button guessButton.Click += (object sender, EventArgs e) => { guessString1 = *1 {// we have a hit Console.WriteLine ("Yes " + guess + " matches"); int lastIndexOf = answerArrayList.LastIndexOf (guess); answerArrayList.RemoveAt (lastIndexOf); match++; } else {// no hit, the guess is wrong Console.WriteLine ("Sorry, your guess is wrong. " + guess + " is not an answer"); } } // updates the GUI matchLabel.Text = match.ToString(); Console.WriteLine (match + " match(es) " + order + " order"); // Check if the game is over if (order == 3 && match == 3) { scoreLabel.Text = "You scored : " + getScore (); } } // Helper function to score the user public int getScore(){ int score = 110 - numberOfGuess*10; if (score < 0) { score = 0; } return score; } } }
WorldClock
やっぱりアプリケーションを作りながら勉強するのが一番楽しいですね!
*1:EditText)guess1).Text; guessString2 = ((EditText)guess2).Text; guessString3 = ((EditText)guess3).Text; // to avoid parsing error (empty string to int) if ((!guessString1.Equals(""))){ guessArray [0] = Int32.Parse(guessString1); } if ((!guessString2.Equals(""))){ guessArray [1] = Int32.Parse(guessString2); } if ((!guessString3.Equals(""))){ guessArray [2] = Int32.Parse(guessString3); } Console.WriteLine("You guesses are "+ guessString1 + " " + guessString2 + " " + guessString3); checkGuesses(); }; } //Helper function that compares the guesses with the answers. It also update the GUI private void checkGuesses(){ //So that we can score the user numberOfGuess ++; int order = 0; int match = 0; //Check if the answers are in the right order for (int i=0; i < 3; i++){ Console.WriteLine("Checking if the order is correct. ANSWER: "+answerArray [i] + " and GUESS: " +guessArray [i]); if (answerArray [i] == guessArray [i]) { order++; Console.WriteLine ("This answer is correct. You score one point in 'order'"); } } // updates the GUI orderLabel.Text = order.ToString(); // the answers get fed into an ArrayList (so that we can resize it) foreach (int answer in answerArray) { answerArrayList.Add (answer); Console.WriteLine ("Putting " + answer + " inside ArrayList"); } // Checks whether the guesses match the answers foreach (int guess in guessArray) { Console.WriteLine ("Checking if :" + guess+ " is inside the ArrayList"); if (answerArrayList.Contains (guess