黑白棋 C#版本
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;namespace WindowsApplication1
{public partial class Form1 : Form{private int x1;//落子位置 private int y1;private int[,] Map = new int[10, 10];//0無子 1黑色 2白色 private int MyColor;string[] Info = new string[61];public Form1(){InitializeComponent();}private void Show_Can_Position(){//用圖片形式顯示可以落子的位置 int i;int j;Graphics g = this.pictureBox1.CreateGraphics();Bitmap bitmap = new Bitmap("Info2.png");//提示圖片 int n = 0;for (i = 1; i <= 8; i++){for (j = 1; j <= 8; j++){if (Map[i, j] == 0 & Can_go(i, j)){Info[n] = i + "|" + j;n = n + 1;g.DrawImage(bitmap, (i - 1) * 45 + 26, (j - 1) * 45 + 26, 30, 30);}}}}//統計可以落子的位置數 private int Show_Can_Num(){int i, j;int n = 0;for (i = 1; i <= 8; i++){for (j = 1; j <= 8; j++){if (Can_go(i, j)){Info[n] = i + "|" + j;n = n + 1;}}}return n;//可以落子的位置個數 }private void Cls_Can_Position(){int n;string a;string b;int x;int y;string s;Graphics g = this.pictureBox1.CreateGraphics();Bitmap bitmap = new Bitmap("BackColor.png");//背景圖片 for (n = 0; n <= 60; n++){s = Info[n];if (string.IsNullOrEmpty(s)) break;a = s.Substring(0, 1);//b = s.Substring(Strings.InStr(s, "|"), 1);b = s.Substring(s.IndexOf('|', 1) + 1);x = Convert.ToInt16(a);y = Convert.ToInt16(b);if (Map[x, y] == 0){g.DrawImage(bitmap, (x - 1) * 45 + 26, (y - 1) * 45 + 26, 30, 30);}//Me.Text = CInt(x) & y }}private bool CheckDirect(int x1, int y1, int dx, int dy){int x, y;bool flag;x = x1 + dx;y = y1 + dy;flag = false;while (InBoard(x, y) & !Ismychess(x, y) & Map[x, y] != 0){x += dx;y += dy;flag = true; //構成夾擊之勢 }if (InBoard(x, y) & Ismychess(x, y) & flag == true){return true; //該方向落子有效 }return false;}private void DirectReverse(int x1, int y1, int dx, int dy){int x, y;bool flag;x = x1 + dx;y = y1 + dy;flag = false;while (InBoard(x, y) & !Ismychess(x, y) & Map[x, y] != 0){x += dx;y += dy;flag = true; //構成夾擊之勢 }if (InBoard(x, y) & Ismychess(x, y) & flag == true){do{x -= dx;y -= dy;if ((x != x1 || y != y1)) FanQi(x, y);} while ((x != x1 || y != y1));}}private bool InBoard(int x, int y){if (x >= 1 & x <= 8 & y >= 1 & y <= 8)return true;elsereturn false;}private bool Can_go(int x1, int y1){//從左,左上,上,右上,右,右下,下,左下八個方向判斷if (CheckDirect(x1, y1, -1, 0) == true)return true;if (CheckDirect(x1, y1, -1, -1) == true)return true;if (CheckDirect(x1, y1, 0, -1) == true)return true;if (CheckDirect(x1, y1, 1, -1) == true)return true;if (CheckDirect(x1, y1, 1, 0) == true)return true;if (CheckDirect(x1, y1, 1, 1) == true)return true;if (CheckDirect(x1, y1, 0, 1) == true)return true;if (CheckDirect(x1, y1, -1, 1) == true)return true;return false;}private void pictureBox1_MouseDown(object sender, MouseEventArgs e){int x, y;int n = 0;x1 = (e.X - 22) / 45 + 1;y1 = (e.Y - 22) / 45 + 1;if (!Can_go(x1, y1)){toolStripStatusLabel1.Text = "此處不能走棋子";return;}if (MyColor == 1){listBox1.Items.Add("黑色落在(" + Convert.ToChar(x1 + 64).ToString() + "," + y1 + ")");}else{listBox1.Items.Add("白色落在(" + Convert.ToChar(x1 + 64).ToString() + "," + y1 + ")");}//(x1,y1)處原色處理 Graphics g = this.pictureBox1.CreateGraphics();Bitmap bitmap = new Bitmap("WhiteStone.png");if (MyColor == 2){Map[x1, y1] = 2;g.DrawImage(bitmap, (x1 - 1) * 45 + 22, (y1 - 1) * 45 + 22, 45, 45);}if (MyColor == 1){Map[x1, y1] = 1;bitmap = new Bitmap("BlackStone.png");g.DrawImage(bitmap, (x1 - 1) * 45 + 22, (y1 - 1) * 45 + 22, 45, 45);}//從左,左上,上,右上,右,右下,下,左下八個方向翻轉 if (CheckDirect(x1, y1, -1, 0) == true) //向左方向形成夾擊之勢DirectReverse(x1, y1, -1, 0);if (CheckDirect(x1, y1, -1, -1) == true) //向左上方向形成夾擊之勢DirectReverse(x1, y1, -1, -1);if (CheckDirect(x1, y1, 0, -1) == true) //向上方向形成夾擊之勢DirectReverse(x1, y1, 0, -1);if (CheckDirect(x1, y1, 1, -1) == true) //向右上方向形成夾擊之勢DirectReverse(x1, y1, 1, -1);if (CheckDirect(x1, y1, 1, 0) == true)DirectReverse(x1, y1, 1, 0);if (CheckDirect(x1, y1, 1, 1) == true)DirectReverse(x1, y1, 1, 1);if (CheckDirect(x1, y1, 0, 1) == true)DirectReverse(x1, y1, 0, 1);if (CheckDirect(x1, y1, -1, 1) == true)DirectReverse(x1, y1, -1, 1);Cls_Can_Position(); //清除提示 if (MyColor == 1){//狀態行提示該對方走棋 MyColor = 2;toolStripStatusLabel1.Text = "白色棋子走";}else{MyColor = 1;toolStripStatusLabel1.Text = "黑色棋子走";}Show_Can_Position(); //顯示提示 if (Show_Can_Num() == 0){MessageBox.Show("提示", "對方無可走位置,請繼續走棋");if (MyColor == 1){MyColor = 2;toolStripStatusLabel1.Text = "白色棋子繼續走";}else{MyColor = 1;toolStripStatusLabel1.Text = "黑色棋子繼續走";}Show_Can_Position(); //顯示提示 }//判斷游戲是否結束 int whitenum = 0;int blacknum = 0;for (x = 1; x <= 8; x++){for (y = 1; y <= 8; y++){if (Map[x, y] != 0){n = n + 1;if (Map[x, y] == 2)whitenum += 1;if (Map[x, y] == 1)blacknum += 1;}}}if (n == 64) //在棋盤下滿時, {if (blacknum > whitenum){MessageBox.Show("游戲結束黑方勝利", "黑方:" + blacknum + "白方:" + whitenum);}else{MessageBox.Show("游戲結束白方勝利", "黑方:" + blacknum + "白方:" + whitenum);}pictureBox1.Enabled = false; //游戲結束,不能走棋 button1.Enabled = true; //"開始游戲"按鈕有效 return;}//在棋盤還沒有下滿時 if (whitenum == 0){MessageBox.Show("游戲結束黑方勝利", "黑方:" + blacknum + "白方:" + whitenum);pictureBox1.Enabled = false; //游戲結束,不能走棋 button1.Enabled = true; //"開始游戲"按鈕有效 }if (blacknum == 0){MessageBox.Show("游戲結束白方勝利", "黑方:" + blacknum + "白方:" + whitenum);pictureBox1.Enabled = false; //游戲結束,不能走棋 button1.Enabled = true; //"開始游戲"按鈕有效 }}private void FanQi(int x, int y){Graphics g = this.pictureBox1.CreateGraphics();Bitmap bitmap = new Bitmap("WhiteStone.png");//1黑色 2白色 if (Map[x, y] == 1){Map[x, y] = 2;g.DrawImage(bitmap, (x - 1) * 45 + 22, (y - 1) * 45 + 22, 45, 45);}else{Map[x, y] = 1;bitmap = new Bitmap("BlackStone.png");g.DrawImage(bitmap, (x - 1) * 45 + 22, (y - 1) * 45 + 22, 45, 45);}Thread.Sleep(200); //延時0.2秒listBox1.Items.Add(" (" + Convert.ToChar(x + 64).ToString() + "," + y + ")處被反色");}private bool Ismychess(int x, int y){if (Map[x, y] == MyColor)return true;elsereturn false;}private void button1_Click(object sender, EventArgs e){//開始游戲按鈕事件過程 int x, y;Graphics g = this.pictureBox1.CreateGraphics();Bitmap bitmap = new Bitmap("WhiteStone.png");pictureBox1.Enabled = true;pictureBox1.Refresh();for (x = 1; x <= 8; x++){for (y = 1; y <= 8; y++){Map[x, y] = 0;}}listBox1.Items.Clear();//棋盤上畫初始4個棋子 x = 4;y = 4;g.DrawImage(bitmap, (x - 1) * 45 + 22, (y - 1) * 45 + 22, 45, 45);x = 5;y = 5;g.DrawImage(bitmap, (x - 1) * 45 + 22, (y - 1) * 45 + 22, 45, 45);bitmap = new Bitmap("BlackStone.png");x = 5;y = 4;g.DrawImage(bitmap, (x - 1) * 45 + 22, (y - 1) * 45 + 22, 45, 45);x = 4;y = 5;g.DrawImage(bitmap, (x - 1) * 45 + 22, (y - 1) * 45 + 22, 45, 45);Map[4, 4] = 2;//0無子 1黑色 2白色 Map[5, 5] = 2;Map[4, 5] = 1;Map[5, 4] = 1;MyColor = 1;//自己棋子顏色為黑色 toolStripStatusLabel1.Text = "黑色棋子先走";this.Text = "黑白棋游戲";Show_Can_Position();}private void button2_Click(object sender, EventArgs e){Show_Can_Position();}private void button3_Click(object sender, EventArgs e){Cls_Can_Position();}private void button4_Click(object sender, EventArgs e){Application.Exit();}}
}
總結
- 上一篇: mardmardh
- 下一篇: 微信群影视机器人登录使用教程