התרגילים שפתרנו היום–29.11.2018

מימוש הפעולה שהופכת מספר

public static int reverseNuber(int num){

     int tmp = 0;

     while (num > 0){

           tmp = tmp*10 + num % 10;

           num = num / 10;

     }

     return tmp;

}

 

 

מימוש המשחק טום וג'רי

public static int movePlayer(int x){

       return x+ ((int)(Math.random()*11))-5;

}

 

public static int checkWinner(int a1,int a2){

       if (a1 == a2)

              return 1;

       if (a1 < 1 || a1 > 50)

              return 2;

       return 0;

}

      

public static void playGame(){

       int posA=10,posB=40,check;

       System.out.println("Starting game ….");

       System.out.println("Player A in position "+posA);

       System.out.println("Player B in position "+posB);

       boolean flag = true;

       while (flag){

              System.out.print("Player A move from possition "+posA);

              posA = movePlayer(posA);

              System.out.println(" to possition "+posA);

              check = checkWinner(posA,posB);

              if (check == 1){

                     System.out.println("Player A won the game for the same possition");

              }

              if (check == 2){

                     System.out.println("Player B won the game for Player A out of board");

              }

              flag = check == 0;

              if (flag){

                     System.out.print("Player B move from possition "+posB);

                     posB = movePlayer(posB);

                     System.out.println(" to possition "+posB);

                     check = checkWinner(posB,posA);

                     if (check == 1){

                           System.out.println("Player B won the game for the same possition");

                     }

                     if (check == 2){

                           System.out.println("Player A won the game for Player B out of board");

                     }

                     flag = check == 0;

              }

       }

}

      

      

public static void main(String[] args) {

      

       playGame();

 

}