C##

Download Raw Clone


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace footest
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. char[,] grid = Init(5, 5);
  13. grid[2, 2] = 'X';
  14. Print(grid);
  15. Console.WriteLine(isFinished(grid, 'X'));
  16. Console.ReadLine();
  17. }
  18. public static void Print(char[,] grid)
  19. {
  20. for (int i = 0; i < grid.GetLength(0); i++)
  21. {
  22. for (int j = 0; j < grid.GetLength(1); j++)
  23. {
  24. Console.Write(grid[i, j]);
  25. Console.Write(' ');
  26. }
  27. Console.WriteLine();
  28. }
  29. }
  30. //this lib is an implementation of Firnen's algo.
  31. public static bool isFinished(char[,] grid, char player)
  32. {
  33. bool output = true;
  34. for (int i = 0; i < grid.GetLength(0); i++)
  35. {
  36. for (int j = 0; j < grid.GetLength(1); j++)
  37. {
  38. output = Right(grid, player, i, j);
  39. output |= Up(grid, player, i, j);
  40. if (output) return true;
  41. }
  42. }
  43. return false;
  44. }
  45. public static bool Right(char[,] grid, char player, int x, int y)
  46. {
  47. //Computes whether the chosen case won on the right
  48. bool isWinning = x < grid.GetLength(0);//isXValid
  49. for (int i = 0; i < 4; i++)
  50. {
  51. isWinning &= y + i < grid.GetLength(1); //isYValid
  52. if (isWinning) { isWinning = grid[x, y + i] == player; }//isCaseCorrect
  53. }
  54. return isWinning;
  55. }
  56. public static bool Up(char[,] grid, char player, int x, int y)
  57. {
  58. //Computes whether the chosen case won on the upside
  59. bool isWinning = y < grid.GetLength(1); //isYValid
  60. for (int i = 0; i > (-4); i--)
  61. {
  62. isWinning = x + i > -1;//isXValid
  63. Console.WriteLine(x + i);
  64. Console.WriteLine(y);
  65. if (isWinning) { isWinning = grid[x + i, y] == player; }//isCaseCorrect
  66. }
  67. return isWinning;
  68. }
  69. public static char[,] Init(int width, int height)
  70. {
  71. char[,] board = new char[height, width];
  72. for (int i = 0; i < height; i++)
  73. for (int j = 0; j < width; j++)
  74. board[i, j] = ' ';
  75. return board;
  76. }
  77. }
  78. }

Raw paste data: