2
\$\begingroup\$

This button's code in gdscript 4 uses selector modes- only one possible at a time. It is attached to the 9 children small buttons of the main button.

There is singleton code that is just an array with each row being a string descriptor, a color, and three booleans- first sets whether to checked by Sudoku rules, second sets whether to set a the main number and hide all small button for that main button, the last removes same numbers in column/row/square as main number.

There should some way to simplify the many modes that this special Sudoku trial buttons uses.

Do you have any suggestions on how to clean this up a bit?

    extends Button
#var Colorgroups=["preset","trial","erase","error","usercolor1","userdefaultset","usercolor2","usercolor3","usercolor4","usercolor5"];
@export var displaynumber:int;
var row:int;
var column:int;
var squarenumber:int;
var bsqux:int;
var bsquy:int;
var bigsquareid:int;
var B= preload("res://main button/B1.tscn");
var allowpresetchanges:bool=true;
var comp=preload("res://Comparebutton/Compare1.tscn");
var buttonstate:int=1;
var last:bool=true;
var butcol1row:int;
var userdefaultcolor:Color;
var errorcolor:Color;
var currentcolor:Color;
var current
var data1=preload("res://base.gd");
var Colorgroupsrow:int; 
var Check:bool;
var Set:bool;
var userproperty:String; 
var selferror:bool=false; 
var previoususerproperty:String="default"; 
var currentuserproperty:String; 
var currentcolorgroupsrow:int=1;
var strsquarenumbergroup:String;
var selferrortemp:bool;
var double:bool=false;

var defaultcolor;
var strrow:String;
var strcol:String;
var strbs:String; 
func setup(row2,col2):
    add_to_group("notblack");
    add_to_group("all");
    row=row2;
    column=col2;

    squarenumber=row+((column)*9);
#   The number of the result button (big) 0 to 80
    bsqux=int(row/3);
    bsquy=int(column/3);
    bigsquareid=bsqux*3+bsquy; 

    strsquarenumbergroup="sq"+str(squarenumber);
#   Set the location of for the created result. 
    add_to_group(strsquarenumbergroup);
    add_to_group(strsquarenumbergroup+"remain");
    add_to_group(str(Base.data[1][0]));
    previoususerproperty="default";
    set_text(str(displaynumber));
    set("custom_colors/font_color",Color.GRAY); 
    pressed.connect(_c); 
    add_to_group(str(displaynumber));
    strrow="r"+str(row)
    add_to_group(strrow);
    strcol="c"+str(column);
    add_to_group(strcol);
    strbs="s"+str(bigsquareid); 
    add_to_group(strbs);
    
func _c():
    if (SudokuChecker.error==true):
        if (selferror==false): # This means if there is error then you have correct the problem number. 
            print("err");
            return
    Colorgroupsrow=Base.currentcolorrow; # This gets a category that the user set- some have specic functions and many are general
    #   print(str(Colorgroupsrow));
    currentuserproperty=str(Base.data[Colorgroupsrow][0]);
    currentcolor=Color(Base.data[Colorgroupsrow][1]);
#   print(str(currentcolor));
    Check=bool(Base.data[Colorgroupsrow][2]);
    var Create=bool(Base.data[Colorgroupsrow][3]);
    var _RR=bool(Base.data[Colorgroupsrow][4]);
    var strsquarenumber:String=str(squarenumber);
    errorcolor=Color(Base.data[3][1]);
    defaultcolor=Color(Base.data[1][1]);
    userdefaultcolor=Color(Base.data[5][1]);
#   Black0,Gray1,White2,Red3,Yellow 4,Orange5,Green6,Blue6,Indigo7,Violet8 Default values
#   The following sets buttons to their respective colour 
    if (Colorgroupsrow!=currentcolorgroupsrow): #the button can be in only one of many states
        SudokuChecker.error=false;
        selferror=false; #errensous button needs to be cleared.
        updateusergroup(previoususerproperty,currentuserproperty);
        SetAllColors(currentcolor);
        currentcolorgroupsrow=Colorgroupsrow; 
        if (Colorgroupsrow==1): #default state
            reset();
        elif (Colorgroupsrow==2): #cleared state
            if (Base.es==true):
                for i in get_parent().get_children():
                    if i.currentcolorgroupsrow==1:
                        i.erasegrey(true);  
            if (get_tree().get_nodes_in_group(strsquarenumbergroup+"remain").size()==1):
                if (Base.lastone==true):
                    get_tree().get_node(strsquarenumber).show();
                else:
                    remove_from_group(strsquarenumbergroup+"remain");   
        else: 
            if (Check==true): #handle initial game setting and user data entering
                var D:int;
                D=int(SudokuChecker.table[row][column]);
#               print("D"+str(D));
                if (D!=0): #ensures only on button's number is being checked
                    for i in get_parent().get_children():
                        if i.displaynumber==D:
                            i.double=true;
                            i.selferror=true;
                            Base.selferror=true; 
                            i.text=str(displaynumber)+"D";
                            i.currentcolor=errorcolor;
                            i.SetAllColors(errorcolor);
                            
                    currentcolor=errorcolor;
#                   if ((checkarea(num,checkrow,squx)==false)or (checkarea(num,checkcol,squy)==false)or (checkarea(num,checksqu,bsqu)==false)):
                    text=str(displaynumber)+"D";
                    get_parent().text="D";
                    
                    SudokuChecker.error=true;
                    selferror=true;
                    SetAllColors(currentcolor);
                elif (SudokuChecker.checktable(displaynumber,row,column)==false):# checks the Sudoku result- works fine
                    currentcolor=errorcolor;
#                   updateusergroup();
                    SudokuChecker.error=true;
                    selferror=true;
                    SetAllColors(currentcolor);
            else: 
                updateusergroup(previoususerproperty,currentuserproperty);
                SetAllColors(currentcolor);
            if (Create==true):
                if (double==true):
                        get_parent().text="D"
                        get_parent().selferror=selferror;
                        selferror=false;
                else: 
                    get_parent().text=str(displaynumber); #makes the main number have its number and hides the children buttons like this one. 
                    get_parent().SetAllColors(currentcolor);
                    get_parent().set_up(Colorgroupsrow);
                    print(str(selferror));
                    print(str(Base.data[Colorgroupsrow][4]));
                    if bool(Base.data[Colorgroupsrow][4])==true:
                        get_tree().call_group(str(displaynumber),"rr",strrow,strcol,strbs);
                    get_parent().selferror=selferror;
                    selferror=false;
                    currentcolor=defaultcolor; 
                    SetAllColors(currentcolor);
                    remove_from_group(currentuserproperty);
                    add_to_group("default");
                for _i in get_parent().get_children():
                    _i.hide()
            else: 
                selferror=selferrortemp;
func updateusergroup(old,new): #swaps button between groups
    if (is_in_group(old)):
        remove_from_group(old);
        add_to_group(new);
        old=new;
        return true;
    else:
        return false;
func Clear(colx):
        SudokuChecker.table[row][column]=0;
        
        SetAllColors(colx);
func SetAllColors(coly): # sets color of button text in all states
    set("theme_override_colors/font_pressed_color",coly);
    set("theme_override_colors/font_hover_color",coly);
    set("theme_override_colors/font_color",coly);
    set("theme_override_colors/font_focus_color",coly);

func erasegrey(change:bool):
    if (change==true):
        SetAllColors(Base.data[2][1]);
    else:
        SetAllColors(Base.data[1][1]); 
        reset();
func reset():
    add_to_group(strsquarenumbergroup+"remain");
    updateusergroup(currentuserproperty,"default");
    currentuserproperty="default";
    currentcolorgroupsrow=1;
    SetAllColors(Base.data[1][1]);
func rr(row1,col1,square): #hides numbers that match the user set number in same row,column, square

    if is_in_group(row1):
        erasegrey(true);
        updateusergroup(currentuserproperty,"default");
    elif is_in_group(col1):
        erasegrey(true);
        updateusergroup(currentuserproperty,"default");
    elif is_in_group(square):
        erasegrey(true);
        updateusergroup(currentuserproperty,"default");
func erasesquare():
    if currentuserproperty=="default":
        erasegrey(true);
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate. \$\endgroup\$ Commented Apr 29, 2022 at 16:28

0

Browse other questions tagged or ask your own question.