C # canvas layout,

Source: Internet
Author: User
Tags linecap

C # canvas layout,

C # plotting is not so beautiful, but for simple graphics, It is very convenient to use C # without paying attention to the aesthetic quality.

You can adjust the background color, line color, and column Size Based on your preferences.

If I do not provide AI code, the ai I designed myself is not perfect, and I will not show myself ugly. The algorithm is more complicated. Besides, I will not do it, looking at this AI algorithm does not really help much. To upload the code, I just want to give beginners some help in drawing (the method in Pen. Graphics). The program is relatively simple and I believe it is easy to understand without explanation.

(If I have time tomorrow, I will upload 2048 of the algorithms <2048 Core algorithms are relatively few, about one hundred lines>, and the graphic interface [PC side], I did a powerful version myself. The gameplay is more exciting than the current online gameplay .)

 

1. main entry point of the application: file name Program. cs

Using System; using System. collections. generic; using System. linq; using System. windows. forms; namespace wuziqi {static class Program {/// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main () {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); Application. run (new Form1 ());}}}

 

2. windows. form Main Mode: file name Form1.cs (the designer and background files have been merged)

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 wuzi {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private System. componentModel. IContainer components = null; protected override void Dispose (bool disposing) {if (disposing & (comp Onents! = Null) {components. dispose ();} base. dispose (disposing);} private void InitializeComponent () {this. gobang1 = new wuziqi. gobang (); this. suspendLayout (); // gobang1 // this. gobang1.BackColor = System. drawing. color. yellow; this. gobang1.Dock = System. windows. forms. dockStyle. fill; this. gobang1.Horizontal = 19; this. gobang1.Location = new System. drawing. point (0, 0); this. gobang1.Name = "gobang1"; this. gobang1.PaintLine = System. drawing. color. black; this. gobang1.Size = new System. drawing. size (581,502); this. gobang1.TabIndex = 0; this. gobang1.Text = "gobang1"; this. gobang1.Vertical = 19; // Form1 // this. autoScaleDimensions = new System. drawing. sizeF (6F, 12F); this. autoScaleMode = System. windows. forms. autoScaleMode. font; this. clientSize = new System. drawing. size (581,502); this. controls. add (this. gobang1); this. name = "Form1"; this. text = "Form1"; this. resumeLayout (false);} private Gobang gobang1 ;}}

 

3. Custom Controls, the main layout of the Board (1): file name 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 wuzi {public partial class Gobang: Control {public Gobang () {SetStyle (ControlStyles. userPaint, true); SetStyle (ControlStyles. allPaintingInWmPaint, true); SetStyle (ControlStyles. doubl EBuffer, true); this. resize + = new EventHandler (GobangControl_Resize); this. mouseClick + = new MouseEventHandler (Gobang_MouseClick); // this. mouseMove + = new MouseEventHandler (Gobang_MouseMove); InitMatrix ();} /// <summary> // The Square Value of the radius /// </summary> private float _ RadiusSquare = 0; /// <summary> /// pedometer /// </summary> private int _ Step = 0; /// <summary> /// Number of longitudinal lines /// </summary> private int _ vertical = 19; [Description ("")] public int Vertical {get {return _ vertical;} set {_ vertical = value; InitMatrix (); Invalidate ();}} /// <summary> /// Number of crosslines /// </summary> private int _ horizontal = 19; [Description ("crosslines")] public int Horizontal {get {return _ horizontal;} set {_ horizontal = value; InitMatrix (); Invalidate ();}} /// <summary> /// vertical and horizontal line Color /// </summary> private Color _ paintLine = Co Lor. 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)/9; pe. graphics. smoothingMode = SmoothingMode. highQuality; using (Pen pen = new Pen (new SolidBrush (_ paintLine), 2) {pen. startCap = LineCap. round; pen. endCap = LineCap. round; for (int row = 1; 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 = 1; 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 = 0; row <this. _ horizontal; row ++) {for (int col = 0; col <this. _ vertical; col ++) {ChessPieces piec = _ Matrix [row * this. _ vertical + col]; if (piec. isOk ){ Using (SolidBrush solidBrush = new SolidBrush (piec. Step % 2 = 1? 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) ;}}}/// when the mouse pointer moves in the area, there is a pawn at the top of the pointer // void Gobang_MouseMove (object sender, MouseEventArgs e) // {//} // click the mouse and drop the void Gobang _ MouseClick (object sender, MouseEventArgs e) {switch (e. button) {case MouseButtons. left: // click the Left button to indicate 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_heig Ht = (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 % 2 = 1) piec. color = Color. black; else piec. color = Color. white; piec. isOk = true; Invalidate () ;}} break; case MouseButtons. right: // Right-click, indicating that the revocation step int maxpos = 0; for (int I = 0; I <_ Matrix. length; I ++) {if (_ Matrix [I]. isOk & _ Matrix [I]. step = _ Step) {_ Step --; _ Matrix [I]. step = 0; _ Matrix [I]. isOk = false; Invalidate (); break ;}} void GobangControl_Resize (object sender, EventArgs e) {Invalidate ();}} public class ChessPieces {public Color {set; get;} public bool IsOk {set; get;} public int Step {set; get ;}}}

 

4. Custom Controls, the main layout of the board (2): file name GobangArithmetic. cs

Using System; using System. collections. generic; using System. linq; using System. text; using System. drawing; namespace wuzi {public partial class Gobang {ChessPieces [] _ Matrix; private void InitMatrix () {_ Matrix = new ChessPieces [this. _ horizontal * this. _ vertical]; for (int I = 0; I <_ Matrix. length; I ++) {_ Matrix [I] = new ChessPieces {Color = Color. black, IsOk = false, Step = 0 };}}}}

 

Then you can click the mouse to know how to play the game.


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.