How can I simplify my code for the future? Can I learn from these mistakes?











up vote
-1
down vote

favorite












A some time ago I made a game which made a bit of profit. I made use of a few clever solutions too. I learned a lot from it's development, but being a simple game, I made a behemoth using over 20 thousands lines of code (yes), which now puts me under a question mark. I made this so maybe people with similar problems can find this post. Please delete if not allowed.



I used Android Studio and a bunch of assets for it. Pretty simple. But have a look on a buying houses methods. 16 the same methods but with slight changes in variables.



b_bldwoodcut.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
b_bldwoodcut_clicked = false;
BuyWoodcuter();
return true;
}else{
b_bldwoodcut_clicked = true;
}
return false;
}
});
b_bldsawmill.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
b_bldsawmill_clicked = false;
BuySawmill();
return true;
}else{
b_bldsawmill_clicked = true;
}
return false;
}
});


...



And so on...



There is also a worker method called once per frame or so:



//sawmill
if (Math.round(ratioautosawmill * Threshold1) >= quick_trigger_val) {
//Stuff that happens over 20it/sec
if (pr_autosawmill >= 1 && sawmills > 0 && logs >= ratioautosawmill * 10 && planks + ratioautosawmill * 10 < storage) {
swsawmill.setBackground(getResources().getDrawable(R.drawable.pbquick));
pr_autosawmill = 0;
logs = Math.round(logs - ratioautosawmill * 10);
planks = Math.round(planks + ratioautosawmill * 10);
//Update resource view
ResTextUpdater();
} else if (pr_autosawmill < 1 && sawmills > 0) {
pr_autosawmill = pr_autosawmill + 0.1 * ProgressWorkerSpeed;
} else {
swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
}
} else {
if (pr_autosawmill >= 1 && sawmills > 0 && logs >= 1 && planks < storage) {
pr_autosawmill = 0;
logs--;
planks++;
//Update resource view
ResTextUpdater();
} else if (pr_autosawmill < 1 && sawmills > 0) {
pr_autosawmill = pr_autosawmill + ratioautosawmill * ProgressWorkerSpeed;
swsawmill.setBackground(getResources().getDrawable(progressBars[(int)Math.round(pr_autosawmill * 10)]));
} else {
swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
}
if ( sawmills < 1){
swsawmill.setBackground(getResources().getDrawable(R.drawable.pberr));
}
}


This is only one building in a worker. There is 16 different buildings, so I made 16 copies of literally everything, which made my code stupidly long. I used SQL for save/load but that requires a lot of boilerplate code anyway. Do I really need to make the same code every single time for every single item? It turns coding from fun to nightmare. That said, it took me half a year to make this game, where now I believe could be made with a few weeks tops. What are your thoughts?










share|improve this question













migrated from stackoverflow.com 22 mins ago


This question came from our site for professional and enthusiast programmers.



















    up vote
    -1
    down vote

    favorite












    A some time ago I made a game which made a bit of profit. I made use of a few clever solutions too. I learned a lot from it's development, but being a simple game, I made a behemoth using over 20 thousands lines of code (yes), which now puts me under a question mark. I made this so maybe people with similar problems can find this post. Please delete if not allowed.



    I used Android Studio and a bunch of assets for it. Pretty simple. But have a look on a buying houses methods. 16 the same methods but with slight changes in variables.



    b_bldwoodcut.setOnTouchListener(new View.OnTouchListener() {
    @SuppressLint("ClickableViewAccessibility")
    public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP){
    b_bldwoodcut_clicked = false;
    BuyWoodcuter();
    return true;
    }else{
    b_bldwoodcut_clicked = true;
    }
    return false;
    }
    });
    b_bldsawmill.setOnTouchListener(new View.OnTouchListener() {
    @SuppressLint("ClickableViewAccessibility")
    public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP){
    b_bldsawmill_clicked = false;
    BuySawmill();
    return true;
    }else{
    b_bldsawmill_clicked = true;
    }
    return false;
    }
    });


    ...



    And so on...



    There is also a worker method called once per frame or so:



    //sawmill
    if (Math.round(ratioautosawmill * Threshold1) >= quick_trigger_val) {
    //Stuff that happens over 20it/sec
    if (pr_autosawmill >= 1 && sawmills > 0 && logs >= ratioautosawmill * 10 && planks + ratioautosawmill * 10 < storage) {
    swsawmill.setBackground(getResources().getDrawable(R.drawable.pbquick));
    pr_autosawmill = 0;
    logs = Math.round(logs - ratioautosawmill * 10);
    planks = Math.round(planks + ratioautosawmill * 10);
    //Update resource view
    ResTextUpdater();
    } else if (pr_autosawmill < 1 && sawmills > 0) {
    pr_autosawmill = pr_autosawmill + 0.1 * ProgressWorkerSpeed;
    } else {
    swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
    }
    } else {
    if (pr_autosawmill >= 1 && sawmills > 0 && logs >= 1 && planks < storage) {
    pr_autosawmill = 0;
    logs--;
    planks++;
    //Update resource view
    ResTextUpdater();
    } else if (pr_autosawmill < 1 && sawmills > 0) {
    pr_autosawmill = pr_autosawmill + ratioautosawmill * ProgressWorkerSpeed;
    swsawmill.setBackground(getResources().getDrawable(progressBars[(int)Math.round(pr_autosawmill * 10)]));
    } else {
    swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
    }
    if ( sawmills < 1){
    swsawmill.setBackground(getResources().getDrawable(R.drawable.pberr));
    }
    }


    This is only one building in a worker. There is 16 different buildings, so I made 16 copies of literally everything, which made my code stupidly long. I used SQL for save/load but that requires a lot of boilerplate code anyway. Do I really need to make the same code every single time for every single item? It turns coding from fun to nightmare. That said, it took me half a year to make this game, where now I believe could be made with a few weeks tops. What are your thoughts?










    share|improve this question













    migrated from stackoverflow.com 22 mins ago


    This question came from our site for professional and enthusiast programmers.

















      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      A some time ago I made a game which made a bit of profit. I made use of a few clever solutions too. I learned a lot from it's development, but being a simple game, I made a behemoth using over 20 thousands lines of code (yes), which now puts me under a question mark. I made this so maybe people with similar problems can find this post. Please delete if not allowed.



      I used Android Studio and a bunch of assets for it. Pretty simple. But have a look on a buying houses methods. 16 the same methods but with slight changes in variables.



      b_bldwoodcut.setOnTouchListener(new View.OnTouchListener() {
      @SuppressLint("ClickableViewAccessibility")
      public boolean onTouch(View v, MotionEvent event) {
      if(event.getAction() == MotionEvent.ACTION_UP){
      b_bldwoodcut_clicked = false;
      BuyWoodcuter();
      return true;
      }else{
      b_bldwoodcut_clicked = true;
      }
      return false;
      }
      });
      b_bldsawmill.setOnTouchListener(new View.OnTouchListener() {
      @SuppressLint("ClickableViewAccessibility")
      public boolean onTouch(View v, MotionEvent event) {
      if(event.getAction() == MotionEvent.ACTION_UP){
      b_bldsawmill_clicked = false;
      BuySawmill();
      return true;
      }else{
      b_bldsawmill_clicked = true;
      }
      return false;
      }
      });


      ...



      And so on...



      There is also a worker method called once per frame or so:



      //sawmill
      if (Math.round(ratioautosawmill * Threshold1) >= quick_trigger_val) {
      //Stuff that happens over 20it/sec
      if (pr_autosawmill >= 1 && sawmills > 0 && logs >= ratioautosawmill * 10 && planks + ratioautosawmill * 10 < storage) {
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pbquick));
      pr_autosawmill = 0;
      logs = Math.round(logs - ratioautosawmill * 10);
      planks = Math.round(planks + ratioautosawmill * 10);
      //Update resource view
      ResTextUpdater();
      } else if (pr_autosawmill < 1 && sawmills > 0) {
      pr_autosawmill = pr_autosawmill + 0.1 * ProgressWorkerSpeed;
      } else {
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
      }
      } else {
      if (pr_autosawmill >= 1 && sawmills > 0 && logs >= 1 && planks < storage) {
      pr_autosawmill = 0;
      logs--;
      planks++;
      //Update resource view
      ResTextUpdater();
      } else if (pr_autosawmill < 1 && sawmills > 0) {
      pr_autosawmill = pr_autosawmill + ratioautosawmill * ProgressWorkerSpeed;
      swsawmill.setBackground(getResources().getDrawable(progressBars[(int)Math.round(pr_autosawmill * 10)]));
      } else {
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
      }
      if ( sawmills < 1){
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pberr));
      }
      }


      This is only one building in a worker. There is 16 different buildings, so I made 16 copies of literally everything, which made my code stupidly long. I used SQL for save/load but that requires a lot of boilerplate code anyway. Do I really need to make the same code every single time for every single item? It turns coding from fun to nightmare. That said, it took me half a year to make this game, where now I believe could be made with a few weeks tops. What are your thoughts?










      share|improve this question













      A some time ago I made a game which made a bit of profit. I made use of a few clever solutions too. I learned a lot from it's development, but being a simple game, I made a behemoth using over 20 thousands lines of code (yes), which now puts me under a question mark. I made this so maybe people with similar problems can find this post. Please delete if not allowed.



      I used Android Studio and a bunch of assets for it. Pretty simple. But have a look on a buying houses methods. 16 the same methods but with slight changes in variables.



      b_bldwoodcut.setOnTouchListener(new View.OnTouchListener() {
      @SuppressLint("ClickableViewAccessibility")
      public boolean onTouch(View v, MotionEvent event) {
      if(event.getAction() == MotionEvent.ACTION_UP){
      b_bldwoodcut_clicked = false;
      BuyWoodcuter();
      return true;
      }else{
      b_bldwoodcut_clicked = true;
      }
      return false;
      }
      });
      b_bldsawmill.setOnTouchListener(new View.OnTouchListener() {
      @SuppressLint("ClickableViewAccessibility")
      public boolean onTouch(View v, MotionEvent event) {
      if(event.getAction() == MotionEvent.ACTION_UP){
      b_bldsawmill_clicked = false;
      BuySawmill();
      return true;
      }else{
      b_bldsawmill_clicked = true;
      }
      return false;
      }
      });


      ...



      And so on...



      There is also a worker method called once per frame or so:



      //sawmill
      if (Math.round(ratioautosawmill * Threshold1) >= quick_trigger_val) {
      //Stuff that happens over 20it/sec
      if (pr_autosawmill >= 1 && sawmills > 0 && logs >= ratioautosawmill * 10 && planks + ratioautosawmill * 10 < storage) {
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pbquick));
      pr_autosawmill = 0;
      logs = Math.round(logs - ratioautosawmill * 10);
      planks = Math.round(planks + ratioautosawmill * 10);
      //Update resource view
      ResTextUpdater();
      } else if (pr_autosawmill < 1 && sawmills > 0) {
      pr_autosawmill = pr_autosawmill + 0.1 * ProgressWorkerSpeed;
      } else {
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
      }
      } else {
      if (pr_autosawmill >= 1 && sawmills > 0 && logs >= 1 && planks < storage) {
      pr_autosawmill = 0;
      logs--;
      planks++;
      //Update resource view
      ResTextUpdater();
      } else if (pr_autosawmill < 1 && sawmills > 0) {
      pr_autosawmill = pr_autosawmill + ratioautosawmill * ProgressWorkerSpeed;
      swsawmill.setBackground(getResources().getDrawable(progressBars[(int)Math.round(pr_autosawmill * 10)]));
      } else {
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pbpause));
      }
      if ( sawmills < 1){
      swsawmill.setBackground(getResources().getDrawable(R.drawable.pberr));
      }
      }


      This is only one building in a worker. There is 16 different buildings, so I made 16 copies of literally everything, which made my code stupidly long. I used SQL for save/load but that requires a lot of boilerplate code anyway. Do I really need to make the same code every single time for every single item? It turns coding from fun to nightmare. That said, it took me half a year to make this game, where now I believe could be made with a few weeks tops. What are your thoughts?







      java android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 23 mins ago









      Tatsurou

      1




      1




      migrated from stackoverflow.com 22 mins ago


      This question came from our site for professional and enthusiast programmers.






      migrated from stackoverflow.com 22 mins ago


      This question came from our site for professional and enthusiast programmers.





























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209957%2fhow-can-i-simplify-my-code-for-the-future-can-i-learn-from-these-mistakes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209957%2fhow-can-i-simplify-my-code-for-the-future-can-i-learn-from-these-mistakes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin