コード例 #1
0
 public void WaitForInput()
 {
     prevRoundState = curRoundState;
     curRoundState  = RoundState.Wait;
 }
コード例 #2
0
        //player button
        private void button2_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int    playerRandomNumber = rnd.Next(1, 11);
            int    cpuRandomNumber    = rnd.Next(1, 11);

            label5.Text = $"Round: {CurrentGameState.CurrentRound} of {CurrentGameState.MaxRounds}";

            drawNumberOnButton(button3, playerRandomNumber);
            drawNumberOnButton(button4, cpuRandomNumber);

            CurrentGameState.CurrentPlayerPoints--;
            CurrentGameState.CurrentCPUPoints--;
            if (CurrentGameState.War)
            {
                button3.BackgroundImage       = Properties.Resources.back;
                button3.BackgroundImageLayout = ImageLayout.Stretch;
                button4.BackgroundImage       = Properties.Resources.back;
                button4.BackgroundImageLayout = ImageLayout.Stretch;
                CurrentGameState.War          = false;
            }
            else
            {
                if (playerRandomNumber == cpuRandomNumber)
                {
                    CurrentGameState.DrawsInRow++;
                    CurrentGameState.War = true;
                    button3.BackColor    = Color.Orange;
                    button4.BackColor    = Color.Orange;
                }
                else if (playerRandomNumber > cpuRandomNumber)
                {
                    CurrentGameState.CurrentPlayerPoints += (CurrentGameState.DrawsInRow + 1) * 2;
                    CurrentGameState.War        = false;
                    button3.BackColor           = Color.Green;
                    button4.BackColor           = Color.Red;
                    CurrentGameState.DrawsInRow = 0;
                }
                else
                {
                    CurrentGameState.CurrentCPUPoints += (CurrentGameState.DrawsInRow + 1) * 2;
                    CurrentGameState.War        = false;
                    button3.BackColor           = Color.Red;
                    button4.BackColor           = Color.Green;
                    CurrentGameState.DrawsInRow = 0;
                }
            }

            label3.Text = CurrentGameState.CurrentPlayerPoints.ToString();
            label8.Text = CurrentGameState.CurrentCPUPoints.ToString();


            var roundState = new RoundState()
            {
                CPUPoints    = CurrentGameState.CurrentCPUPoints,
                PlayerPoints = CurrentGameState.CurrentPlayerPoints,
                RoundNumber  = CurrentGameState.CurrentRound
            };

            CurrentGameState.RoundsStates.Add(roundState);
            CurrentGameState.CurrentRound++;

            if (CurrentGameState.CurrentRound > CurrentGameState.MaxRounds ||
                CurrentGameState.CurrentPlayerPoints <= 0 ||
                CurrentGameState.CurrentCPUPoints <= 0)
            {
                string result = "";
                if (CurrentGameState.CurrentPlayerPoints > CurrentGameState.CurrentCPUPoints)
                {
                    result = "Win!";
                }
                else if (CurrentGameState.CurrentPlayerPoints < CurrentGameState.CurrentCPUPoints)
                {
                    result = "Defeat!";
                }
                else
                {
                    result = "Draw!";
                }

                var highscoresLine     = $"{CurrentGameState.PlayerName},{CurrentGameState.CurrentPlayerPoints},{CurrentGameState.CurrentRound - 1};";
                var highscoresFilePath = System.Environment.CurrentDirectory + @"\highscores.hs";
                File.AppendAllText(highscoresFilePath, highscoresLine);

                DialogResult dialogResult1 = MessageBox.Show($"{result}! Show postgame stats?", "Game over", MessageBoxButtons.YesNo);
                if (dialogResult1 == DialogResult.Yes)
                {
                    var chart = new Form4(this, CurrentGameState);
                    chart.Show();
                }
                else
                {
                    DialogResult dialogResult2 = MessageBox.Show($"{result}. Wanna play again?", "Finish", MessageBoxButtons.YesNo);
                    if (dialogResult2 == DialogResult.Yes)
                    {
                        CreateNewGame(CurrentGameState.PlayerName, CurrentGameState.CPUName, CurrentGameState.MaxRounds, CurrentGameState.Sets);
                        return;
                    }
                    else if (dialogResult2 == DialogResult.No)
                    {
                        var secondDialogResult = showCloseDialog();
                        if (secondDialogResult == DialogResult.Yes)
                        {
                            timer.Tick -= TimerEventProcessor;
                            Application.Exit();
                        }
                    }
                }
            }
        }