here is my code for my tic tac toe game, I am still working on the computer player.
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 wintictactoe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
int [,] board_x=new int[3,3];
int[,] board_o = new int[3, 3];
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Random rnd = new Random();
int col = rnd.Next(325, 476);
int row = rnd.Next(200, 351);
Graphics g1 = this.CreateGraphics();
if (e.X - 10 >= 325 && e.X - 10 <= 375 && e.Y - 10 >= 200 && e.Y - 10 <= 250)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 215));
board_x[0, 0] = 1;
}
if (e.X - 10 >= 375 && e.X - 10 <= 425 && e.Y - 10 >= 200 && e.Y - 10 <= 250)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 215));
board_x[0, 1] = 1;
}
if (e.X - 10 >= 425 && e.X - 10 <= 475 && e.Y - 10 >= 200 && e.Y - 10 <= 250)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 215));
board_x[0, 2] = 1;
}
if (e.X - 10 >= 325 && e.X - 10 <= 375 && e.Y - 10 >= 250 && e.Y - 10 <= 300)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 260));
board_x[1, 0] = 1;
}
if (e.X - 10 >= 375 && e.X - 10 <= 425 && e.Y - 10 >= 250 && e.Y - 10 <= 300)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 260));
board_x[1, 1] = 1;
}
if (e.X - 10 >= 425 && e.X - 10 <= 475 && e.Y - 10 >= 250 && e.Y - 10 <= 300)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 260));
board_x[1, 2] = 1;
}
if (e.X - 10 >= 325 && e.X - 10 <= 375 && e.Y - 10 >= 300 && e.Y - 10 <= 350)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 315));
board_x[2, 0] = 1;
}
if (e.X - 10 >= 375 && e.X - 10 <= 425 && e.Y - 10 >= 300 && e.Y - 10 <= 350)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 315));
board_x[2, 1] = 1;
}
if (e.X - 10 >= 425 && e.X - 10 <= 475 && e.Y - 10 >= 300 && e.Y - 10 <= 350)
{
g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 315));
board_x[2, 2] = 1;
}
if (col >= 325 && col <= 375 && row >= 200 && row <= 250)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 215));
board_o[0, 0] = 1;
}
if (col >= 375 && col <= 425 && row >= 200 && row <= 250)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 215));
board_o[0, 1] = 1;
}
if (col >= 425 && col <= 475 && row >= 200 && row <= 250)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 215));
board_o[0, 2] = 1;
}
if (col >= 325 && col <= 375 && row >= 250 && row <= 300)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 260));
board_o[1, 0] = 1;
}
if (col >= 375 && col <= 425 && row >= 250 && row <= 300)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 260));
board_o[1, 1] = 1;
}
if (col >= 425 && col <= 475 && row >= 250 && row <= 300)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 260));
board_o[1, 2] = 1;
}
if (col >= 325 && col <= 375 && row >= 300 && row <= 350)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 315));
board_o[2, 0] = 1;
}
if (col >= 375 && col <= 425 && row >= 300 && row <= 350)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 315));
board_o[2, 1] = 1;
}
if (col >= 425 && col <= 475 && row >= 300 && row <= 350)
{
g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 315));
board_o[2, 2] = 1;
}
g1.Dispose();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen redPen = new Pen(Color.Red,3);
g.DrawLine(redPen, 375, 200, 375, 350);
g.DrawLine(redPen, 425, 200, 425, 350);
g.DrawLine(redPen, 325, 250, 475, 250);
g.DrawLine(redPen, 325, 300, 475, 300);
g.Dispose();
}
}
}
I have made some progress on my game, here is my code so far.
my only problem is that my O's draw over my X's sometimes.