Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14. public partial class PlayGround : Form
  15. {
  16. int counter = 1;
  17. const int REQUIRED = 4;
  18. Boolean playerTurn = true;
  19. Button[,] buttons = new Button[5, 5];
  20.  
  21. public PlayGround()
  22. {
  23. InitializeComponent();
  24. initGame();
  25. }
  26.  
  27. private void initGame()
  28. {
  29. int i = 0;
  30. int j = 0;
  31. foreach (Control c in this.Controls)
  32. {
  33. if (c is Button)
  34. {
  35. buttons[i, j] = (Button) c;
  36. }
  37. j++;
  38. if (j > 4)
  39. {
  40. i++;
  41. j = 0;
  42. }
  43.  
  44. }
  45.  
  46.  
  47. }
  48.  
  49. private void button_Click(object sender, EventArgs e)
  50. {
  51. Button b = (Button)sender;
  52. if (playerTurn)
  53. b.Text = "X";
  54. else b.Text = "O";
  55. checkForWin(playerTurn);
  56. playerTurn = !playerTurn;
  57. }
  58.  
  59. private void checkForWin(Boolean playerTurn)
  60. {
  61. string znak;
  62. bool win = false;
  63. if (playerTurn) znak = "X";
  64. else znak = "O";
  65. for (int x = 0; x < 5; x++) {
  66. for (int y = 0; y < 5; y++)
  67. {
  68. for(int i = -1; i <= 1; i++)
  69. for (int j = -1; j <= 1; j++)
  70. {
  71. if (i == 0 && j == 0)
  72. {
  73. win = check(znak, x + i, y + j, i, j);
  74. if (!win) counter = 1;
  75. }
  76. }
  77. }
  78. }
  79.  
  80. if (win)
  81. MessageBox.Show("One of the players won!", "Congratulation!");
  82. }
  83.  
  84. private Boolean check(string znak, int x, int y, int i, int j){
  85. if (counter == REQUIRED)
  86. {
  87. return true;
  88. }
  89. if ((x >= 0 && x <= 4) && (y >= 0 && y <= 4))
  90. {
  91. if (buttons[x, y].Text == znak)
  92. {
  93. counter++;
  94. return check(znak, x + i, y +j, i, j);
  95. }
  96. else return false;
  97.  
  98. }
  99. return false;
  100. }
  101.  
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement