Continous Integration in der Praxis Gruppenarbeit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.0 KiB

  1. using System;
  2. namespace MiniGames.Shared.Models
  3. {
  4. public class TicTacToeBrett
  5. {
  6. public const int LEER = -1;
  7. int[,] Felder;
  8. public int Groesse
  9. {
  10. get { return Felder.GetLength(0) * Felder.GetLength(1); }
  11. }
  12. public TicTacToeBrett()
  13. {
  14. Felder = new[,]
  15. {
  16. { LEER, LEER, LEER },
  17. { LEER, LEER, LEER },
  18. { LEER, LEER, LEER },
  19. };
  20. }
  21. public TicTacToeBrett(int[,] werte)
  22. {
  23. Felder = new int[3, 3];
  24. if (werte == null)
  25. {
  26. werte = new int[0, 0];
  27. }
  28. for (int i = 0; i < Felder.GetLength(0); i++)
  29. {
  30. for (int j = 0; j < Felder.GetLength(1); j++)
  31. {
  32. if (i < werte.GetLength(0) && j < werte.GetLength(1))
  33. {
  34. Felder[i, j] = werte[i, j];
  35. }
  36. else
  37. {
  38. Felder[i, j] = LEER;
  39. }
  40. }
  41. }
  42. }
  43. public bool Gleich(TicTacToeBrett anderes)
  44. {
  45. for (int i = 0; i < Groesse; i++)
  46. {
  47. if (Get(i) != anderes.Get(i))
  48. {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. public int Get(int pos)
  55. {
  56. int x = pos % 3;
  57. int y = pos / 3;
  58. return Felder[x, y];
  59. }
  60. public bool Set(int pos, int wert)
  61. {
  62. int x = pos % 3;
  63. int y = pos / 3;
  64. try
  65. {
  66. if (Felder[x, y] == LEER)
  67. {
  68. Felder[x, y] = wert;
  69. return true;
  70. }
  71. }
  72. catch (IndexOutOfRangeException e)
  73. {
  74. // absichtlich leer
  75. }
  76. return false;
  77. }
  78. public bool Voll()
  79. {
  80. for (int i = 0; i < Groesse; i++)
  81. {
  82. if (Get(i) == LEER)
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. protected bool dreiGleichGefuellt(int a, int b, int c)
  90. {
  91. return a != LEER && a == b && b == c;
  92. }
  93. public int Gewinner()
  94. {
  95. for (int i = 0; i < 3; i++)
  96. {
  97. if (dreiGleichGefuellt(Felder[i, 0], Felder[i, 1], Felder[i, 2])) return Felder[i, 0];
  98. if (dreiGleichGefuellt(Felder[0, i], Felder[1, i], Felder[2, i])) return Felder[0, i];
  99. }
  100. if (
  101. dreiGleichGefuellt(Felder[0, 0], Felder[1, 1], Felder[2, 2]) ||
  102. dreiGleichGefuellt(Felder[2, 0], Felder[1, 1], Felder[0, 2])
  103. )
  104. {
  105. return Felder[1, 1];
  106. }
  107. return LEER;
  108. }
  109. }
  110. }