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.

127 lines
3.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MiniGames.Shared.Models
  7. {
  8. public class TicTacToeBrett
  9. {
  10. public const int LEER = -1;
  11. int[,] Felder;
  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 < 3; i++)
  46. {
  47. for (int j = 0; j < 3; j++)
  48. {
  49. if (Felder[i, j] != anderes.Felder[i, j])
  50. {
  51. return false;
  52. }
  53. }
  54. }
  55. return true;
  56. }
  57. public bool Set(int pos, int wert)
  58. {
  59. int x = pos % 3;
  60. int y = pos / 3;
  61. try
  62. {
  63. if (Felder[x, y] == LEER)
  64. {
  65. Felder[x, y] = wert;
  66. return true;
  67. }
  68. }
  69. catch (IndexOutOfRangeException e)
  70. {
  71. // absichtlich leer
  72. }
  73. return false;
  74. }
  75. public bool Voll()
  76. {
  77. for (int i = 0; i < 3; i++)
  78. {
  79. for (int j = 0; j < 3; j++)
  80. {
  81. if (Felder[i, j] == LEER)
  82. {
  83. return false;
  84. }
  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. }