⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fiveform.cs

📁 五子棋游戏,给大家看看,能不能有所帮助,下载看看-呵呵.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 五子棋
{
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    /// 
    /// summary description for form1. 
    /// 
    public partial class fiveform : Form
    {
        /// 
        /// required designer variable. 
        /// 
        private System.ComponentModel.Container components;
        private 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 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 void onpaint(PaintEventArgs e)
        {
            //you may paint 
            Graphics g = e.Graphics;
        }
        protected void onmousedown(System.Windows.Forms.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",MessageBoxButtons.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", MessageBoxButtons.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 > 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", MessageBoxButtons.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 > 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", MessageBoxButtons.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(new Point(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.Draw(g, 8 + 20 * x, 5 + 20 * y, 20, 20, 0);
        }
        private void drawwhite(Graphics g, int npos)
        {
            //draw white chess 
            int x, y;
            x = npos % 15;
            y = npos / 15;
            imagelistbw.Draw(g, 8 + 20 * x, 5 + 20 * y, 20, 20, 1);
        }
        /// 
        /// clean up any resources being used. 
        /// 
        public 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.Windows.Forms.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.Windows.Forms.ImageListStreamer)resources.GetObject("imagelistbw.imagestream");
            imagelistbw.ColorDepth = System.Windows.Forms.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;
        }
    }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -