Home>

I am making a rock-paper-scissors game with Visual Sutdio.
The cumulative wins and losses are displayed in characters, and if you win 5 times first, the number of wins, the number of losses, and the number of divisions are displayed in the window.

If i win 5 times, the result of winning or losing will be displayed in the window, but the value is different between the displayed characters and MessageBox.
To be exact, it will be displayed in the MessageBox before the displayed characters change. When you close the MessageBox, the characters will have the same value as the MessageBox.
How can this be improved?

Error message
Corresponding source code
   public partial class Form1: Form
    {
        Random r = new Random ();
        int [] array_match_result = new int [0];

        public Form1 ()
        {
            InitializeComponent ();
        }
        private void Form1_Load (object sender, EventArgs e)
        {
            this.picCPU.Image = null;
            this.picYou.Image = null;
            this.lblResult.Text = "";
        }
        private void btnGoo_Click (object sender, EventArgs e)
        {
            MatchStart (0);
        }
        private void btnChoki_Click (object sender, EventArgs e)
        {
            MatchStart (1);
        }
        private void btnPar_Click (object sender, EventArgs e)
        {
            MatchStart (2);
        }
            private void MatchStart (int my_hand)
            {
                string message = "";
                string mess = "";
                string grades = "";
                int cpu_hand = GetCpuHand ();
                this.picYou.Image = GetJankenImage (my_hand);
                this.picCPU.Image = GetJankenImage (cpu_hand);
                int judge = JadgeJanken (my_hand, cpu_hand);
                int hantei = JadgeJanken (my_hand, cpu_hand);
                int rate = JadgeJanken (my_hand, cpu_hand);

                Array.Resize (ref array_match_result, array_match_result.Length + 1);
                array_match_result [array_match_result.Length --1] = judge;

                int ret = ConvertVictoryMessage (judge, ref message);
                int ret2 = ConvertVictory (hantei, ref mess);
                int ret3 = Syouritu (rate, ref grades);

                label2.Text = "you";
                label3.Text = "My";
            if (ret == 0)
                {
                    this.lblResult.Text = message;
                }
                else else
                {
                    this.lblResult.Text = "Incorrect result returned.";
                }
                if (ret2 == 0)
                {
                    this.label4.Text = mess;
                }else else
                {
                this.label4.Text = "Incorrect result returned.";
                }
                if (ret3 == 0)
                {
                    this.WinRate.Text = grades;
                }
                else else
                {
                    this.WinRate.Text = "Incorrect results returned.";
                }


        }
        private int GetCpuHand ()
            {
                int cpu_hand = r.Next (3);
                if (cpu_hand<0&&cpu_hand>2)
                {
                    return -1;
                }
                return cpu_hand;
            }
            private Image GetJankenImage (int index)
            {
                if (index<0&&index>2)
                {
                    return null;
                }
                return imgJankenList.Images [index];
            }
            private int JadgeJanken (int my_hand, int cpu_hand)
            {
                return (my_hand --cpu_hand + 3)% 3;
            }
            private int ConvertVictoryMessage (int judge, ref string victory)
            {
                switch (judge)
                {
                    case 0:
                        victory = "";
                        break;
                    case 1:
                        victory = "losing";
                        break;
                    case 2:
                        victory = "win";
                        break;
                    default: default:
                        victory = "";
                        return -1;
                }
                return 0;
        }
            private int ConvertVictory (int hantei, ref string mess)
            {
                switch (hantei)
                {
                    case 0:
                        mess = "Deiko";
                        break;
                    case 1:
                        mess = "with your";
                        break;
                    case 2:
                        mess = "with your";
                        break;
                    default: default:mess = "";
                        return -1;
                }
                return 0;
            }
        private int Syouritu (int rate, ref string grades)
        {
            switch (rate)
            {
                case 0:
                    grades = "";
                    break;
                case 1:
                    grades = "";
                    break;
                case 2:
                    grades = "";
                    break;
                default: default:
                    grades = "";
                    return -1;
            }
            int cnt_win = 0;
            int cnt_lose = 0;
            int cnt_draw = 0;
            foreach (int res in array_match_result)
            {
                switch (res)
                {
                    case 0:
                        ++ cnt_draw;
                        break;
                    case 1:
                        ++ cnt_lose;
                        break;
                    case 2:
                        ++ cnt_win;
                        break;
                    default: default:
                        break;
                }
            }
                grades + = array_match_result.Length.ToString () + "during the war"
                 + "You:" + cnt_win.ToString () + "Win"
                 + "I:" + cnt_lose.ToString () + "Win"
                 + "Aiko:" + cnt_draw.ToString ();
            if (cnt_win == 5)
            {
                    MessageBox.Show (array_match_result.Length.ToString () + "During the war" + cnt_win.ToString () + "Win"
                                    + cnt_lose.ToString () + "Loss" + cnt_draw.ToString () + "You have won by dividing.", "Win/Loss Decision");
            }
            else if (cnt_lose == 5)
            {
                    MessageBox.Show (array_match_result.Length.ToString () + "During the war" + cnt_lose.ToString () + "Win"
                                           + cnt_win.ToString () + "Loss" + cnt_draw.ToString () + "I won by dividing.", "Win/Loss decision");
            }
            return 0;

        }


Enter the language name here
Source code
`` ```

What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    While the MessageBox is displayed, processing stops there until it is closed.
    Isn't it better to update Label etc. before displaying MessageBox?

  • Answer # 2

    Since the MessageBox is displayed before the screen is updated, isn't it just that the process stops there and the wording on the screen has not been updated yet?

    If you write what you are doing in a very simple way

    message = "lose";
    mess = "with your";
    grades + = array_match_result.Length.ToString () + "during the war"
                     + "You:" + cnt_win.ToString () + "Win"
                     + "I:" + cnt_lose.ToString () + "Win"
                     + "Aiko:" + cnt_draw.ToString ();
    MessageBox.Show (array_match_result.Length.ToString () + "During the war" + cnt_win.ToString () + "Win"
                                        + cnt_lose.ToString () + "Loss" + cnt_draw.ToString () + "You have won by dividing.", "Win/Loss Decision");
    this.lblResult.Text = message;
    this.label4.Text = mess;
    this.WinRate.Text = grades;


    It will be.
    The screen label is not updated because the continuation does not continue until the message box is closed with MessageBox.Show.
    So, let's update it and then put out the message box.