Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class PlayGround : Form
- {
- int counter = 1;
- const int REQUIRED = 4;
- Boolean playerTurn = true;
- Button[,] buttons = new Button[5, 5];
- public PlayGround()
- {
- InitializeComponent();
- initGame();
- }
- private void initGame()
- {
- int i = 0;
- int j = 0;
- foreach (Control c in this.Controls)
- {
- if (c is Button)
- {
- buttons[i, j] = (Button) c;
- }
- j++;
- if (j > 4)
- {
- i++;
- j = 0;
- }
- }
- }
- private void button_Click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (playerTurn)
- b.Text = "X";
- else b.Text = "O";
- checkForWin(playerTurn);
- playerTurn = !playerTurn;
- }
- private void checkForWin(Boolean playerTurn)
- {
- string znak;
- bool win = false;
- if (playerTurn) znak = "X";
- else znak = "O";
- for (int x = 0; x < 5; x++) {
- for (int y = 0; y < 5; y++)
- {
- for(int i = -1; i <= 1; i++)
- for (int j = -1; j <= 1; j++)
- {
- if (i == 0 && j == 0)
- {
- win = check(znak, x + i, y + j, i, j);
- if (!win) counter = 1;
- }
- }
- }
- }
- if (win)
- MessageBox.Show("One of the players won!", "Congratulation!");
- }
- private Boolean check(string znak, int x, int y, int i, int j){
- if (counter == REQUIRED)
- {
- return true;
- }
- if ((x >= 0 && x <= 4) && (y >= 0 && y <= 4))
- {
- if (buttons[x, y].Text == znak)
- {
- counter++;
- return check(znak, x + i, y +j, i, j);
- }
- else return false;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement