Hey ma dudes.
I've done another programming project!
The last one I showed you I think was Event Driven Programming, now what I'm showing is Object Oriented Programming.
This project is quite small but very good to learn from. Once I created this project I felt proud of my achievement because last year I was only doing really basic stuff in programming, now we moved onto the best stuff in year 2.
This project is about creating a game, tic-tac-toe. To some of you this may be easy, but it took time for me.
I also made this program using feedback from my other post about the
Find the Lady project which thank you
CharlieKelly and
Yiggles Moto for your feedback!
Btw this has to be the most code I've done this year.
If you guys want to give me any feedback, please yourself!
I haven't quite finished this project yet but it is nearly done.
I haven't done when there is no winner it resets but if someone would like to add a bit of code into that feel free to do so.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace tictactoefixed
{
public partial class Form1 : Form
{
ticButton[,] tb;
int intW = 3;
int intH = 3;
int intUser = -1;
public Form1()
{
InitializeComponent();
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
48,
FontStyle.Regular,
GraphicsUnit.Pixel);
tb = new ticButton[intW, intH]; //whenever the variable 'tb' is used it creates a new button that includes the properties of intW and intH
for (int c = 0; c < intW; c++)
{
for (int r = 0; r < intH; r++)
{
tb[c, r] = new ticButton(c, r);
tb[c, r].Left = 150 + (150 * c); //sets the left position of the button
tb[c, r].Top = 150 + (150 * r); //sets the top position of the button
tb[c, r].Width = 150; //sets the width of the button
tb[c, r].Height = 150; //sets the height of the button
tb[c, r].Click += new EventHandler(tb_Click); //registers a new event
tb[c, r].Font = font; //applies any text applied to the button to be the font specified in the font variable
Controls.Add(tb[c, r]); //adds controls to the button
}
}
}
private void reset()
{
for (int c = 0; c < intW; c++)
{
for (int r = 0; r < intH; r++)
{
tb[c, r].Text = "";
tb[c, r].intVal = 0;
}
}
intUser = -1;
}
private void tb_Click(object sender, EventArgs e)
{
ticButton temp = (ticButton)sender;
if (temp.intVal == 0)
{
temp.intVal = intUser;
if (intUser == -1)
{
temp.Text = "X"; //if the button is clicked on the first player, it shows X in the middle of the clicked button
intUser = 1;
}
else
{
temp.Text = "0"; //if the button is clicked on the second player, it shows 0 in the middle of the button
intUser = -1;
}
if (checkWin())
{
MessageBox.Show(temp.Text + " is the winner" + "\n" + "Click OK to reset"); //when there is a winner it displays this message, when clicked OK it will reset
reset(); //refers to the reset variable which resets the game
}
}
}
public bool checkWin()
{
int intRow = 0;
int intCol = 0;
int intTrBl = 0;
int intTlBr = 0;
bool bolWin = false;
for (int c=0;c<intW;c++)
{
for (int r=0;r<intH;r++)
{
intCol += tb[c,r].intVal;
intRow += tb[r,c].intVal;
}
if (intCol == -3 | intCol == 3 | intRow == -3 || intRow == 3)
{
bolWin = true;
}
intRow = 0;
intCol = 0;
intTlBr += tb[c, c].intVal;
intTrBl += tb[intH - 1 - c, c].intVal;
if (intTlBr == -3 || intTlBr == 3 || intTrBl == -3 || intTrBl == 3)
{
bolWin = true;
}
}
return bolWin;
}
}
public partial class ticButton : Button
{
public int intX {private set;get;} //doesn't allow modifications of intX but allows retrieval
public int intY {private set;get;} //doesn't allow modifications of intY but allows retrieval
public int intVal;
public ticButton(int intX, int intY)
{
this.intX = intX;
this.intY = intY;
}
}
}
#hopefully this is indented correctly.
if the comments aren't correct also please give me feedback!
Yiggles and Charlie, hopefully you will respond with any good stuff:)