Sunday 15 December 2013

c# gobang









using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace gobang
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            boundary1 = new Point(this.Width / 2 / square - 2, this.Height / 2 / square - 2);
            boundary2 = new Point(this.Width / 2 / square + 2, this.Height / 2 / square + 2);
            timer1.Start();
        }

        private Point[] white, black;
        private int whitenum = 0, blacknum = 0, x, y, square = 30, picnum=0, mousex=0, mousey=0;
        private bool turn = false, preturn, busy = false;
        private bool[] p, q, r, s, h, hh, pp, qq, rr, ss, np, nq, nr, ns, npp, nqq, nrr, nss;
        private Point boundary1, boundary2;

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            x = e.X / square;
            y = e.Y / square;

            if (busy == false)
            {
                busy = true;

                playgo();
                getboundary();

                calculate(turn, black, white, blacknum, whitenum);

                this.Refresh();
                busy = false;
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //board lines
            for (int i = 0; i * square < this.Size.Width; i++)
            {
                Pen bluepen = new Pen(Color.DarkBlue, 2);
                Point a = new Point(square / 2 + square * i, 0);
                Point b = new Point(square / 2 + square * i, this.Size.Height);
                e.Graphics.DrawLine(bluepen, a, b);
            }

            for (int i = 0; i * square < this.Size.Height; i++)
            {
                Pen bluepen = new Pen(Color.DarkBlue, 2);
                Point a = new Point(0, square / 2 + square * i);
                Point b = new Point(this.Size.Height, square / 2 + square * i);
                e.Graphics.DrawLine(bluepen, a, b);
            }

            //white piece
            for (int i = 0; i < whitenum; i++)
            {
                SolidBrush whitebrush = new SolidBrush(Color.White);
                e.Graphics.FillEllipse(whitebrush, square * white[i].X, square * white[i].Y, square, square);
            }

            //black piece
            for (int i = 0; i < blacknum; i++)
            {
                SolidBrush blackbrush = new SolidBrush(Color.Black);
                e.Graphics.FillEllipse(blackbrush, square * black[i].X, square * black[i].Y, square, square);
            }

            //boundary
            Pen greenpen = new Pen(Color.Green, 10);
            e.Graphics.DrawRectangle(greenpen, square * boundary1.X - square / 2, square * boundary1.Y - square / 2, square * (boundary2.X - boundary1.X), square * (boundary2.Y - boundary1.Y));

            //last move
            Pen redpen = new Pen(Color.Red, 1);
            e.Graphics.DrawRectangle(redpen, x*square, y*square, square, square);

            //mouse
            SolidBrush whitebrush1 = new SolidBrush(Color.White);
            e.Graphics.FillEllipse(whitebrush1, mousex-square/2, mousey-square/2, square, square);
        }

        private void playgo()
        {
            busy = true;
            bool occupy = false;

            if (turn == false)//white turn
            {
                turn = true;
                if (checkoccupy(white, whitenum, x, y) || checkoccupy(black, blacknum, x, y))
                {
                    occupy = true;
                }

                if (occupy == false)
                {
                    whitenum++;
                    Array.Resize(ref white, whitenum);
                    white = addarray(white, whitenum, x, y);
                    preturn = turn;
                }
                else
                {
                    turn = preturn;
                }
            }
            else//black turn
            {
                turn = false;
                if (checkoccupy(white, whitenum, x, y) || checkoccupy(black, blacknum, x, y))
                {
                    occupy = true;
                }

                if (occupy == false)
                {
                    blacknum++;
                    Array.Resize(ref black, blacknum);
                    black = addarray(black, blacknum, x, y);
                    preturn = turn;
                }
                else
                {
                    turn = preturn;
                }
            }
        }

        private void calculate(bool t, Point[] p1, Point[] p2, int num1, int num2)
        {
            if (t == true)//black turn
            {
                for (int depth = 0; depth != 35; depth++)
                {
                    for (int i = boundary1.X; i < boundary2.X; i++)
                    {
                        for (int j = boundary1.Y; j < boundary2.Y; j++)
                        {
                            if (!checkoccupy(p1, num1, i, j) && !checkoccupy(p2, num2, i, j))
                            {
                                p = new bool[] { false, false, false, false, false, false, false, false };
                                np = new bool[] { false, false, false, false, false, false, false, false };
                                q = new bool[] { false, false, false, false, false, false, false, false };
                                nq = new bool[] { false, false, false, false, false, false, false, false };
                                r = new bool[] { false, false, false, false, false, false, false, false };
                                s = new bool[] { false, false, false, false, false, false, false, false };
                                nr = new bool[] { false, false, false, false, false, false, false, false };
                                h = new bool[] { false, false, false, false, false, false, false, false };

                                p = emptypointinfo(p1, num1, i, j, 1, p);
                                q = emptypointinfo(p1, num1, i, j, 2, q);
                                r = emptypointinfo(p1, num1, i, j, 3, r);
                                s = emptypointinfo(p1, num1, i, j, 4, s);
                                h = emptypointinfo(p1, num1, i, j, 5, h);
                                nq = emptypointinfo(p1, num1, i, j, -2, nq);
                                np = emptypointinfo(p1, num1, i, j, -1, np);
                                nr = emptypointinfo(p1, num1, i, j, -3, nr);


                                pp = new bool[] { false, false, false, false, false, false, false, false };
                                qq = new bool[] { false, false, false, false, false, false, false, false };
                                nqq = new bool[] { false, false, false, false, false, false, false, false };
                                npp = new bool[] { false, false, false, false, false, false, false, false };
                                rr = new bool[] { false, false, false, false, false, false, false, false };
                                nrr = new bool[] { false, false, false, false, false, false, false, false };
                                ss = new bool[] { false, false, false, false, false, false, false, false };
                                hh = new bool[] { false, false, false, false, false, false, false, false };

                                pp = emptypointinfo(p2, num2, i, j, 1, pp);
                                qq = emptypointinfo(p2, num2, i, j, 2, qq);
                                nqq = emptypointinfo(p2, num2, i, j, -2, nqq);
                                npp = emptypointinfo(p2, num2, i, j, -1, npp);
                                rr = emptypointinfo(p2, num2, i, j, 3, rr);
                                nrr = emptypointinfo(p2, num2, i, j, -3, nrr);
                                ss = emptypointinfo(p2, num2, i, j, 4, ss);
                                hh = emptypointinfo(p2, num2, i, j, 5, hh);

                                int[] count = new int[20];
                                for (int ii = 0; ii < 20; ii++)
                                {
                                    count[ii] = 0;
                                }

                                for (int k = 0; k < 8; k++)
                                {
                                    if (pp[k] && qq[k] && rr[k] && ss[k] && hh[k]) { count[0]++; }//oxxxxx                                
                                    else if (p[k] && q[k] && r[k] && s[k]) { count[1]++; }//oxxxx
                                    else if (p[k] && q[k] && r[k] && np[k]) { count[1]++; }//xoxxx
                                    else if (p[k] && q[k] && np[k] && nq[k]) { count[1]++; }//xxoxx
                                    else if (pp[k] && qq[k] && rr[k] && ss[k]) { count[2]++; }//oxxxx
                                    else if (pp[k] && qq[k] && rr[k] && npp[k]) { count[2]++; }//xoxxx
                                    else if (pp[k] && qq[k] && npp[k] && nqq[k]) { count[2]++; }//xxoxx
                                    else if (p[k] && q[k] && r[k] && ss[k] && !npp[k] && !np[k]) { count[3]++; }//-oxxx\
                                    else if (p[k] && q[k] && s[k] && !rr[k] && !r[k] && hh[k]) { count[3]++; }//oxx-x\
                                    else if (p[k] && r[k] && s[k] && !qq[k] && !q[k] && hh[k]) { count[3]++; }//ox-xx\
                                    else if (q[k] && r[k] && s[k] && !pp[k] && !p[k] && hh[k]) { count[3]++; }//o-xxx\
                                    else if (p[k] && q[k] && np[k] && rr[k] && !nq[k] && !nqq[k]) { count[3]++; }//-xoxx\
                                    else if (p[k] && q[k] && np[k] && !rr[k] && !r[k] && nqq[k]) { count[3]++; }//\xoxx-
                                    else if (pp[k] && qq[k] && rr[k] && s[k] && !np[k] && !npp[k]) { count[4]++; }//-oxxx\
                                    else if (pp[k] && qq[k] && ss[k] && !r[k] && !rr[k] && h[k]) { count[4]++; }//oxx-x\
                                    else if (pp[k] && rr[k] && ss[k] && !q[k] && !qq[k] && h[k]) { count[4]++; }//ox-xx\
                                    else if (qq[k] && rr[k] && ss[k] && !p[k] && !pp[k] && h[k]) { count[4]++; }//o-xxx\
                                    else if (pp[k] && qq[k] && npp[k] && r[k] && !nq[k] && !nqq[k]) { count[4]++; }//-xoxx\
                                    else if (pp[k] && qq[k] && npp[k] && !r[k] && !rr[k] && nq[k]) { count[4]++; }//\xoxx-
                                    else if (p[k] && q[k] && r[k] && !ss[k] && !s[k] && !npp[k] && !np[k]) { count[7]++; } //-oxxx-
                                    else if (p[k] && q[k] && np[k] && !rr[k] && !r[k] && !nqq[k] && !nq[k]) { count[7]++; } //-xoxx-
                                    else if (pp[k] && qq[k] && rr[k] && !s[k] && !ss[k] && !np[k] && !npp[k]) { count[8]++; } //-oxxx-
                                    else if (pp[k] && qq[k] && npp[k] && !r[k] && !rr[k] && !nq[k] && !nqq[k]) { count[8]++; } //-xoxx-
                                    else if (k < 4 && p[k] && np[k] && !qq[k] && !nqq[k] && !nq[k] && !nqq[k] && !q[k] && !qq[k] && !(rr[k] && nrr[k])) { count[5]++; }//-xox-
                                    else if (p[k] && q[k] && !rr[k] && !r[k] && !npp[k] && !np[k] && !(ss[k] && nqq[k])) { count[5]++; }//-oxx-
                                    else if (!pp[k] && !p[k] && q[k] && r[k] && !ss[k] && !s[k] && !npp[k] && !np[k]) { count[5]++; }//-o-xx-
                                    else if (!pp[k] && q[k] && !rr[k] && !r[k] && np[k] && !nqq[k] && !nq[k]) { count[5]++; }//-xo-x-
                                    else if (p[k] && !qq[k] && !q[k] && r[k] && !ss[k] && !s[k] && !npp[k] && !np[k]) { count[5]++; }//-ox-x-
                                    else if (k < 4 && pp[k] && npp[k] && !q[k] && !qq[k] && !nq[k] && !nqq[k] && !qq[k] && !nqq[k] && !(r[k] && nr[k])) { count[6]++; }//-xox-
                                    else if (pp[k] && qq[k] && !r[k] && !rr[k] && !np[k] && !npp[k] && !(s[k] && nq[k])) { count[6]++; }//-oxx-
                                    else if (!p[k] && !pp[k] && qq[k] && rr[k] && !s[k] && !ss[k] && !np[k] && !npp[k]) { count[6]++; }//-o-xx-
                                    else if (!p[k] && !pp[k] && qq[k] && !r[k] && !rr[k] && npp[k] && !nq[k] && !nqq[k]) { count[6]++; }//-xo-x-
                                    else if (pp[k] && !q[k] && !qq[k] && rr[k] && !s[k] && !ss[k] && !np[k] && !npp[k]) { count[6]++; }//-ox-x-
                                    else if (!np[k] && !npp[k] && p[k] && !q[k] && !qq[k] && !r[k] && !rr[k]) { count[9]++; } //-ox--
                                    else if (!np[k] && !npp[k] && !p[k] && !pp[k] && q[k] && !r[k] && !rr[k]) { count[9]++; }//-o-x-
                                    else if (!np[k] && !npp[k] && pp[k] && !q[k] && !qq[k] && !r[k] && !rr[k]) { count[10]++; } //-ox--
                                    else if (!np[k] && !npp[k] && !p[k] && !pp[k] && qq[k] && !r[k] && !rr[k]) { count[10]++; }//-o-x-
                                }

                                switch (depth)
                                {
                                    case 0: //5
                                        if (count[0] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            MessageBox.Show("white wins");
                                            Application.Exit();
                                        }
                                        break;

                                    case 1: //A -4
                                        if (count[1] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 2://D -4
                                        if (count[2] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 3://A \3-x\3-
                                        if (count[3] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 4://D \3-x\3-
                                        if (count[4] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 5://A \3-x-2-
                                        if (count[3] > 0 && count[5] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 6://D \3-x-2-
                                        if (count[4] > 0 && count[6] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 7://A -3-
                                        if (count[7] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 8://D -3- && A -2-x-2-
                                        if (count[8] > 0 && count[5] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 9://D -3- && A -2-x-1-
                                        if (count[8] > 0 && count[5] > 0 && count[9] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 10://D -3- && A -1-x-1-
                                        if (count[8] > 0 && count[9] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 11://D -3- && A -2-
                                        if (count[8] > 0 && count[5] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 12://D -3-
                                        if (count[8] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 13://A -2-x-2-
                                        if (count[5] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 14://D -2-x-2-
                                        if (count[6] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 15://A -2-x-1- && D -2-x-1-
                                        if (count[5] > 0 && count[9] > 0 && count[6]>0 && count[10]>0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 16://A -2-x-1- && D -1-x-1-
                                        if (count[5] > 0 && count[9] > 0 && count[10] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 17://A -2-x-1- && D -2-
                                        if (count[5] > 0 && count[9] > 0 && count[6] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 18://A -2-x-1-
                                        if (count[5] > 0 && count[9] >0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 19://D -2-x-1- && A -1-x-1-
                                        if (count[6] > 0 && count[10] > 0 && count[9] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 20://D -2-x-1- && A -2-
                                        if (count[6] > 0 && count[10] > 0 && count[5] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 21://D -2-x-1-
                                        if (count[6] > 0 && count[10] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 22://A -1-x-1- && D -1-x-1-
                                        if (count[9] > 1 && count[10] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 23://A -1-x-1- && D -2-
                                        if (count[9] > 1 && count[6] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 24://A -1-x-1-
                                        if (count[9] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 25://D -1-x-1- && A -2-
                                        if (count[10] > 1 && count[5] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 26://D -1-x-1-
                                        if (count[10] > 1)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 27://A -2-
                                        if (count[5] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 28://D -2-
                                        if (count[6] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 29://A \3-
                                        if (count[3] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 30://D \3-
                                        if (count[4] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 31://A -1-
                                        if (count[9] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    case 32://D -1-
                                        if (count[10] > 0)
                                        {
                                            MessageBox.Show("depth " + depth);
                                            decision(i, j); if (turn == false) { return; }
                                        }
                                        break;

                                    default:
                                        return;
                                }



                            }
                        }
                    }
                }
            }
        }

        private bool checkoccupy(Point[] pp, int num, int px, int py)
        {
            for (int i = 0; i < num; i++)
            {
                if (pp[i].X == px && pp[i].Y == py) { return true; }
            }
            return false;
        }

        private Point[] addarray(Point[] array1, int num, int px, int py)
        {
            Point[] oldarray = array1;
            for (int i = 0; i < num - 1; i++)
            {
                array1[i] = oldarray[i];
            }
            array1[num - 1].X = px;
            array1[num - 1].Y = py;

            return array1;
        }

        private bool[] emptypointinfo(Point[] p, int num, int ex, int ey, int level, bool[] s)
        {
            bool[] info = new bool[8];
            info[0] = checkoccupy(p, num, ex - level, ey - level);
            info[1] = checkoccupy(p, num, ex, ey - level);
            info[2] = checkoccupy(p, num, ex + level, ey - level);
            info[3] = checkoccupy(p, num, ex + level, ey);
            info[4] = checkoccupy(p, num, ex + level, ey + level);
            info[5] = checkoccupy(p, num, ex, ey + level);
            info[6] = checkoccupy(p, num, ex - level, ey + level);
            info[7] = checkoccupy(p, num, ex - level, ey);
            return info;
        }

        private void decision(int i, int j)
        {
            x = i;
            y = j;
            playgo();
        }

        private void getboundary()
        {

            for (int i = 0; i < whitenum; i++)
            {
                if (white[i].X - 1 < boundary1.X) { boundary1.X = white[i].X - 1; }
                if (white[i].Y - 1 < boundary1.Y) { boundary1.Y = white[i].Y - 1; }
                if (white[i].X + 3 > boundary2.X) { boundary2.X = white[i].X + 3; }
                if (white[i].Y + 3 > boundary2.Y) { boundary2.Y = white[i].Y + 3; }
            }

            for (int i = 0; i < blacknum; i++)
            {
                if (black[i].X - 1 < boundary1.X) { boundary1.X = black[i].X - 1; }
                if (black[i].Y - 1 < boundary1.Y) { boundary1.Y = black[i].Y - 1; }
                if (black[i].X + 3 > boundary2.X) { boundary2.X = black[i].X + 3; }
                if (black[i].Y + 3 > boundary2.Y) { boundary2.Y = black[i].Y + 3; }
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Rectangle bounds = this.Bounds;
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
            Graphics g = Graphics.FromImage(bitmap);

            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
            bitmap.Save("test" + picnum + ".jpg");
            picnum++;
            this.Refresh();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            mousex = e.X;
            mousey = e.Y;
        }

    }
}




mouse event
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx

Saturday 14 December 2013

c# insatiable snake






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace snake
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Random r = new Random();
            preypoint = new Point[preynum];
            obspoint = new Point[obsnum];
            tailpoint = new Point[tailnum];

            //create obstacle
            for (int i = 0; i < obsnum; i++)
            {
                obspoint[i] = new Point(-1, -1);
            }

            for (int j = 0; j < obsnum; )
            {
                Point newobs = new Point(50 * Convert.ToInt32(r.Next(10)), 50 * Convert.ToInt32(r.Next(10)));
                bool replica = false;

                for (int k = 0; k < obsnum; k++)
                {
                    if (obspoint[k] == newobs)
                    {
                        replica = true;
                        break;
                    }
                }

                if (replica == false)
                {
                    obspoint[j] = newobs;
                    j++;
                }
            }

            //create prey
            for (int i = 0; i < preynum; i++)
            {
                preypoint[i] = new Point(-1, -1);
            }
         
            for (int j = 0; j < preynum; )
            {
                Point newprey = new Point( 50 * Convert.ToInt32(r.Next(10)), 50 * Convert.ToInt32(r.Next(10)));
                bool replica = false;

                for (int i = 0; i < obsnum; i++)
                {
                    if (obspoint[i] == newprey)
                    {
                        replica = true;
                        break;
                    }
                }

                if (replica == false)
                {
                    for (int k = 0; k < preynum; k++)
                    {
                        if (preypoint[k] == newprey)
                        {
                            replica = true;
                            break;
                        }
                    }
                }  

                if (replica == false)
                {
                    preypoint[j] = newprey;
                    j++;
                }
            }

            timer1.Start();
            timer2.Start();
        }

        private Point head = new Point(25, 25);
        private int rotateangle = 0, offsetx = 0, offsety=0, dir=2, predir=2;
        private int preynum = 15,tailnum=0, obsnum = 10, picnum=0;
        private Point[] preypoint,tailpoint,obspoint;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //obstacle
            Pen redpen = new Pen(Color.Red, 3);
            for (int i = 0; i < obsnum; i++)
            {
                Point obs1 = new Point(obspoint[i].X + 50, obspoint[i].Y + 50);
                Point obs2 = new Point(obspoint[i].X + 50, obspoint[i].Y);
                Point obs3 = new Point(obspoint[i].X, obspoint[i].Y + 50);
                e.Graphics.DrawLine(redpen, obspoint[i], obs1);
                e.Graphics.DrawLine(redpen, obs2, obs3);
            }

            //prey
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            Size rectsize = new Size(50,50);
            for (int i = 0; i < preynum; i++)
            {
                Rectangle preyrect = new Rectangle(preypoint[i], rectsize);
                e.Graphics.FillRectangle(greenBrush, preyrect);
            }

            //snake tail
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            for (int i = 0; i < tailnum; i++)
            {
                Rectangle tailrect = new Rectangle(tailpoint[i], rectsize);
                e.Graphics.FillRectangle(blackBrush, tailrect);
            }
           
            //snake head
            Point point1 = new Point(0, 0);
            Point point2 = new Point(50 , 0 );
            Point point3 = new Point(25 , 25 );
            Point point4 = new Point(50 , 50 );
            Point point5 = new Point(0 , 50);
            Point[] curvePoints = { point1, point2, point3, point4, point5, };
                                                                                   
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            GraphicsPath gp = new GraphicsPath();
            gp.AddPolygon(curvePoints);          

            //snake crawl
            Matrix rmatrix = new Matrix(1, 0, 0, 1, 0, 0);
            rmatrix.RotateAt(rotateangle,head);
            gp.Transform(rmatrix);

            Matrix omatrix = new Matrix(1, 0, 0, 1, 0, 0);
            omatrix.Translate(offsetx, offsety);
            gp.Transform(omatrix);

            e.Graphics.FillPath(blueBrush, gp);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            string key = e.KeyData.ToString();
            switch (key)
            {
                case "Left":
                    dir = 1;
                    break;
                case "Right":
                    dir = 2;
                    break;
                case "Up":
                    dir = 3;
                    break;
                case "Down":
                    dir = 4;
                    break;
                default:
                    break;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //cannot make 180 turn
            if (predir == 1 && dir == 2) { dir = 1; }
            if (predir == 2 && dir == 1) { dir = 2; }
            if (predir == 3 && dir == 4) { dir = 3; }
            if (predir == 4 && dir == 3) { dir = 4; }

            bool eat = false;
            int eatindex = 0, oldoffsetx = offsetx, oldoffsety = offsety;

            //move head, if hit wall, game over
            switch (dir)
            {
                case 1:
                    rotateangle = 180;
                    if (offsetx > 0) { offsetx -= 50; }
                    else { timer1.Stop(); MessageBox.Show("game over"); }
                    break;
                case 2:
                    rotateangle = 0;
                    if (offsetx < 450) { offsetx += 50; }
                    else { timer1.Stop(); MessageBox.Show("game over"); }
                    break;
                case 3:
                    rotateangle = -90;
                    if (offsety > 0) { offsety -= 50; }
                    else { timer1.Stop(); MessageBox.Show("game over"); }
                    break;
                case 4:
                    rotateangle = 90;
                    if (offsety < 450) { offsety += 50; }
                    else { timer1.Stop(); MessageBox.Show("game over"); }
                    break;
                default:
                    break;
            }
            predir = dir;        

            //if head eats tail, game over
            for (int i = 0; i < tailnum; i++)
            {
                if (tailpoint[i].X == offsetx && tailpoint[i].Y == offsety)
                {
                    timer1.Stop(); MessageBox.Show("game over");
                }
            }

            //if hit obstacle, game over
            for (int i = 0; i < obsnum; i++)
            {
                if (obspoint[i].X == offsetx && obspoint[i].Y == offsety)
                {
                    timer1.Stop(); MessageBox.Show("game over");
                }
            }

            //check if catch prey
            for (int i = 0; i < preynum; i++)
            {
                if (preypoint[i].X == offsetx && preypoint[i].Y == offsety)
                {
                    eat = true;
                    eatindex = i;
                    break;
                }
            }

            //if catch prey, eat prey, grow tail
            if (eat == true)
            {              
                Point[] oldprey = new Point[preynum];
                oldprey = preypoint;
                Point[] oldtail = new Point[tailnum];
                oldtail = tailpoint;
                preynum--; tailnum++;
                Array.Resize(ref preypoint, preynum);
                Array.Resize(ref tailpoint, tailnum);

                for (int j = 0; j <= preynum; j++)
                {
                    if (j < eatindex)
                    {
                        preypoint[j] = oldprey[j];
                    }
                    else if (j > eatindex)
                    {
                        preypoint[j - 1] = oldprey[j];
                    }
                }

                for (int k = 0; k < tailnum - 1; k++)
                {
                    tailpoint[k] = oldtail[k];
                }
                tailpoint[tailnum - 1].X = oldoffsetx;
                tailpoint[tailnum - 1].Y = oldoffsety;
            }

            //no prey, continue prowl
            else if (tailnum > 0)
            {
                for (int i = 0; i < tailnum - 1; i++)
                {
                    tailpoint[i] = tailpoint[i + 1];
                }

                tailpoint[tailnum - 1].X = oldoffsetx;
                tailpoint[tailnum - 1].Y = oldoffsety;
            }

            //win game
            if (preynum == 0)
            {
                timer1.Stop();
                MessageBox.Show("you win");
            }

            this.Refresh();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Rectangle bounds = this.Bounds;
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
            Graphics g = Graphics.FromImage(bitmap);

            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
            bitmap.Save("test" + picnum + ".jpg");
            picnum++;
        }
    }
}

Friday 13 December 2013

microsoft office 2010

http://pan.baidu.com/share/link?shareid=1633963157&uk=3238514412&fid=638246614

activation
http://ishare.iask.sina.com.cn/f/14017869.html

c# GDI region



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace irregular
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }

        private float f=0f;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(20, 20, 110, 110);
            Rectangle a = new Rectangle(110, 110, 220, 220);
            gp.AddRectangle(a);

            Matrix rotation = new Matrix(1, 0, 0, 1, 1, 1);
            PointF rotationpoint = new PointF(110f, 110f);
            rotation.RotateAt(f, rotationpoint);

            gp.Transform(rotation);
            e.Graphics.DrawPath(Pens.Red, gp);

            f += 1;
            this.Region = new Region(gp);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }
    }
}