Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Frogs Game

Rate me:
Please Sign up or sign in to vote.
3.96/5 (14 votes)
21 Aug 2008CPOL6 min read 62.9K   2.1K   16  
In this article, we explain the backtracking algorithm which is a refinement of brute force approach.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GameFrogs
{
    public partial class ATable : UserControl
    {
        Button[] frog = new Button[7];
        public string GetButtonText(int i)
        {
            return frog[i].Text; 
        }
        public void SetButtonText(int i,string k)
        {
            frog[i].Text = k;
        }
        StringBuilder situation = new StringBuilder("LLL RRR", 7);

        public StringBuilder Situation
        {
            get { return situation; }
        }
        EventHandler LegalMove;
        public delegate void Next(int position);
        public event Next NextM;
        public ATable()
        {
            InitializeComponent();
            LegalMove = new EventHandler(MoveB);
            for (int i = 0; i < 7; i++)
            {
                frog[i] = new Button();
                frog[i].Text = situation[i].ToString();
                Repair(i);
                frog[i].Click += LegalMove;
                tLP.Controls.Add(frog[i], i, 1);

            }
        }
        public void Repair(int i)
        {
            if (frog[i].Text == "L")
            {
                frog[i].FlatStyle = FlatStyle.Popup;
                frog[i].FlatAppearance.BorderColor = Color.Green;
                frog[i].FlatAppearance.MouseDownBackColor = Color.Green;
                frog[i].FlatAppearance.MouseOverBackColor = Color.DarkGreen;
                frog[i].BackColor = Color.LightGreen;
            }
            else if (frog[i].Text == "R")
            {
                frog[i].FlatStyle = FlatStyle.Popup;
                frog[i].FlatAppearance.BorderColor = Color.Blue;
                frog[i].FlatAppearance.MouseDownBackColor = Color.Blue;
                frog[i].FlatAppearance.MouseOverBackColor = Color.DarkBlue;
                frog[i].BackColor = Color.LightBlue;
            }
            else
            {
                frog[i].FlatStyle = FlatStyle.Popup;
                frog[i].FlatAppearance.BorderColor = SystemColors.Control;
                frog[i].FlatAppearance.MouseDownBackColor = SystemColors.Control;
                frog[i].FlatAppearance.MouseOverBackColor = SystemColors.Control;
                frog[i].BackColor = SystemColors.Control;
            }
        }

        private void MoveB(object sender, EventArgs e)
        {
            if (((Button)sender).Text == "L")
                MoveFromLeftToRight(situation, tLP.GetColumn((Button)sender));
               
            else
                MoveFromRightToLeft(situation, tLP.GetColumn((Button)sender));
            if (NextM != null)
                this.NextM(tLP.GetColumn((Button)sender));
        }
        private void MoveFromLeftToRight(StringBuilder s, int j)
        {
            //legal move
            //text = situation
            //color=depends on text
            if (j < 6 && frog[j].Text == "L")
            {
                if (s[j + 1] == ' ')
                {
                    s[j + 1] = 'L';
                    s[j] = ' ';
                    frog[j + 1].Text = situation[j + 1].ToString();
                    frog[j].Text = situation[j].ToString(); ;
                    Repair(j);
                    Repair(j + 1);
                    return;
                }
                if (s[j + 1] == 'R' && s[j + 2] == ' ')
                {
                    s[j + 2] = 'L';
                    s[j] = ' ';
                    frog[j + 2].Text = situation[j + 2].ToString();
                    frog[j].Text = situation[j].ToString();
                    Repair(j + 2);
                    Repair(j);
                    return;
                }
            }
            MessageBox.Show("This move is not possible",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);

        }
        private void MoveFromRightToLeft(StringBuilder s, int j)
        {
            //legal move
            if (j > 0 && frog[j].Text == "R")
            {
                if (s[j - 1] == ' ')
                {
                    s[j - 1] = 'R';
                    s[j] = ' ';
                    frog[j - 1].Text = situation[j - 1].ToString();
                    frog[j].Text = situation[j].ToString();
                    Repair(j - 1);
                    Repair(j);
                    return;
                }
                if (s[j - 1] == 'L' && s[j - 2] == ' ')
                {
                    s[j - 2] = 'R';
                    s[j] = ' ';
                    frog[j - 2].Text = situation[j - 2].ToString();
                    frog[j].Text = situation[j].ToString();
                    Repair(j - 2);
                    Repair(j);
                    return;
                }
            }
            MessageBox.Show("This move is not possible",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);

        }
        public void ResetSituation()
        {
            situation = new StringBuilder("LLL RRR");
            for (int i = 0; i < 7; i++)
            {
                frog[i].Text = situation[i].ToString();
                Repair(i);
                tLP.Controls.Add(frog[i], i, 1);

            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Instructor / Trainer Tehniški šolski center Nova Gorica
Slovenia Slovenia
I have a degree in mathematics, but I've worked as a programmer and now I teach programming in high school in Slovenia.

Comments and Discussions