/// <summary> /// Check to see if a player can win /// </summary> /// <param name="startX">Start X</param> /// <param name="startY">Start Y</param> /// <param name="board">PlayField Type</param> /// <param name="dx">Directional X</param> /// <param name="dy">Directional Y</param> /// <param name="look">What symbol to look for (X,O)</param> /// <returns>True/False if a opponent has a winning chance </returns> public static bool CheckFields(int startX, int startY, PlayField board, int dx, int dy, string look) { // Reset Values/ Initialize variables xCount = 0; oCount = 0; emptyCount = 0; // Is this used=??? yxPos = new int[2]; // Loops through the playfield for (var i = 0; i < 3; i++) { int y = startY + dy * i; int x = startX + dx * i; if (board.field[y, x] == "") { yxPos[0] = y; yxPos[1] = x; emptyCount++; // Is this used=?? } if (board.field[y, x] == "X") { xCount++; } if (board.field[y, x] == "O") { oCount++; } } if (oCount.Equals(2) && emptyCount.Equals(1) && look == "O") { canWin = true; return(true); } if (xCount.Equals(2) && emptyCount.Equals(1) && look == "X") { canWin = true; return(true); } canWin = false; return(false); }
/// <summary> /// Checks for counter/winning moves /// </summary> /// <param name="board">PlayField Type</param> /// <param name="con">All form controls</param> /// <param name="player">String from ENUM</param> public static bool CheckMove(PlayField board, Control.ControlCollection con, string player) { currentPlayer = player; // Set the symbols to look for lookFor = new string[] { "O", "X" }; // Goes through all symbols in order foreach (string s in lookFor) { // Check columns for counter/winning moves for (var x = 0; x < board.field.GetLength(0); x++) { if (CheckFields(x, 0, board, 0, 1, s)) { return(true); } } // Check rows for counter/winning moves for (var y = 0; y < board.field.GetLength(0); y++) { if (CheckFields(0, y, board, 1, 0, s)) { return(true); } } // Check diagonals for counter/winning moves if (CheckFields(0, 0, board, 1, 1, s)) { return(true); } if (CheckFields(2, 0, board, -1, 1, s)) { return(true); } } return(false); }
/// <summary> /// AI Move Invoke Handler. /// Advanced Game Mode /// </summary> /// <param name="board">PlayField Type</param> /// <param name="con">All form controls</param> /// <param name="player">String from ENUM</param> /// <param name="UsedBricks">Current used bricks (int)</param> public static void Move(PlayField board, Control.ControlCollection con, string player, int UsedBricks) { }
/// <summary> /// AI Move Invoke Handler. /// Simple Game Mode /// </summary> /// <param name="board">PlayField Type</param> /// <param name="con">All form contorls</param> /// <param name="player">String from ENUM</param> public static void Move(PlayField board, Control.ControlCollection con, string player) { CheckMove(board, con, player); MakeMove(con); }
/// <summary> /// Places a brick in the first availble (empty) field in the PlayField /// </summary> /// <param name="g"></param> /// <returns>The number of the button to place the brick in</returns> public static string AIDicision(PlayField g) { String button = ""; // Check for winning first // Checks every row from left to right, both X and O if (g.field[0, 0] == g.field[0, 1] && g.field[0, 1] == "O" && g.field[0, 2] == "") { button = "3"; } if (g.field[1, 0] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[1, 2] == "") { button = "6"; } if (g.field[2, 0] == g.field[2, 1] && g.field[2, 1] == "O" && g.field[2, 2] == "") { button = "9"; } // if (g.field1 == g.field2 && g.field2 == "O" && g.field3 == "") button = "3"; // if (g.field4 == g.field5 && g.field5 == "O" && g.field6 == "") button = "6"; // if (g.field7 == g.field8 && g.field8 == "O" && g.field9 == "") button = "9"; // Checks every row from right to left, both X and O if (g.field[0, 2] == g.field[0, 1] && g.field[0, 1] == "O" && g.field[0, 0] == "") { button = "1"; } if (g.field[1, 2] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[1, 0] == "") { button = "4"; } if (g.field[2, 2] == g.field[2, 1] && g.field[2, 1] == "O" && g.field[2, 0] == "") { button = "7"; } // if (g.field3 == g.field2 && g.field3 == "O" && g.field1 == "") button = "1"; // if (g.field6 == g.field5 && g.field6 == "O" && g.field4 == "") button = "4"; // if (g.field9 == g.field8 && g.field9 == "O" && g.field7 == "") button = "7"; // Check every row for side match, both X and O if (g.field[0, 0] == g.field[0, 2] && g.field[0, 2] == "O" && g.field[0, 1] == "") { button = "2"; } if (g.field[1, 0] == g.field[1, 2] && g.field[1, 2] == "O" && g.field[1, 1] == "") { button = "5"; } if (g.field[2, 0] == g.field[2, 2] && g.field[2, 2] == "O" && g.field[2, 1] == "") { button = "8"; } // if (g.field[0, 0] == g.field[0, 2] && g.field[0, 2] == "O" && g.field[0, 1] == "") button = "2"; // if (g.field[1, 0] == g.field[1, 2] && g.field[1, 2] == "O" && g.field[1, 1] == "") button = "5"; // if (g.field[2, 0] == g.field[2, 2] && g.field[2, 2] == "O" && g.field[2, 1] == "") button = "8"; // Check every colum from top to bottom, both X and O if (g.field[0, 0] == g.field[1, 0] && g.field[1, 0] == "O" && g.field[2, 0] == "") { button = "7"; } if (g.field[0, 1] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[2, 2] == "") { button = "8"; } if (g.field[0, 2] == g.field[1, 2] && g.field[1, 2] == "O" && g.field[2, 2] == "") { button = "9"; } // if (g.field1 == g.field4 && g.field4 == "O" && g.field7 == "") button = "7"; // if (g.field2 == g.field5 && g.field5 == "O" && g.field8 == "") button = "8"; // if (g.field3 == g.field6 && g.field6 == "O" && g.field9 == "") button = "9"; // Check every colum from bottom to top, both X and O if (g.field[2, 0] == g.field[1, 0] && g.field[1, 0] == "O" && g.field[0, 0] == "") { button = "1"; } if (g.field[2, 1] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[0, 1] == "") { button = "2"; } if (g.field[2, 2] == g.field[1, 2] && g.field[1, 2] == "O" && g.field[0, 2] == "") { button = "3"; } // if (g.field7 == g.field4 && g.field4 == "O" && g.field1 == "") button = "1"; // if (g.field8 == g.field5 && g.field5 == "O" && g.field2 == "") button = "2"; // if (g.field9 == g.field6 && g.field6 == "O" && g.field3 == "") button = "3"; // Check every colum for side match, both X and O if (g.field[0, 0] == g.field[2, 0] && g.field[2, 0] == "O" && g.field[1, 0] == "") { button = "4"; } if (g.field[0, 1] == g.field[2, 1] && g.field[2, 1] == "O" && g.field[1, 1] == "") { button = "5"; } if (g.field[0, 2] == g.field[2, 2] && g.field[2, 2] == "O" && g.field[1, 2] == "") { button = "6"; } // if (g.field[0, 0] == g.field[2, 0] && g.field[2, 0] == "O" && g.field[1, 1] == "") button = "4"; // if (g.field[0, 1] == g.field[2, 1] && g.field[2, 1] == "O" && g.field[1, 1] == "") button = "5"; // if (g.field[0, 2] == g.field[2, 2] && g.field[2, 2] == "O" && g.field[1, 1] == "") button = "6"; // Chech diagnally from top to bottom, both X and O if (g.field[0, 0] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[2, 2] == "") { button = "9"; } if (g.field[0, 2] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[2, 0] == "") { button = "7"; } // if (g.field1 == g.field5 && g.field5 == "O" && g.field9 == "") button = "9"; // if (g.field3 == g.field5 && g.field5 == "O" && g.field7 == "") button = "7"; // Chech diagnally from bottom to top, both X and O if (g.field[2, 0] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[0, 2] == "") { button = "3"; } if (g.field[2, 2] == g.field[1, 1] && g.field[1, 1] == "O" && g.field[0, 0] == "") { button = "1"; } // if (g.field7 == g.field5 && g.field5 == "O" && g.field3 == "") button = "3"; // if (g.field9 == g.field5 && g.field5 == "O" && g.field1 == "") button = "1"; // if none of the above (winning moves) can be made - see if enemy can win if (button == "") { // Checks every row from left to right, both X and O if (g.field[0, 0] == g.field[0, 1] && g.field[0, 1] == "X" && g.field[0, 2] == "") { button = "3"; } if (g.field[1, 0] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[1, 2] == "") { button = "6"; } if (g.field[2, 0] == g.field[2, 1] && g.field[2, 1] == "X" && g.field[2, 2] == "") { button = "9"; } // if (g.field1 == g.field2 && g.field2 == "O" && g.field3 == "") button = "3"; // if (g.field4 == g.field5 && g.field5 == "O" && g.field6 == "") button = "6"; // if (g.field7 == g.field8 && g.field8 == "O" && g.field9 == "") button = "9"; // Checks every row from right to left, both X and O if (g.field[0, 2] == g.field[0, 1] && g.field[0, 1] == "X" && g.field[0, 0] == "") { button = "1"; } if (g.field[1, 2] == g.field[1, 1] && g.field[1, 1] == "X" && (g.field[1, 0]) == "") { button = "4"; } if (g.field[2, 2] == g.field[2, 1] && g.field[2, 1] == "X" && g.field[2, 0] == "") { button = "7"; } // if (g.field3 == g.field2 && g.field3 == "O" && g.field1 == "") button = "1"; // if (g.field6 == g.field5 && g.field6 == "O" && g.field4 == "") button = "4"; // if (g.field9 == g.field8 && g.field9 == "O" && g.field7 == "") button = "7"; // Check every row for side match, both X and O if (g.field[0, 0] == g.field[0, 2] && g.field[0, 2] == "X" && g.field[0, 1] == "") { button = "2"; } if (g.field[1, 0] == g.field[1, 2] && g.field[1, 2] == "X" && g.field[1, 1] == "") { button = "5"; } if (g.field[2, 0] == g.field[2, 2] && g.field[2, 2] == "X" && g.field[2, 1] == "") { button = "8"; } // if (g.field[0, 0] == g.field[0, 2] && g.field[0, 2] == "O" && g.field[0, 1] == "") button = "2"; // if (g.field[1, 0] == g.field[1, 2] && g.field[1, 2] == "O" && g.field[1, 1] == "") button = "5"; // if (g.field[2, 0] == g.field[2, 2] && g.field[2, 2] == "O" && g.field[2, 1] == "") button = "8"; // Check every colum from top to bottom, both X and O if (g.field[0, 0] == g.field[1, 0] && g.field[1, 0] == "X" && g.field[2, 0] == "") { button = "7"; } if (g.field[0, 1] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[2, 2] == "") { button = "8"; } if (g.field[0, 2] == g.field[1, 2] && g.field[1, 2] == "X" && g.field[2, 2] == "") { button = "9"; } // if (g.field1 == g.field4 && g.field4 == "O" && g.field7 == "") button = "7"; // if (g.field2 == g.field5 && g.field5 == "O" && g.field8 == "") button = "8"; // if (g.field3 == g.field6 && g.field6 == "O" && g.field9 == "") button = "9"; // Check every colum from bottom to top, both X and O if (g.field[2, 0] == g.field[1, 0] && g.field[1, 0] == "X" && g.field[0, 0] == "") { button = "1"; } if (g.field[2, 1] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[0, 1] == "") { button = "2"; } if (g.field[2, 2] == g.field[1, 2] && g.field[1, 2] == "X" && g.field[0, 2] == "") { button = "3"; } // if (g.field7 == g.field4 && g.field4 == "O" && g.field1 == "") button = "1"; // if (g.field8 == g.field5 && g.field5 == "O" && g.field2 == "") button = "2"; // if (g.field9 == g.field6 && g.field6 == "O" && g.field3 == "") button = "3"; // Check every colum for side match, both X and O if (g.field[0, 0] == g.field[2, 0] && g.field[2, 0] == "X" && g.field[1, 0] == "") { button = "4"; } if (g.field[0, 1] == g.field[2, 1] && g.field[2, 1] == "X" && g.field[1, 1] == "") { button = "5"; } if (g.field[0, 2] == g.field[2, 2] && g.field[2, 2] == "X" && g.field[1, 2] == "") { button = "6"; } // if (g.field[0, 0] == g.field[2, 0] && g.field[2, 0] == "O" && g.field[1, 1] == "") button = "4"; // if (g.field[0, 1] == g.field[2, 1] && g.field[2, 1] == "O" && g.field[1, 1] == "") button = "5"; // if (g.field[0, 2] == g.field[2, 2] && g.field[2, 2] == "O" && g.field[1, 1] == "") button = "6"; // Chech diagnally from top to bottom, both X and O if (g.field[0, 0] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[2, 2] == "") { button = "9"; } if (g.field[0, 2] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[2, 0] == "") { button = "7"; } // if (g.field1 == g.field5 && g.field5 == "O" && g.field9 == "") button = "9"; // if (g.field3 == g.field5 && g.field5 == "O" && g.field7 == "") button = "7"; // Chech diagnally from bottom to top, both X and O if (g.field[2, 0] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[0, 2] == "") { button = "3"; } if (g.field[2, 2] == g.field[1, 1] && g.field[1, 1] == "X" && g.field[0, 0] == "") { button = "1"; } // if (g.field7 == g.field5 && g.field5 == "O" && g.field3 == "") button = "3"; // if (g.field9 == g.field5 && g.field5 == "O" && g.field1 == "") button = "1"; } // returns button value if not "" if (button != "") { return(button); } return(""); }
/// <summary> /// Checks for a winner /// </summary> /// <param name="g"></param> /// <returns>Winner - Possible: X,O</returns> public static string DicideWinner(PlayField g) { // Check every row if (g.field[0, 0] == g.field[0, 1] && g.field[0, 1] == g.field[0, 2] && g.field[0, 2] == "X") { return("X"); } if (g.field[1, 0] == g.field[1, 1] && g.field[1, 1] == g.field[1, 2] && g.field[1, 2] == "X") { return("X"); } if (g.field[2, 0] == g.field[2, 1] && g.field[2, 1] == g.field[2, 2] && g.field[2, 2] == "X") { return("X"); } if (g.field[0, 0] == g.field[0, 1] && g.field[0, 1] == g.field[0, 2] && g.field[0, 2] == "O") { return("O"); } if (g.field[1, 0] == g.field[1, 1] && g.field[1, 1] == g.field[1, 2] && g.field[1, 2] == "O") { return("O"); } if (g.field[2, 0] == g.field[2, 1] && g.field[2, 1] == g.field[2, 2] && g.field[2, 2] == "O") { return("O"); } // Check every colum if (g.field[0, 0] == g.field[1, 0] && g.field[1, 0] == g.field[2, 0] && g.field[2, 0] == "X") { return("X"); } if (g.field[0, 1] == g.field[1, 1] && g.field[1, 1] == g.field[2, 1] && g.field[2, 1] == "X") { return("X"); } if (g.field[0, 2] == g.field[1, 2] && g.field[1, 2] == g.field[2, 2] && g.field[2, 2] == "X") { return("X"); } if (g.field[0, 0] == g.field[1, 0] && g.field[1, 0] == g.field[2, 0] && g.field[2, 0] == "O") { return("O"); } if (g.field[0, 1] == g.field[1, 1] && g.field[1, 1] == g.field[2, 1] && g.field[2, 1] == "O") { return("O"); } if (g.field[0, 2] == g.field[1, 2] && g.field[1, 2] == g.field[2, 2] && g.field[2, 2] == "O") { return("O"); } // Chech diagnally if (g.field[0, 0] == g.field[1, 1] && g.field[1, 1] == g.field[2, 2] && g.field[2, 2] == "X") { return("X"); } if (g.field[2, 0] == g.field[1, 1] && g.field[1, 1] == g.field[2, 0] && g.field[2, 0] == "X") { return("X"); } if (g.field[0, 0] == g.field[1, 1] && g.field[1, 1] == g.field[2, 2] && g.field[2, 2] == "O") { return("O"); } if (g.field[2, 0] == g.field[1, 1] && g.field[1, 1] == g.field[2, 0] && g.field[2, 0] == "O") { return("O"); } return(""); }