求C#的小程序代码

求几个小程序的代码
2024-11-19 11:43:40
推荐回答(4个)
回答1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace RIF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public class Arr
{
public int[,] arr = new int[25, 25];
public bool win;
}

Arr a = new Arr();
bool cc = true;

public void PaintLab()
{
Bitmap image = new Bitmap(300,300);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Tan);
Pen pen = new Pen(Color.Black, 1);
int i, j;
i = j = 0;
while (i <= 300)
{
g.DrawLine(pen, i, 0, i, 300);
i = i + 20;
}
while (j <= 300)
{
g.DrawLine(pen, 0, j, 300, j);
j = j + 20;
}
img.Image = image;

}

private void Form1_Load(object sender, EventArgs e)
{
PaintLab();
}

private void img_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int x = e.X;
int y = e.Y;
Graphics g = Graphics.FromImage(img.Image);
Brush pen;
int myx = x / 20;
int myy = y / 20;
if (a.arr[myx, myy] != 0)
{
MessageBox.Show("这里已经有棋子了!");
return;
}
else
{
if (cc)
{
pen = new SolidBrush(Color.White);
cc = false;
a.arr[myx, myy] = 1;
}
else
{
pen = new SolidBrush(Color.Black);
cc = true;
a.arr[myx, myy] = 2;
}
g.FillEllipse(pen, myx * 20 + 2, myy * 20 + 2, 16, 16);
img.Invalidate();
int z = IsWin(myx, myy, cc);
if (z != 0)
{
if (z == 1)
{
MessageBox.Show("白色获胜!");
}
else
{
MessageBox.Show("黑色获胜!");
}
img.Enabled = false;
}
}
}
else
{
MessageBox.Show("本程序由Cantahu开发","作者信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}

}

private int IsWin(int x, int y,bool cc)
{
int m, n, count, p, q;
int val = 0;
bool win=false;
if (cc)
{
val = 2;
}
else
{
val = 1;
}

#region 横向判断
count = 1;
int f = 0;
m = x-1;
n = x+1;
while (1==1)
{
if (count == 5)
{
win = true;
break;
}
else if (f == 5)
{
win = false;
break;

}
if (m >= 0 && n <= 300)
{
if (a.arr[m, y] == val)
{
count = count + 1;
m = m - 1;
}
if (a.arr[n, y] == val)
{
count = count + 1;
n = n + 1;
}
}
f = f + 1;
}
if (win)
{
return val;
}
#endregion

#region 纵向判断
m = y - 1;
n = y + 1;
f = 0;
count = 1;
while (1 == 1)
{
if (count == 5)
{
win = true;
break;
}
if (f == 5)
{
win = false;
break;
}
if (m >= 0 && n <= 300)
{
if(a.arr[x,m]==val)
{
count = count + 1;
m = m - 1;
}
if(a.arr[x,n]==val)
{
count = count + 1;
n = n + 1;
}
}
f = f + 1;

}
if (win)
{
return val;
}
#endregion

#region 左斜向判断
count = 1;
f = 0;
m = x - 1;
n = y - 1;
p = x + 1;
q = y + 1;
while (1 == 1)
{
if (count == 5)
{
win = true;
break;
}
if (f == 5)
{
win = false;
break;
}
if (m >= 0 && n >= 0 && p <= 300 && q <= 300)
{
if (a.arr[m, n] == val)
{
count = count + 1;
m = m - 1;
n = n - 1;
}
if (a.arr[p, q] == val)
{
count = count + 1;
p = p + 1;
q = q + 1;
}
}
f = f + 1;
}
if (win)
{
return val;
}
#endregion

#region 右斜向
count = 1;
f = 0;
m = x - 1;
n = y + 1;
p = x + 1;
q = y - 1;
while (1 == 1)
{
if (count == 5)
{
win = true;
break;
}
if (f == 5)
{
win = false;
break;
}
if (m >= 0 && n <= 300 && p <= 300 && q >= 0)
{
if (a.arr[m, n] == val)
{
count = count + 1;
m = m - 1;
n = n + 1;
}
if (a.arr[p, q] == val)
{
count = count + 1;
p = p + 1;
q = q - 1;
}
}
f = f + 1;
}
if (win)
{
return val;
}
#endregion

return 0;
}

private void Btnstart_Click(object sender, EventArgs e)
{
img.Enabled = true;

PaintLab();
}

private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}

}
}
这是我自己写的 五子棋代码 希望对你有帮助

回答2:

锐英源有专业的C#指导,保证掌握工作能力,写上万行软件。

回答3:

那你也得把题发出来啊
。。。。

回答4:

这个小不到50行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int i,j,k;
string[] xiushi= new string[]
{ "聪明的","善良的","热情的","沉默的","害羞的","勤奋的","固执的","魅惑的",
"调皮的","美貌的","冷静的","坚强的","勇敢的","不朽的","大胆的","乐观的","漂亮的","温柔的"};
string[] name = new string[]
{ "阿米莉娅","塞亚","雪儿","阿黛拉","克里斯汀","尤娜","夏娜","艾格文","蒂娜","安妮","杰西卡","卢西娅","瓦娜斯","布兰切特","雅典娜","伊丽萨","娜莎"};
int[] num = new int[18] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11,12,13,14,15,16,17};
Random ra = new Random();
Console.WriteLine("输入1表示继续生成随机名字,输入2表示退出,请输入数字:");

for (k=1;;k++)
{

string m = Console.ReadLine();
if (m == "1")
{
i = ra.Next(num[0], num[17]);

Console.Write(xiushi[i]);
j = ra.Next(num[0], num[17]);
Console.WriteLine(name[j]);

}
else if (m == "2")
break;
else
Console.WriteLine("输入错误!");
}

}
}
}