using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp11 { public partial class Form1 : Form { //public static Nullable[,] grid = new Nullable[3, 3]; public static char[,] grid = new char[3, 3]; public static int W_UNIT; public static int H_UNIT; public Form1() { InitializeComponent(); W_UNIT = Canvas.Width / 3; H_UNIT = Canvas.Height / 3; } private void Canvas_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; // Draw the Board using (Pen myPen = new Pen(Color.Gold)) { g.DrawLine(myPen, Canvas.Width / 3, 0, Canvas.Width / 3, Canvas.Height); g.DrawLine(myPen, (2 * Canvas.Width / 3), 0, (2 * Canvas.Width / 3), Canvas.Height); g.DrawLine(myPen, 0, Canvas.Height / 3, Canvas.Width, Canvas.Height / 3); g.DrawLine(myPen, 0, (2 * Canvas.Height / 3), Canvas.Width, (2 * Canvas.Height / 3)); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { // Draw the X's and O's if (grid[j, i] == 'X') // Then draw an X { g.DrawLine(myPen, (i * Canvas.Height / 3) + (j * Canvas.Width / 3), 0 + (i * Canvas.Height / 3), W_UNIT + (j * Canvas.Width / 3), H_UNIT + (i * Canvas.Height / 3)); g.DrawLine(myPen, (j * Canvas.Width / 3), H_UNIT + (i * Canvas.Height / 3), W_UNIT + (j * Canvas.Width / 3), (i * Canvas.Height / 3)); } } } } private void Canvas_MouseDown(object sender, MouseEventArgs e) { /* * We can now know where the user has clicked on the Board MessageBox.Show("[" + e.X / (Canvas.Width / 3) + ", " + e.Y / (Canvas.Height / 3) + "]"); */ grid[e.X / (Canvas.Width / 3), e.Y / (Canvas.Height / 3)] = 'X'; Canvas.Refresh(); } } }