public bool IsMoveCorrect(Player p, Move m) { if (board[m.x, m.y] != Cell.Empty) return false; else { if (p == Player.White) for (int i = 0; i < 8; i++) { try { if (board[m.x + combinations[i, 0], m.y + combinations[i, 1]] == Cell.Black) { for (int j = 2; j < 7; j++) { if (m.x + j * combinations[i, 0] > 7 || m.y + j * combinations[i, 1] > 7) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.Empty) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.White) { return true; } } } } catch (IndexOutOfRangeException) { } } else { for (int i = 0; i < 8; i++) { try { if (board[m.x + combinations[i, 0], m.y + combinations[i, 1]] == Cell.White) { for (int j = 2; j < 7; j++) { if (m.x + j * combinations[i, 0] > 7 || m.y + j * combinations[i, 1] > 7) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.Empty) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.Black) { return true; } } } } catch (IndexOutOfRangeException) { } } } } return false; }
static void Main(string[] args) { Board b = new Board(); Player p = Player.White; Console.WriteLine(b); while(true) { Console.WriteLine("White Move: o "); Move m = new Move(); Console.WriteLine("Введите координату x:"); m.x=Int32.Parse(Console.ReadLine()); Console.WriteLine("Введите координату y:"); m.y = Int32.Parse(Console.ReadLine()); b.PerformMove(p, m); Console.WriteLine("\n"); Console.WriteLine(b); if(p==Player.White)p=Player.Black; else p=Player.White; Node max = AB.GetOptimalMove(p, b, 5); b.PerformMove(p, max.m); if (p == Player.White) p = Player.Black; else p = Player.White; Console.WriteLine("\n"); Console.WriteLine(b); } /* Console.WriteLine(b); Console.WriteLine(b.Price(p)); p = Player.White; Node max=AB.GetOptimalMove(p, b, 5); Console.WriteLine("x="+max.m.x+" y="+max.m.y+" Price="+max.max_price);*/ }
public List<Move> GetMoves(Player p) { List<Move> moves = new List<Move>(); for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { var m = new Move{x=i, y=j}; if (IsMoveCorrect(p, m)) moves.Add(m); } return moves; }
public void PerformMove(Player p, Move m) { if (IsMoveCorrect(p, m)) if (p == Player.Black) { board[m.x, m.y] = Cell.Black; for (int i = 0; i < 8; i++) { try { if (board[m.x + combinations[i, 0], m.y + combinations[i, 1]] == Cell.White) { for (int j = 2; j < 7; j++) { if (m.x + j * combinations[i, 0] > 7 || m.y + j * combinations[i, 1] > 7) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.Empty) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.Black) { for (int k = 1; k < j; k++) { board[m.x + k * combinations[i, 0], m.y + k * combinations[i, 1]] = Cell.Black; } break; } } } } catch (IndexOutOfRangeException) { } } } else if (p == Player.White) { board[m.x, m.y] = Cell.White; for (int i = 0; i < 8; i++) { try { if (board[m.x + combinations[i, 0], m.y + combinations[i, 1]] == Cell.Black) { for (int j = 2; j < 7; j++) { if (m.x + j * combinations[i, 0] > 7 || m.y + j * combinations[i, 1] > 7) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.Empty) { break; } if (board[m.x + j * combinations[i, 0], m.y + j * combinations[i, 1]] == Cell.White) { for (int k = 1; k < j; k++) { board[m.x + k * combinations[i, 0], m.y + k * combinations[i, 1]] = Cell.White; } break; } } } } catch (IndexOutOfRangeException) { } } } else throw new ArgumentException(); }