We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D maze in browser controlled by Small Basic
#88
Hi, 
This is the code in C# for maze solving based on Sub animateCone in LD3DMazeGame.sb, litdev's  SendWebRequest and AB's post #19 (Hand on The Wall strategy):

Code:
using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Timer = System.Threading.Thread;

public class MazeData
{
    public string name { get; set; }
    public double posX { get; set; }
    public double posZ { get; set; }
    public double angle { get; set; }
    public double dist { get; set; }
    public int move { get; set; }
    public int left { get; set; }
    public int right { get; set; }
    public int cellX { get; set; }
    public int cellZ { get; set; }
    public int dir0 { get; set; }
    public int dir90 { get; set; }
    public int dir180 { get; set; }
    public int dir270 { get; set; }
    public int animate { get; set; }
    public int rotate { get; set; }
    public double forward { get; set; }
    public string game { get; set; }
    public string state { get; set; }
    public string distLabel { get; set; }
}


class Hello
{
    static string SendWebRequest(string url)
    {
        StreamReader streamReader = null;
        WebResponse webResponse = null;
        string result = "";
        try
        {
            WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WebRequest webRequest = WebRequest.Create(url);
            webResponse = webRequest.GetResponse();
            streamReader = new StreamReader(webResponse.GetResponseStream());
            result = streamReader.ReadToEnd();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            if (streamReader != null) streamReader.Close();
            if (webResponse != null) webResponse.Close();
        }
        return result;
    }


    static void Main()
    {
        string urlGet = "https://litdev.uk/apps/maze3D/maze3D.php?action=get";
        string urlSet = "https://litdev.uk/apps/maze3D/maze3D.php?action=set";

        int moveDir, dirForward = 1, dirBack, dirRight, dirLeft;
        int[] dirs = new int[4] { 270, 0, 90, 180 }; // 0 1 2 3

        int[] move = new int[4];

        moveDir = dirForward;
        string direction = "";

        while (true)
        {
            string jsonMaze = SendWebRequest(urlGet);
            Console.WriteLine(jsonMaze);

            MazeData objMaze = JsonConvert.DeserializeObject<MazeData>(jsonMaze);
            Console.WriteLine("cellX={0}, cellZ={1}", objMaze.cellX, objMaze.cellZ);

            dirForward = moveDir;

            dirLeft = moveDir - 1;
            if (dirLeft < 0)
                dirLeft += 4;

            dirBack = moveDir - 2;
            if (dirBack < 0)
                dirBack += 4;

            dirRight = moveDir - 3;
            if (dirRight < 0)
                dirRight += 4;


            move[0] = objMaze.dir270;
            move[1] = objMaze.dir0;
            move[2] = objMaze.dir90;
            move[3] = objMaze.dir180;

            if (move[dirLeft] == 1)
            {
                moveDir = dirLeft;
                direction = "dirLeft";
            }
            else if (move[dirForward] == 1)
            {
                moveDir = dirForward;
                direction = "dirForward";
            }
            else if (move[dirRight] == 1)
            {
                moveDir = dirRight;
                direction = "dirRight";
            }
            else
            {
                moveDir = dirBack;
                direction = "dirBack";
            }

            Console.WriteLine("dir = {0}, movDir = {1}", direction, moveDir);
            SendWebRequest(urlSet + "&animate=" + dirs[moveDir].ToString());
            Timer.Sleep(2500);
        }
    }
}

Compiling: 
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /r:LitDev.dll maze.cs
Run:
maze

Newtonsoft.Json is located in LitDev.dll.

st
[-] The following 1 user Likes stevantosic's post:
  • litdev
Reply


Messages In This Thread
RE: 3D maze in browser controlled by Small Basic - by stevantosic - 02-19-2025, 04:05 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)