6

My project is finally complete, but my only problem is that my teacher does not accept "breaks" in our code. Can someone help me resolve this issue, I have been working on it for days and I just can't seem to get the program to work without using them. The breaks are located in my DropYellowDisk and DropRedDisk methods. Other then that issue, my connect four program is flawless.

    private static void DropYellowDisk(String[][] grid) {

        int number = 0;
         Scanner keyboard = new Scanner (System.in);
         System.out.println("Drop a yellow disk at column (1–7): ");
         int c = 2*keyboard.nextInt()+1;


         for (int i=6;i>=0;i--)
            {
              if (grid[i][c] == " ")
              {
                grid[i][c] = "Y";
                break;
              }}
    }

    private static void DropRedDisk(String[][] grid) {

         Scanner keyboard = new Scanner (System.in);
         System.out.print("Drop a red disk at column (1–7): ");
         int c = 2*keyboard.nextInt()+1;
         for (int i =6;i>=0;i--)
            {
              if (grid[i][c] == " ")
              {
                grid[i][c] = "R";
                break;
              }

    }}
Alex
  • 79
  • 4

3 Answers3

8

my teacher does not accept "breaks"

From a programming standpoint, that's just plain silly (although I'm sure it has merit from an instructional one).

But there's an easy workaround in this particular case because the loops your breaking from are all at the end of their respective methods. As such, you can replace them with return statements. i.e:

private static void DropYellowDisk(String[][] grid) {

  for (int i=6;i>=0;i--)
    {
      if (grid[i][c] == " ")
      {
        grid[i][c] = "Y";
        return; //break
      }}
}
drew moore
  • 31,565
  • 17
  • 75
  • 112
  • 9
    If you teacher doesn't accept breaks, then use `goto` and surprise him to hell. – mostruash Nov 24 '14 at 21:42
  • It might not be a good style to use 'goto'-s. `break`s and `continue`s are really like them, especially with labels. `return` statements might also increase cyclomatic complexity, make the result harder to understand, reason about. (My take: no prohibition, but not encouraging it either.) – Gábor Bakos Nov 24 '14 at 21:45
  • I've been there too Alex no worries. About `goto`s, if you are checking some nested constraints involving nested loops and when `goto` makes really readable code for that instance, it's great to use IMO. Otherwise it's a tool to surprise your teacher. – mostruash Nov 24 '14 at 21:51
  • 1
    It is good practice when teaching/ giving assignments to students . The goal is make sure students can do it with the topics covered so far. – Xline Nov 24 '14 at 21:52
  • Thank you everyone especially @drewmoore. The simplest stuff is what really kills me in this class, but I do love it! – Alex Nov 24 '14 at 21:59
4
boolean flag = false;
for (int i=6;i>=0 && !flag;i--) {
    if (grid[i][c] == " ") {
        grid[i][c] = "Y";
        flag = true;
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
userDEV
  • 495
  • 4
  • 18
3

You can use boolean flag instead of break with while loop. Also you should compare strings by the equals method.

private static void DropYellowDisk(String[][] grid) {

    int number = 0; boolean flag=true;
     Scanner keyboard = new Scanner (System.in);
     System.out.println("Drop a yellow disk at column (1–7): ");
     int c = 2*keyboard.nextInt()+1;

      int i=6;
      while(i>=0&& flag)
      {
          if(grid[i][c].equals(" "))
          {
              grid[i][c]="Y";
              flag=false;
          }    
          i--;
      }
}
Xline
  • 804
  • 5
  • 13