C#绘图不是那么美,不过对于简单的图形,不注重美感的图质,用C#还是很方便的。

背景颜色、绘制图表线色、纵横列大小可按照个人喜好调节。

不提供AI代码,我自己设计的AI不是很完美,就不拿出来献丑了,算法比较复杂比较多,再说不做算法的人,看这个AI算法确实也没什么大帮助。上传这些代码,只是想给初学者一下绘图上的帮助(Pen.Graphics下的方法),程序比较简单,相信不用解释说明就很易懂。

(如果明天有时间,我上传2048的算法<2048的核心算法相对来说非常少,一百行左右>,和图形界面的做法[PC端],我自己做的是威力加强版,玩法要比当下网络中的玩法更新颖刺激。)

1.应用程序的主入口点:文件名Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace 五子棋
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

2.windows.form主模式:文件名Form1.cs(设计器和后台文件已经合并)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 五子棋
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} 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.gobang1 = new 五子棋.Gobang();
this.SuspendLayout();
//
// gobang1
//
this.gobang1.BackColor = System.Drawing.Color.Yellow;
this.gobang1.Dock = System.Windows.Forms.DockStyle.Fill;
this.gobang1.Horizontal = ;
this.gobang1.Location = new System.Drawing.Point(, );
this.gobang1.Name = "gobang1";
this.gobang1.PaintLine = System.Drawing.Color.Black;
this.gobang1.Size = new System.Drawing.Size(, );
this.gobang1.TabIndex = ;
this.gobang1.Text = "gobang1";
this.gobang1.Vertical = ;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.gobang1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
private Gobang gobang1;
}
}

3.自定义控件,围棋盘的主要布局(1):文件名GobangControl.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; namespace 五子棋
{
public partial class Gobang : Control
{
public Gobang()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true); this.Resize += new EventHandler(GobangControl_Resize);
this.MouseClick += new MouseEventHandler(Gobang_MouseClick);
//this.MouseMove += new MouseEventHandler(Gobang_MouseMove); InitMatrix();
} /// <summary>
/// 半径的平方值
/// </summary>
private float _RadiusSquare = ;
/// <summary>
/// 计步器
/// </summary>
private int _Step = ; /// <summary>
/// 纵线数
/// </summary>
private int _vertical = ;
[Description("纵线数")]
public int Vertical
{
get { return _vertical; }
set
{
_vertical = value;
InitMatrix();
Invalidate();
}
} /// <summary>
/// 横线数
/// </summary>
private int _horizontal = ;
[Description("横线数")]
public int Horizontal
{
get { return _horizontal; }
set
{
_horizontal = value;
InitMatrix();
Invalidate();
}
} /// <summary>
/// 纵横线颜色
/// </summary>
private Color _paintLine = Color.Black;
[Description("纵横线颜色")]
public Color PaintLine
{
get { return _paintLine; }
set { _paintLine = value; }
} protected override void OnPaint(PaintEventArgs pe)
{
float vag_width = (float)(1.0 * this.Width / this._vertical);
float vag_height = (float)(1.0 * this.Height / this._horizontal); this._RadiusSquare = (vag_width * vag_width + vag_height * vag_height) / ; pe.Graphics.SmoothingMode = SmoothingMode.HighQuality; using (Pen pen = new Pen(new SolidBrush(_paintLine), ))
{
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
for (int row = ; row <= this._horizontal; row++)
{
pe.Graphics.DrawLine(pen, (float)(vag_width * 0.5), (float)(vag_height * (row - 0.5)), (float)(vag_width * (this._vertical - 0.5)), (float)(vag_height * (row - 0.5)));
base.OnPaint(pe);
} for (int col = ; col <= this._vertical; col++)
{
pe.Graphics.DrawLine(pen, (float)(vag_width * (col - 0.5)), (float)(vag_height * 0.5), (float)(vag_width * (col - 0.5)), (float)(vag_height * (this._horizontal - 0.5)));
base.OnPaint(pe);
}
} for (int row = ; row < this._horizontal; row++)
{
for (int col = ; col < this._vertical; col++)
{
ChessPieces piec = _Matrix[row * this._vertical + col]; if (piec.IsOk)
{
using(SolidBrush solidBrush = new SolidBrush(piec.Step % == ? Color.Black : Color.White))
{
float x = (float)((col + 0.2) * vag_width);
float y = (float)((row + 0.2) * vag_height);
float width = (float)(vag_width * 0.6);
float height = (float)(vag_height * 0.6); pe.Graphics.FillEllipse(solidBrush, x, y, width, height);
base.OnPaint(pe);
}
}
}
}
} ////鼠标指针在区域内移动时,指针顶端有一颗棋子
//void Gobang_MouseMove(object sender, MouseEventArgs e)
//{ //} //点击鼠标、落子
void Gobang_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
//点击左键,表示落子 float vag_width = (float)(1.0 * this.Width / this._vertical);
float vag_height = (float)(1.0 * this.Height / this._horizontal); int nx = (int)Math.Round(e.X / vag_width - 0.5);
int ny = (int)Math.Round(e.Y / vag_height - 0.5); float div_width = (float)((nx + 0.5) * vag_width) - e.X;
float div_height = (float)((ny + 0.5) * vag_height) - e.Y; if (div_width * div_width + div_height * div_height <= this._RadiusSquare)
{
ChessPieces piec = this._Matrix[nx + ny * this._vertical];
if (!piec.IsOk)
{
piec.Step = ++_Step;
if (_Step % == )
piec.Color = Color.Black;
else
piec.Color = Color.White;
piec.IsOk = true; Invalidate();
}
} break; case MouseButtons.Right:
//点击右键,表示撤销步骤 int maxpos = ; for (int i = ; i < _Matrix.Length; i++)
{
if (_Matrix[i].IsOk && _Matrix[i].Step == _Step)
{
_Step--;
_Matrix[i].Step = ;
_Matrix[i].IsOk = false; Invalidate(); break;
}
} break;
}
} void GobangControl_Resize(object sender, EventArgs e)
{
Invalidate();
}
} public class ChessPieces
{
public Color Color { set; get; }
public bool IsOk { set; get; }
public int Step { set; get; }
}
}

4.自定义控件,围棋盘的主要布局(2):文件名GobangArithmetic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing; namespace 五子棋
{
public partial class Gobang
{
ChessPieces[] _Matrix; private void InitMatrix()
{
_Matrix = new ChessPieces[this._horizontal * this._vertical]; for (int i = ; i < _Matrix.Length; i++)
{
_Matrix[i] = new ChessPieces
{
Color = Color.Black,
IsOk = false,
Step =
};
}
}
}
}

然后,你用鼠标点一点就知道怎么玩了,呵呵。

C# 围棋盘的画法的更多相关文章

  1. zobrist hashing

    Zobrist 哈希是一种专门针对棋类游戏而提出来的编码方式,以其发明者 Albert L.Zobrist 的名字命名.Zobrist 哈希通过一种特殊的置换表,也就是对棋盘上每一位置的各个可能状态赋 ...

  2. 用内置的库turtle来画一朵花,python3

    题目:用内置的库turtle来画一朵花 看了群主最后成像的图片,应该是循环了36次画方框,每次有10度的偏移. 当然不能提前看答案,自己试着写代码. 之前有用过海龟画图来画过五角星.奥运五环.围棋盘等 ...

  3. 【u232】围棋游戏

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 为了增强幼儿园小朋友的数数能力,小虎老师给了一个家庭游戏作业.让小虎那一块空的围棋盘,随机在一些方格中 ...

  4. 强化学习-学习笔记5 | AlphaGo

    本文不是论文阅读笔记,只是一个学习笔记,重在理解,在严谨程度上可能稍差. AlphaGo 论文指路: Mastering the game of Go with deep neural network ...

  5. P1169 [ZJOI2007]棋盘制作 && 悬线法

    P1169 [ZJOI2007]棋盘制作 给出一个 \(N * M\) 的 \(01\) 矩阵, 求最大的正方形和最大的矩形交错子矩阵 \(n , m \leq 2000\) 悬线法 悬线法可以求出给 ...

  6. TYVJ1035 棋盘覆盖

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 给出一张n*n(n<=100)的国际象棋棋盘,其中被删除了一些点,问可以使用多少1*2的多米诺骨牌进行掩 ...

  7. POJ 1321 棋盘问题(dfs)

    传送门 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38297   Accepted: 18761 Descri ...

  8. 设计一个自动生成棋盘格子的JS小程序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. BZOJ1057[ZJOI2007]棋盘制作 [单调栈]

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

随机推荐

  1. 自动挂载文件/etc/fstab功能详解

    今天看了这篇文章,对于自动挂载中的一些小细节和参数有了更深的理解,所以这次把它摘下来,留做查询 一./etc/fstab文件的作用 1.我们把磁盘手动挂载之后如果不把它写入/etc/fstab这个文件 ...

  2. 【九度OJ】题目1202:排序

    题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100).    接下来的一行包括n个整数. 输出: 可能有多组测试数据,对于每组数据,将排序后 ...

  3. 自话自说——POI使用需要注意一个地方

    2015.12.1  天气 不怎么好   心情跟天气一样.知道为什么吗,因为昨晚一晚没睡你懂吗... 今天在用POI操作excel的时候,遇到了一个很恶心的地方,这个地方真的有那种让我不相信编程的感觉 ...

  4. java-7311练习(下)

    java练习,仅供参考! 欢迎同学们交流讨论. JDK 1.8 API帮助文档 JDK 1.6 API中文文档 第一次小组作业:模拟双色球彩票 第一次小组作业(一) 控制台版 游戏规则: • 双色球为 ...

  5. LINUX 虚拟机克隆与网络配置

    虚拟机克隆后,启动之后发现网卡没有启动.发现提示错误信息“Device eth0 does not seem to be present, delaying initialization.” 解决方法 ...

  6. ruby 常注意的

    1.ruby中生成字符串有两种形式 一种单引号,这种在使用时,对字符串不作处理,照原样输出 双引号就不同了,他会查找字符串中需要替换的字符,例如\n,#{}这种都会先替换为需要的值. 所以在使用的时候 ...

  7. c++ STL中的vector与list为什么没有提供find操作?

    map里有,set里也有,vector,list没有,太不公平了吧. 其实应该考虑为什么map,set里有find操作. include<algorithm>里有通用的find操作,通用的 ...

  8. 【转】react 状态与属性区别

    prop                  state 能否从父组件获取初始值             是                      否 能否由父组件修改               ...

  9. 排列组合算法(PHP)

    用php实现的排列组合算法.使用递归算法,效率低,胜在简单易懂.可对付元素不多的情况. //从$input数组中取$m个数的组合算法 function comb($input, $m) { if($m ...

  10. android wifi P2P CONNECT, INVITE和JOIN流程选择

    android wifi P2P CONNECT, INVITE和JOIN流程选择