Register
Login 




New Topic Post Reply  [ 1 post ] 
  Print view
Previous topic | Next topic 
Author Message
Offline 
 Post subject: Another project...
PostPosted: Wed Jan 25, 2017 5:38 am 
User avatar
Staff
Legend
Steam ID: STEAM_0:1:221617357
Joined: Thu May 19, 2016 9:30 am
Posts: 2168
Location: United Kingdom
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:)
BRITNEY SPEARS killed <>< with knife.

Image

Spoiler: Show


Spoiler: Show

Spoiler: Show


1. Join EgN [ X ]
2. Become a Recruit [ X ]
3. Become an EgN Member [ X ]
4. Become a Admin [ X ]
5. Become an Elite [ X ]
6. Become a Veteran [ X ]
7. Become a part of Events Team [ X ]
8. Become a Manager [ ]
9. Become a Staff [ X ]
10. Become an Advisor [ ]
11. Become the next EgN Leader! [ ]

EgN| Mr Geeza™ : Why do I always get you and sinister mixed up lol sorry xD
EgN-S| Phillip: Cause you a racist

Jafari asks the wall: What is the meaning of life?
The wall responds with: Kill yourself. DUN DUN DUUUUUUUUUUN
EgN-S| Mr.GoldGames: I asked for a demotion, where's my demotion at?
1 month later....
The following people have been recognised for their service to the community:

Veteran to Staff
Mr.GoldGames

EgN-S| Mr.GoldGames: I ask for a demotion but I got a promotion instead? Life sucks sometimes.


Top
 Profile  
 Social 
Display posts from previous:  Sort by  
New Topic Post Reply  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  

cron
Powered by phpBB3 ©
Website mods by Doldol, banner by 3n19ma, synthic, Mootiny.