Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im making a 4 in a row game in C# Visual Studio. I have 7 rows and 49 colons, Named the rows ABCDEFG, and on row A its liks A1-A7 - B2-7 etc. But im getting a winner after 1 row of full X wins like 7 x = wins.i want a winner after 4 symbols. im new at this hope somebody can help me :) sorry for my bad explaination and english, just feel free to ask, here is the code:

C#
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 _4PåRad
{
    public partial class Form1 : Form
    {
        bool turn = true; //true = x tur; false = Y tur
        int turn_count = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void beskrivelseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Laget Av Petar", "Fire på rad");
        }

        private void lukkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
                b.Text = "X";
            else
                b.Text = "O";

            turn = !turn;
            b.Enabled = false;

            checkforwinner();
        }

        private void checkforwinner()
        {
            bool there_is_a_winner = false;
            //Horisontol
            if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (A3.Text == A4.Text) && (A4.Text == A5.Text) && (A5.Text == A6.Text) && (A6.Text == A7.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (B3.Text == B4.Text) && (B4.Text == B5.Text) && (B5.Text == B6.Text) && (B6.Text == B7.Text) && (!B1.Enabled))
                there_is_a_winner = true;
            else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (C3.Text == C4.Text) && (C4.Text == C5.Text) && (C5.Text == C6.Text) && (C6.Text == C7.Text) && (!C1.Enabled))
                there_is_a_winner = true;
            else if ((D1.Text == D2.Text) && (D2.Text == D3.Text) && (D3.Text == D4.Text) && (D4.Text == D5.Text) && (D5.Text == D6.Text) && (D6.Text == D7.Text) && (!D1.Enabled))
                there_is_a_winner = true;
            else if ((E1.Text == E2.Text) && (E2.Text == E3.Text) && (E3.Text == E4.Text) && (E4.Text == E5.Text) && (E5.Text == E6.Text) && (E6.Text == E7.Text) && (!E1.Enabled))
                there_is_a_winner = true;
            else if ((F1.Text == F2.Text) && (F2.Text == F3.Text) && (F3.Text == F4.Text) && (F4.Text == F5.Text) && (F5.Text == F6.Text) && (F6.Text == F7.Text) && (!F1.Enabled))
                there_is_a_winner = true;
            else if ((G1.Text == G2.Text) && (G2.Text == G3.Text) && (G3.Text == G4.Text) && (G4.Text == G5.Text) && (G5.Text == G6.Text) && (G6.Text == G7.Text) && (!G1.Enabled))
                there_is_a_winner = true;


            if (there_is_a_winner)
            {
                String winner = "";
                if (turn)
                    winner = "O";
                else
                    winner = "X";

                MessageBox.Show(winner + " Wins!", "Yay!");
            }
        }
    }
}
Posted

1 solution

Ok, there are a few problems there (and they are "columns" not "colons" - a "colon is a very different thing altogether :O )

What I assume you mean is that you have 7 rows by 7 columns, and you have implemented them as Button objects and named them A1, A2, ... B1, B2, ...
The first thing to notice is that that is a real pain to work with - because you have to use the names for each button when you check them.

Instead of adding all your buttons at design time, add them at run time, and use an array instead:
C#
private Button[,] theBoard = new Button[7, 7];

Then, in your form constructor, fill the board:
C#
int y = 10;
for (int i = 0; i < 7; i++)
    {
    int x = 10;
    for (int j = 0; j < 7; j++)
        {
        Button b = new Button();
        b.Top = y;
        b.Left = x;
        b.Width = 20;
        b.Text = "";
        b.Click += new EventHandler(button_click);
        Controls.Add(b);
        x += 25;
        }
    y += 25;
    }
Now, you can check your board by referring to X,Y coordinates - which means you can write a method for checking all directions from a point, and use that for each coordinate on the board instead of naming them explicitly. There are 8 directions, and each is a combination of the current point plus or minus an x and y offset or 1, 0, or -1. Which means you can use another method that checks for a match of four identical objects given a point and two offsets, then call that 8 times.

Give it a try: it's going to be a lot easier than your way, very, very quickly!
 
Share this answer
 
Comments
Petar Sertic 6-Dec-14 9:28am    
Sorry but i think its harder your way :p been trying to get a 4 in a row to work for a 1 month now. trying to se if somebody else have made one but nobody has made 4 in a row :/ i have 1 more but this code is in norwegian i but i gave it up. :(
OriginalGriff 6-Dec-14 9:55am    
You would think it was harder, wouldn't you? :laugh:
But that turns out not to be the case.
Think about it: how do you check if four items in a horizontal row are the same?
With your way, you have to check all the possible combinations:
A1+A2+A3+A4, A2+A3+A4+A5, A3+A4...
B1+B2+B3+B4, B2+...
C1...
And for verticals, again you have to specify each possible combination:
A1+B1+C1+D1, B1+C1+D1+...
A2+B2+...
...
And then you have to check the diagonals as well:
A1+B2+C3+D4, A2+B3+...
B1+C2+...
...
That's a lot of combinations! And if you want to expand this to "Match 5" or an 8 x 8 grid, you have a lot of work to do.

Writing a method which checks four "cells" given a start point, and X & Y offsets is pretty simple!
Then it works for whatever grid size you use, and it's trivial to make it match 5, or three instead of four.
And you just call that in a loop looking for a positive.

Try it: it really is easier than what you are doing - even if it seems like more work. (Trust me, I'm not a doctor - but I've done this kind of thing loads of times before)
Petar Sertic 6-Dec-14 11:21am    
Ok 1 one question if i wanted to go with my code, how can i get a winner after 4 in row instead of 7? mean like 4 = x = win and not 7? our is it to hard with my code?
OriginalGriff 6-Dec-14 11:39am    
"is it to hard with my code?"
Yes. Well, no, it's not *hard*. It's just a huge amount of typing... Literally.
Look at the comment I made above: "you have to check all the possible combinations"
That means you have to write a complete "if" statement for each of the sections between the commas:
if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (A3.Text == A4.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A2.Text == A3.Text) && (A3.Text == A4.Text) && (A4.Text == A5.Text) && (!A2.Enabled))
there_is_a_winner = true;
...
There are four of those per row (and seven rows), then there are the verticals, and the diagonals - do you see what I mean? And it's horribly easy to make a mistake and makes your eyes water trying to find it... :laugh:

It really is simpler to do it the "proper" way, honest!
Petar Sertic 6-Dec-14 13:02pm    
i see gonna take to loong, maybe im giving up on this game. i cant find nobody make 4 in a row to se what they are doing, but have you every made that kind of a game?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900