c#五子棋实验报告_(C#)五子棋

(C#)五子棋myGobangBB

*本程序是在myGobangAA基础上进行改进,成为一个基本完整的项目。

*[

为了保留下原myGobangAA代码,以简化本项目myGobangBB的生成,可先新建一个文件夹myGobangBB,然后把

*原myGobangAA项目的文件都copy到myGobangBB,必要时可将原文件中命名空间的名称myGobangAA改为myGobangBB(不改也可)]

*

*

*要点:

* 1)五子棋基本算法大多从JAVA代码中复制过来。除了数组,C#

与JAVA语法基本通用。

* 数组表示的差异,可用菜单"编辑>查找,替换"快速解决。(将"][" 替换为",")

* 2)消除锯齿

* (应先加入:using System.Drawing.Drawing2D;)

* Graphics g_Image =

Graphics.FromImage(myImage);//创建一个在myImage中绘图的Graphics

* g_Image.SmoothingMode = SmoothingMode.AntiAlias;

*

*

3)本例有二个计时器Timer,均从工具箱拖入,一个命名为MyTimer,一个命名为DateTimer.

* 3a)MyTimer(显示一盘对局持续时间);

*  Interval属性设为100(1000为一秒)

* 将找到事件tick,双击即可自动生成MyTimer_Tick()代码块,在其中添加每隔一段时间需要执行代码.

*

(VS2005将自动在Form1.Designer.cs中添加注删该事件侦听器的代码:

* this.MyTimer.Tick += new

System.EventHandler(this.MyTimer_Tick);

* 3b)DateTimer(显示系统的日期与时间);

* textDate.Text= DateTime.Now.ToString();//显示系统时间

* textDate是一个TextBox控件,其multiline属性应设是true,TextAlign属性应设为center

*

* 5)退出程序: Application.Exit();

* 6)

ShowWinLabel控件背景色BackColor设为透明:transparent

*

*

*以下是原myGobangAA的说明

*实现功能:

* 点击五子棋盘时,邻近的线条交点上,出现黑子。

*

(以后将在些基础上添加五子棋算法,自动下出白子,并判断胜负)

* (每下一手棋,都重绘一遍棋盘和棋子)

* 要点:

*

* 1)请特别注意ReDraw(),

其它成员方法(函数)大多由它来调用。

* 2)如何添加事件

* 本例添加的Form1_MouseClick()事件的具体方法:在可视设计界面的属性窗口,找到该事件后双击即可。

* 添加成功后,Form1.Designer.cs文件中会自动生成以下代码(注册监听程序):

* this.MouseClick += new

System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);

* 3)为Form1添加成员变量和方法成员(函数)

*  在Form1.CS中直接添加。

*

4)为Form1添加较多的成员后,可以在代码界面的左上方的列表框中查看所有成员,把握总体结构。

*   还可很方便地跳转到选定成员的代码块。

* 5)定义二维数组:int[,] P = new int[15,

15];

* 6)双倍缓冲绘图

*    Graphics g_Image =

Graphics.FromImage(myImage);;从图像中获得g_Image,它是在图像中进行绘制的Graphics

*   把绘好的myImage,

一次性展现在指定的区域(myRec:displayGraphics.DrawImage(myImage, myRec);

* 7)几个数学函数。

*

其实不必刻意去记,VS2005设计界面的提供了强大的提示功能,

*

若想使用某一数学计算函数(如三角函数,开平方等),只要输入一个"m",后面就可以通过在列表框中的选择搞定。

*

using System;

using

System.Collections.Generic;

using

System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Text;

using

System.Windows.Forms;

using

System.Drawing.Drawing2D;

namespace

myGobangBB

{

public

partial class Gobang : Form

{

const int myBorder = 10;//棋盘左边与主窗体左边的间隔=棋盘上边与主窗体上边的间隔

const int myPadding = 20;//棋盘边白(棋盘边缘与最相邻的线条的间隔)

const int myCell = 30;//小方格边长

const int myLine = 15;//横,竖均为15条线

const int myR = (int)(myCell / 2.2);//棋子半径

(棋子直径略小于小方格边长)

int[,] P = new int[myLine,

myLine];//棋盘交叉点(落子点),值为1:黑子,2:白子,0:无子

int LastChessX, LastChessY;//最后一手棋的下标

const int scanDirection = 4;//扫描方向数

int[ ,,] WhiteValue = new int[myLine ,myLine,scanDirection+1];//扫描白子棋形后得出的各点重要性的值,

//scanDirection+1:多出的一个用来存入四个方向合计数 :P[X,Y,0]=P[X,Y,1]+P[X,Y,2]

+P[X,Y,3] +P[X,Y,4]

int [ ,,] BlackValue = new int[myLine

,myLine,scanDirection+1];//扫描黑子棋形后得出的各点重要性的值,详见init()中的注释

// AudioClip audio1,audio2;//音响剪辑类(落子, 胜利)

int TheEnd=0; //(0-中止,1-黑胜,2-白胜,大于2的数-正在进行对局)

int PassedMinute =0,PassedSecond = 0;

Rectangle myRec = new Rectangle(myBorder, myBorder, (int)(myLine -

1) * myCell + 2 * myPadding, (int)(myLine - 1) * myCell + 2 *

myPadding);//屏幕上的矩形

Rectangle myImageRec = new Rectangle(0, 0, (int)(myLine - 1) *

myCell + 2 * myPadding, (int)(myLine - 1) * myCell + 2 *

myPadding); //Image中的矩形

public Gobang()

{

InitializeComponent();

initP();

StateText.Text = "对局未开始";

ShowWinlabel.Visible = false;

}

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

g.FillRectangle(Brushes.DarkGreen,

ClientRectangle);//全部客户区涂背景色

ReDraw();

}

protected void ReDraw()

{

<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 C# 五子棋游戏示例,你可以参考它来开始编写你自己的五子棋游戏。这个示例使用了 Windows Forms 应用程序。 ``` using System; using System.Drawing; using System.Windows.Forms; namespace FiveInARow { public partial class Form1 : Form { private int[,] board = new int[15, 15]; // 棋盘 private int player = 1; // 玩家,1 表示黑色,2 表示白色 private bool gameOver = false; // 游戏结束标志 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.BackColor = Color.Orange; this.ClientSize = new Size(600, 600); this.Paint += new PaintEventHandler(DrawBoard); this.MouseClick += new MouseEventHandler(PlayChess); } private void DrawBoard(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(Color.Orange); for (int i = 0; i < 15; i++) { g.DrawLine(Pens.Black, 50, 50 + i * 40, 570, 50 + i * 40); g.DrawLine(Pens.Black, 50 + i * 40, 50, 50 + i * 40, 570); } } private void PlayChess(object sender, MouseEventArgs e) { if (gameOver) { return; } int x = e.X / 40; int y = e.Y / 40; if (board[x, y] != 0) { return; } board[x, y] = player; Graphics g = this.CreateGraphics(); g.FillEllipse(player == 1 ? Brushes.Black : Brushes.White, x * 40 - 16, y * 40 - 16, 32, 32); if (CheckWin(x, y)) { gameOver = true; MessageBox.Show("Player " + player + " Wins!"); } else { player = player == 1 ? 2 : 1; } } private bool CheckWin(int x, int y) { int count = 1; int i, j; // 水平方向 for (i = x - 1; i >= 0 && board[i, y] == player; i--) { count++; } for (i = x + 1; i < 15 && board[i, y] == player; i++) { count++; } if (count >= 5) return true; // 垂直方向 count = 1; for (j = y - 1; j >= 0 && board[x, j] == player; j--) { count++; } for (j = y + 1; j < 15 && board[x, j] == player; j++) { count++; } if (count >= 5) return true; // 左上到右下方向 count = 1; for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i, j] == player; i--, j--) { count++; } for (i = x + 1, j = y + 1; i < 15 && j < 15 && board[i, j] == player; i++, j++) { count++; } if (count >= 5) return true; // 右上到左下方向 count = 1; for (i = x + 1, j = y - 1; i < 15 && j >= 0 && board[i, j] == player; i++, j--) { count++; } for (i = x - 1, j = y + 1; i >= 0 && j < 15 && board[i, j] == player; i--, j++) { count++; } if (count >= 5) return true; return false; } } } ``` 这个示例实现了基本的五子棋游戏逻辑,包括绘制棋盘、落子、判断胜负等功能。当然,还有很多可以改进和扩展的地方,比如增加 AI 对手、添加音效、美化界面等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值