Two TickTackToe questions....

Topics on common programming languages
Post Reply
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Two TickTackToe questions....

Post by Trebor29 » Sun Nov 21, 2010 6:29 pm

Hi,
Im making a TickTackToe game in Visual Studio 2010, C# / .NET -
Has anyone got an idea as to what I can write to make the form resize based on the selected grid size... button size is 50px / space between buttons is 10px... so form needs to resize 60px for every button created .Left and .Top

The form loads at 400 x 400, and I can get it to resize but it either goes smaller :? or massive, taking over my 2 screens and can't seem to be able to tweek it just right! :? Also, Ive put the code inside the button's 'for' loop, thinking I want it to grow with the buttons..!! although thinking about it.. its running that code every time it goes through the loop :!: humm, is this right? or should it be outside the 'for' loop?

Another problem is that when I get it to resize, the groupBox doen't resize, so hiding the buttons anyway!

The other thing was: Where I am selecting a button Image when ButtonClicked, it is not showing the second image on the second clickedButton, but when using clickedButton.Text ("X" "O") it worked fine! :roll: The buttons start with no image so is the 'null' value ive used correct? previously it was clickedButton.Text == " ";

Here's the code, problem areas marked as: ?????? :-S
Thank u :D

Code: Select all

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;
using System.Media;

namespace TickTackToe
{
    public partial class frmTickTackToe : Form
    {
        public frmTickTackToe()
        {
            InitializeComponent();
        }

        private void frmTickTackToe_Load(object sender, EventArgs e)
        {
            cboGridWidth.SelectedIndex = 0;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            int grid1 = int.Parse(cboGridWidth.Text);
            //int grid2 = int.Parse(cboGridHeight.Text);

            Button[,] tickTackButton = new Button[grid1, grid1];

            for (int i = 0; i < grid1; i++)
            {
                for (int x = 0; x < grid1; x++)
                {

                    tickTackButton[i, x] = new Button();
                    tickTackButton[i, x].Name = "tickTackButton" + i + ", " + x;
                    tickTackButton[i, x].Width = 50;
                    tickTackButton[i, x].Height = 50;
                    tickTackButton[i, x].Left = tickTackButton[i, x].Left + tickTackButton[i, x].Width + (i * 60);
                    tickTackButton[i, x].Top = tickTackButton[i, x].Top + tickTackButton[i, x].Top + 50 + (x * 60);
                    tickTackButton[i, x].Text = " ";

                    gbxButtons.Controls.Add(tickTackButton[i, x]);

                    tickTackButton[i, x].Click += new EventHandler(Button_Click);

                    frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(400, 400);
                    frmTickTackToe.ActiveForm.Width = frmTickTackToe.ActiveForm.Width; // ????? :-S
                    frmTickTackToe.ActiveForm.Height = frmTickTackToe.ActiveForm.Height; /// ????? :-S
                }
            }
                    
        }

        private void Button_Click(object sender, EventArgs e)
        {
            SoundPlayer[] sounds = new SoundPlayer[3]; // created soundplayer array of 3 .wav sounds.
            Random random = new Random(); // creates a new random to play sounds randomly.

            sounds[0] = new SoundPlayer(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\1.wav");
            sounds[1] = new SoundPlayer(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\2.wav");
            sounds[2] = new SoundPlayer(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\3.wav");

            sounds[random.Next(0, 3)].Play(); // 

            Button clickedButton = (Button)sender;

            string index = clickedButton.Name.Substring("tickTackButton".Length);

            string targetTextBox = "tickTackButton" + index;

            int myIndex = gbxButtons.Controls.IndexOfKey(targetTextBox);

            Image[] playerImage = new Image[2];

            playerImage[0] = Image.FromFile(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\A.png");
            playerImage[1] = Image.FromFile(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\B.png");

            if (myIndex != -1)
            {
                //MessageBox.Show(index);
                if (clickedButton.Image == null) // ?????? :-S
                {
                    clickedButton.Image = playerImage[0];
                }
                if (clickedButton.Image == playerImage[0])
                {
                    clickedButton.Image = playerImage[1];
                }
                if (clickedButton.Image == playerImage[1])
                {
                    clickedButton.Image = playerImage[0];
                }
            }
 
        }

        private void btnNewGame_Click(object sender, EventArgs e)
        {
            gbxButtons.Controls.Clear();
            frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(400, 400);
        }
    }
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Two TickTackToe questions....

Post by Neo » Sun Nov 21, 2010 10:59 pm

frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(400, 400);
frmTickTackToe.ActiveForm.Width = frmTickTackToe.ActiveForm.Width; // ????? :-S
frmTickTackToe.ActiveForm.Height = frmTickTackToe.ActiveForm.Height; /// ????? :-S
Enough to have,

Code: Select all

frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(x, y);
Also, it should come after the loop. No point in running this for every button.

Code: Select all

x = number_of_buttons * width_of_a_button + constant_horizontal_margins;
y = number_of_buttons * height_of_a_button + constant_vertical_margins;
frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(x, y);
if (clickedButton.Image == null) // ?????? :-S
{
clickedButton.Image = playerImage[0];
}
To be sure, we can set tickTackButton[i, x].Image = null; within the for loop after tickTackButton[i, x].Text = " ";.
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Two TickTackToe questions....

Post by Trebor29 » Mon Nov 22, 2010 3:42 am

Ive sorted the form resize :D by multiplying the comboBox.SelectedIndex by button size and space between the buttons.... so.... (Button 50px & space 10px)=

Code: Select all

            //gbxButtons.Size = gbxButtons + cboGridWidth.SelectedIndex * 60; << Not working!
            frmTickTackToe.ActiveForm.Width = frmTickTackToe.ActiveForm.Width + (cboGridWidth.SelectedIndex * 60);
            frmTickTackToe.ActiveForm.Height = frmTickTackToe.ActiveForm.Height + (cboGridWidth.SelectedIndex * 60);
Cant believe it, ive just been doing that in other applications for another assignment as well!! Cant resize the groupBox now though, lol.. getting an error "Operator '+' cannot be applied to operands of type 'System.Windows.Forms.GroupBox' and 'int'" - Love it.. lol :mrgreen:

When I Debug the program and go through the clickedButton.Image == playerImage[], it reads the first 'if null' and outputs the correct image but then fails to read through the other 'if's', skipping straight to the end of 'if (myIndex != -1)' and continues to fail to read any when going through the program again and again!
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Two TickTackToe questions....

Post by Neo » Mon Nov 22, 2010 9:28 am

frmTickTackToe.ActiveForm.Width = frmTickTackToe.ActiveForm.Width + (cboGridWidth.SelectedIndex * 60);
frmTickTackToe.ActiveForm.Height = frmTickTackToe.ActiveForm.Height + (cboGridWidth.SelectedIndex * 60);
According to what I see, this code is wrong. This will keep on increasing the size of the form rather than adjusting it to fit the set of buttons.

For example:
say initial width=400, height=400

first run with 3x3 buttons,
height = width = 400 + 60x3 = 580

second run with 5x5 buttons,
height = width = 580 + 60x5 = 880 which should be 400 + 60x5 = 700

I think you are clear on the problem. To fix that, you need to use a fix initial size.

See my code in previous post,
x = number_of_buttons * width_of_a_button + constant_horizontal_margins;
y = number_of_buttons * height_of_a_button + constant_vertical_margins;
frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(x, y);

So this will become as below for you (I use 100 as the constant margin),
x = (cboGridWidth.SelectedIndex * 60) + 100;
y = (cboGridWidth.SelectedIndex * 60) + 100;
frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(x, y);

This should work for you. Make sure you adjust the value 100 as required for you.

Code: Select all

frmTickTackToe.ActiveForm.Size = new System.Drawing.Size((cboGridWidth.SelectedIndex * 60) + 100, (cboGridWidth.SelectedIndex * 60) + 100);

Similarly for Group Box,
//gbxButtons.Size = gbxButtons + cboGridWidth.SelectedIndex * 60; << Not working!
Note that I have used 20 as the fixed margin. You need to change that to suit your layout.

Code: Select all

gbxButtons.Size = new System.Drawing.Size((cboGridWidth.SelectedIndex * 60) + 20, (cboGridWidth.SelectedIndex * 60) + 20);
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Two TickTackToe questions....

Post by Trebor29 » Mon Nov 22, 2010 7:11 pm

aaahhhh Neo... bursting my bubble! :lol:

Your code does work better than mine, but I still need to make the (100 margin) 400.. which seems to make it very similar. Any smaller than 400 just shrinks the form.

Not sure about (Width = Height = 400 + 60*3 = 580), doesn't this say the buttons are added on to the end of the form size 400? The form loads at 400 x 400 and the buttons 3x3 appear inside the 400 x 400. The numbers in the comboBox start at 3 (meaning 3 x 3).... through to 10 (10 x 10).

Have I missed something? :? Is there a way I can show you it running?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Two TickTackToe questions....

Post by Neo » Mon Nov 22, 2010 8:19 pm

To be sure, lets discuss base don an image.
form.PNG
form.PNG (12.34 KiB) Viewed 10904 times
Now we can write simple formulas for Group box width and height as below. Note that draw position of the left-top button is (a1, b1).

N - number_of_buttons_per_side

gbox_width = a1 + a2 + (p1 * N) + p2 * (N-1)
gbox_height = b1 + b2 + (q1 * N) + q2 * (N-1)

Similarly, we can write formulas for form,

form_width = w1 + w2 + gbox_width
form_height = h1 + h2 + gbox_height

Note: p1, p2, q1, q2, (a1 + a2), (b1 + b2), (w1 + w2) and (h1 + h2) are all constants.

These values will change for given N from your combo box.

Can you implement this, so the logic will become clear?
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Two TickTackToe questions....

Post by Trebor29 » Mon Nov 22, 2010 8:55 pm

:shock: I maybe some time with this...! :)
Post Reply

Return to “.Net & Other Programming”