public void Clear() { turn = 棋子.无; panel.Clear(); black = null; white = null; }
public void OnMessage(MessageKey name, object param) { switch (name) { case MessageKey.Restart: ChessPlayer[] players = param as ChessPlayer[]; Initialize(players[0].Name, players[1].Name); break; case MessageKey.FinishTurn: object[] os = param as object[]; int x = (int)os[1]; int y = (int)os[2]; 棋子 side = (棋子)os[0]; Record(side, x, y); break; case MessageKey.Finish: 棋子 s = (棋子)param; Finish(s); break; case MessageKey.Equal: Finish(棋子.无); break; } }
public void OnMessage(MessageKey name, object param) { if (this.Enabled && this.Visible) { switch (name) { case MessageKey.Finish: 棋子 side = (棋子)(param); Finish(side); break; case MessageKey.FinishTurn: this.Invalidate(); break; case MessageKey.Equal: Finish(棋子.无); break; case MessageKey.Restart: ChessPlayer[] players = param as ChessPlayer[]; this.Restart(players[0], players[1]); break; } } }
private void Record(棋子 side, int x, int y) { if (_writer != null) { _writer.WriteLine("{0} {1} {2}", (int)side, x, y); } }
public void MakeStep(棋子 side, int x, int y) { if (panel.Has(x, y)) { return; } panel.MakeStep(x, y, side); }
private void Finish(棋子 side) { if (_writer != null) { _writer.Write((int)side); _writer.Close(); _stream.Close(); _writer = null; } }
public void Finish(棋子 side) { turn = 棋子.无; if (side == 棋子.无) { Messager.Instance.SendMessageLater(MessageKey.Equal, null); } else { Messager.Instance.SendMessageLater(MessageKey.Finish, side); } }
public static 棋子 GetOppside(棋子 side) { switch (side) { case 棋子.白子: return(棋子.黑子); case 棋子.黑子: return(棋子.白子); } return(棋子.无); }
private int CalculateLine(棋子[][] positions, int deltaX, int deltaY, int x, int y, 棋子 side, Position p, int delta) { if (side == 棋子.无) { return(0); } int leftNum = 0; int rightNum = 0; bool left = true; bool right = true; for (int i = 1; i <= 4; i++) { if (left) { 棋子 s = GetSide(x - deltaX * i, y - deltaY * i, positions.Length, positions[x].Length, positions); if (s == side) { leftNum++; } else if (s != 棋子.无) { left = false; } else if (IsBeyondBorder(x - deltaX * i, positions.Length) || IsBeyondBorder(y - deltaY * i, positions.Length)) { left = false; } } if (right) { 棋子 s = GetSide(x + deltaX * i, y + deltaY * i, positions.Length, positions[x].Length, positions); if (s == side) { rightNum++; } else if (s != 棋子.无) { right = false; } else if (IsBeyondBorder(x - deltaX * i, positions.Length) || IsBeyondBorder(y - deltaY * i, positions.Length)) { right = false; } } if (!left && !right) { break; } } return(CalculateValue(leftNum, rightNum, left, right, p, delta)); }
private 棋子 GetOpposite(棋子 q) { if (q == 棋子.白子) { return(棋子.黑子); } if (q == 棋子.黑子) { return(棋子.白子); } return(棋子.无); }
public void ChangeTurn() { if (turn == 棋子.无) { turn = 棋子.黑子; } else if (turn == 棋子.黑子) { turn = 棋子.白子; } else if (turn == 棋子.白子) { turn = 棋子.黑子; } }
public void Restart() { if (this.black.IsHuman() || this.white.IsHuman()) { this.ChangeTurn(); this.OnYourTurn(); } else { 棋子 side = 棋子.无; int x = 0; int y = 0; while (true) { this.ChangeTurn(); this.OnYourTurn(); if (turn == 棋子.白子) { side = this.white.LastMove.Side; x = this.white.LastMove.Position.X; y = this.white.LastMove.Position.Y; } else if (turn == 棋子.黑子) { side = this.black.LastMove.Side; x = this.black.LastMove.Position.X; y = this.black.LastMove.Position.Y; } else { MessageBox.Show("Error!过程中出现错误!"); return; } MakeStep(side, x, y); if (Judge(x, y, side)) { Finish(side); return; } else if (JudgeEqual()) { Finish(棋子.无); return; } } } }
public bool Finish(棋子 side) { LeagueResult result = RecordPerformance(side); if (result.Win + result.Lose + result.Equal < _times) { return(true); } else if (_queue.Count > 0) { _pair = _queue.Dequeue(); return(true); } else { return(false); } }
public void OnMessage(MessageKey name, object param) { if (black == null || white == null || panel == null) { return; } if (!this.black.IsHuman() && !this.white.IsHuman()) { return; } switch (name) { case MessageKey.MouseDown: { int[] array = param as int[]; int x = array[0]; int y = array[1]; if (panel.Has(x, y)) { break; } if (turn == 棋子.白子) { white.OnMouseClick(x, y); } else if (turn == 棋子.黑子) { black.OnMouseClick(x, y); } } break; case MessageKey.FinishTurn: { object[] os = param as object[]; int x = (int)os[1]; int y = (int)os[2]; 棋子 p = (棋子)os[0]; FinishTurn(p, x, y); } break; } }
private void DrawPosition(int x, int y, 棋子 side, Graphics graphics) { Brush brush = null; if (side == 棋子.白子) { brush = Brushes.White; } else if (side == 棋子.黑子) { brush = Brushes.Black; } else { return; } graphics.FillEllipse(brush, left + x * interval - interval * 0.4f, top + y * interval - interval * 0.4f, interval * 0.8f, interval * 0.8f); }
private ChessMove StartOneChess() { 棋子 side = 棋子.无; int x = 0; int y = 0; while (true) { _rule.ChangeTurn(); _rule.OnYourTurn(); if (_rule.GetTurn() == 棋子.白子) { side = _rule.GetWhite().LastMove.Side; x = _rule.GetWhite().LastMove.Position.X; y = _rule.GetWhite().LastMove.Position.Y; } else if (_rule.GetTurn() == 棋子.黑子) { side = _rule.GetBlack().LastMove.Side; x = _rule.GetBlack().LastMove.Position.X; y = _rule.GetBlack().LastMove.Position.Y; } else { MessageBox.Show("Error!过程中出现错误!"); return(new ChessMove(side, new Position(x, y))); } _rule.MakeStep(side, x, y); if (_rule.Judge(x, y, side)) { _rule.Finish(side); return(new ChessMove(side, new Position(x, y))); } else if (_rule.JudgeEqual()) { _rule.Finish(棋子.无); return(new ChessMove(棋子.无, new Position(x, y))); } } }
public bool Judge(int x, int y, 棋子 side) { if (JudgeLine(0, 1, x, y, side)) { return(true); } if (JudgeLine(1, 0, x, y, side)) { return(true); } if (JudgeLine(-1, 1, x, y, side)) { return(true); } if (JudgeLine(1, 1, x, y, side)) { return(true); } return(false); }
public void FinishTurn(棋子 side, int x, int y) { MakeStep(side, x, y); if (Judge(x, y, side)) { Finish(side); } else if (JudgeEqual()) { Finish(棋子.无); } else { if (turn != 棋子.无) { ChangeTurn(); OnYourTurn(); } } }
private LeagueResult RecordPerformance(棋子 side) { LeagueResult result = _performance[_pair.Key][_pair.Value]; switch (side) { case 棋子.无: result.Equal++; break; case 棋子.白子: result.Lose++; break; case 棋子.黑子: result.Win++; break; } _performance[_pair.Key][_pair.Value] = result; return(result); }
private bool JudgeLine(int deltaX, int deltaY, int x, int y, 棋子 side) { if (side == 棋子.无) { return(false); } int x1 = x; int x2 = x; int y1 = y; int y2 = y; while (true) { x1 -= deltaX; y1 -= deltaY; if (!Utility.IsInChess(x1, y1)) { break; } if (panel.Positions[x1][y1] != side) { break; } } while (true) { x2 += deltaX; y2 += deltaY; if (!Utility.IsInChess(x2, y2)) { break; } if (panel.Positions[x2][y2] != side) { break; } } return(Math.Max(Math.Abs(x1 - x2), Math.Abs(y1 - y2)) >= 6); }
public void MakeStep(int x, int y, 棋子 side) { if (x < 0 || x >= sizeX) { return; } if (y < 0 || y >= sizeY) { return; } positions[x][y] = side; if (side == 棋子.白子) { whiteList.Add(new Position(x, y)); } else if (side == 棋子.黑子) { blackList.Add(new Position(x, y)); } mark[0] = new Position(x, y); }
public void Initialize(int sizeX, int sizeY) { positions = null; positions = new 棋子[sizeX][]; _positions = new 棋子[sizeX][]; this.sizeX = sizeX; this.sizeY = sizeY; for (int i = 0; i < sizeX; i++) { positions[i] = new 棋子[sizeY]; _positions[i] = new 棋子[sizeY]; for (int j = 0; j < sizeY; j++) { positions[i][j] = 棋子.无; _positions[i][j] = 棋子.无; } } blackList.Clear(); whiteList.Clear(); mark.Add(new Position()); }
private int CalculateValue(int x, int y, 棋子[][] positions, Position p, int delta) { if (positions[x][y] != 棋子.无) { return(-1); } int value = 0; value += CalculateLine(positions, 0, 1, x, y, selfSide, p, delta); value += CalculateLine(positions, 1, 0, x, y, selfSide, p, delta); value += CalculateLine(positions, 1, 1, x, y, selfSide, p, delta); value += CalculateLine(positions, -1, 1, x, y, selfSide, p, delta); 棋子 opp = GetOpposite(selfSide); value += CalculateLine(positions, 0, 1, x, y, opp, p, delta); value += CalculateLine(positions, 1, 0, x, y, opp, p, delta); value += CalculateLine(positions, 1, 1, x, y, opp, p, delta); value += CalculateLine(positions, -1, 1, x, y, opp, p, delta); return(value); }
private float CalculateValue(棋子[][] positions, 棋子 side, List <KeyValuePair <int, int> > list) { float val = 0f; if (positions == null || positions[0] == null) { return(0f); } //纵向扫描 for (int i = 0; i < positions.Length; i++) { val += ScanLine(i, 0, side, positions, 0, 1, list); } //横向扫描 for (int i = 0; i < positions.Length; i++) { val += ScanLine(0, i, side, positions, 1, 0, list); } //左上右下扫描 for (int i = 0; i < positions.Length; i++) { val += ScanLine(i, 0, side, positions, 1, 1, list); } for (int i = 1; i < positions.Length; i++) { val += ScanLine(0, i, side, positions, 1, 1, list); } //右上左下扫描 for (int i = 0; i < positions.Length; i++) { val += ScanLine(0, i, side, positions, 1, -1, list); } for (int i = 1; i < positions.Length; i++) { val += ScanLine(i, positions.Length - 1, side, positions, 1, -1, list); } return(val); }
private void CalculateAroudPosition(Position p, 棋子 side, 棋子[][] positions) { int minX = Math.Max(0, p.X - 4); int maxX = Math.Min(positions[0].Length - 1, p.X + 4); int minY = Math.Max(0, p.Y - 4); int maxY = Math.Min(positions.Length - 1, p.Y + 4); for (int i = minX; i <= maxX; i++) { for (int j = minY; j <= maxY; j++) { int deltaX = Math.Abs(p.X - i); int deltaY = Math.Abs(p.Y - j); if (datas[i][j] >= 0) { if (deltaX == 0 && deltaY == 0) { datas[i][j] = -1; } else if (deltaX == 0) { datas[i][j] = CalculateValue(i, j, positions, p, deltaY); //datas[i][j] += (5 - deltaY) * 50; } else if (deltaY == 0) { datas[i][j] = CalculateValue(i, j, positions, p, deltaX); } else if (deltaX == deltaY) { datas[i][j] = CalculateValue(i, j, positions, p, deltaX); } } } } }
public void Finish(棋子 side) { DisplayLabel.Text = side.ToString() + " Win!"; BlackPlayersBox.Enabled = true; WhitePlayersBox.Enabled = true; }
public ChessMove(棋子 s, Position p) { Side = s; Position = p; }
public AIResult(棋子 side, int x, int y) { X = x; Y = y; Side = side; }
public void SetSide(棋子 side) { this.selfSide = side; }
public ChessMove(棋子 s, int x, int y) { Side = s; Position = new Position(x, y); }