// If a player has three in a row, return Mark.O or Mark.X depending on which player. // Otherwise, return Mark.None. public Mark GetWinner() { //Check for 3 of the same in each row for (var y = 0; y < board.GetLength(0); y++) { if (GetSquare(0, y) != Mark.None && board[0, y] == board[1, y] && board[0, y] == board[2, y]) { return(board[0, y]); } } //Check for 3 of the same in each diagonal if (GetSquare(1, 1) != Mark.None && (board[1, 1] == board[0, 0] && board[1, 1] == board[2, 2] || board[1, 1] == board[2, 0] && board[1, 1] == board[0, 2])) { return(board[1, 1]); } //Check for 3 of the same in each column for (var x = 0; x < board.GetLength(0); x++) { if (GetSquare(x, 0) != Mark.None && board[x, 0] == board[x, 1] && board[x, 0] == board[x, 2]) { return(board[x, 0]); } } return(Mark.None); }
public Point TakeTurn(Mark[,] gameField) { Dictionary <double, Point> options = new Dictionary <double, Point>(); Dictionary <double, List <int> > summarys = new Dictionary <double, List <int> >(); for (int i = 0; i < gameField.GetLength(0); i++) { for (int j = 0; j < gameField.GetLength(1); j++) { if (gameField[i, j] == Mark.Blank) { Mark[,] option = (Mark[, ])gameField.Clone(); option[i, j] = InstanceRole; (List <int> features, double evaluation) = Summarize(option); if (!options.Keys.Contains(evaluation)) { options.Add(evaluation, new Point(j, i)); summarys.Add(evaluation, features); } } } } KeyValuePair <double, Point> pair = options.Where(p => p.Key == options.Keys.Max()).ToList()[0]; _history.Add(new KeyValuePair <double, List <int> >(pair.Key, summarys[pair.Key])); return(pair.Value); }
private String PrintGameField() { StringBuilder sb = new StringBuilder(); sb.Append("\n"); sb.Append(" | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | \n"); sb.Append(" |---|---|---|---|---|---|---|---|---|---|\n"); char c = 'A'; for (int i = 0; i < gameField.GetLength(0); i++) { sb.Append(c + " | "); c++; for (int j = 0; j < gameField.GetLength(1); j++) { sb.Append(gameField[i, j] + " | "); } sb.Append("\n"); sb.Append(" |---|---|---|---|---|---|---|---|---|---|\n"); } String field = sb.ToString(); Console.WriteLine(field); return(field); }
// If a player has three in a row, return Mark.O or Mark.X depending on which player. // Otherwise, return Mark.None. public Mark GetWinner() { for (int length = 0; length < boardSpaces.GetLength(0); length++) //Sets { if (occupiedSpacesX.Contains(GetSquare(length, 0)) && occupiedSpacesX.Contains(GetSquare(length, 1)) && occupiedSpacesX.Contains(GetSquare(length, 2))) { //Uses the length value to determine the length of the grid return(Mark.X); //returns Mark.X if each of these values are met } } for (int height = 0; height < boardSpaces.GetLength(0); height++) //the same is done for height { if (occupiedSpacesX.Contains(GetSquare(0, height)) && occupiedSpacesX.Contains(GetSquare(0, height)) && occupiedSpacesX.Contains(GetSquare(0, height))) { return(Mark.X);; } } if (occupiedSpacesX.Contains(GetSquare(0, 0)) && occupiedSpacesX.Contains(GetSquare(1, 1)) && occupiedSpacesX.Contains(GetSquare(2, 2))) { return(Mark.X); } if (occupiedSpacesX.Contains(GetSquare(0, 2)) && occupiedSpacesX.Contains(GetSquare(1, 1)) && occupiedSpacesX.Contains(GetSquare(0, 2))) { return(Mark.X); } for (int length = 0; length < boardSpaces.GetLength(0); length++) { if (occupiedSpacesO.Contains(GetSquare(length, 0)) && occupiedSpacesO.Contains(GetSquare(length, 1)) && occupiedSpacesO.Contains(GetSquare(length, 2))) { return(Mark.O); } } for (int height = 0; height < boardSpaces.GetLength(0); height++) { if (occupiedSpacesO.Contains(GetSquare(0, height)) && occupiedSpacesO.Contains(GetSquare(0, height)) && occupiedSpacesO.Contains(GetSquare(0, height))) { return(Mark.O);; } } if (occupiedSpacesO.Contains(GetSquare(0, 0)) && occupiedSpacesO.Contains(GetSquare(1, 1)) && occupiedSpacesO.Contains(GetSquare(2, 2))) { return(Mark.O); } if (occupiedSpacesO.Contains(GetSquare(0, 2)) && occupiedSpacesO.Contains(GetSquare(1, 1)) && occupiedSpacesO.Contains(GetSquare(0, 2))) { return(Mark.O); } return(Mark.None); }
// Constructor. Perform any necessary data initialisation here. // Uncomment the optional parameters if attempting the stretch goal -- keep the default values to avoid breaking unit tests. public OxoBoard(int width = 3, int height = 3, int inARowToWin = 3) { inARow = inARowToWin; Board = new Mark[width, height]; for (int y = 0; y < Board.GetLength(1); y++) { for (int x = 0; x < Board.GetLength(0); x++) { Board[x, y] = Mark.None; } } }
// If the specified square is currently empty, fill it with mark and return true. // If the square is not empty, leave it as-is and return False. public bool SetSquare(int x, int y, Mark mark) { if (x < board.GetLength(0) && y < board.GetLength(1)) { if (board[x, y] == Mark.None) { board[x, y] = mark; return(true); } } return(false); }
// Constructor. Perform any necessary data initialisation here. // Uncomment the optional parameters if attempting the stretch goal -- keep the default values to avoid breaking unit tests. public OxoBoard(/* int width = 3, int height = 3, int inARow = 3 */) { board = new Mark[3, 3]; for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { board[i, j] = Mark.None; } } //throw new NotImplementedException("TODO: implement this function and then remove this exception"); }
private (bool winningState, Mark?winningMark, List <Point> positions) CheckWinningState(Mark[,] gameField) { for (int i = 0; i < gameField.GetLength(0); i++) { Mark[] row = gameField.GetRow(i); if (row.ToList().FindAll(e => e == Mark.O).Count == GameField.ColumnDefinitions.Count || row.ToList().FindAll(e => e == Mark.X).Count == GameField.ColumnDefinitions.Count) { return(true, row[0], new List <Point>() { new Point(0, i), new Point(GameField.ColumnDefinitions.Count - 1, i) }); } } for (int i = 0; i < gameField.GetLength(1); i++) { Mark[] column = gameField.GetColumn(i); if (column.ToList().FindAll(e => e == Mark.O).Count == GameField.RowDefinitions.Count || column.ToList().FindAll(e => e == Mark.X).Count == GameField.RowDefinitions.Count) { return(true, column[0], new List <Point>() { new Point(i, 0), new Point(i, GameField.RowDefinitions.Count - 1) }); } } var diagonals = new List <Mark[]>() { gameField.GetDiagonal(DiagonalType.Left), gameField.GetDiagonal(DiagonalType.Right) }; foreach (var diagonal in diagonals) { if (diagonal.ToList().FindAll(e => e == Mark.O).Count == GameField.RowDefinitions.Count || diagonal.ToList().FindAll(e => e == Mark.X).Count == GameField.RowDefinitions.Count) { return(true, diagonal[0], (diagonals.IndexOf(diagonal) == 0) ? new List <Point>() { new Point(0, 0), new Point(GameField.ColumnDefinitions.Count - 1, GameField.RowDefinitions.Count - 1) } : new List <Point>() { new Point(GameField.ColumnDefinitions.Count - 1, 0), new Point(0, GameField.RowDefinitions.Count - 1) }); } } return(false, null, null); }
private Mark[,] Clone(Mark[,] oldBoard) { Mark[,] board = new Mark[oldBoard.GetLength(0), oldBoard.GetLength(1)]; for (int x = 0; x < oldBoard.GetLength(0); x++) { for (int y = 0; y < oldBoard.GetLength(1); y++) { board[x, y] = oldBoard[x, y]; } } return(board); }
// If the specified square is currently empty, fill it with mark and return true. // If the square is not empty, leave it as-is and return False. public bool SetSquare(int x, int y, Mark mark) { if(x < borad.GetLength(0) && y < borad.GetLength(1)) { if (borad[x,y] == Mark.None) { borad[x, y] = mark; return true; } } return false; }
// If there are still empty squares on the board, return false. // If there are no empty squares, return true. public bool IsBoardFull() { for (int x = 0; x < gameBoard.GetLength(0); x++) { for (int y = 0; y < gameBoard.GetLength(1); y++) { if (gameBoard[x, y] == Mark.None) { return(false); } } } return(true); }
// If there are still empty squares on the board, return false. // If there are no empty squares, return true. public bool IsBoardFull() { for (int x = 0; x < boardSize.GetLength(0); x++) { for (int y = 0; y < boardSize.GetLength(1); y++) { if (boardSize[x, y] == Mark.None) { return(false); } } } return(true); }
// If there are still empty squares on the board, return false. // If there are no empty squares, return true. public bool IsBoardFull() { for (int i = 0; i < Board.GetLength(0); i++) { for (int j = 0; j < Board.GetLength(1); j++) { if (Board[i, j] == 0) { return(false); } } } return(true); }
private int Calc(Mark[,] board, Mark curMark, Coordinate pos, Mark myMark, int depth) { //check for winning move int winValue = 0; if (BoardController.CheckWin(board, curMark, pos)) { if (myMark == curMark) { winValue = 10; } else { winValue = -10; } return(winValue); } //make a copy of the board to manipulate Mark[,] myboard = Clone(board); //place my mark myboard[pos.x, pos.y] = curMark; Mark nextMark; if (curMark == Mark.X) { nextMark = Mark.O; } else { nextMark = Mark.X; } for (int x = 0; x < myboard.GetLength(0); x++) { for (int y = 0; y < myboard.GetLength(0); y++) { if (myboard[x, y] == Mark.non) { winValue += Calc(myboard, nextMark, new Coordinate(x, y), myMark, depth++); } } } return(winValue); }
// If there are still empty squares on the board, return false. // If there are no empty squares, return true. public bool IsBoardFull() { for (int i = 0; i < oxoGrid.GetLength(0); i++) { for (int j = 0; j < oxoGrid.GetLength(1); j++) { Mark currentMark = oxoGrid[i, j]; if (currentMark == Mark.None) { return(false); } } } return(true); }
// If there are still empty squares on the board, return false. // If there are no empty squares, return true. public bool IsBoardFull() { for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { if (board[i, j] == Mark.None) { return(false); } } } return(true); }
/// <summary> /// Looks at the board and checks if there are any available spaces left to play /// </summary> /// <param name="board">The board to exame</param> /// <returns>True if there are no spaces and its a cat game, else false</returns> public static bool CatGame(Mark[,] board) { for (int x = 0; x < board.GetLength(0); x++) { for (int y = 0; y < board.GetLength(1); y++) { if (board[x, y] == Mark.non) { return(false); } } } return(true); }
private bool checkIfFull() { for (var i = 0; i < mResults.GetLength(0); i++) { for (var j = 0; j < mResults.GetLength(1); j++) { if (mResults[i, j] == Mark.Free) { return(false); } } } MessageBox.Show("Game Ended"); newGame(); return(true); }
static Mark[] GetMarksWithIndexes(Mark[,] field) { int index = 0; var marks = new Mark[field.Length]; var x = field.GetLength(0); var y = field.GetLength(1); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { marks[index++] = field[i, j]; } } return(marks); }
private void newGame() { mResults = new Mark[3, 3]; for (var i = 0; i < mResults.GetLength(0); i++) { for (var j = 0; j < mResults.GetLength(1); j++) { mResults[i, j] = Mark.Free; } } playerOneTurn = true; /// Iterate every button Container.Children.Cast <Button>().ToList().ForEach(button => { button.Content = String.Empty; button.Background = Brushes.White; button.Foreground = Brushes.Blue; }); gameEnded = false; }
public static bool CheckColumn(int columnIndex, Mark[,] field, out Mark outputMark) { outputMark = Mark.Empty; int length = field.GetLength(0); for (int row = 0; row < length - 1; row++) { if (field[row, columnIndex] != field[row + 1, columnIndex]) { return(false); } } outputMark = field[0, columnIndex]; return(true); }
public static bool CheckRow(int rowIndex, Mark[,] field, out Mark outputMark) { outputMark = Mark.Empty; int length = field.GetLength(0); for (int column = 0; column < length - 1; column++) { if (field[rowIndex, column] != field[rowIndex, column + 1]) { return(false); } } outputMark = field[rowIndex, 0]; return(true); }
public static bool CheckDiagonalTwo(Mark[,] field, out Mark outputMark) { int length = field.GetLength(0); outputMark = Mark.Empty; for (int i = 0; i < length - 1; i++) { if (field[length - 1 - i, i] != field[length - 2 - i, i + 1]) { return(false); } } outputMark = field[length - 1, 0]; return(true); }
/// <summary> /// Checks to see if this placing this mark causes a win (3 in a row) /// </summary> /// <param name="board">the current board</param> /// <param name="newMark">the mark placed</param> /// <param name="loc">the location of the mark placed</param> /// <returns>true when 3 in a row (win)</returns> public static bool CheckWin(Mark[,] board, Mark newMark, Coordinate loc) { if (loc.x < 0 || loc.x > board.GetLength(0) || newMark == Mark.non || loc.y < 0 || loc.y > board.GetLength(1)) { return(false); } for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; } //look for the first mark in a row if (loc.x + x >= 0 && loc.x + x < board.GetLength(0) && loc.y + y >= 0 && loc.y + y < board.GetLength(1) && board[loc.x + x, loc.y + y] == newMark) { //look for second mark in the same direction if (loc.x + (2 * x) >= 0 && loc.x + (2 * x) < board.GetLength(0) && loc.y + (2 * y) >= 0 && loc.y + (2 * y) < board.GetLength(1) && board[loc.x + (2 * x), loc.y + (2 * y)] == newMark) { return(true); } //look for second mark in the oposite direction if (loc.x - x >= 0 && loc.x - x < board.GetLength(0) && loc.y - y >= 0 && loc.y - y < board.GetLength(1) && board[loc.x - x, loc.y - y] == newMark) { return(true); } } } } return(false); }
private static Mark[][] GetAllLines(Mark[,] field) { var length = field.GetLength(0); // Assuming field is square var lines = new Mark[2 * length + 2][]; // get rows for (int row = 0; row < length; row++) { lines[row] = new Mark[length]; for (int col = 0; col < length; col++) { lines[row][col] = field[row, col]; } } // get columns for (int row = 0; row < length; row++) { lines[row + length] = new Mark[length]; for (int col = 0; col < length; col++) { lines[row + length][col] = field[col, row]; } } // get diagonals (only two) for (int row = 2 * length; row < 2 * length + 2; row++) { lines[row] = new Mark[length]; } for (int item = 0; item < length; item++) { lines[2 * length][item] = field[item, item]; lines[2 * length + 1][item] = field[item, length - 1 - item]; } return(lines); }
/// <summary> /// Tries to place the mark of the current player in the selected spot. /// </summary> /// <param name="spot">location on the board to place mark</param> /// <returns>true when the mark is placed, otherwise false</returns> public bool SelectSpot(Coordinate spot) { //make sure the spot is in bounds or the mark is wrong if (spot.x < 0 || spot.x >= _board.GetLength(0) || spot.y < 0 || spot.y >= _board.GetLength(1)) { return(false); } //check that the spot is empty if (_board[spot.x, spot.y] == Mark.non) { if (SetMark(spot, _curTurn)) { if (BoardController.CheckWin(_board, _curTurn, spot)) { GameManager.PlayerWins(_curTurn); if (OnLocalPlayerTurn != null) { OnLocalPlayerTurn(false); } } else { if (CatGame(_board)) { GameManager.PlayerWins(Mark.non); if (OnLocalPlayerTurn != null) { OnLocalPlayerTurn(false); } } else { ChangeTurn(); } } return(true); } } return(false); }
private (List <int> features, double summary) Summarize(Mark[,] gameField) { int completedSequences_Self = 0; int completedSequences_Opponent = 0; int sequencesToBeCompleted_Self = 0; int sequencesToBeComplited_Opponent = 0; int singleMarks_Self = 0; int singleMarks_Opponent = 0; List <Mark[]> rows = new List <Mark[]>(); List <Mark[]> columns = new List <Mark[]>(); List <Mark[]> diagonals = new List <Mark[]>(); for (int i = 0; i < gameField.GetLength(0); i++) { rows.Add(gameField.GetRow(i)); } for (int i = 0; i < gameField.GetLength(1); i++) { columns.Add(gameField.GetColumn(i)); } diagonals.Add(gameField.GetDiagonal(DiagonalType.Left)); diagonals.Add(gameField.GetDiagonal(DiagonalType.Right)); foreach (var line in rows.Concat(columns).Concat(diagonals)) { if (!line.ToList().Exists(m => m != InstanceRole)) { completedSequences_Self++; } if (!line.ToList().Exists(m => m != _opponentRole)) { completedSequences_Opponent++; } if (line.Where(m => m == InstanceRole).Count() > 1 && !line.Contains(_opponentRole)) { sequencesToBeCompleted_Self++; } if (line.Where(m => m == _opponentRole).Count() > 1 && !line.Contains(InstanceRole)) { sequencesToBeComplited_Opponent++; } if (line.Where(m => m == InstanceRole).Count() == 1 && !line.Contains(_opponentRole)) { singleMarks_Self++; } if (line.Where(m => m == _opponentRole).Count() == 1 && !line.Contains(InstanceRole)) { singleMarks_Opponent++; } } return(new List <int> { completedSequences_Self, completedSequences_Opponent, sequencesToBeCompleted_Self, sequencesToBeComplited_Opponent, singleMarks_Self, singleMarks_Opponent }, _weights[0] + _weights[1] * completedSequences_Self + _weights[2] * completedSequences_Opponent + _weights[3] * sequencesToBeCompleted_Self + _weights[4] * sequencesToBeComplited_Opponent + _weights[5] * singleMarks_Self + _weights[6] * singleMarks_Opponent); }
public static GameResult GetGameResult(Mark[,] field) { bool isCircleWin = false; bool isCrossWin = false; int length = field.GetLength(0); Mark currentMark; for (int index = 0; index < length - 1; index++) { bool rowCheckResult = CheckRow(index, field, out currentMark); if (currentMark == Mark.Circle) { isCircleWin = true; } else if (currentMark == Mark.Cross) { isCrossWin = true; } bool columnCheckResult = CheckColumn(index, field, out currentMark); if (currentMark == Mark.Circle) { isCircleWin = true; } else if (currentMark == Mark.Cross) { isCrossWin = true; } } if (CheckDiagonalOne(field, out currentMark)) { if (currentMark == Mark.Circle) { isCircleWin = true; } else if (currentMark == Mark.Cross) { isCrossWin = true; } } if (CheckDiagonalTwo(field, out currentMark)) { if (currentMark == Mark.Circle) { isCircleWin = true; } else if (currentMark == Mark.Cross) { isCrossWin = true; } } if (isCircleWin == isCrossWin) { return(GameResult.Draw); } else { return(isCircleWin ? GameResult.CircleWin : GameResult.CrossWin); } }
/// <summary> /// Проверка всех выиграшных возможностей /// </summary> /// <param name="iY">Индекс строки</param> /// <param name="iX">Индекс столбца</param> /// <param name="mark">Отметка</param> /// <param name="ltd">Линия для отрисовки</param> /// <returns>Возвращает символ, для отрисовки линии</returns> char CheckStaightLine(int iY, int iX, Mark mark, out LineToDraw ltd) { ltd = new LineToDraw(iY, iX, iY, iX); // Предполагается что текущий ход уже имеет 1 заполненую ячейку int totalX = 1, totalY = 1; for (int i = 1; i <= lineToWin; i++) { if (iX + i < cells.GetLength(1)) { if (cells[iY, iX + i] == mark) { totalX++; ltd.SetEnd(iY, iX + i); continue; } } break; } for (int i = 1; i <= lineToWin; i++) { if (iX - i >= 0) { if (cells[iY, iX - i] == mark) { totalX++; ltd.SetStart(iY, iX - i); continue; } } break; } if (totalX >= lineToWin) { return('-'); } for (int j = 1; j <= lineToWin; j++) { if (iY + j < cells.GetLength(0)) { if (cells[iY + j, iX] == mark) { totalY++; ltd.SetEnd(iY + j, iX); continue; } } break; } for (int j = 1; j <= lineToWin; j++) { if (iY - j >= 0) { if (cells[iY - j, iX] == mark) { totalY++; ltd.SetStart(iY - j, iX); continue; } } break; } if (totalY >= lineToWin) { return('|'); } return(' '); }