如果可以的话,麻烦再帮我做一份实验报告,还会给钱的。这周日之前。太感谢了。实在是没有时间去雪这些东西,打算假期好好补一补
课程设计报告参考下面链接
https://max.book118.com/html/2021/0705/8042002101003116.shtm
2两个游戏者开始拥有23根火柴(或小棒)。每个游戏者轮流移走一根火柴或者两根、三根火柴,拿到最后一根火柴的就算输了。
用C#语言编写一个程序,实现人与计算机玩这个游戏(WinForm类程序开发)。
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;
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 {
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
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();
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);
this.lbMatchDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbMatchDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 20 F, 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;
this.btnTake1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16 F, 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);
this.btnTake2.Font = new System.Drawing.Font("Microsoft Sans Serif", 16 F, 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);
this.btnTake3.Font = new System.Drawing.Font("Microsoft Sans Serif", 16 F, 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);
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);
this.AutoScaleDimensions = new System.Drawing.SizeF(6 F, 13 F);
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;
}
}
前段刚好回答过这个问题,运行效果如图:
请参考:
私信把你的邮箱发我,可提供源码。