Click here to Skip to main content
15,436,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so I'm making this project in Windows forms (Visual Studio 2013, C#). Which method would you recommend for adding a feature which would allow players to move freely between turns?

I am stuck at the part where some other button (than the X/O-able) is supposed to go one turn back. Said button should identify the two last playable buttons pressed, but this variable changes obviously each turn and I can't seem to get a hang on how to do it.

I can write what the button is supposed to do, just not where.

here's my turn routine:

C#
public partial class Form1 : Form
    {
        double q = 0;
        bool turn = true; //true = X turn, false = Y turn
        int turn_count = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
                b.Text = "X";
            else
                b.Text = "O";

            b.Enabled = false;
            turn_count++;

            turn = !turn;

            CheckForWinner();
            
        }


I have spent plenty of hours in individual research in here, youtube, google generally but it seems noone has tried to explain this, or I couldn't identify it because I have just started programming as a hobby. I am sure though that noone posted question concerning button-identifying and TTT combined. Thanks for help! :-)
Posted
Updated 15-Mar-15 8:14am
v2

OK, If I understand the question correctly, you are basically wanting to create an "Un-DO" button, that will take you back to before the LAST move?

Create another partial class that stores each move.
Then set the LastMove details after each move.
Then, you can go back to those values!

Maybe not the way someone else would do it, but I would try something like this.
C#
public partial class Form1 : Form
    {
        double q = 0;
        bool turn = true; //true = X turn, false = Y turn
        int turn_count = 0;
        public LastMove lMove = new LastMove();

        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
                b.Text = "X";
            else
                b.Text = "O";
            b.Enabled = false;
            turn_count++;
            lMove.turn = turn;
            lMove.button = b;
            lMove.tCount = turn_count;
            turn = !turn;
            //CheckForWinner();
        }

        public void UnDo_Button_click(object sender, EventArgs e)
        {
            turn = !turn;  // Sets back to the previous player
            lMove.button.Text = ""; // Clears the Text from the button
            lMove.button.Enabled = true; // Sets button back to Enabled
        }
    }

    public partial class LastMove
    {
        public bool turn { get; set; }
        public Button button { get; set; }
        public int tCount { get; set; }
    }
 
Share this answer
 
Comments
Member 11520771 18-Mar-15 17:27pm    
Awesome!! Thanks so much, I can't believe stackoverflow couldn't digest that :D
Or you could even store every move by using an Array.

Then you could do something like this:

public partial class Form1 : Form
   {
       double q = 0;
       bool turn = true; //true = X turn, false = Y turn
       int turn_count = 0;
       public LastMove[] lMove = Enumerable
           .Range(0, 9)
           .Select(i => new LastMove())
           .ToArray();

       public Form1()
       {
           InitializeComponent();
       }

       private void exitToolStripMenuItem_Click(object sender, EventArgs e)
       {
           Application.Exit();
       }

       public void button_click(object sender, EventArgs e)
       {
           Button b = (Button)sender;
           if (turn)
               b.Text = "X";
           else
               b.Text = "O";
           b.Enabled = false;
           lMove[turn_count].turn = turn;
           lMove[turn_count].button = b;
           lMove[turn_count].tCount = turn_count;
           turn_count++;
           turn = !turn;
           //CheckForWinner();
       }

       public void UnDo_Button_click(object sender, EventArgs e)
       {
           turn_count = turn_count - 1;
           if (turn_count >= 0)
           {
               turn = !turn;  // Sets back to the previous player
               lMove[turn_count].button.Text = ""; // Clears the Text from the button
               lMove[turn_count].button.Enabled = true; // Sets button back to Enabled
           }
       }
   }

   public partial class LastMove
   {
       public bool turn { get; set; }
       public Button button { get; set; }
       public int tCount { get; set; }
   }
 
Share this answer
 
Comments
Member 11520771 18-Mar-15 17:27pm    
Awesome!! Thanks so much, I can't believe stackoverflow couldn't digest that :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900