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 xDialog;
namespace five_in_a_row {
public enum ChessBoardState { empty = 0, black = 1, white = 2, outrange =3 }; public partial class Form1 : Form { private ChessBoardState nextstate; private bool GameEnable = false; private Point ChessLocation; private Rule rule=new Rule(); private GameJudge myjudge = new GameJudge(); private ChessBoardState mystate; public Form1() { InitializeComponent(); this.nextstate = ChessBoardState.empty; this.comboBox1.Text = this.comboBox1.Items[0].ToString(); this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.textBox1.AppendText("请点击开始游戏\r\n祝您游戏愉快"); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen pen2 = new Pen(Color.Brown, 3); for (int i = 0; i < 19; i++) { g.DrawLine(pen2, 15, 15 + i * 30, 615, 15 + i * 30); } for (int i = 0; i < 21; i++) { g.DrawLine(pen2, 15 + i * 30, 15, 15 + i * 30, 465); } } private void button1_Click(object sender, EventArgs e) { this.textBox1.ResetText(); this.textBox1.AppendText("黑子先行\n"); if(this.comboBox1.Text=="黑子") { this.textBox1.AppendText("您执黑子\n"); mystate = ChessBoardState.white; this.nextstate = ChessBoardState.black; } else { this.textBox1.AppendText("您执白子\n"); mystate = ChessBoardState.black; this.nextstate = ChessBoardState.white; rule.SetChessBoardState(9, 10, ChessBoardState.black); myjudge.ChessJudgeInitSet(9, 10, ChessBoardState.black); ChessLocation.X = 9; ChessLocation.Y = 10; Graphics g = this.pictureBox1.CreateGraphics(); Pen pen3 = new Pen(Color.Black, 10); g.DrawEllipse(pen3, 10 + 30 * ChessLocation.X, 10 + 30 * ChessLocation.Y, 10, 10); g.Dispose(); } this.comboBox1.Enabled = false; GameEnable = true; } private void button2_Click(object sender, EventArgs e) { this.textBox1.ResetText(); this.textBox1.AppendText("只能悔棋一次哦:-)\n"); rule.SetChessBoardState(ChessLocation.X, ChessLocation.Y, ChessBoardState.empty); myjudge.ChessJudgeInitSet(ChessLocation.X, ChessLocation.Y, ChessBoardState.empty); this.pictureBox1.Refresh(); Graphics g = this.pictureBox1.CreateGraphics(); for (int i=0;i { for(int j = 0;j { if (rule.GetChessBoardState(i, j) == ChessBoardState.black) { Pen pen3 = new Pen(Color.Black, 10); g.DrawEllipse(pen3, 10 + 30 * i, 10 + 30 * j, 10, 10); pen3.Dispose(); } else if(rule.GetChessBoardState(i, j) == ChessBoardState.white) { Pen pen4 = new Pen(Color.Aqua, 10); g.DrawEllipse(pen4, 10 + 30 * i, 10 + 30 * j, 10, 10); pen4.Dispose(); } } } if (this.nextstate == ChessBoardState.black) this.nextstate = ChessBoardState.white; else this.nextstate = ChessBoardState.black; g.Dispose(); } private void button3_Click(object sender, EventArgs e) { this.Close(); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if(GameEnable == true) { ChessLocation.X = (e.X + 30) / 30 - 1; ChessLocation.Y = (e.Y + 30) / 30 - 1; if (rule.GetChessBoardState(ChessLocation.X, ChessLocation.Y) != ChessBoardState.empty) return; Graphics g = this.pictureBox1.CreateGraphics(); if (this.nextstate == ChessBoardState.black) { Pen pen3 = new Pen(Color.Black, 10); g.DrawEllipse(pen3, 10 + 30 * ChessLocation.X, 10 + 30 * ChessLocation.Y, 10, 10); } else { Pen pen4 = new Pen(Color.Aqua, 10); g.DrawEllipse(pen4, 10 + 30 * ChessLocation.X, 10 + 30 * ChessLocation.Y, 10, 10); } rule.SetChessBoardState(ChessLocation.X, ChessLocation.Y, this.nextstate); myjudge.ChessJudgeInitSet(ChessLocation.X, ChessLocation.Y, this.nextstate); if (rule.CheckCountLIne(ChessLocation.X, ChessLocation.Y, this.nextstate) == true) { if(this.nextstate == ChessBoardState.white) { this.textBox1.AppendText("\n白子胜利!\n"); DialogResult result = MsgBox.Show("白子胜利", "本局结束", MsgBox.Buttons.OK, MsgBox.Icon.Exclamation, MsgBox.AnimateStyle.SlideDown); } else { this.textBox1.AppendText("\n黑子胜利!\n"); DialogResult result = MsgBox.Show("黑子胜利", "本局结束", MsgBox.Buttons.OK, MsgBox.Icon.Exclamation, MsgBox.AnimateStyle.SlideDown); } for (int i = 0; i < Rule.COL; i++) { for (int j = 0; j < Rule.ROW; j++) { rule.SetChessBoardState(i, j, ChessBoardState.empty); myjudge.ChessJudgeInitSet(i, j, ChessBoardState.empty); } } this.pictureBox1.Refresh(); this.textBox1.ResetText(); this.textBox1.AppendText("请点击开始游戏"); this.comboBox1.Enabled = true; } else { this.textBox1.ResetText(); this.textBox1.AppendText("此子得分:" + myjudge.GetPointScore(ChessLocation.X, ChessLocation.Y, this.nextstate).ToString() + "分\r\n"); Point next = myjudge.NextJudge(mystate,out int max_score); rule.SetChessBoardState(next.X,next.Y,mystate); myjudge.ChessJudgeInitSet(next.X, next.Y, mystate); if (mystate == ChessBoardState.black) { Pen pen3 = new Pen(Color.Black, 10); g.DrawEllipse(pen3, 10 + 30 * next.X, 10 + 30 * next.Y, 10, 10); } else { Pen pen4 = new Pen(Color.Aqua, 10); g.DrawEllipse(pen4, 10 + 30 * next.X, 10 + 30 * next.Y, 10, 10); } g.Dispose(); this.textBox1.AppendText("电脑预测:" + max_score.ToString()+"分"); if (rule.CheckCountLIne(next.X, next.Y, mystate) == true) { if (mystate == ChessBoardState.white) { this.textBox1.AppendText("\n白子胜利!\n"); DialogResult result = MsgBox.Show("白子胜利", "本局结束", MsgBox.Buttons.OK, MsgBox.Icon.Exclamation, MsgBox.AnimateStyle.SlideDown); } else { this.textBox1.AppendText("\n黑子胜利!\n"); DialogResult result = MsgBox.Show("黑子胜利", "本局结束", MsgBox.Buttons.OK, MsgBox.Icon.Exclamation, MsgBox.AnimateStyle.SlideDown); } for (int i = 0; i < Rule.COL; i++) { for (int j = 0; j < Rule.ROW; j++) { rule.SetChessBoardState(i, j, ChessBoardState.empty); myjudge.ChessJudgeInitSet(i, j, ChessBoardState.empty); } } this.pictureBox1.Refresh(); this.textBox1.ResetText(); this.textBox1.AppendText("请点击开始游戏"); this.comboBox1.Enabled = true; } } } } private void button4_Click(object sender, EventArgs e) { GameEnable = false; this.pictureBox1.Refresh(); for (int i = 0; i < Rule.COL; i++) { for (int j = 0; j < Rule.ROW; j++) { rule.SetChessBoardState(i, j, ChessBoardState.empty); myjudge.ChessJudgeInitSet(i, j, ChessBoardState.empty); } } this.textBox1.ResetText(); this.textBox1.AppendText("请点击开始游戏"); } } class Rule { public const int ROW = 18; public const int COL = 21; private ChessBoardState[,] ChessBoard; private int CurrentX; private int CurrentY; private ChessBoardState CurrentState; public Rule() { this.ChessBoard = new ChessBoardState[COL, ROW]; this.CurrentX = -1; this.CurrentY = -1; this.CurrentState = ChessBoardState.empty; } public ChessBoardState GetChessBoardState(int x, int y) { if (x < 0 || x >= COL || y < 0 || y >= ROW) return ChessBoardState.outrange; return this.ChessBoard[x, y]; } public bool SetChessBoardState(int x, int y, ChessBoardState state) { if (x < 0 || x >= COL || y < 0 || y >= ROW) return false; else { this.ChessBoard[x, y] = state; this.CurrentX = x; this.CurrentY = y; this.CurrentState = state; return true; } } public bool SetChessBoardState(Point point, ChessBoardState state) { if (point.X < 0 || point.X >= COL || point.Y < 0 || point.Y >= ROW) return false; else { this.ChessBoard[point.X, point.Y] = state; this.CurrentX = point.X; this.CurrentY = point.Y; this.CurrentState = state; return true; } } public bool CheckCountLIne(int x, int y, ChessBoardState state) {
int j, count = 0; for (j = -4; j < 5; j++) { if (GetChessBoardState(x + j, y) == state) { count += 1; if (count >= 5) return true; } else count = 0; } count = 0; for (j = -4; j < 5; j++) { if (GetChessBoardState(x, y - j) == state) { count += 1; if (count >= 5) return true; } else count = 0; } count = 0; for (j = -4; j < 5; j++) { if (GetChessBoardState(x + j, y + j) == state) { count += 1; if (count >= 5) return true; } else count = 0; } count = 0; for (j = -4; j < 5; j++) { if (GetChessBoardState(x + j, y - j) == state) { count += 1; if (count >= 5) return true; } else count = 0; } return false; } }
class GameJudge { Rule chessjudge = new Rule(); private const int BE_FIVE = 10000000; private const int ACTIVIE_FOUR = 40000; private const int NON_FOUR = 5000; private const int ACTIVIE_THREE = 5000; private const int NON_THREE = 300; private const int ACTIVE_TWO = 300; private const int NON_TWO = 10; private const int OTHER = 1; private const int SEARCH_RANGE = 2;
public void ChessJudgeInitSet(int x,int y,ChessBoardState state) { chessjudge.SetChessBoardState(x, y, state); } public int GetPointScore(int x,int y,ChessBoardState state) { int j, count=0,count_max = 0; int score = 0; bool non_flag = false, now_non_flag=false; ChessBoardState non_state; if (state == ChessBoardState.black) non_state = ChessBoardState.white; else non_state = ChessBoardState.black; for (j = -4; j <= 4; j++) { if (chessjudge.GetChessBoardState(x + j, y) == state) { if (count == 0 && (chessjudge.GetChessBoardState(x + j - 1, y) == non_state || chessjudge.GetChessBoardState(x + j - 1, y) == ChessBoardState.outrange)) non_flag = true; count += 1; } else { if (chessjudge.GetChessBoardState(x + j, y) == non_state || chessjudge.GetChessBoardState(x + j, y) == ChessBoardState.outrange) non_flag = true; if (count > count_max) { count_max = count; now_non_flag = non_flag; } count = 0; non_flag =false; } } score += ScoreJudge(count_max, now_non_flag); count = 0; count_max = 0;
for (j = -4; j <= 4; j++) { if (chessjudge.GetChessBoardState(x, y - j) == state) { if (count == 0 && (chessjudge.GetChessBoardState(x, y - j + 1) == non_state || chessjudge.GetChessBoardState(x, y - j + 1) == ChessBoardState.outrange)) non_flag = true; count += 1; } else { if (chessjudge.GetChessBoardState(x, y - j) == non_state || chessjudge.GetChessBoardState(x, y - j) == ChessBoardState.outrange) non_flag = true; if (count > count_max) { count_max = count; now_non_flag = non_flag; } count = 0; non_flag = false; } } score += ScoreJudge(count_max, now_non_flag); count = 0; count_max = 0; for (j = -4; j < 5; j++) { if (chessjudge.GetChessBoardState(x + j, y + j) == state) { if (count == 0 && (chessjudge.GetChessBoardState(x + j - 1, y + j - 1) == non_state || chessjudge.GetChessBoardState(x + j - 1, y + j - 1) == ChessBoardState.outrange)) non_flag = true; count += 1; } else { if (chessjudge.GetChessBoardState(x + j, y + j) == non_state || chessjudge.GetChessBoardState(x + j, y + j) == ChessBoardState.outrange) non_flag = true; if (count > count_max) { count_max = count; now_non_flag = non_flag; } count = 0; non_flag = false; } } score += ScoreJudge(count_max, now_non_flag); count = 0; count_max = 0; for (j = -4; j < 5; j++) { if (chessjudge.GetChessBoardState(x + j, y - j) == state) { if (count == 0 && (chessjudge.GetChessBoardState(x + j - 1, y - j + 1) == non_state || chessjudge.GetChessBoardState(x + j - 1, y - j + 1) == ChessBoardState.outrange)) non_flag = true; count += 1; } else { if (chessjudge.GetChessBoardState(x + j, y - j) == non_state || chessjudge.GetChessBoardState(x + j, y - j) == ChessBoardState.outrange) non_flag = true; if (count > count_max) { count_max = count; now_non_flag = non_flag; } count = 0; non_flag = false; } } score += ScoreJudge(count_max, now_non_flag); return score; } private int ScoreJudge(int count,bool flag) { switch (count) { case 5: return BE_FIVE; case 4: if (flag == false) return ACTIVIE_FOUR; else return NON_FOUR; case 3: if (flag == false) return ACTIVIE_THREE; else return NON_THREE; case 2: if (flag == false) return ACTIVE_TWO; else return NON_TWO; default: return OTHER; } } bool SearchRange(int x,int y,ChessBoardState state) { if(chessjudge.GetChessBoardState(x,y)==ChessBoardState.empty) { for (int i = (x - SEARCH_RANGE); i <= (x + SEARCH_RANGE); i++) { for (int j = (y - SEARCH_RANGE); j <= (y + SEARCH_RANGE); j++) if (chessjudge.GetChessBoardState(i,j) == ChessBoardState.white ||chessjudge.GetChessBoardState(i,j)==ChessBoardState.black) { if (i != x || j != y) return true; } } } return false; }
private Point[] tmp = new Point[100]; public Point NextJudge(ChessBoardState state,out int max) { ChessBoardState anti_state; int cnt=0,score_tmp; Point best=new Point(); int[] min = new int[200]; max = -BE_FIVE - 1; for (int i=0;i<200;i++) { min[i] = BE_FIVE+1; } if (state == ChessBoardState.black) anti_state = ChessBoardState.white; else anti_state = ChessBoardState.black; int score = 0; for(int i=0;i { for (int j = 0; j < Rule.ROW; j++) { if (SearchRange(i, j, state) == true) { chessjudge.SetChessBoardState(i, j, state); score_tmp = GetPointScore(i, j, state); for(int m=0;m { for(int n=0;n { if (SearchRange(m, n, anti_state) == true) { chessjudge.SetChessBoardState(m, n, anti_state); score = score_tmp - GetPointScore(m, n, anti_state); if(score { tmp[cnt].X = i; tmp[cnt].Y = j; min[cnt] = score; } chessjudge.SetChessBoardState(m, n, ChessBoardState.empty); } } } chessjudge.SetChessBoardState(i, j, ChessBoardState.empty); cnt++; } } } cnt--; while (cnt >= 0) { if (min[cnt] > max) { max = min[cnt]; best.X = tmp[cnt].X; best.Y = tmp[cnt].Y; } cnt--; } return best; } } }
|