Author Topic: A Program to Make SG Bots  (Read 5737 times)

Offline Praesidium

  • Bot Neophyte
  • *
  • Posts: 1
    • View Profile
A Program to Make SG Bots
« on: March 30, 2008, 06:15:20 AM »
I suppose this would be the right place to post this. It was an idea I had back when I first started messing around with the DNA, but I got sidetracked by other things. A few days ago I finally sat down and wrote it, using python, and managed to get it working. It takes the path of the bot you want to make an SG Bot out of, then allows you to specify a different file to save the result into (optional). As far as my testing has concluded the results work perfectly, even if the resulting code is ugly, though errors may still occur. I'd appreciate any feedback on it. Oh, it requires the Python interpreter be installed (available free from python.org), and then the source code saved in a .py file should run like a .exe (under windows, on other operating systems you may need to add a line of code to the top, unfortunately, I can never remember what it is...). I'll simply post the code here rather than uploading the file, just paste it into a text editor and save it as a .py, it should run like an executable after that. Try it out, just be sure to either use a copy of the bot or have it save to a different file, I think it's bug-free, but I'd hate to be responsible for a bot getting messed up.

Code: [Select]
print """      Copyright (C) 2008 Praesidium
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>."""

class SGBot():
    def __init__(self, path = "default.txt"):
        self.path = open(path, "r")
        self.text = []
        for line in self.path: self.text.append(line)
        self.path.close()

    def toSG(self):
        self.conds = []
        self.cond = []
        self.cur = ""
        self.iscond = 0
        self.curcond = 0
        for line in self.text:
            if "cond" in line: self.iscond = 1
            elif "start" in line:
                self.iscond = 2
                self.cur = "".join(self.cond) + " mult "
                self.cond = []
            elif "stop" in line: self.iscond = 0
            elif "end" in line: break
            elif self.iscond == 1:
                line = line.replace("\n", "")
                if " >=" in line: line = line.replace(">=", "sub 1 add sgn 0 floor ")
                elif " <=" in line: line = line.replace("<=", "sub 1 sub sgn - 0 floor ")
                elif " >" in line: line = line.replace(">", "sub sgn 0 floor ")
                elif " <" in line: line = line.replace("<", "sub sgn - 0 floor ")
                elif " !=" in line: line = line.replace("!=", "sub sgn abs ")
                elif " =" in line: line = line.replace("=", "sub sgn abs - ++ ")
                if " xor" in line: line = line.replace("xor", "sub dup div ")
                elif " or" in line: line = line.replace("or", "add sgn ")
                elif " and" in line: line = line.replace("and", "mult ")
                elif " not" in line: line = line.replace("not", "1 sub dup div ")
                elif len(self.cond) > 0: line += " mult "
                self.cond.append(line)
            elif self.iscond == 2:
                line = line.replace("store", self.cur + " store")
                line = line.replace("inc", self.cur + "inc")
                line = line.replace("dec", self.cur + "dec")
                self.conds.append(line)
        return "cond\nstart\n" + "".join(self.conds) + "stop\nend"

    def save(self, saveto, content = []):
        self.saving = open(saveto, "w")
        self.saving.write("".join(content))
        self.saving.close()

path = raw_input("Enter path: ")
deci = raw_input("Would you like to save to the same file?[y/n] ")
parser = SGBot(path)
if not deci == "y": save = raw_input("Where would you like to save it? ")
else: save = path
parser.save(save, parser.toSG())

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
A Program to Make SG Bots
« Reply #1 on: March 30, 2008, 04:17:18 PM »
Neat, I love DNA compilers.

Offline goffrie

  • Bot Builder
  • **
  • Posts: 65
    • View Profile
A Program to Make SG Bots
« Reply #2 on: April 03, 2008, 07:25:17 AM »
Quote
on other operating systems you may need to add a line of code to the top, unfortunately, I can never remember what it is...)
Code: [Select]
#!/usr/bin/python

It seems to enter "^M"'s (carriage returns, but not line feeds) into scripts, always before a "mult".
Also, you don't need the "cond" at the beginning of the script anymore.

Anyways, neat script.

Offline Trafalgar

  • Bot Destroyer
  • ***
  • Posts: 122
    • View Profile
A Program to Make SG Bots
« Reply #3 on: May 05, 2008, 10:20:28 PM »
Quote from: Praesidium
if " xor" in line: line = line.replace("xor", "sub dup div ")
            elif " or" in line: line = line.replace("or", "add sgn ")
            elif " and" in line: line = line.replace("and", "mult ")
            elif " not" in line: line = line.replace("not", "1 sub dup div ")

Suggestion: Use these instead:

and becomes &
or becomes |
xor becomes ^