Click here to Skip to main content
15,892,298 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 63K   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.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GameFrogs
{
    public partial class Frogs : Form
    {
        string goal = "RRR LLL";
        public Frogs()
        {
            InitializeComponent();
        }

        private void aTable1_NextM(int position)
        {
            if (TheEnd())
                MessageBox.Show("Congratulations! You won!",
                                "The end",
                                 MessageBoxButtons.OK,
                                 MessageBoxIcon.Information);
            else
                if (NoMovePossible())
                {
                    MessageBox.Show("No more moves are possible\n",
                                     "The end",
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Information);
                    //reset game
                    aTable1.ResetSituation();

                }
        }
        private bool TheEnd()
        {
            return (aTable1.Situation.ToString() == goal);
        }
        private bool NoMovePossible()
        {
            for (int i = 0; i < 6; i++)
            {
                if (aTable1.Situation[i] == 'L' && aTable1.Situation[i + 1] == ' ')
                    return false;
                if (i < 5)
                {
                    if (aTable1.Situation[i] == 'L' & aTable1.Situation[i + 1] == 'R' &
                        aTable1.Situation[i + 2] == ' ')
                        return false;
                }
            }
            for (int i = 6; i > 0; i--)
            {
                if (aTable1.Situation[i] == 'R' && aTable1.Situation[i - 1] == ' ')
                    return false;
                if (i > 1)
                {
                    if (aTable1.Situation[i] == 'R' & aTable1.Situation[i - 1] == 'L' &
                        aTable1.Situation[i - 2] == ' ')
                        return false;
                }
            }

            return true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            aTable1.ResetSituation();
        }

        private void btnSolution_Click(object sender, EventArgs e)
        {
            Solutions a = new Solutions();
            a.Show(this);
        }
    }
}

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