0

My problems is that in the player ship method. My for loop doesn't repeat the string input for my scanner but works for the int inputs. The scanner asks me to input direction, and it works, but it doesn't for the second loop.

/**
 * Write a description of class SuperBattleShip here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.Random;
import java.util.Scanner;
public class SuperBattleShip
{
   static final int[][]Computer = new int[5][5];
   static final int[][]Player = new int[5][5];
   static Scanner scanner = new Scanner(System.in);
   static int Player1;
   static int Player2;
   static String Player3;
   Random random = new Random();
   public static void Intiateboard()
   {
       for(int x=0; x < 5; x++)
       {
           for(int y=0; y < 5; y++)
           {
                Player[x][y] = -1;
                 Computer[x][y] = -1;
           }
        }

    }
   public static void ShowboardPlayer()
   {

       System.out.println("      Your Ocean   ");
       System.out.println("  ");
       System.out.println("  ");
       System.out.println("   | 0 | 1 | 2 | 3 | 4 | ");
       System.out.println("   _____________________ ");
       System.out.println("  ");
       for(int x=0 ; x<5; x++)
       {

         System.out.print(x+"  |");
         for(int y=0 ; y<5; y++)
         {
                if(Player[x][y]==-1)
                {
                    System.out.print(" ~ |");

                }
                else if(Player[x][y]==0)
                {
                    System.out.print(" * |");

                }
                else if(Player[x][y]==1)
                {
                    System.out.print(" X |");

                }
                else if(Player[x][y]==2)
                {
                    System.out.print(" S |");
                }
            }
            System.out.print("\n");
            System.out.println("   _____________________ ");
            System.out.println("  ");
        }





       //System.out.println(" 0 | "+ Player[0][0]+" | "+ Player[0][1]+" | "+ Player[0][2]+" | "+ Player[0][3]+" | "+ Player[0][4]+" | ");
       //System.out.println("   _____________________ ");
       //System.out.println("  ");
       //System.out.println(" 1 | "+ Player[1][0]+" | "+ Player[1][1]+" | "+ Player[1][2]+" | "+ Player[1][3]+" | "+ Player[1][4]+" | ");
       //System.out.println("   _____________________ ");
       //System.out.println("  ");
       //System.out.println(" 2 | "+ Player[2][0]+" | "+ Player[2][1]+" | "+ Player[2][2]+" | "+ Player[2][3]+" | "+ Player[2][4]+" | ");
       //System.out.println("   _____________________ ");
       //System.out.println("  ");
       //System.out.println(" 3 | "+ Player[3][0]+" | "+ Player[3][1]+" | "+ Player[3][2]+" | "+ Player[3][3]+" | "+ Player[3][4]+" | ");
       //System.out.println("   _____________________ ");
       //System.out.println("  ");
       //System.out.println(" 4 | "+ Player[4][0]+" | "+ Player[4][1]+" | "+ Player[4][2]+" | "+ Player[4][3]+" | "+ Player[4][4]+" | ");
       //System.out.println("   _____________________ ");
       //System.out.println("  ");


    }
   public static void ShowboardComputer()
   {


       System.out.println("      Computer's Ocean   ");
       System.out.println("  ");
       System.out.println("  ");
       System.out.println("   | 0 | 1 | 2 | 3 | 4 | ");
       System.out.println("   _____________________ ");
       System.out.println("  ");
        for(int x=0 ; x<5; x++)
       {

         System.out.print(x+"  |");
         for(int y=0 ; y<5; y++)
         {
                if(Computer[x][y]==-1)
                {
                    System.out.print(" ~ |");

                }
                else if(Computer[x][y]==0)
                {
                    System.out.print(" * |");

                }
                else if(Computer[x][y]==1)
                {
                    System.out.print(" X |");

                }
            }
            System.out.print("\n");
            System.out.println("   _____________________ ");
            System.out.println("  ");
        }



   }
   public static void Showfield()
   {
     Intiateboard();
     ShowboardPlayer();
     System.out.println("\n\n  ");
     ShowboardComputer(); 
    }

   public static void Playerships()
   {
        System.out.println("Please choose the location of you ships.\n\n\n");
        for( int s = 0; s<3;s++)
        {

             System.out.println("Ship "+(s+1)+"\n\n");
          if( s>= 0 && s<3)
          {
          System.out.println("Type in Left,Right,Up, or Down for the direction of ship.");
          Player3 = scanner.nextLine();  
          System.out.println("Type the x coordinate.");
          Player1 = scanner.nextInt();
          System.out.println("Type the y coordinate.");
          Player2 = scanner.nextInt();
          {
          if(Player3.equals("Left"))
          {
            if(Player1 >= 2)
                {
                 for(int x = Player1; x>Player1-3 ; x--)
                  {
                    Player[Player2][x] = 2;
                  }

                }
            else if(Player1 < 2)
           {
                System.out.println("Not possible due to your x coordinate, so choose a larger coordinate.");

            }
         }
         else if(Player3.equals("Right"))
         {
            if(Player1 <=2)
                {

                 for(int x = Player1; x<Player1+3 ; x++)
                  {
                    Player[Player2][x] = 2;
                  }

                }
            else if(Player1 > 2)
            {
                System.out.println("Not possible due to your x coordinate, so choose a smaller coordinate.");

            }

         }
         else if(Player3.equals("Up"))
         {
          if(Player2 >=2)
                {

                 for(int y = Player2; y>Player2-3 ; y--)
                  {
                    Player[y][Player1] = 2;
                  }

                }
            else if(Player2 < 2)
            {
                System.out.println("Not possible due to your y coordinate, so choose a larger coordinate.");

            }  
         }
         else if(Player3.equals("Down"))
         {
          if(Player2 <=2)
                {


                for(int y = Player2; y<Player2+3 ; y++)
                 {
                    Player[y][Player1] = 2;
                  }

                }
            else if(Player2 > 2)
            {
                System.out.println("Not possible due to your x coordinate, so choose a smaller coordinate.");

            }  
         }
        }

        ShowboardPlayer();
       }
      }
   }

   public static void Playershoot()
   {




    }

   public static void main(String[]args)
   {
      Intiateboard();
      Showfield(); 
      Playerships();
      ShowboardPlayer();
    }
}
nullpotent
  • 9,162
  • 1
  • 31
  • 42
  • @user3055877 So what is the problem? The direction isn't being picked up? – fdsa Dec 02 '13 at 03:00
  • 1
    Here, the last `nextInt` in one iteration of the loop screws over the `nextLine` starting the next. – Dennis Meng Dec 02 '13 at 03:00
  • Also, if the issue is in the `Playerships()` method, it's probably best to just post that method first; that way we don't have to wade through a wall of code to see what's going on. You can always edit in more methods if you need to. – Dennis Meng Dec 02 '13 at 03:02
  • 1
    Welcome to Stack Overflow. I was randomly assigned to review your first post. As a suggestion, it would help people help you if you isolated your problem to a smaller piece of code. I see many obviously unrelated lines, bad indentation, much whitespace, and an unimplemented method. – s.bandara Dec 02 '13 at 03:21

0 Answers0