//returns the number of unpicked pieces remaining that share atleast one property with the piece residing on a given vector public static int CommonPiecesRemaining(EvaluationDirection d, Board b, Piece[] remainingPieces, int x = 0, int y = 0) { int[] binary; int count = 0; switch (d) { case EvaluationDirection.Row: binary = CommonProperties(EvaluationDirection.Row, b, x, y); count = 0; for (int i = 0; i < remainingPieces.Length; i++) { if (remainingPieces[i] != null) { int[] xor = remainingPieces[i].BinaryXOR(binary); if (BinarySum(xor) > 0) { count++; } } } return(count); case EvaluationDirection.Column: binary = CommonProperties(EvaluationDirection.Column, b, x, y); count = 0; for (int i = 0; i < remainingPieces.Length; i++) { if (remainingPieces[i] != null) { int[] xor = remainingPieces[i].BinaryXOR(binary); if (BinarySum(xor) > 0) { count++; } } } return(count); case EvaluationDirection.Diagonal: binary = CommonProperties(EvaluationDirection.Diagonal, b, x, y); count = 0; for (int i = 0; i < remainingPieces.Length; i++) { if (remainingPieces[i] != null) { int[] xor = remainingPieces[i].BinaryXOR(binary); if (BinarySum(xor) > 0) { count++; } } } return(count); } return(0); }
//returns the number of open spots for a given vector public static int SpotsRemaining(EvaluationDirection d, Board b, int x, int y) { int count = 0; switch (d) { case EvaluationDirection.Row: for (int i = 0; i < 4; i++) { if (b.GetPiece(i, y) == null) { count++; } } break; case EvaluationDirection.Column: for (int i = 0; i < 4; i++) { if (b.GetPiece(x, i) == null) { count++; } } break; case EvaluationDirection.Diagonal: if (x == y) { for (int i = 0; i < 4; i++) { if (b.GetPiece(i, i) == null) { count++; } } } else if (x + y == 3) { for (int i = 0; i < 4; i++) { if (b.GetPiece(3 - i, i) == null) { count++; } } } else { return(0); } break; } return(count); }
private PieceColor EvaluatePieceForWinner(int i, int j, EvaluationDirection dir) { GamePiece currentPiece = board.Board[i, j]; if (currentPiece.Color == PieceColor.Blank) { return(PieceColor.Blank); } int inARow = 1; int iNext = i; int jNext = j; while (inARow < 4) { switch (dir) { case EvaluationDirection.Up: jNext = jNext - 1; break; case EvaluationDirection.UpRight: iNext = iNext + 1; jNext = jNext - 1; break; case EvaluationDirection.Right: iNext = iNext + 1; break; case EvaluationDirection.DownRight: iNext = iNext + 1; jNext = jNext + 1; break; } if (iNext < 0 || iNext >= 7 || jNext < 0 || jNext >= 6) { break; } if (board.Board[iNext, jNext].Color == currentPiece.Color) { inARow++; } else { return(PieceColor.Blank); } } return(inARow >= 4 ? currentPiece.Color : PieceColor.Blank); }
//Checks if the location provided resides on a diagonal with 4 tiles public static int Winnable(EvaluationDirection d, Board b, int x, int y) { switch (d) { case EvaluationDirection.Row: return(1); case EvaluationDirection.Column: return(1); case EvaluationDirection.Diagonal: if (x == y) { return(1); } else if (x + y == 3) { return(1); } return(0); } return(0); }
private WinningPlay EvaluatePieceForWinner(int i, int j, EvaluationDirection dir) { GamePiece currentPiece = Board[i, j]; if (currentPiece.Style == PieceStyle.Blank) { return(null); } int inARow = 1; int iNext = i; int jNext = j; var winningMoves = new List <string>(); while (inARow < 3) { switch (dir) { case EvaluationDirection.Up: jNext -= 1; break; case EvaluationDirection.UpRight: iNext += 1; jNext -= 1; break; case EvaluationDirection.Right: iNext += 1; break; case EvaluationDirection.DownRight: iNext += 1; jNext += 1; break; } if (iNext < 0 || iNext >= 3 || jNext < 0 || jNext >= 3) { break; } if (Board[iNext, jNext].Style == currentPiece.Style) { winningMoves.Add($"{iNext},{jNext}"); inARow++; } else { return(null); } } if (inARow >= 3) { winningMoves.Add($"{i},{j}"); return(new WinningPlay() { WinningMoves = winningMoves, WinningStyle = currentPiece.Style, WinningDirection = dir, }); } return(null); }
//computes an XOR on all pieces in a vector resulting in a 4bit binary array that represents which properties are shared by all pieces public static int[] CommonProperties(EvaluationDirection d, Board b, int x = 0, int y = 0) { List <Piece> pieceList = new List <Piece>(); int[] summary = new int[4] { 1, 1, 1, 1 }; switch (d) { case EvaluationDirection.Row: for (int i = 0; i < 4; i++) { if (b.GetPiece(i, y) == null) { continue; } else { pieceList.Add(b.GetPiece(i, y)); } } if (pieceList.Count > 1) { summary = pieceList[0].GetBinary(); } else { return(summary); } for (int i = 1; i < pieceList.Count; i++) { summary = pieceList[i].BinaryXOR(summary); } return(summary); case EvaluationDirection.Column: for (int i = 0; i < 4; i++) { if (b.GetPiece(x, i) == null) { continue; } else { pieceList.Add(b.GetPiece(x, i)); } } if (pieceList.Count > 1) { summary = pieceList[0].GetBinary(); } else { return(summary); } for (int i = 1; i < pieceList.Count; i++) { summary = pieceList[i].BinaryXOR(summary); } return(summary); case EvaluationDirection.Diagonal: if (x == y) { for (int i = 0; i < 4; i++) { if (b.GetPiece(i, i) == null) { continue; } else { pieceList.Add(b.GetPiece(i, i)); } } } else if (x + y == 3) { for (int i = 0; i < 4; i++) { if (b.GetPiece(3 - i, i) == null) { continue; } else { pieceList.Add(b.GetPiece(3 - i, i)); } } } else { //point given is not on a diagonal return(new int[4] { 0, 0, 0, 0 }); } if (pieceList.Count > 1) { summary = pieceList[0].GetBinary(); } else { return(summary); } for (int i = 1; i < pieceList.Count; i++) { summary = pieceList[i].BinaryXOR(summary); } return(summary); } return(null); }
//The method to search for tic-tac-toes for a single given space looks like this: private WinningPlay EvaluatePieceForWinner(int i, int j, EvaluationDirection dir) { GamePiece currentPiece = Board[i, j]; if (currentPiece.Style == PieceStyle.Blank) { return(null); } int inARow = 1; int iNext = i; int jNext = j; var winningMoves = new List <string>(); while (inARow < 3) { //For each direction, increment the pointers to the next space //to be evaluated switch (dir) { case EvaluationDirection.Up: jNext -= 1; break; case EvaluationDirection.UpRight: iNext += 1; jNext -= 1; break; case EvaluationDirection.Right: iNext += 1; break; case EvaluationDirection.DownRight: iNext += 1; jNext += 1; break; } //If the next "space" is off the board, don't check it. if (iNext < 0 || iNext >= 3 || jNext < 0 || jNext >= 3) { break; } //If the next space has a matching letter... if (Board[iNext, jNext].Style == currentPiece.Style) { //Add this space to the collection of winning spaces. winningMoves.Add($"{iNext},{jNext}"); inARow++; } else //Otherwise, no tic-tac-toe is found for this space/direction { return(null); } } //If we found three in a row if (inARow >= 3) { //Return this set of spaces as the winning set winningMoves.Add($"{i},{j}"); return(new WinningPlay() { WinningMoves = winningMoves, WinningStyle = currentPiece.Style, WinningDirection = dir, }); } //If we got here, we didn't find any tic-tac-toes for the given space. return(null); }