0
\$\begingroup\$
            List<int> boardmove = new List<int>();
            boardmove.Add(0);
            boardmove.Add(1);
            boardmove.Add(2);
            boardmove.Add(3);
            boardmove.Add(4);
            boardmove.Add(5);
            boardmove.Add(6);
            boardmove.Add(7);
            boardmove.Add(8);

            int rand = rnd.Next(1, count);

            int num_one = boardmove[rand];

            boardmove.RemoveAt(num_one);

            count--;

I am trying to implement a computer player for my tic tac toe game. I am using a list to move the computer player, I want to index in the list and then remove the value I have selected. I want to select a unique value from the list each time.

            List<int> boardmove = new List<int>();
            boardmove.Add(0);
            boardmove.Add(1);
            boardmove.Add(2);
            boardmove.Add(3);
            boardmove.Add(4);
            boardmove.Add(5);
            boardmove.Add(6);
            boardmove.Add(7);
            boardmove.Add(8);

            int selectedIndex = rnd.Next(0, boardmove.Count);

            int selectedMove= boardmove[selectedIndex];
            Console.WriteLine(selectedMove);

            boardmove.RemoveAt(selectedIndex);
            Console.WriteLine(selectedIndex);

enter image description here

\$\endgroup\$
3
\$\begingroup\$

You're close, but you want to make a few small changes:

int rand = rnd.Next(1, count);

Let's use more descriptive variable names, following camel-casing conventions, and remember that list indexing starts at zero. We don't need a separate count variable because the List<T> has one built-in:

int selectedIndex = random.Next(0, boardMove.Count);

Then

int num_one = boardmove[rand];

becomes:

int selectedMove = boardMove[selectedIndex];

And finally, here we're trying to remove from an indexed location, but passing a value from that index instead of the index itself.

boardmove.RemoveAt(num_one);

We can fix that as either:

boardMove.Remove(selectedMove);

or (slightly faster)

boardMove.RemoveAt(selectedIndex);

Just make sure you're matching apples to apples. Methods that want an index should be passed an index. Methods that want a value should be passed a value.


Also, make sure you're not running this code back-to-back the way you've shown it here. I assumed you'd just snipped out the relevant bits for brevity, but just in case, you'll want your code to be in at least three parts:

// 1. Declare a member variable to hold your list of remaining moves,
//    so it can persist between turns. Prep its capacity to 9 entries.
List<int> boardMove = new List<int>(9);

// 2. Have a dedicated method to put stuff in order when starting a new game.
void StartGame() {
    // Erase any remaining moves left-over from last game.
    boardMove.Clear();

    // Prep the moves list with one of each move.
    for(int i = 0; i < 9; i++)
        boardMove.Add(i);

    // Do other stuff like setting up the board / etc...
}

// 3. Have a function to choose a random move when you need one.
//    This just picks from the existing list, it doesn't make a new one.
int SelectRandomMove() {
    int selectedIndex = random.Next(0, boardMove.Count);
    int selectedMove = boardMove[selectedIndex];
    boardMove.RemoveAt(selectedIndex);
    return selectedMove;
}
\$\endgroup\$
1
\$\begingroup\$
            Random rnd = new Random();

        List<int> boardmove = new List<int>(9);
        for (int i = 1; i < 10; i++)
            boardmove.Add(i);

        while (gameOver==false)
        {
            Console.Write("Enter X spot (1-9): ");
            int num = Convert.ToInt32(Console.ReadLine());

            int selectedIndex = rnd.Next(1, boardmove.Count);

            int selectedMove= boardmove[selectedIndex];
            Console.WriteLine(selectedMove);

            boardmove.RemoveAt(selectedIndex);

I have figured out my problem by moving my list initialization before my while loop

\$\endgroup\$
  • \$\begingroup\$ one final question about my game, the O's overwrite my X's on my board. I am unsure of how to compare X's to O's so they do not repeat themselves. also I do get the values of O's to be unique to the list. \$\endgroup\$ – gamer67 Jan 24 at 22:25
  • \$\begingroup\$ When X plays, remove their move selection (num) from boardMove so that space they chose is no longer eligible for O to pick. Also, please get in the habit of naming your variables with something meaningful. num tells us nothing about the variable except that it's a number — something we already know anyway because it's an int. Calling it selectedMoveX or playerSelectedMove in contrast to selectedMoveO or opponentSelectedMove makes your code much more clear. \$\endgroup\$ – DMGregory Jan 25 at 13:07
0
\$\begingroup\$
        Random rnd = new Random();

        List<int> boardmove = new List<int>(9);
        for (int i = 1; i < 10; i++)
            boardmove.Add(i);

        while (gameOver==false)
        {
            Console.Write("Enter X spot (1-9): ");
            int selectedMoveX= Convert.ToInt32(Console.ReadLine());

            int selectedIndex = rnd.Next(1, boardmove.Count);

            int selectedMoveO= boardmove[selectedIndex];

            boardmove.RemoveAt(selectedMoveX);
            boardmove.RemoveAt(selectedIndex);

            switch (selectedMoveX)

this code works correctly I am posting so that I can help others. is there anyway I can make the X input more bulletproof.

\$\endgroup\$
  • \$\begingroup\$ Please consider editing your existing answer instead of posting a new one. If you want to ask a question about your X input, use the "Ask Question" button instead. \$\endgroup\$ – DMGregory Jan 26 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.