đây là code em làm, em đã vẽ được bàn cờ và thực hiện đánh được vào các button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DeAnCaro123
{
public partial class MainWindow : Window
{
public bool Switch = true;
public int[,] A = new int[25, 25];
int point_x = 0; int point_o = 0;
public MainWindow()
{
InitializeComponent();
populateButtons();
}
public void populateButtons()
{
for (int i = 0; i < 25; i++)
{
int xPos = i * 25 + 25;
for (int j = 0; j < 25; j++)
{
Button foo = new Button();
foo.Width = 25;// sizeValue;
foo.Height = 25;// sizeValue;
foo.Name = "but_" + (i * 25 + j);
int yPos = j * 25 + 25;// ranNum.Next(200);
foo.HorizontalAlignment = HorizontalAlignment.Left;
foo.VerticalAlignment = VerticalAlignment.Top;
foo.Margin = new Thickness(xPos, yPos, 0, 0);
foo.Click += new RoutedEventHandler(buttonClick);
canvas.Children.Add(foo);
}
}
}
private void buttonClick(object sender, EventArgs e)
{
Button clicked = (Button)sender;
int x, y;
bool win;
TachTenLayToaDo(clicked.Name, out x, out y);
MessageBox.Show(x.ToString() + " " + y.ToString());
if (Switch == true)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("C:/Users/Asus/Desktop/DeAnCaro123/DeAnCaro123/DeAnCaro123/source/X.jpg"));
clicked.Background = brush;
Switch = false;
point_x += 1;
win = CongTheoHangNgang(A, x, y);
if (win == true)
{
MessageBox.Show("Player X win");
}
}
else if (Switch == false)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("C:/Users/Asus/Desktop/DeAnCaro123/DeAnCaro123/DeAnCaro123/source/O.jpg"));
clicked.Background = brush;
Switch = true;
point_o += 1;
win = CongTheoHangNgang(A, x, y);
if (win == true)
{
MessageBox.Show("Player O win");
}
}
else if (Switch == true || Switch == false)
{
return;
}
}
private bool CongTheoHangNgang(int[,] A, int x, int y)
{
if (point_x == 5 || point_o == 5 )
{
return true;
}
else
{
return false;
}
}
private void TachTenLayToaDo(string s, out int x, out int y)
{
string temp = s.Substring(4);
int so = int.Parse(temp);
x = so % 25;
y = so / 25;
}
}
}