C # written Gobang program for learning WinForms mouse events and using GDI +, source download.

Source: Internet
Author: User
Tags continue
program | Download a few days ago nothing, wrote a small program, can be used to learn C #.
The program uses the vs.net environment to compile, and your machine can run as long as the. NET Framework SDK is installed.
Source code and execution files can be downloaded
You do not want to download can also read the source (picture resources, etc. need to download).
Namespace Leimom.fivechess
{
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.WinForms;
Using System.Data;
<summary>
Summary description for Form1.
</summary>
public class FiveForm:System.WinForms.Form
{
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components;
Private System.WinForms.ImageList IMAGELISTBW;
Define the hot Rectangle
Private rectangle[] pointsquares;
Chess information
Private int[] chesstable;
private int Nextturn;
Private Const int bturn = 1;
Private Const int Wturn = 2;
Private Stack Chessindex;
Public Fiveform ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();
//
Todo:add any constructor the code after InitializeComponent call
//
Chessindex = new Stack ();
Nextturn = Bturn;
chesstable = new int[225];
Pointsquares = new rectangle[225];
Size size = new size (18,18);
int x = 0;
int y = 0;
for (int i = 0;i < 225;i++)
{
x = i%15;
y = I/15;
Pointsquares[i]. size = size;
Pointsquares[i]. Offset (9+X*20,6+Y*20);
Chesstable[i] = 0;
}
}

protected override void OnPaint (PaintEventArgs e)
{
For May paint
Graphics g = e.graphics;
}
protected override void OnMouseDown (System.WinForms.MouseEventArgs e)
{
Switch (E.button)
{
Take left button down
Case MouseButtons.Left:
OnLButtonDown (new Point (E.X,E.Y));
Break
Take right button down
Case Mousebuttons.right:
Onrbuttondown (new Point (E.X,E.Y));
Break
}
Base. OnMouseDown (e);
}
private void OnLButtonDown (point P)
{
int NPOs = Getrectid (p);
Click Hot Rectangle witch have no chess
if (NPOs!= -1&&chesstable[npos] = = 0)
{
Graphics g = this. CreateGraphics ();
if (Nextturn==bturn)
{
Draw White Chess
Drawblack (G,npos);
Chesstable[npos] = Bturn;
Nextturn = Wturn;
Chessindex.push (Bturn);
Chessindex.push (NPOs);
}
Else
{
Draw Black Chess
Drawwhite (G,npos);
Chesstable[npos] = Wturn;
Nextturn = Bturn;
Chessindex.push (Wturn);
Chessindex.push (NPOs);
}
G.dispose ();
Witch win
Checkgameresult (Npos,nextturn);
}
}
private void Checkgameresult (int npos,int nextturn)
{
Witch win
Stack isfive = new stack ();
int Thisturn = (Nextturn = = bturn)? Wturn:bturn;
int x = npos%15;
int y = NPOS/15;
Scan X have five
for (int i=0;i<15;i++)
{
if (chesstable[y*15+i] = = Thisturn)
{
Isfive.push (Y*15+i);
if (Isfive.count = 5)
{
MessageBox.Show ("Game over", "Notes", Messagebox.ok);
Resetgame ();
Return
}
}
Else
{
Isfive.clear ();
}
}
Isfive.clear ();
Scan Y have five
for (int i=0;i<15;i++)
{
if (chesstable[i*15+x] = = Thisturn)
{
Isfive.push (I*15+X);
if (Isfive.count = 5)
{
MessageBox.Show ("Game over", "Notes", Messagebox.ok);
Resetgame ();
Return
}
}
Else
{
Isfive.clear ();
}
}
Isfive.clear ();
Scan X=y have five
for (int i=-14;i<15;i++)
{
if (x+i<0| | x+i>14| | y-i<0| | Y-I&GT;14)
{
Continue
}
Else
{
if (chesstable[(y-i) *15+x+i] = = Thisturn)
{
Isfive.push ((y-i) *15+x+i);
if (Isfive.count = 5)
{
MessageBox.Show ("Game over", "Notes", Messagebox.ok);
Resetgame ();
Return
}
}
Else
{
Isfive.clear ();
}
}
}
Isfive.clear ();
Scan X=-y have five
for (int i=-14;i<15;i++)
{
if (x+i<0| | x+i>14| | y+i<0| | Y+I&GT;14)
{
Continue
}
Else
{
if (chesstable[(y+i) *15+x+i] = = Thisturn)
{
Isfive.push ((y+i) *15+x+i);
if (Isfive.count = 5)
{
MessageBox.Show ("Game over", "Notes", Messagebox.ok);
Resetgame ();
Return
}
}
Else
{
Isfive.clear ();
}
}
}
Isfive.clear ();
}
private void Resetgame ()
{
Reset Game
Nextturn = Bturn;
for (int i=0;i<225;i++)
{
Chesstable[i] = 0;
}
This. Invalidate ();
}
private int Getrectid (point P)
{
Get Witch Rectangle click
for (int i = 0;i < 225;i++)
{
if (pointsquares[i). Contains (P))
{
return i;
}
}
return-1;
}
private void Onrbuttondown (point P)
{
Regret chess
int npos,x,y;
if (chessindex.count!= 0)
{
NPOs = (int) chessindex.pop ();
x = npos%15;
y = NPOS/15;
Chesstable[npos] = 0;
Nextturn = (int) chessindex.pop ();
This. Invalidate (New Rectangle (8+X*20,5+Y*20), New Size (20,20));
}
}
private void Drawblack (Graphics g,int NPOs)
{
Draw Black Chess
int x,y;
x = npos%15;
y = NPOS/15;
Imagelistbw.drawimage (g,8+20*x,5+20*y,20,20,0,0);
}
private void Drawwhite (Graphics g,int NPOs)
{
Draw White Chess
int x,y;
x = npos%15;
y = NPOS/15;
Imagelistbw.drawimage (g,8+20*x,5+20*y,20,20,0,1);
}
<summary>
Clean up any being used.
</summary>
public override void Dispose ()
{
Base. Dispose ();
Components. Dispose ();
}
<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof (Fiveform));
this.components = new System.ComponentModel.Container ();
THIS.IMAGELISTBW = new System.WinForms.ImageList ();
@this. Trayheight = 90;
@this. TrayLargeIcon = false;
@this. Trayautoarrange = true;
@imageListbw. setlocation (New System.Drawing.Point (7, 7));
Imagelistbw.imagesize = new System.Drawing.Size (20, 20);
Imagelistbw.imagestream = (System.WinForms.ImageListStreamer) resources. GetObject ("Imagelistbw.imagestream");
Imagelistbw.colordepth = System.WinForms.ColorDepth.Depth8Bit;
Imagelistbw.transparentcolor = System.Drawing.Color.Yellow;
This. Text = "Fiveform";
This. MaximizeBox = false;
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. BorderStyle = System.WinForms.FormBorderStyle.FixedSingle;
This. BackgroundImage = (System.Drawing.Image) resources. GetObject ("$this. BackgroundImage ");
This. TransparencyKey = System.Drawing.Color.White;
This. ClientSize = new System.Drawing.Size (314, 311);
}

<summary>
The main entry point is for the application.
</summary>
public static int Main (string[] args)
{
Application.Run (New Fiveform ());
return 0;
}
}
}


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.