23根火柴游戏 C# 窗体程序

问题遇到的现象和发生背景

2两个游戏者开始拥有23根火柴(或小棒)。每个游戏者轮流移走一根火柴或者两根、三根火柴,拿到最后一根火柴的就算输了。
用C#语言编写一个程序,实现人与计算机玩这个游戏(WinForm类程序开发)。

问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

简单写了一个示例,先看效果:

img

Form1.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        private int _remainNumbers = 23;
        private Random _random;
        private Player _computer;
        private Player _player;
        private bool _isFinished;
        private Player _winner;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _remainNumbers = 23;
            lbMatchDisplay.Text = _remainNumbers.ToString();
            Finished();
            _random = new Random();
            _computer = new Computer();
            _player = new People();
        }

        private void btnTake1_Click(object sender, EventArgs e)
        {
            Play(_player, 1);
            ComputerPlay();
        }

        private void btnTake2_Click(object sender, EventArgs e)
        {
            Play(_player, 2);
            ComputerPlay();
        }

        private void btnTake3_Click(object sender, EventArgs e)
        {
            Play(_player, 3);
            ComputerPlay();
        }

        private void Play(Player player, int take)
        {
            if (_isFinished)
            {
                MessageBox.Show("此局游戏已结束,请重新开始");
                return;
            }
            if (take <= 0)
            {
                MessageBox.Show("移走数需大于0");
                return;
            }
            if (_remainNumbers <= 0)
            {
                MessageBox.Show("没有可以移走的火柴啦");
                return;
            }
            player.Take(take);
            if (_remainNumbers < take)
            {
                take = _remainNumbers;
                WriteLog($"已经没有足够的火柴了,拿走剩下的{take}根.");
            }
            _remainNumbers -= take;
            lbMatchDisplay.Text = _remainNumbers.ToString();
            WriteLog($"{player.Name}拿走{take}根火柴.");
            if (_remainNumbers == 0)
            {
                Finished();
                _winner = player is Computer ? _player : _computer;
                WriteLog($"没有剩下的火柴了,{_winner.Name}赢啦!游戏结束!");
                WriteLog($"{_computer.Name}移走火柴的历史记录:{string.Join(",", _computer.TakeHistory)}");
                WriteLog($"{_player.Name}移走火柴的历史记录:{string.Join(",", _player.TakeHistory)}");
            }
        }

        private void InitControls()
        {
            _isFinished = true;
            btnTake1.Enabled = false;
            btnTake2.Enabled = false;
            btnTake3.Enabled = false;
        }

        private void Finished()
        {
            _isFinished = true;
            btnStart.Enabled = true;
            btnTake1.Enabled = false;
            btnTake2.Enabled = false;
            btnTake3.Enabled = false;
        }
        private void Replay()
        {
            _isFinished = false;
            btnTake1.Enabled = true;
            btnTake2.Enabled = true;
            btnTake3.Enabled = true;
            btnStart.Enabled = false;
            rtxtLog.Clear();
            WriteLog("游戏开始...");
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            _remainNumbers = 23;
            Replay();
            _winner = null;
            // TODO:假设电脑先拿
            ComputerPlay();
        }

        private void ComputerPlay()
        {
            if (_isFinished)
            {
                return;
            }
            var take = _random.Next(1, 4);
            Play(_computer, take);
        }

        private void WriteLog(string message)
        {
            rtxtLog.AppendText($"{message}{Environment.NewLine}");
            rtxtLog.SelectionStart = rtxtLog.Text.Length;
            rtxtLog.ScrollToCaret();
        }

        private void rtxtLog_TextChanged(object sender, EventArgs e)
        {
            //rtxtLog.SelectionStart = rtxtLog.Text.Length;
            //rtxtLog.ScrollToCaret();
        }
    }

    public abstract class Player
    {
        protected Player()
        {
            TakeHistory = new List<int>();
        }

        public abstract string Name { get; }
        public List<int> TakeHistory { get; }

        public void Take(int take)
        {
            TakeHistory.Add(take);
        }
    }

    public class Computer : Player
    {
        public override string Name => "电脑";
    }

    public class People : Player
    {
        public override string Name => "Tom";
    }
}

设计器Form1.Designer.cs

using System.Windows.Forms;

namespace WindowsFormsApp2
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows Form Designer generated code
        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnStart = new System.Windows.Forms.Button();
            this.lbMatchDisplay = new System.Windows.Forms.Label();
            this.btnTake1 = new System.Windows.Forms.Button();
            this.btnTake2 = new System.Windows.Forms.Button();
            this.btnTake3 = new System.Windows.Forms.Button();
            this.rtxtLog = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // btnStart
            // 
            this.btnStart.Location = new System.Drawing.Point(32, 56);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new System.Drawing.Size(212, 31);
            this.btnStart.TabIndex = 0;
            this.btnStart.Text = "开始";
            this.btnStart.UseVisualStyleBackColor = true;
            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
            // 
            // lbMatchDisplay
            // 
            this.lbMatchDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lbMatchDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lbMatchDisplay.Location = new System.Drawing.Point(32, 9);
            this.lbMatchDisplay.Name = "lbMatchDisplay";
            this.lbMatchDisplay.Size = new System.Drawing.Size(212, 33);
            this.lbMatchDisplay.TabIndex = 1;
            this.lbMatchDisplay.Text = "23";
            this.lbMatchDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // btnTake1
            // 
            this.btnTake1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnTake1.Location = new System.Drawing.Point(32, 93);
            this.btnTake1.Name = "btnTake1";
            this.btnTake1.Size = new System.Drawing.Size(51, 51);
            this.btnTake1.TabIndex = 2;
            this.btnTake1.Text = "1";
            this.btnTake1.UseVisualStyleBackColor = true;
            this.btnTake1.Click += new System.EventHandler(this.btnTake1_Click);
            // 
            // btnTake2
            // 
            this.btnTake2.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnTake2.Location = new System.Drawing.Point(108, 93);
            this.btnTake2.Name = "btnTake2";
            this.btnTake2.Size = new System.Drawing.Size(51, 51);
            this.btnTake2.TabIndex = 3;
            this.btnTake2.Text = "2";
            this.btnTake2.UseVisualStyleBackColor = true;
            this.btnTake2.Click += new System.EventHandler(this.btnTake2_Click);
            // 
            // btnTake3
            // 
            this.btnTake3.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.btnTake3.Location = new System.Drawing.Point(193, 93);
            this.btnTake3.Name = "btnTake3";
            this.btnTake3.Size = new System.Drawing.Size(51, 51);
            this.btnTake3.TabIndex = 4;
            this.btnTake3.Text = "3";
            this.btnTake3.UseVisualStyleBackColor = true;
            this.btnTake3.Click += new System.EventHandler(this.btnTake3_Click);
            // 
            // rtxtLog
            // 
            this.rtxtLog.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.rtxtLog.Location = new System.Drawing.Point(-1, 150);
            this.rtxtLog.Name = "rtxtLog";
            this.rtxtLog.Size = new System.Drawing.Size(281, 125);
            this.rtxtLog.TabIndex = 5;
            this.rtxtLog.Text = "";
            this.rtxtLog.TextChanged += new System.EventHandler(this.rtxtLog_TextChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(282, 277);
            this.Controls.Add(this.rtxtLog);
            this.Controls.Add(this.btnTake3);
            this.Controls.Add(this.btnTake2);
            this.Controls.Add(this.btnTake1);
            this.Controls.Add(this.lbMatchDisplay);
            this.Controls.Add(this.btnStart);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private Button btnStart;
        private Label lbMatchDisplay;
        private Button btnTake1;
        private Button btnTake2;
        private Button btnTake3;
        private RichTextBox rtxtLog;
    }
}

用这个算法,然后窗体调用一下


public int Get1(int count)

        {
            int[] array = new int[] { 3, 2, 1 };

            foreach(var item in array)

            {

                if(count <= 4)

                {

                    return count - 1;

                }

                // 如果每次取item根火柴,取了奇数次后剩余1,则赢,如果最后一次火柴数小于item,则按一次来算

                if(Math.Ceiling(((decimal)count - 1) / item) % 2 == 1)

                {

                    return item;

                }

            }

            // 输了,取1根

            return 1;

        }