1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Connect Four Game

  1. Jun 14, 2016 #1
    1. The problem statement, all variables and given/known data
    Need help programming the game connect four in C#.

    2. Relevant equations


    3. The attempt at a solution
    Code (C):
           

    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.
     
  2. jcsd
  3. Jun 14, 2016 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Why do you check for the end of the game for each row separately?
    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?
     
  4. Jun 14, 2016 #3

    Mark44

    Staff: Mentor

    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.
     
  5. Jun 15, 2016 #4

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    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.
     
  6. Jun 15, 2016 #5

    Mark44

    Staff: Mentor

    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.
     
  7. Jun 15, 2016 #6
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Connect Four Game
  1. Hashtag game. (Replies: 1)

  2. Four-mesh network (Replies: 3)

  3. Connect Four Games (Replies: 10)

Loading...