五子棋
汉中名士杨松吧
全部回复
仅看楼主
level 5
namespace Leimom.FiveChess{using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.WinForms;using System.Data;///
/// Summary description for Form1./// public class FiveForm : System.WinForms.Form{///
/// Required designer variable./// private System.ComponentModel.Container components;private System.WinForms.ImageList imageListbw;//define the hot Rectangleprivate Rectangle[] pointSquares;//chess informationprivate 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 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){//you may paintGraphics g = e.Graphics;}protected override void OnMouseDown(System.WinForms.MouseEventArgs e) {switch( e.Button ){//take left button downcase MouseButtons.Left:OnLButtonDown(new Point(e.X,e.Y));break;//take right button downcase 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 chessif(nPos != -1&&chessTable[nPos] == 0){Graphics g = this.CreateGraphics();if(nextTurn==bTurn){//draw white chessDrawBlack(g,nPos);chessTable[nPos] = bTurn;nextTurn = wTurn;chessIndex.Push(bTurn);chessIndex.Push(nPos);}else{//draw Black chessDrawWhite(g,nPos);chessTable[nPos] = wTurn;nextTurn = bTurn;chessIndex.Push(wTurn);chessIndex.Push(nPos);}g.Dispose();//witch winCheckGameResult(nPos,nextTurn);} }private void CheckGameResult(int nPos,int nextTurn){//witch winStack isFive = new Stack();int thisTurn = (nextTurn == bTurn)?wTurn:bTurn;int x = nPos%15;int y = nPos/15;//scan x have fivefor(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 fivefor(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 fivefor(int i=-14;i<15;i++){if(x+i<0||x+i>14||y-i<0||y-i>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);
2007年09月18日 06点09分 1
level 5
ReSetGame();return;}}else{isFive.Clear();}}}isFive.Clear(); //scan x=-y have fivefor(int i=-14;i<15;i++){if(x+i<0||x+i>14||y+i<0||y+i>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 gamenextTurn = bTurn;for(int i=0;i<225;i++){chessTable[i] = 0;}this.Invalidate();}private int GetRectID(Point p){//get witch rectangle clickfor(int i = 0;i < 225;i++){if(pointSquares[i].Contains( p )){return i;}}return -1;}private void OnRButtonDown(Point p){//regret chessint 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(new Point(8+x*20,5+y*20),new Size(20,20)));}}private void DrawBlack(Graphics g,int nPos){//draw Black chessint 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 chessint x,y;x = nPos%15;y = nPos/15;imageListbw.DrawImage(g,8+20*x,5+20*y,20,20,0,1);}///
/// Clean up any resources being used./// public override void Dispose(){base.Dispose();components.Dispose();}///
/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// 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);} ///
/// The main entry point for the application./// public static int Main(string[] args) {Application.Run(new FiveForm());return 0;}}} 
2007年09月18日 06点09分 2
level 5
...卡丁车我是菜鸟啊,好,你来教我玩~
2007年10月30日 08点10分 5
1