Connect Four in C#: Solving Stats Issues

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

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.
 

Related to Connect Four in C#: Solving Stats Issues

1. What is Connect Four?

Connect Four is a two-player strategy game where players take turns dropping colored discs into a 6x7 grid. The goal is to be the first player to connect four discs of your color in a row, column, or diagonal.

2. How is Connect Four played in C#?

In C#, Connect Four is typically played using a console application. The game logic is programmed using loops, conditional statements, and data structures like arrays or lists. The game is played by inputting column numbers to drop the discs and the game board is displayed after each turn.

3. What are the common stats issues when solving Connect Four in C#?

The most common stats issues when solving Connect Four in C# include keeping track of player scores, detecting winning combinations, preventing illegal moves, and handling ties. It is important to carefully plan and test the game logic to ensure all possible scenarios are accounted for.

4. How can I improve the efficiency of my Connect Four program in C#?

To improve the efficiency of your Connect Four program in C#, you can use techniques like minimizing the number of nested loops, using efficient data structures, and optimizing algorithms. It is also important to avoid unnecessary code and regularly debug your program for any errors.

5. Can I incorporate artificial intelligence into my Connect Four program in C#?

Yes, it is possible to incorporate artificial intelligence into your Connect Four program in C#. This can be done by implementing an algorithm that predicts the best possible moves for the computer player based on the current game state. However, this may require advanced programming skills and thorough testing to ensure the AI makes appropriate and challenging moves.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
33
Views
6K
Back
Top