LoginSignup
0
1

More than 3 years have passed since last update.

C#バグ奮闘記 1日目

Last updated at Posted at 2020-05-20

どうも、べです。
プログラミングの知識はほぼ皆無です。 今はC#でテトリスが作れるようになることが目標です。コロナで自宅謹慎、今後仕事で使うであろうC#を独学する日々です。意見やアドバイスなどコメント頂けると嬉しいです。

C#の自己紹介のようなテキストを一通り終えたので、実際にプログラムを作ってみようと思い、マイクロソフト公式のチュートリアル↓をやっています。
https://docs.microsoft.com/ja-jp/previous-versions/visualstudio/visual-studio-2013/dd492171%28v=vs.120%29

Visual BasicとC#の2つの言語のどちらかで3つの簡単(多分?)なプログラム(ピクチャビューア、計算クイズ、絵合わせゲーム)を作ってみようってやつです。
マイクロソフト公式だし、あまり楽しさは期待していませんでしたがやってみると結構面白い。計算クイズなんかは作っていて、2ケタx2ケタも30秒以内に計算するとか考えたら結構むずくね?とか色々考えてしまいます。

ページ内でVBとC#のコードの切り替えができる?っぽいのですが僕はそのやり方が分からないので言語を変換してくれるオンラインページ↓を使っていました。
https://converter.telerik.com/

それで、説明が丁寧なので特に問題なく来ていましたが、3つ目の絵合わせゲームで初めてバグと戦うことになりました。
コメントで//xってしているのがバグ↓の発生した部分です。

CS0825 C# The contextual keyword 'var' may only appear within a local variable declaration or in script code

「そんなとこにvar使わんといてくれ」みたいなことを言われている気がします。Listオブジェクト使うときには型を指定しないといけない?んですかね。
ちょっとネットで調べてみて↓、//xの下にあるように修正したら直りました(これが何を意味するのかはよく分からない)。
https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers

public partial class Form1 : Form
{
    //random icons
    private Random random = new Random();

    //icons list of webdings font
    //x        private var icons = new List<string>()
    //x       { "!", "!", "N", "N", ",", ",", "k", "k","b", "b", "v", "v", "w", "w", "z", "z" };

    private List <string> icons = new List<string>
    { "!", "!", "N", "N", ",", ",", "k", "k","b", "b", "v", "v", "w", "w", "z", "z" };

また、Listオブジェクトを呼び出す時にもバグ↓が発生しました。

CS1955 C# Non-invocable member cannot be used like a method.

よく分からないけど、使い方を間違ってるよーと言われている気がします。これは以下のサイト↓のように、配列と同じように[]でくくってあげることで解決しました。
https://programming.pc-note.net/csharp/list.html

    private void AssignIconsToSquares()
    {
        //pull icon from the list and add to label
        foreach(var control in tableLayoutPanel1.Controls)
        {
            var iconLabel = control as Label;
            if (iconLabel != null)
            {
                var randomNumber = random.Next(icons.Count);
                //x iconLabel.Text = icons(randomNumber);
                iconLabel.Text = icons[randomNumber];
                //iconLabel.ForeColor = iconLabel.BackColor;
                icons.RemoveAt(randomNumber);
            }
        }
    }

最後の勝利判定では本文の中にもcongraturations!を入れたかったので改行したく、C#では\rだけで改行できるということを調べて知りました↓。
https://dobon.net/vb/dotnet/string/newline.html

    private void CheckForWinner()
    {
        //go through all the labels
        //check all matched
        foreach (var control in tableLayoutPanel1.Controls)
        {
            var iconLabel = control as Label;
            if (iconLabel != null && iconLabel.ForeColor == iconLabel.BackColor)
            {
                return;
            }
        }

        //if the loop didn't return,
        //it did't find any unmatched icons
        //show a message "user won"
        MessageBox.Show("You matched all the icons! \r congraturations!(-ω☆)キラリ", "congraturations!");
        Close();
    }

とりあえずこんな感じで楽しいゲームができました。
素朴なゲームですけど、自分で作るとすごく楽しく感じますよね。
絵文字の代わりに用意した画像で絵合わせしたり、手数制限を加えたりするともっと面白くなると思いました。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1