Connect Four in C#: Solving Stats Issues

  • Thread starter Veronica_Oles
  • Start date
  • Tags
    Game
Click_1(object sender, EventArgs e) { for (int i = 4; i > -1; i--) { if (board[i, 2] == 0)
  • #1

Homework Statement


Need help programming the game connect four in C#.

Homework Equations




The Attempt at a Solution


Code:
private Boolean check_winner_v()
        {

            Boolean winner = false;

            for (int i = 0; i < 7; i++)
            {

                for (int j = 0; j < 3; j++)
                {

                    if (board[j, i] == 1 && board[j + 1, i] == 1 && board[j + 2, i] == 1 && board[j + 3, i] == 1 || board[j, i] == 2 && board[j + 1, i] == 2 && board[j + 2, i] == 2 && board[j + 3, i] == 2)
                    {

                        winner = true;

                        if (board[j, i] == 1)
                        {
                            player1_wins = 1;
                            games++;
                           
                           
                        }

                        else
                        {
                            player2_wins = 2;
                            games++;
                        }



                    }

                }
            }
            return winner;

        }
        private void button17_Click_1(object sender, EventArgs e)
        {
            for (int i = 5; i > -1; i--)
            {

                if (board[i, 1] == 0)
                {
                    if (turn == 0)
                    {
                        board[i, 1] = 1;
                        two[i].BackColor = Color.Red;
                        turn = 1;
                        row2++;
                    }
                    else
                    {
                        board[i, 1] = 2;
                        two[i].BackColor = Color.Yellow;
                        turn = 0;
                        row2++;
                    }

                    two[i].Visible = true;
                    i = -1;
                }

            }

            if (row2 == 6)
            {

                button17.Enabled = false;
                button17.Text = "Full";
            }

            textBox1.Text = check_winner_v().ToString();
            textBox2.Text = check_winner_h().ToString();

            if (winner == false && turn == 42)
            {
                textBox5.Text = tie.ToString();
                tie++;

            }

         
        }

        private void button68_Click(object sender, EventArgs e)
        {
            init_board();

            for (int i = 0; i < 6; i++)
            {

                one[i].Visible = false;
                two[i].Visible = false;
                three[i].Visible = false;
                four[i].Visible = false;
                five[i].Visible = false;
                six[i].Visible = false;
                seven[i].Visible = false;
            }
            row1 = 0;
            row2 = 0;
            row3 = 0;
            row4 = 0;
            row5 = 0;
            row6 = 0;
            row7 = 0;
            turn = 0;
           
            games++;

            textBox1.Text = "";
            textBox2.Text = "";

            button28.Enabled = true;

            button16.Enabled = true;
            button16.Text = "";
            button17.Enabled = true;
            button17.Text = "";
            button18.Enabled = true;
            button18.Text = "";
            button19.Enabled = true;
            button19.Text = "";
            button8.Enabled = true;
            button8.Text = "";
            button20.Enabled = true;
            button20.Text = "";
            button21.Enabled = true;
            button21.Text = "";

        }


I'm having trouble programming the statistics. My stats do not work:

For when the player tie it for some reason will not tally up the tie and this is the part that should tally it up but it does not.
if (winner == false && turn == 42)

{

textBox5.Text = tie.ToString();

tie++;



}


Games does not want to increment either, which is incremented in the method of check_winner_v.

I did not give entire program only parts that I thought were necessary.
 
Physics news on Phys.org
  • #2
Why do you check for the end of the game for each row separately?
Veronica_Oles said:
if (winner == false && turn == 42)
Did you check which condition fails? Is it winner==false or turn==42?

Is the variable "winner" global? (Don't do that)
If yes, the second call of the winner function will override the result of the first one.
If no, where does the variable come from?
 
  • #3
This line near the top of your code really stands out, but not in a good way. I have no way of telling what it's supposed to be doing, since there are no comments to help a reader understand its purpose. You have all of these Boolean expressions, with some connected with && and some connected with ||. If board[j, i] is true, the whole expression is true, regardless of the truth values of all of the other expressions.
Veronica_Oles said:
C:
if (board[j, i] == 1 && board[j + 1, i] == 1 && board[j + 2, i] == 1 && board[j + 3, i] == 1 || board[j, i] == 2 && board[j + 1, i] == 2 && board[j + 2, i] == 2 && board[j + 3, i] == 2)

If I were grading this program, I would take off points for 1) lack of comments, and 2) unhelpful variable names (e.g. the arrays named one, two, three and so on, and buttonX and textBoxY), even if the logic in the program was correct.
 
  • #4
Mark44 said:
If board[j, i] is true, the whole expression is true, regardless of the truth values of all of the other expressions.
Why? I would expect the expression to be interpreted as

if ( ((board[j, i] == 1) && (board[j + 1, i] == 1) && (board[j + 2, i] == 1) && (board[j + 3, i] == 1)) || ((board[j, i] == 2) && (board[j + 1, i] == 2) && (board[j + 2, i] == 2) && (board[j + 3, i] == 2)))

In other words, if (field j,i is held by player 1 AND field j+1,j is held by player one AND ... AND ...) OR (those four fields are held by player 2), then the expression is true. Could get some comment about rows/lines and the meaning of 1 and 2, could get more brackets, but I don't think that is so bad.
 
  • #5
mfb, you're right. Still, with such a long expression, adding parentheses as you did, or better yet, breaking it up into smaller chunks would help the reader comprehend what's going on.
 
  • #6
Mark44 said:
This line near the top of your code really stands out, but not in a good way. I have no way of telling what it's supposed to be doing, since there are no comments to help a reader understand its purpose. You have all of these Boolean expressions, with some connected with && and some connected with ||. If board[j, i] is true, the whole expression is true, regardless of the truth values of all of the other expressions.

If I were grading this program, I would take off points for 1) lack of comments, and 2) unhelpful variable names (e.g. the arrays named one, two, three and so on, and buttonX and textBoxY), even if the logic in the program was correct.
I am well aware of the fact I have to add comments I have not gotten to that yet just trying to figure out the stats first.
 

Suggested for: Connect Four in C#: Solving Stats Issues

Replies
7
Views
792
Replies
15
Views
715
Replies
4
Views
651
Replies
8
Views
1K
Replies
2
Views
681
Replies
3
Views
758
Replies
3
Views
573
Replies
10
Views
1K
Replies
1
Views
429
Back
Top