• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 80
  • Last Modified:

Array Question

Hi,

I am making a C# Tic Tac Toe game. There is no player interaction, simply populate values and show the winner.

I have my board with 9 labels (lblVal1, lblVal2, etc...)

This is my array code here, I am lost and could use a kick in the right direction.
- How to populate the labels
- How do Identify the winner?

private void newGameButton_Click(object sender, EventArgs e)
        {

            const int ROW = 3;
            const int COL = 3;

            int[,] values = new int[ROW, COL];

            Random rand = new Random();

            for(int row = 0; row < ROW; row++)
            {
                for(int col = 0; col < COL; col++)
                {
                    values[row, col] = rand.Next(1);
                }
            }
            for (int row = 0; row < ROW; row++)
            {
                for (int col = 0; col < COL; col++)
                {
                    
                }
            }

Open in new window


Thanks!!
0
Computer Guy
Asked:
Computer Guy
  • 9
  • 5
  • 2
  • +2
1 Solution
 
Computer GuyAuthor Commented:
Thanks. Didn't realize that.
0
 
Computer GuyAuthor Commented:
Hi,

I have done more work on this and have populated the labels with values of 0 and 1.

I have a feeling that this is wrong because the numbers don't always match up.

Can someone please give me a kick in the right direction on how to make this work properly?

Also, how do I change the 0 to O and 1 to X?

Thanks!

     const int ROW = 3;
            const int COL = 3;

            int[,] values = new int[ROW, COL];

            Random rand = new Random();

            for(int row = 0; row < ROW; row++)
            {
                for(int col = 0; col < COL; col++)
                {
                    values[row, col] = rand.Next(2);
                    lbl01.Text = values[0, 0].ToString();
                    lbl02.Text = values[0, 1].ToString();
                    lbl03.Text = values[0, 2].ToString();

                    lbl04.Text = values[1, 0].ToString();
                    lbl05.Text = values[1, 1].ToString();
                    lbl06.Text = values[1, 2].ToString();

                    lbl07.Text = values[2, 0].ToString();
                    lbl08.Text = values[2, 1].ToString();
                    lbl09.Text = values[2, 2].ToString();
                }
            }

Open in new window

0
 
mlmccCommented:
One way to convert the values to X and O is to use another array

Basic idea.  You can fix the syntax since I am not a C# programmer

char[1] charvalues = new char[1];
charvalues [0] = 'O';
charvalues [1] = 'X';

Change your label assignments to

lbl01.Text = charvalues [values[0,0]];

mlmcc
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Shaun KlineLead Software EngineerCommented:
Unless you are changing the rules of tic tac toe, the logic you are using is flawed. In traditional tic tac toe, X and O take turns. So your random routine should be used to select a cell on the board, not to decide whether an X or O goes into the cell. Using that method, you could end up with a board with all X's or all O's.

So, you could instead use a one dimensional array of length 9 and randomly fill the array alternating between 0 (for X) and 1 (for O). Once all nine elements has a value, you can assign each of the nine values to a cell on your board, starting in the upper left (position 0,0) and going through to the bottom right (position 2,2) switching the 0 and 1 to X and O, respectively.

If you need to ensure only one winner, you will want to "check" each row/column/diagonal to see if either X or O won before moving to the next iteration of the loop.
0
 
David Johnson, CD, MVPOwnerCommented:
123
456
789

Winning Combinations 1,2,3; 4,5,6; 7,8,9; 1,4,7;2,5,8; 3,6,9; 1,5.9; 3,5,7
0
 
ambienceCommented:
To give a realistic visualization of the game - in addition to what Shaun has said above - use random number to select a vacant cell, instead of filling starting from the upper left (0,0) and so on.

It often helps to breakdown the logic into small methods and combining them in an intuitive manner. Just for example

int SelectRandomVacantCell() {
   // write code here to return a cell that is empty
}

enum Player { X, O };

void MakeMove(int cell, Player player) {
  // Update array
}

Player OtherPlayer(Player player) {
   return player == Player.X ? Player.O:Player.X;
}

enum GameStatus { XWon, OWon, Draw, InProgress };
GameStatus CheckGameStatus() {
  // Check for a win, draw or In-progress.
}

bool IsGameOver() {
  return CheckGameStatus() != GameStatus.InProgress;
}

//////////////////////////

Player playerToMove = Player.X;
while (!IsGameOver())
{
       int cell = SelectRandomVacantCell();
       MakeMove(cell, playerToMove);
       playerToMove= OtherPlayer(playerToMove);
}

Console.Write("Game Over, result = {0}", CheckGameStatus());

//////////////////////////

This is pretty structured at the moment, but you are encouraged to use classes and methods as appropriate.
0
 
Computer GuyAuthor Commented:
This is just to demo a 2d array. I need to populate x and o at once and then say who wins
0
 
Computer GuyAuthor Commented:
Hi,

Tried playing with this more. Again, I just click one button and all values will be generated and the winner will be determined.

- The numbers get generated and the labels get filled with the values just fine here, though is the the best way to do it?

- I still can't convert the array values from 0 and 1 to O to X.

- Also, how do I figure out the winner, I get this concept (Winning Combinations 1,2,3; 4,5,6; 7,8,9; 1,4,7;2,5,8; 3,6,9; 1,5.9; 3,5,7) just don't know how to apply it to the code.

How can I accomplish this



            // declare array size
            const int ROW = 3;
            const int COL = 3;
            int[,] values = new int[ROW, COL];

            // random number generators
            Random rand = new Random();

            // loop to create values in array
            for(int row = 0; row < ROW; row++)
            {
                for(int col = 0; col < COL; col++)
                {
                    values[row, col] = rand.Next(2);

                    // change 0,1 to X,O
                    char[] charvalues = new char[1];
                    charvalues [0] = 'O';
                    charvalues [1] = 'X';

                    // fill labels
                   lbl01.Text = charvalues[values[0,0]].ToString(); // test to replace char
                   // lbl01.Text = values[0, 0].ToString();
                    lbl02.Text = values[0, 1].ToString();
                    lbl03.Text = values[0, 2].ToString();

                    lbl04.Text = values[1, 0].ToString();
                    lbl05.Text = values[1, 1].ToString();
                    lbl06.Text = values[1, 2].ToString();

                    lbl07.Text = values[2, 0].ToString();
                    lbl08.Text = values[2, 1].ToString();
                    lbl09.Text = values[2, 2].ToString();
                }
            }
        }

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
Did lbl01.Text not get filled in with either an X or O? If it did, just make that change for your other labels.

For the winning combinations, you need to check if the same value exists in each cell of any row, in each cell of any column, or in each cell of the diagonals. From David Johnson's comment, cell 1 in you array is [0,0], cell 2 is [0,1] cell 3 is [0,2] cell 4 is [1,0], and so forth.
0
 
Computer GuyAuthor Commented:
For the first line of code where I try to change the labels from 0,1 to x,o (commented out) program crashes.

For winning how is this for logic
If
0,0 & 0,1 & 0,2 all = 0 or 1 then 0 or 1 is winner.

But for every possibility?
0
 
Shaun KlineLead Software EngineerCommented:
What is the error that you received? My guess is something to do with an index out of range. Your charvalues array is dimensioned to only have one element. You need to change it to be char[2].
0
 
Computer GuyAuthor Commented:
Yea that makes sense.

How about determining the winner part?
0
 
Shaun KlineLead Software EngineerCommented:
To find the winner, you can check if the three cells in each way to win are equal to each other:

if(values[0,0] == values[0,1] && values[0,1] == values[0,2])
  winner = charvalues[values[0,0]];
else if (values[1,0] == values[1,1] && values[1,1] == values[1,2])
  winner = charvalues[values[1,0]];

repeating for each possible way to win. The one issue you may find is that the method in which you are filling your values array could result in both X and O having a winning row/column.
0
 
Computer GuyAuthor Commented:
Thanks!

How do you suggest I re-do that method so I don't run into that issue?
0
 
David Johnson, CD, MVPOwnerCommented:
after every move you have to check for a winner. or you could end up with
x00
x0x
x0x where both x and 0 are winning combinations. x gets 5 moves and 0 gets 4 moves, you can speed things up slightly by only checking from x's move 3 onwards.
Whether or not you use a 2d array or a 1d array is up to you. A 1D array makes it slightly simpler and saves calling rnd() once for each row/column.
0
 
Computer GuyAuthor Commented:
Hi,

I've done this so far, I want to declare a tie if say 0,0, 0,1, 0,2 & 0,0, 1,0, 2,0 are the same char.

Any ideas with that one?
0
 
Computer GuyAuthor Commented:
                    // Horizontal
                   if (values[0,0] == values[0,1] && values[0,1] == values[0,2])
                       winnerLabel.Text = charvalues[values[0,0]].ToString() + " wins";
                   else if (values[1,0] == values[1,1] && values[1,1] == values[1,2])
                       winnerLabel.Text = charvalues[values[1, 0]].ToString() + " wins";
                   else if (values[2,0] == values[2,1] && values[2,1] == values[2,2])
                       winnerLabel.Text = charvalues[values[2,0]].ToString() + " wins";

                    //Vertical
                   if (values[0, 0] == values[1,0] && values[1,0] == values[2,0])
                       winnerLabel.Text = charvalues[values[0,0]].ToString() + " wins";
                   else if (values[0,1] == values[1,1] && values[1,1] == values[2,1])
                       winnerLabel.Text = charvalues[values[0,1]].ToString() + " wins";
                   else if (values[0,2] == values[1,2] && values[1,2] == values[2,2])
                       winnerLabel.Text = charvalues[values[0,2]].ToString() + " wins";

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
If you wish to avoid the issue of having multiple winners, you need to change how you are filling in your 2D array. Instead of looping through your 2D array, row by row, column by column, you need to randomly select one of the cells in the array. This would best be done by using one loop that runs 9 times. Each time through the loop, you will fill in the randomly selected cell in your 2D array.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now