/// <summary>Инициализация класса <see cref="Step"/></summary> /// <param name="dimension">Размерность игрового поля</param> /// <param name="line">Номер строки на поле</param> /// <param name="column">Номер колонки на поле</param> /// <param name="cellValue"> Тип заполнения (Крестик или Нолик)</param> public Cell(int dimension, int line, int column, CellValues cellValue = CellValues.Empty) { this.Dimension = dimension; this.Line = line; this.Column = column; this.CellValue = cellValue; }
/// <summary> /// 构造函数 /// </summary> /// <param name="type">属性类型</param> /// <param name="displayOpenXmlAttr"><see cref="DisplayOpenXmlAttribute"/></param> public PropertyDto(Type type, DisplayOpenXmlAttribute displayOpenXmlAttr) { _type = type; _typeCodeValue = TypeUtil.GetTypeCode(_type); _cellType = GetCellValues(_typeCodeValue); _displayOpenXmlAttr = displayOpenXmlAttr; }
private Cell CreateContentCell(string header, UInt32 index, string text, CellValues contentType) { Cell cell = new Cell { DataType = contentType, CellReference = header + index }; if (contentType != CellValues.Number) { InlineString istring = new InlineString(); Text t = new Text { Text = text }; istring.AppendChild(t); cell.AppendChild(istring); } else { cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(text); } return(cell); }
public static void WriteValueInCell(Worksheet worksheet, CellValues cellValue, string text, int column, int row) { Cell cell = CellFinder.GetCell(worksheet, column, row); cell.CellValue = new CellValue(text); cell.DataType = new EnumValue <CellValues>(cellValue); }
// nhận sự kiện bắt đầu chơi online. public void OnStartAutoOnline(bool is_First) { if (m_TypePlay == 4) { if (is_First) { Point stepAuto = new Point(6, 6); boardViewModel.CurrentBoard.PlayAt((int)stepAuto.Y, (int)stepAuto.X, m_TypePlay); this.Dispatcher.Invoke((Action)(() => { CaroTable[(int)stepAuto.Y, (int)stepAuto.X].Content = setPicture("Picture/Player1.png"); })); socket.SendPoint(stepAuto); } } if (m_TypePlay == 3) { if (is_First) { m_player = CellValues.Player1; } else { m_player = CellValues.Player2; } } }
public void AutoPlay(int row, int col) { if (MainWindow.endGame) { return; } Cells[row, col] = ActivePlayer; // Check win state // Vertiacal check if (CountPlayerItem(row, col, 1, 0) >= 5 || CountPlayerItem(row, col, 0, 1) >= 5 || CountPlayerItem(row, col, 1, 1) >= 5 || CountPlayerItem(row, col, 1, -1) >= 5) { MainWindow.endGame = true; if (OnPlayerWin != null) { OnPlayerWin(player: ActivePlayer); return; } return; } if (ActivePlayer == CellValues.AI) ActivePlayer = CellValues.Player1; else { ActivePlayer = CellValues.AI; } }
/// <summary>Текстовое отображение значения ячейки</summary> /// <param name="cellValue">The cell value.</param> /// <returns></returns> public static string CellAsString(CellValues cellValue) { switch (cellValue) { case CellValues.Cross: return("X"); break; case CellValues.Empty: return(" "); break; case CellValues.Zero: return("O"); break; default: return(" "); break; } }
public static string GetCellRealString(SpreadsheetDocument document, Cell cell) { string res = null; if (cell != null) { if (cell.DataType != null) { CellValues cellType = cell.DataType; if (cellType == CellValues.SharedString) { res = getShareString(document, int.Parse(cell.CellValue.Text)); } else { res = cell.CellValue.Text; } } else { res = cell.CellValue == null ? "" : cell.CellValue.Text; } } return(res); }
public ColumnDef(int index, string name, Type dataType) { this.Index = index; this.Name = name; if (dataType == typeof(Int16) || dataType == typeof(Int32) || dataType == typeof(Int64) || dataType == typeof(Single) || dataType == typeof(Double) || dataType == typeof(Decimal)) { this.CellValues = CellValues.Number; } else if (dataType == typeof(Boolean)) { this.CellValues = CellValues.Boolean; } else if (dataType == typeof(DateTime)) { this.CellValues = CellValues.Date; } else { this.CellValues = CellValues.String; } }
private void WriteCellValue(string cellValue, string cellReference, CellValues cellType, List <OpenXmlAttribute> attributes) { attributes.Add(cellValueAttributes[cellType]); cellReferenceAttribute.Value = cellReference; attributes.Add(cellReferenceAttribute); this.writer.WriteStartElement(new Cell(), attributes); switch (cellType) { case CellValues.InlineString: var parsedInlineString = new InlineString(new Text(cellValue)); this.writer.WriteElement(parsedInlineString); break; case CellValues.SharedString: var sharedStringIndex = this.InsertSharedString(cellValue); this.writer.WriteElement(new CellValue(sharedStringIndex)); break; default: this.writer.WriteElement(new CellValue(cellValue)); break; } this.writer.WriteEndElement(); }
private string STCellType(CellValues v) { switch (v) { case CellValues.Boolean: return("b"); case CellValues.Date: return("d"); case CellValues.Error: return("e"); case CellValues.InlineString: return("inlineStr"); case CellValues.Number: return("n"); case CellValues.SharedString: return("s"); case CellValues.String: return("str"); default: return(""); } }
public Node GetMoves(CellValues [,] BoardCell) { Node node = new Node(); Random rand = new Random(); int count = rand.Next(4); // Lấy 1 số từ 0 -> 4 EBoard.ResetBoard(); GetGenResult(BoardCell); // Tìm nước đi if (CanWin) { node = PlayerMoves[1]; } else { EBoard.ResetBoard(); EvalueGomokuBoard(CellValues.Machine, BoardCell); node = EBoard.GetMaxNode(); if (!CanLose) for (int i = 0; i < count; i++) { EBoard.Board[node.Row, node.Column] = 0; node = EBoard.GetMaxNode(); } } Console.WriteLine("New pos: " + node.Row + " " + node.Column); return node; }
public override Cell Evaluate(SpoolSpace Memory) { Cell x = this.Evaluate(Memory); Cell y = this.Evaluate(Memory); return(x == y ? CellValues.Null(x.AFFINITY) : x); }
public void PlayAt(int row, int col) { Cells[row, col] = ActivePlayer; // Check win state // Vertiacal check if (CountPlayerItem(row, col, 1, 0) >= 5 || CountPlayerItem(row, col, 0, 1) >= 5 || CountPlayerItem(row, col, 1, 1) >= 5 || CountPlayerItem(row, col, 1, -1) >= 5) { if (OnPlayerWin != null) { OnPlayerWin(player: ActivePlayer); } return; } if (ActivePlayer == CellValues.Player1) { ActivePlayer = CellValues.Player2; } else { ActivePlayer = CellValues.Player1; } }
public void AutoPlay(int row, int col) { if (MainWindow.endGame) { return; } Cells[row, col] = ActivePlayer; // Check win state // Vertiacal check if (CountPlayerItem(row, col, 1, 0) >= 5 || CountPlayerItem(row, col, 0, 1) >= 5 || CountPlayerItem(row, col, 1, 1) >= 5 || CountPlayerItem(row, col, 1, -1) >= 5) { MainWindow.endGame = true; if (OnPlayerWin != null) { OnPlayerWin(player: ActivePlayer); return; } return; } if (ActivePlayer == CellValues.AI) { ActivePlayer = CellValues.Player1; } else { ActivePlayer = CellValues.AI; } }
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Node node = (Node)e.Result; NextPlayer = CellValues.Machine; PCVsPlayerPlayAt(node.Row - 1, node.Column - 1); }
public static Cell InsertCell(this SheetData data, WorkbookPart book, string columnName, uint rowIndex, string text, CellValues dataType = CellValues.SharedString, string formula = null, bool isPercentage = false, log4net.ILog logger = null, Stopwatch sw = null) { var currentCell = data.GetCell(rowIndex, columnName); //Log(logger, sw); var shareStringPart = dataType == CellValues.SharedString ? GetSharedStringTablePart(book) : null; //Log(logger, sw); var index = dataType == CellValues.SharedString ? shareStringPart.InsertSharedStringItem(text).ToString() : text; //Log(logger, sw); var x = currentCell.StyleIndex != null ? currentCell.StyleIndex : dataType == CellValues.Number ? isPercentage ? book.GetPercentageStyleIndex() : book.GetDoubleStyleIndex() : null; //Log(logger, sw); currentCell.CellFormula = !string.IsNullOrWhiteSpace(formula) ? new CellFormula(formula) : null; //Log(logger, sw); currentCell.CellValue = new CellValue(dataType == CellValues.Number ? index.Replace(',', '.') : index); //Log(logger, sw); currentCell.DataType = dataType != CellValues.Number ? new EnumValue <CellValues>(dataType) : null; //Log(logger, sw); currentCell.StyleIndex = x; //Log(logger, sw); return(currentCell); }
public ExcelColumn(int index, string name, Type dataType) { Index = index; Name = name; if (dataType == typeof(short) || dataType == typeof(int) || dataType == typeof(long) || dataType == typeof(float) || dataType == typeof(double) || dataType == typeof(decimal)) { CellValues = CellValues.Number; } else if (dataType == typeof(bool)) { CellValues = CellValues.Boolean; } else if (dataType == typeof(DateTime)) { CellValues = CellValues.Date; } else { CellValues = CellValues.String; } }
private Cell ConstructCell(string value, CellValues dataType, uint styleIndex = 0) { return(new Cell() { CellValue = new CellValue(value), DataType = new EnumValue <CellValues>(dataType), StyleIndex = styleIndex }); }
private ExcelCellData GetExcelCellData(SpreadsheetDocument excelDoc, string cellAddress) { //get string table in case the cell value is not an accual value, //but an index of sharedstringtable, where the value is stored by excel var stringTable = excelDoc.WorkbookPart .GetPartsOfType <SharedStringTablePart>() .FirstOrDefault(); Cell thisCell = excelDoc.WorkbookPart .WorksheetParts.First() .Worksheet.Descendants <Cell>() .Where(c => c.CellReference == cellAddress).FirstOrDefault(); CellValues cellDataType = CellValues.String; var cellValue = ""; //if cell in given range == null, treat it like an empty string //get value for each cell if (thisCell != null) { cellValue = thisCell.InnerText; double cellValueDouble = 0; if (thisCell.DataType != null) { //if cell value is an index and not an accual value if (thisCell.DataType.Value == CellValues.SharedString) { if (stringTable != null) { //get an accual cell's value cellValue = stringTable.SharedStringTable .ElementAt(int.Parse(cellValue)) .InnerText; } } } //DateType is null for numerics and date values if (double.TryParse(cellValue, out cellValueDouble)) { //try to parse value as date try { cellValue = DateTime.FromOADate(cellValueDouble).ToString(); cellDataType = CellValues.Date; } catch (ArgumentException ex) // this wasn't valid date so it must be numeric value { cellDataType = CellValues.Number; } } //if cell isn't null -> assign value return(new ExcelCellData() { CellValue = cellValue, CellDataType = GetCellType(cellDataType) }); } return(null); }
public override Cell AggRender(Cell Work) { if (Work[0].IsZero) { return(CellValues.Null(Work[1].Affinity)); } return(Work[1] / Work[0]); }
public AutoMovesBoard() { CurrentPlayer = CellValues.Player1; EBoard = new EValueBoard(); MAX_SQUARE = Gomuku.Properties.Settings.Default.MAX_SQUARE; }
Cell getCell(string value, CellValues dataType) { return(new Cell { CellValue = new CellValue(value), DataType = dataType }); }
private DocumentFormat.OpenXml.Spreadsheet.Cell ConstructCell(string value, CellValues dataType) { return(new DocumentFormat.OpenXml.Spreadsheet.Cell() { CellValue = new CellValue(value), DataType = new EnumValue <CellValues>(dataType) }); }
public static Cell ConstructCell(string value, CellValues dataType) { return(new Cell() { CellValue = new CellValue(value), DataType = new DocumentFormat.OpenXml.EnumValue <CellValues>(dataType) }); }
private Cell crearCelda(string value, CellValues dataType) { return(new Cell() { CellValue = new CellValue(value), DataType = new EnumValue <CellValues>(dataType), }); }
private static Cell ConstructCell(object value, CellValues dataType) { return(new Cell() { CellValue = new CellValue(value.ToString()), DataType = new EnumValue <CellValues>(dataType) }); }
private Cell ConstructCell(string value, CellValues dataType) { return(new Cell() { CellValue = new CellValue(value), DataType = new EnumValue <CellValues>(dataType) }); }
public static Cell CreateCell(CellValues type, string value) { var cell = new Cell(); cell.DataType = type; cell.CellValue = new CellValue(value); return(cell); }
void CurrentBoard_OnPlayerWin(CellValues player) { ChatMessage chatMessage = new ChatMessage("Server", DateTime.Now.ToString("hh:mm:ss tt"), player.ToString() + " win!"); chatBox.VerticalAlignment = System.Windows.VerticalAlignment.Top; chatBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; chatBox.Items.Add(chatMessage); endGame = true; }
private Cell CreateCell(string text, CellValues cellValues = CellValues.Number, uint styleIndex = 1U) { return(new Cell() { StyleIndex = styleIndex, DataType = cellValues, CellValue = new CellValue(text) }); }
public static Cell ConstructCell(string value, CellValues dataType, uint styleIndex = 0) { return(new Cell() { CellValue = value == "0" ? new CellValue() : new CellValue(value), DataType = new EnumValue <CellValues>(dataType), StyleIndex = styleIndex }); }
private Cell ConstructCell(string value, CellValues dataType, string cellReference) { return(new Cell() { CellValue = new CellValue(value), DataType = new EnumValue <CellValues>(dataType), CellReference = cellReference }); }
public ChessBoard() { BoardSize = Settings.Default.BOARD_SIZE; Cells = new CellValues[BoardSize, BoardSize]; ActivePlayer = CellValues.Player1; eBoard = new BangLuongGiacBanCo(this); ResetBoard(); bw.DoWork += OnDoWork; bw.RunWorkerCompleted += OnRun; }
private static Cell CreateCell(int columnIndex, int rowIndex, object cellValue, CellValues cellValues) { Cell cell = new Cell { CellReference = GetCellReference(columnIndex) + rowIndex, CellValue = new CellValue { Text = cellValue.ToString() }, DataType = new EnumValue<CellValues>(cellValues), StyleIndex = 0 }; return cell; }
public void ResetBoard() { CurrentPlayer = CellValues.Player1; NextPlayer = CellValues.Player1; for (int i = 0; i < MAX_SQUARE + 2; i++) for (int j = 0; j < MAX_SQUARE + 2; j++) { if (i != 0 && j != 0 && i != MAX_SQUARE + 1 && j != MAX_SQUARE + 1) BoardCell[i, j] = CellValues.None; } }
public void GetCellTypeAndValue(object value, out CellValues cellType, out string cellValue) { string parsedCellValue = null; var converter = _converters.FirstOrDefault(x => x.TryConvertToCellValue(value, out parsedCellValue)); if (converter == null) { var msg = string.Format("Cannot find a propery converter to convert the CSharp value to cell. Value: '{0}'", value); throw new NotSupportedException(msg); } cellValue = parsedCellValue; cellType = converter.CellType; }
public void PlayAt(int row, int col) { Cells[row, col] = ActivePlayer; // Check win state // Vertiacal check if (CountPlayerItem(row, col, 1, 0) >= 5 || CountPlayerItem(row, col, 0, 1) >= 5 || CountPlayerItem(row, col, 1, 1) >= 5 || CountPlayerItem(row, col, 1, -1) >= 5) { if (OnPlayerWin != null) OnPlayerWin(player: ActivePlayer); return; } if (ActivePlayer == CellValues.Player1) ActivePlayer = CellValues.Player2; else { ActivePlayer = CellValues.Player1; } }
public bool CheckWin(int row, int col, int increRow, int increCol, CellValues [,] BoardCell) { int CountCell = 0; int cRow = row; int cCol = col; while (IsInBoard(cRow, cCol)) { if (BoardCell[cRow, cCol] == CurrentPlayer) { CountCell++; cRow += increRow; cCol += increCol; } else break; } CountCell--; cRow = row; cCol = col; while (IsInBoard(cRow, cCol)) { if (BoardCell[cRow, cCol] == CurrentPlayer) { CountCell++; cRow -= increRow; cCol -= increCol; } else break; } if (CountCell >= 5) return true; return false; }
public Board() { CurrentPlayer = CellValues.Player1; NextPlayer = CellValues.Player1; HighlitghtCell = new List<Node>(); MAX_SQUARE = Gomuku.Properties.Settings.Default.MAX_SQUARE; BoardCell = new CellValues[MAX_SQUARE + 2, MAX_SQUARE + 2]; ResetBoard(); AMB = new AutoMovesBoard(); AMB.CurrentPlayer = CurrentPlayer; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); }
// Tìm đường đi public void GetGenResult(CellValues [,] BoardCell) { CanWin = false; CanLose = false; PlayerMoves = new Node[MaxDepth + 1]; for (int i = 0; i <= MaxDepth; i++) PlayerMoves[i] = new Node(); for (int i = 0; i < MaxStep; i++) PCMoves[i] = new Node(); Depth = 0; GenerateMoves(BoardCell); }
public void PlayerPlayAt(int row, int col) { row++; col++; if (!IsInBoard(row, col)) return; if (BoardCell[row, col] == CellValues.None) { BoardCell[row, col] = NextPlayer; if (NextPlayer == CellValues.Player1) { CurrentPlayer = NextPlayer; NextPlayer = CellValues.Player2; } else { CurrentPlayer = NextPlayer; NextPlayer = CellValues.Player1; } PaintCellEvent(row - 1, col - 1); // Kiểm tra hòa nhau if (CheckDicken(row, col)) { PlayerDicken(); return; } // Kiểm tra thắng if (CheckWin(row, col, 1, 0) || CheckWin(row, col, 0, 1) || CheckWin(row, col, 1, 1) || CheckWin(row, col, -1, 1)) { // Phat su kien co nguoi choi thang GetHighlightCell(row, col); HighlightCellWinnerEvent(HighlitghtCell); PlayerWin(CurrentPlayer); } } }
public void PlayerWinEvent(CellValues Player) { MessageBox.Show(Player + " win!!", "End Game"); EnableUnenableRDB(true); }
/// <summary> /// Inserts the value. /// </summary> /// <param name="text">The text.</param> /// <param name="column">The column.</param> /// <param name="rowindex">The rowindex.</param> /// <param name="valueType">Type of the value.</param> public void InsertValue(string text, string column, int rowindex, CellValues valueType) { InsertValue(text, column, rowindex, null, valueType); }
// nhận sự kiện bắt đầu chơi online. public void OnStartAutoOnline(bool is_First) { if(m_TypePlay ==4) { if (is_First) { Point stepAuto = new Point(6, 6); boardViewModel.CurrentBoard.PlayAt((int)stepAuto.Y, (int)stepAuto.X, m_TypePlay); this.Dispatcher.Invoke((Action)(() => { CaroTable[(int)stepAuto.Y, (int)stepAuto.X].Content = setPicture("Picture/Player1.png"); })); socket.SendPoint(stepAuto); } } if (m_TypePlay == 3) { if (is_First) m_player = CellValues.Player1; else m_player = CellValues.Player2; } }
// Xử lý sự kiện nhân step từ server public void OnSteppSever(Point step) { this.Dispatcher.Invoke((Action)(() => { CaroTable[(int)step.Y, (int)step.X].Content = setPicture("Picture/Player2.png"); })); if (m_TypePlay == 4) // choi auto -> chuyen về chơi với máy. boardViewModel.CurrentBoard.PlayAt((int)step.X, (int)step.Y, 2); if (m_TypePlay == 3) // chơi online chuyển về chơi thủ công giữa 2 người. boardViewModel.CurrentBoard.PlayAt((int)step.X, (int)step.Y, 1); m_player = CellValues.Player1; }
// Tạo bảng lượng giá cho bàn cờ private void EvalueGomokuBoard(CellValues player, CellValues[,] BoardCell) { int row, col, i; int cPlayer, cMachine; #region Lượng giá cho hàng for (row = 1; row <= MAX_SQUARE; row++) for (col = 1; col <= MAX_SQUARE - 4; col++) { cPlayer = 0; cMachine = 0; for (i = 0; i < 5; i++) { if (BoardCell[row, col + i] == CellValues.Player1) cPlayer++; if (BoardCell[row, col + i] == CellValues.Machine) cMachine++; } // Tạo bảng lượng giá if (cPlayer * cMachine == 0 && cPlayer != cMachine) for (i = 0; i < 5; i++) if (BoardCell[row, col + i] == CellValues.None) { if (cMachine == 0) { if (player == CellValues.Machine) EBoard.Board[row, col + i] += TScore[cPlayer]; else EBoard.Board[row, col + i] += KScore[cPlayer]; // Bị chặn 2 đầu if (BoardCell[row, col - 1] == CellValues.Machine && BoardCell[row, col + 5] == CellValues.Machine) EBoard.Board[row, col + i] = 0; } if (cPlayer == 0) { if (player == CellValues.Player1) EBoard.Board[row, col + i] += TScore[cMachine]; else EBoard.Board[row, col + i] += KScore[cMachine]; // Bị chặn 2 đầu if (BoardCell[row, col - 1] == CellValues.Player1 && BoardCell[row, col + 5] == CellValues.Player1) EBoard.Board[row, col + i] = 0; } if ((cPlayer == 4 || cMachine == 4) && (BoardCell[row, col + i - 1] == CellValues.None || BoardCell[row, col + i + 1] == CellValues.None)) EBoard.Board[row, col + i] *= 2; } } #endregion #region Lượng giá cho cột for (col = 1; col <= MAX_SQUARE; col++) for (row = 1; row <= MAX_SQUARE - 4; row++) { cPlayer = 0; cMachine = 0; for (i = 0; i < 5; i++) { if (BoardCell[row + i, col] == CellValues.Player1) cPlayer++; if (BoardCell[row + i, col] == CellValues.Machine) cMachine++; } // Tạo bảng lượng giá if (cPlayer * cMachine == 0 && cMachine != cPlayer) for (i = 0; i < 5; i++) if (BoardCell[row + i, col] == CellValues.None) { if (cMachine == 0) { if (player == CellValues.Machine) EBoard.Board[row + i, col] += TScore[cPlayer]; else EBoard.Board[row + i, col] += KScore[cPlayer]; // Bị chặn 2 đầu if (BoardCell[row - 1, col] == CellValues.Machine && BoardCell[row + 5, col] == CellValues.Machine) EBoard.Board[row + i, col] = 0; } if (cPlayer == 0) { if (player == CellValues.Player1) EBoard.Board[row + i, col] += TScore[cMachine]; else EBoard.Board[row + i, col] += KScore[cMachine]; // Bị chặn 2 đầu if (BoardCell[row - 1, col] == CellValues.Player1 && BoardCell[row + 5, col] == CellValues.Player1) EBoard.Board[row + i, col] = 0; } if ((cPlayer == 4 || cMachine == 4) && (BoardCell[row + i - 1, col] == CellValues.None || BoardCell[row + i + 1, col] == CellValues.None)) EBoard.Board[row + i, col] *= 2; } } #endregion #region Lượng giá đường chéo xuống for (row = 1; row <= MAX_SQUARE - 4; row++) for (col = 1; col <= MAX_SQUARE - 4; col++) { cPlayer = 0; cMachine = 0; for (i = 0; i < 5; i++) { if (BoardCell[row + i, col + i] == CellValues.Player1) cPlayer++; if (BoardCell[row + i, col + i] == CellValues.Machine) cMachine++; } // Tạo bảng lượng giá if (cPlayer * cMachine == 0 && cMachine != cPlayer) for (i = 0; i < 5; i++) if (BoardCell[row + i, col + i] == CellValues.None) { if (cMachine == 0) { if (player == CellValues.Machine) EBoard.Board[row + i, col + i] += TScore[cPlayer]; else EBoard.Board[row + i, col + i] += KScore[cPlayer]; // Bị chặn 2 đầu if (BoardCell[row - 1, col - 1] == CellValues.Machine && BoardCell[row + 5, col + 5] == CellValues.Machine) EBoard.Board[row + i, col + i] = 0; } if (cPlayer == 0) { if (player == CellValues.Player1) EBoard.Board[row + i, col + i] += TScore[cMachine]; else EBoard.Board[row + i, col + i] += KScore[cMachine]; // Bị chặn 2 đầu if (BoardCell[row - 1, col - 1] == CellValues.Player1 && BoardCell[row + 5, col + 5] == CellValues.Player1) EBoard.Board[row + i, col + i] = 0; } if ((cPlayer == 4 || cMachine == 4) && (BoardCell[row + i - 1, col + i - 1] == CellValues.None || BoardCell[row + i + 1, col + i + 1] == CellValues.None)) EBoard.Board[row + i, col + i] *= 2; } } #endregion #region Lượng giá đường chéo lên for (row = 5; row <= MAX_SQUARE - 4; row++) for (col = 1; col <= MAX_SQUARE - 4; col++) { cMachine = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (BoardCell[row - i, col + i] == CellValues.Player1) cPlayer++; if (BoardCell[row - i, col + i] == CellValues.Machine) cMachine++; } // Tạo bảng lượng giá if (cPlayer * cMachine == 0 && cPlayer != cMachine) for (i = 0; i < 5; i++) if (BoardCell[row - i, col + i] == CellValues.None) { if (cMachine == 0) { if (player == CellValues.Machine) EBoard.Board[row - i, col + i] += TScore[cPlayer]; else EBoard.Board[row - i, col + i] += KScore[cPlayer]; // Bị chặn 2 đầu if (BoardCell[row + 1, col - 1] == CellValues.Machine && BoardCell[row - 5, col + 5] == CellValues.Machine) EBoard.Board[row - i, col + i] = 0; } if (cPlayer == 0) { if (player == CellValues.Player1) EBoard.Board[row - i, col + i] += TScore[cMachine]; else EBoard.Board[row - i, col + i] += KScore[cMachine]; // Bị chặn 2 đầu if (BoardCell[row + 1, col - 1] == CellValues.Player1 && BoardCell[row - 5, col + 5] == CellValues.Player1) EBoard.Board[row - i, col + i] = 0; } if ((cPlayer == 4 || cMachine == 4) && (BoardCell[row - i + 1, col + i - 1] == CellValues.None || BoardCell[row - i - 1, col + i + 1] == CellValues.None)) EBoard.Board[row - i, col + i] *= 2; } } #endregion }
// Đệ quy sinh nước đi public void GenerateMoves(CellValues[,] BoardCell) { if (Depth >= MaxDepth) return; Depth++; CanWin = false; Node PCNode = new Node(); // Duong di quan ta. Node CompetitorNode = new Node(); // Duong di doi thu. int count = 0; // Tạo bảng lượng giá cho Machine EvalueGomokuBoard(CellValues.Machine, BoardCell); #region Lấy tất cả các bước đi tốt nhất vào danh sách các bước đi for (int i = 1; i <= MaxStep; i++) { PCNode = EBoard.GetMaxNode(); PCMoves[i] = PCNode; EBoard.Board[PCNode.Row, PCNode.Column] = 0; } #endregion #region Lấy các bước đi tốt nhất để thử count = 0; while (count < MaxStep) { count++; PCNode = PCMoves[count]; PlayerMoves.SetValue(PCNode, Depth); BoardCell[PCNode.Row, PCNode.Column] = CellValues.Machine; // Tìm nước đi tốt nhất EBoard.ResetBoard(); EvalueGomokuBoard(CellValues.Player1, BoardCell); for (int i = 1; i <= MaxStep; i++) { CompetitorNode = EBoard.GetMaxNode(); CompetitorMoves[i] = CompetitorNode; EBoard.Board[CompetitorNode.Row, CompetitorNode.Column] = 0; } for (int i = 1; i <= MaxStep; i++) { CompetitorNode = CompetitorMoves[i]; BoardCell[CompetitorNode.Row, CompetitorNode.Column] = CellValues.Player1; // Đi thử if ((CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 0, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 0, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, -1, 1, BoardCell)) && CurrentPlayer == CellValues.Machine) // Có thể thắng CanWin = true; if ((CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 0, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 0, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, 1, 1, BoardCell) || CheckWin(CompetitorNode.Row, CompetitorNode.Column, -1, 1, BoardCell)) && CurrentPlayer == CellValues.Player1) // Có thể thua CanLose = true; // Loại bỏ nước đi thử if (CanLose || CanWin) { BoardCell[CompetitorNode.Row, CompetitorNode.Column] = CellValues.None; BoardCell[PCNode.Row, PCNode.Column] = CellValues.None; if (CanWin) CanLose = false; return; } else GenerateMoves(BoardCell); BoardCell[CompetitorNode.Row, CompetitorNode.Column] = CellValues.None; } BoardCell[PCNode.Row, PCNode.Column] = CellValues.None; } #endregion }
// Sử dụng nguồn từ souce mẫu trên mạng. private void LuongGia(CellValues player) { int cntHuman = 0, cntCom = 0;//Biến đếm Human,Com #region Luong gia cho hang int row = Settings.Default.BOARD_SIZE; int column = Settings.Default.BOARD_SIZE; for (int i = 0; i < row; i++) { for (int j = 0; j < column - 4; j++) { //Khởi tạo biến đếm cntHuman = cntCom = 0; //Đếm số lượng con cờ trên 5 ô kế tiếp của 1 hàng for (int k = 0; k < 5; k++) { if (Cells[i, j + k] == CellValues.Player1) cntHuman++; if (Cells[i, j + k] == CellValues.Player2) cntCom++; } //Lượng giá //Nếu 5 ô kế tiếp chỉ có 1 loại cờ (hoặc là Human,hoặc la Com) if (cntHuman * cntCom == 0 && cntHuman != cntCom) { //Gán giá trị cho 5 ô kế tiếp của 1 hàng for (int k = 0; k < 5; k++) { //Nếu ô đó chưa có quân đi if (Cells[i, j + k] == CellValues.None) { //Nếu trong 5 ô đó chỉ tồn tại cờ của Human if (cntCom == 0) { //Nếu đối tượng lượng giá là Com if (player == CellValues.Player2) { //Vì đối tượng người chơi là Com mà trong 5 ô này chỉ có Human //nên ta sẽ cộng thêm điểm phòng thủ cho Com eBoard.GiaTri[i, j + k] += PhongThu[cntHuman]; } //Ngược lại cộng điểm phòng thủ cho Human else { eBoard.GiaTri[i, j + k] += TanCong[cntHuman]; } } //Tương tự như trên if (cntHuman == 0) //Nếu chỉ tồn tại Com { if (player == CellValues.Player1) //Nếu người chơi là Người { eBoard.GiaTri[i, j + k] += PhongThu[cntCom]; } else { eBoard.GiaTri[i, j + k] += TanCong[cntCom]; } } if ((j + k - 1 > 0) && (j + k + 1 <= column - 1) && (cntHuman == 4 || cntCom == 4) && (Cells[i, j + k - 1] == CellValues.None || Cells[i, j + k + 1] == CellValues.None)) { eBoard.GiaTri[i, j + k] *= 3; } } } } } } #endregion //Tương tự như lượng giá cho hàng #region Luong gia cho cot for (int i = 0; i < row - 4; i++) { for (int j = 0; j < column; j++) { cntHuman = cntCom = 0; for (int k = 0; k < 5; k++) { if (Cells[i + k, j] == CellValues.Player1) cntHuman++; if (Cells[i + k, j] == CellValues.Player2) cntCom++; } if (cntHuman * cntCom == 0 && cntCom != cntHuman) { for (int k = 0; k < 5; k++) { if (Cells[i + k, j] == CellValues.None) { if (cntCom == 0) { if (player == CellValues.Player1) eBoard.GiaTri[i + k, j] += PhongThu[cntHuman]; else eBoard.GiaTri[i + k, j] += TanCong[cntHuman]; // Truong hop bi chan 2 dau. if ((i - 1) >= 0 && (i + 5) <= row - 1 && Cells[i - 1, j] == CellValues.Player2 && Cells[i + 5, j] == CellValues.Player2) { eBoard.GiaTri[i + k, j] = 0; } } if (cntHuman == 0) { if (player == CellValues.Player1) eBoard.GiaTri[i + k, j] += PhongThu[cntCom]; else eBoard.GiaTri[i + k, j] += TanCong[cntCom]; } if ((i + k - 1) >= 0 && (i + k + 1) <= row - 1 && (cntHuman == 4 || cntCom == 4) && (Cells[i + k - 1, j] == CellValues.None || Cells[i + k + 1, j] == CellValues.None)) { eBoard.GiaTri[i + k, j] *= 3; } } } } } } #endregion //Tương tự như lượng giá cho hàng #region Luong gia tren duong cheo chinh (\) for (int i = 0; i < row - 4; i++) { for (int j = 0; j < column - 4; j++) { cntHuman = cntCom = 0; for (int k = 0; k < 5; k++) { if (Cells[i + k, j + k] == CellValues.Player1) cntHuman++; if (Cells[i + k, j + k] == CellValues.Player2) cntCom++; } if (cntHuman * cntCom == 0 && cntCom != cntHuman) { for (int k = 0; k < 5; k++) { if (Cells[i + k, j + k] == CellValues.None) { if (cntCom == 0) { if (player == CellValues.Player1) eBoard.GiaTri[i + k, j + k] += PhongThu[cntHuman]; else eBoard.GiaTri[i + k, j + k] += TanCong[cntHuman]; } if (cntHuman == 0) { if (player == CellValues.Player1) eBoard.GiaTri[i + k, j + k] += PhongThu[cntCom]; else eBoard.GiaTri[i + k, j + k] += TanCong[cntCom]; } if ((i + k - 1) >= 0 && (j + k - 1) >= 0 && (i + k + 1) <= row - 1 && (j + k + 1) <= column - 1 && (cntHuman == 4 || cntCom == 4) && (Cells[i + k - 1, j + k - 1] == CellValues.None || Cells[i + k + 1, j + k + 1] == CellValues.None)) { eBoard.GiaTri[i + k, j + k] *= 3; } } } } } } #endregion //Tương tự như lượng giá cho hàng #region Luong gia tren duong cheo phu (/) for (int i = 4; i < row - 4; i++) { for (int j = 0; j < column - 4; j++) { cntCom = 0; cntHuman = 0; for (int k = 0; k < 5; k++) { if (Cells[i - k, j + k] == CellValues.Player1) cntHuman++; if (Cells[i - k, j + k] == CellValues.Player2) cntCom++; } if (cntHuman * cntCom == 0 && cntHuman != cntCom) { for (int k = 0; k < 5; k++) { if (Cells[i - k, j + k] == CellValues.None) { if (cntCom == 0) { if (player == CellValues.Player1) eBoard.GiaTri[i - k, j + k] += PhongThu[cntHuman]; else eBoard.GiaTri[i - k, j + k] += TanCong[cntHuman]; // Truong hop bi chan 2 dau. if (i + 1 <= row - 1 && j - 1 >= 0 && i - 5 >= 0 && j + 5 <= column - 1 && Cells[i + 1, j - 1] == CellValues.Player1 && Cells[i - 5, j + 5] == CellValues.Player1) { eBoard.GiaTri[i - k, j + k] = 0; } } if (cntHuman == 0) { if (player == CellValues.Player1) eBoard.GiaTri[i - k, j + k] += PhongThu[cntCom]; else eBoard.GiaTri[i - k, j + k] += TanCong[cntCom]; } if ((i - k + 1) <= row - 1 && (j + k - 1) >= 0 && (i - k - 1) >= 0 && (j + k + 1) <= column - 1 && (cntHuman == 4 || cntCom == 4) && (Cells[i - k + 1, j + k - 1] == CellValues.None || Cells[i - k - 1, j + k + 1] == CellValues.None)) { eBoard.GiaTri[i - k, j + k] *= 3; } } } } } } #endregion }
/// <summary> /// CellValues = Boolean -> expects cellValue "True" or "False" /// CellValues = InlineString -> stores string within sheet /// CellValues = SharedString -> stores index within sheet. If this is called, please call CreateShareStringPart after creating all sheet data to create the shared string part /// CellValues = Date -> expects ((DateTime)value).ToOADate().ToString(CultureInfo.InvariantCulture) as cellValue /// and new OpenXmlAttribute[] { new OpenXmlAttribute("s", null, "1") }.ToList() as attributes so that the correct formatting can be applied /// /// </summary> /// <param name="writer"></param> /// <param name="cellValue"></param> /// <param name="dataType"></param> /// <param name="attributes"></param> public void WriteCellValueSax(OpenXmlWriter writer, string cellValue, CellValues dataType, List<OpenXmlAttribute> attributes = null) { switch (dataType) { case CellValues.InlineString: { if (attributes == null) { attributes = new List<OpenXmlAttribute>(); } attributes.Add(new OpenXmlAttribute("t", null, "inlineStr")); writer.WriteStartElement(new Cell(), attributes); writer.WriteElement(new InlineString(new Text(cellValue))); writer.WriteEndElement(); break; } case CellValues.SharedString: { if (attributes == null) { attributes = new List<OpenXmlAttribute>(); } attributes.Add(new OpenXmlAttribute("t", null, "s"));//shared string type writer.WriteStartElement(new Cell(), attributes); if (!_shareStringDictionary.ContainsKey(cellValue)) { _shareStringDictionary.Add(cellValue, _shareStringMaxIndex); _shareStringMaxIndex++; } //writing the index as the cell value writer.WriteElement(new CellValue(_shareStringDictionary[cellValue].ToString())); writer.WriteEndElement();//cell break; } case CellValues.Date: { if (attributes == null) { writer.WriteStartElement(new Cell() { DataType = CellValues.Number }); } else { writer.WriteStartElement(new Cell() { DataType = CellValues.Number }, attributes); } writer.WriteElement(new CellValue(cellValue)); writer.WriteEndElement(); break; } case CellValues.Boolean: { if (attributes == null) { attributes = new List<OpenXmlAttribute>(); } attributes.Add(new OpenXmlAttribute("t", null, "b"));//boolean type writer.WriteStartElement(new Cell(), attributes); writer.WriteElement(new CellValue(cellValue == "True" ? "1" : "0")); writer.WriteEndElement(); break; } default: { if (attributes == null) { writer.WriteStartElement(new Cell() { DataType = dataType }); } else { writer.WriteStartElement(new Cell() { DataType = dataType }, attributes); } writer.WriteElement(new CellValue(cellValue)); writer.WriteEndElement(); break; } } }
//public event PlayerAtHandler OnPlayerAt; public Board() { BoardSize = Settings.Default.BOARD_SIZE; Cells = new CellValues[BoardSize, BoardSize]; ActivePlayer = CellValues.Player1; }
public void PCVsPlayerPlayAt(int row, int col) { row++; col++; if (!IsInBoard(row, col)) return; if (BoardCell[row, col] == CellValues.None) { if (NextPlayer != CellValues.None) { BoardCell[row, col] = NextPlayer; CurrentPlayer = NextPlayer; if (CurrentPlayer == CellValues.Machine) NextPlayer = CellValues.Player1; else NextPlayer = CellValues.None; // Phát sự kiện tô màu PaintCellEvent(row - 1, col - 1); // Kiểm tra hòa if (CheckDicken(row, col)) { PlayerDicken(); return; } // Kiểm tra thắng if (CheckWin(row, col, 1, 0) || CheckWin(row, col, 0, 1) || CheckWin(row, col, 1, 1) || CheckWin(row, col, -1, 1)) { // Phat su kien co nguoi choi thang GetHighlightCell(row, col); HighlightCellWinnerEvent(HighlitghtCell); PlayerWin(CurrentPlayer); return; } if (CurrentPlayer == CellValues.Player1) bw.RunWorkerAsync(); } } }
/// <summary> /// Inserts the value. /// </summary> /// <param name="text">The text.</param> /// <param name="column">The column.</param> /// <param name="rowindex">The rowindex.</param> /// <param name="align">The align.</param> /// <param name="valueType">Type of the value.</param> public void InsertValue(string text, string column, int rowindex, HorizontalAlignmentValues? align, CellValues valueType) { if (CurrentSheet == null) throw new Exception("No sheet selected"); Cell cell = LocateCell(column, rowindex); cell.CellValue = new CellValue(text); cell.DataType = new EnumValue<CellValues>(valueType); CurrentSheet.Worksheet.Save(); }
// sự kiện lick private void CaroButtonTable_Click(object sender, RoutedEventArgs e) { CaroButton cell = (CaroButton)sender; if(boardViewModel.CurrentBoard.CheckNone(cell.Y, cell.X)) { if (m_TypePlay == 1) { if (boardViewModel.CurrentBoard.ActivePlayer == CellValues.Player1) cell.Content = setPicture("Picture/Player1.png"); else cell.Content = setPicture("Picture/Player2.png"); boardViewModel.CurrentBoard.PlayAt(cell.Y, cell.X, m_TypePlay); } else if (m_TypePlay == 2) { cell.Content = setPicture("Picture/Player1.png"); boardViewModel.CurrentBoard.PlayAt(cell.Y, cell.X, m_TypePlay); } else if(m_TypePlay == 3 && m_player == CellValues.Player1) { if(socket.m_StartGame == true) { cell.Content = setPicture("Picture/Player1.png"); boardViewModel.CurrentBoard.PlayAt(cell.Y, cell.X, 1); socket.SendPoint(new Point(cell.Y, cell.X)); m_player = CellValues.Player2; } } } }
// sử lý sự kiện có người win // type 1 và 2 -> cho chơi lại // type 3 và 4 -> thoát ra menu private void CurrentBoard_OnPlayerWin(CellValues player) { if (m_TypePlay == 1 || m_TypePlay == 2) { MessageBox.Show(player.ToString() + "win !"); showWinArray(boardViewModel.CurrentBoard.winArray); try { if (m_TypePlay == 1 || m_TypePlay == 2) { MessageBoxResult dialogResult = MessageBox.Show("Bạn có muốn chơi lại??? ", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (dialogResult == MessageBoxResult.Yes) ResetBoard(); else { Frm_Menu menu = new Frm_Menu(); if (Exit_changed != null) Exit_changed(); menu.ShowDialog(); } } else { Frm_Menu menu = new Frm_Menu(); if (Exit_changed != null) Exit_changed(); menu.ShowDialog(); } } catch { MessageBox.Show("Bạn không thể tiếp tục!"); } } }
//================================================= static xlContentType ToxlContentType(CellValues? val) { switch (val) { case CellValues.Boolean: return xlContentType.Boolean; case CellValues.Date: return xlContentType.Date; case CellValues.Error: throw new Exception(string.Format("Unknown cell type {0}", CellValues.Error)); case CellValues.InlineString: throw new Exception(string.Format("Unknown cell type {0}", CellValues.InlineString)); case CellValues.Number: return xlContentType.Double; case CellValues.SharedString: return xlContentType.SharedString; case CellValues.String: return xlContentType.String; default: return xlContentType.Void; } }