Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 17
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
All today's birthdays', congrats!
Chris (39)
JamesH (17)
Oakley (21)


Next birthdays
11/27 Dax (42)
11/27 Mino (49)
11/29 Sonic (58)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Tiger Lights Out Algorithm....

Move Thread LAN_403
Slava
Fri Mar 04 2016, 01:50AM Print
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
I am currently trying to write an algorithm that solves the lights out game...

I am writing it in C# ... the idea is this...

start at the top and toggle the light under the one that is turned on. Work your way down to the bottom. If the pass doesn't solve the puzzle start at the top again with a random light at the top turned on.

Here is a web based version of the game to try it out. Link2

Here is my C# code... (Yes, I can make a "DidWin()" function faster but it works for now, the game should solve in only a few iteration...)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace LightsOut002
{
    class Program
    {
        static void Main(string[] args)
        {

            LightsOutBoard lo1 = new LightsOutBoard();
            lo1.Grid[1, 2] = true;
            lo1.Grid[2, 2] = true;
            lo1.Grid[3, 2] = true;

            Console.Clear();
            Console.WriteLine("PRESS ENTER TO START...");
            Console.ReadLine();



            while (true)
            {

                for (int yy = 1; yy < 5; yy++)
                {
                    

                    for (int xx = 0; xx < 5; xx++)
                    {


                        // show while working
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(lo1.ToStr());
                        Thread.Sleep(50);


                        if (lo1.Grid[xx, yy - 1] == true)
                        {
                            lo1.Press(xx, yy);
                        }
                    }


                    

                }

                if (lo1.DidWin() == false)
                {
                    Random rnd1 = new Random(Environment.TickCount);
                    int top_num = rnd1.Next(0, 4);
                    lo1.Grid[top_num, 0] = true;
                }
                else
                {
                    Console.WriteLine("YOU WIN!!!!!");
                    Console.ReadLine();
                }


                Thread.Sleep(500);
            }



        }














        class LightsOutBoard
        {
            public bool[,] Grid = new bool[5, 5];

            public bool DidWin()
            {
                int cnt = 0;
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                    {
                        if (Grid[x, y] == true) { cnt++; }
                    }
                }

                bool ret = false;
                if (cnt < 1) { ret = true; } else { ret = false; }
                return ret;
            }


            public void Press(int x, int y)
            {
                if (Grid[x, y] == true) { Grid[x, y] = false; } else { Grid[x, y] = true; }




                if ((x - 1) >= 0)
                {
                    Grid[x - 1, y] = !Grid[x - 1, y];
                }

                if ((x + 1) <= 4)
                {
                    Grid[x + 1, y] = !Grid[x + 1, y];
                }

                if ((y - 1) >= 0)
                {
                    Grid[x, y - 1] = !Grid[x, y - 1];
                }

                if ((y + 1) <= 4)
                {
                    Grid[x, y + 1] = !Grid[x, y + 1];
                }

            }



            public string ToStr()
            {
                string ret = "";
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                    {
                        if (Grid[x, y] == true)
                        {
                            ret += "X ";
                        }
                        else
                        {
                            ret += "- ";
                        }
                    }

                    ret += "\n\n";

                }

                return ret;
            }


        }


    }
}



Back to top
Bjørn
Fri Mar 04 2016, 11:32AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
I can see two flaws in the code.

rnd1.Next(0, 4);
will never generate the number 4 so the rightmost light will never be randomly set.

There exists a pattern that causes your algorithm enter an infinite loop:
Back to top
Slava
Fri Mar 04 2016, 02:52PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
It works now!!!!

Thanks!
Back to top
Slava
Fri Mar 04 2016, 03:07PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
Lights Out Solve 001
Back to top
Slava
Fri Mar 04 2016, 05:18PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
Here is a more efficient approach based on the algorithm I found in this video... For some reason it still takes a lot of iterations to solve...




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace LightsOut002
{
    class Program
    {
        static void Main(string[] args)
        {

            LightsOutBoard lo1 = new LightsOutBoard();
            lo1.Grid[1, 2] = true;
            lo1.Grid[2, 2] = true;
            lo1.Grid[3, 2] = true;

            Console.Clear();
            Console.WriteLine("PRESS ENTER TO START...");
            Console.ReadLine();



            while (true)
            {

                for (int yy = 1; yy < 5; yy++)
                {


                    for (int xx = 0; xx < 5; xx++)
                    {


                        // show while working
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(lo1.ToStr());
                        Thread.Sleep(50);


                        if (lo1.Grid[xx, yy - 1] == true)
                        {
                            lo1.Press(xx, yy);
                        }
                    }




                }

                if (lo1.DidWin() == false)
                {

                    bool gotit = false;

                    if ((lo1.Grid[0, 4] == true) & (gotit == false))
                    {
                        gotit = true;
                        lo1.Grid[3, 0] = true;
                        lo1.Grid[4, 0] = true;
                    }
                    if ((lo1.Grid[1, 4] == true) & (gotit == false))
                    {
                        gotit = true;
                        lo1.Grid[1, 0] = true;
                        lo1.Grid[4, 0] = true;
                    }
                    if ((lo1.Grid[2, 4] == true) & (gotit == false))
                    {
                        gotit = true;
                        lo1.Grid[3, 0] = true;
                    }

                    if (gotit == false)
                    {
                        Random rnd1 = new Random(Environment.TickCount);
                        int top_num = rnd1.Next(0, 5);
                        lo1.Grid[top_num, 0] = true;
                    }



                }
                else
                {
                    Console.WriteLine("YOU WIN!!!!!");
                    Console.ReadLine();
                }


                Thread.Sleep(500);
            }



        }














        class LightsOutBoard
        {
            public bool[,] Grid = new bool[5, 5];

            public bool DidWin()
            {
                int cnt = 0;
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                    {
                        if (Grid[x, y] == true) { cnt++; }
                    }
                }

                bool ret = false;
                if (cnt < 1) { ret = true; } else { ret = false; }
                return ret;
            }


            public void Press(int x, int y)
            {
                if (Grid[x, y] == true) { Grid[x, y] = false; } else { Grid[x, y] = true; }




                if ((x - 1) >= 0)
                {
                    Grid[x - 1, y] = !Grid[x - 1, y];
                }

                if ((x + 1) <= 4)
                {
                    Grid[x + 1, y] = !Grid[x + 1, y];
                }

                if ((y - 1) >= 0)
                {
                    Grid[x, y - 1] = !Grid[x, y - 1];
                }

                if ((y + 1) <= 4)
                {
                    Grid[x, y + 1] = !Grid[x, y + 1];
                }

            }



            public string ToStr()
            {
                string ret = "";
                for (int y = 0; y < 5; y++)
                {
                    for (int x = 0; x < 5; x++)
                    {
                        if (Grid[x, y] == true)
                        {
                            ret += "X ";
                        }
                        else
                        {
                            ret += "- ";
                        }
                    }

                    ret += "\n\n";

                }

                return ret;
            }


        }


    }
}
Back to top
jdub1581hv
Fri Mar 04 2016, 06:28PM
jdub1581hv Registered Member #55219 Joined: Tue Jun 09 2015, 11:21PM
Location:
Posts: 80
not sure if it's the same in C#, but it is usually a good idea in for loops to have the following:
for( int i = 0; i <= 5; i++){ code }
adding the 'or equals' helps to avoid memory leaks..

I'm a java whore so it may be different... But have you thought of Java? the JavaFX is a really clean UI, and easy to use..
There is also the added bonus of lambdas, FXML (UI markup), and javascript all built in.. Syntax is 'almost' identical..
Almost forgot the Stream api as well, it's Very fast with list sorting, mapping, etc...

I did a lot of 3d work with it, and with the Stream api, I could iterate 5 million+ vertices in no time at all, on top of the other items / textures in the scenegraph
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.