1

I'm writing a Java program that will play a game. Basically you choose the no. of players and rounds, then the program shows you what every player should say, in order, considering the following rules:

-assuming the players are standing in a circle, they start counting one-by-one clockwise until someone reaches a number (larger than 10) made of only the same digit. For example 11, 22, 33, .. , 444 etc, then they start counting counter clockwise

E.g.: P9: 9; P10: 10; P11: 11; P12: 13; P11: 14 etc (P10 = Player 10)

-when the get to a number that is multiple of 7, contains 7 or the sum of the digits is 7, they say "Boltz"

E.g.: P1: 13; P2: Boltz (instead of 14); P3: 15; P4 Boltz (16); P5: Boltz (17); P6:18 etc

I have the code in Java, but i can't seem to get the switching from clockwise turns to counterclockwise at numbers made up from only one digit

Can you please help me on SameDigits function? Thank you!

import java.util.Scanner;

public class Boltz {
  private static Scanner keyboard;

  public static void main(String[] args) {
    keyboard = new Scanner(System.in);

    int nPlayers = 0;
    int nRounds = 0;
    int currentPlayer = 0;
    int sum = 0;
    int x = 0;
    boolean isSameDigit = true;

    System.out.print("Cati jucatori sunt? ");
    nPlayers = keyboard.nextInt();

    System.out.print("Cate runde sunt? ");
    nRounds = keyboard.nextInt();

    System.out.print("Jucatori: " + nPlayers + "; Runde: " + nRounds + "\n");

    for (x = 1; x <= nPlayers * nRounds; x++) {

        isSameDigit = SameDigits(currentPlayer);

        if (currentPlayer < nPlayers && isSameDigit == false) {
            currentPlayer++;
        } else {
            currentPlayer = 1;
        }

        if (currentPlayer > 1 && isSameDigit == true) {
            currentPlayer--;
        } else {
            currentPlayer = nPlayers;
        }

        sum = digitSum(x);

        if (x % 7 == 0 || String.valueOf(x).contains("7") || sum == 7) {
            System.out.println("P:" + currentPlayer + " Boltz");
        } else {
            System.out.println("P:" + currentPlayer + " " + x);
        }
    }
  }

  public static int digitSum(int num) {
    int suma = 0;
    while (num > 0) {
        suma = suma + num % 10;
        num = num / 10;
    }
    return suma;
  }

  public static boolean SameDigits(int num) {
    int add = 0, add2 = 0;

    while (num > 0) {
        add = add + num % 10;
        add2 = add2 + add % 10;
        num = num / 10;
    }
    if (add == add2) {
        return true;
    } else {
        return false;
    }

  }

}
Naman
  • 27,789
  • 26
  • 218
  • 353
Mario
  • 13
  • 3

3 Answers3

1

That would be something like:

public static boolean sameDigits(int number) {
        //speical case
        if (number < 10)
            return false;
        String string = String.valueOf(number);

        for (int i = 1; i <= 9; i++) {
            if (string.replaceAll(String.valueOf(i), "").length() == 0)
                return true;
        }
        return false;
}
Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
  • 1
    Why do you return false when `number < 10`? – shmosel Jan 15 '17 at 03:03
  • 1
    I base it on the rule "assuming the players are standing in a circle, they start counting one-by-one clockwise until someone reaches a number (larger than 10) made of only the same digit. For example 11, 22, 33, .. , 444 etc, then they start counting counter clockwise" only. Besides it is quite easy to change ;) – Grzegorz Górkiewicz Jan 15 '17 at 03:04
  • Fair enough. Not the greatest method name, I guess. – shmosel Jan 15 '17 at 03:05
  • Yep. I think it is a design issue... Another interesting fact is that the author wants to avoid the so well-known camelCase and call it SameDigits instead of sameDigits... – Grzegorz Górkiewicz Jan 15 '17 at 03:09
  • 2
    Cute idea, but it would be more efficient as: `return string.replaceAll(string.substring(0, 1), "").length() == 0;` instead of looping over all possible single digit strings. – Ted Hopp Jan 15 '17 at 03:09
  • @TedHopp, great idea! – Grzegorz Górkiewicz Jan 15 '17 at 03:10
  • 1
    Or `for (int i = 1; i < string.length(); i++) if (string.charAt(i) != string.charAt(0)) return false;`. – shmosel Jan 15 '17 at 03:10
  • Or `return string.chars().distinct().count() == 1;`. – shmosel Jan 15 '17 at 03:13
1

If I understand you correctly, you want SameDigits to return true if the number is all the same digits and false otherwise. Single-digit numbers should also return true. This should do it:

public static boolean SameDigits(int num) {
    if (num < 0) return false; // or something else?

    int onesDigit = num % 10;
    num /= 10;
    while (num > 0) {
        if (onesDigit != num % 10) return false; // fail if digits differ
        num /= 10;
    }
    return true;
}

P.S. You should conform to Java naming conventions and name your methods starting with a lower-case letter (sameDigits instead of SameDigits).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

I'd use a regular expression. This one checks whether a String consists of a single digit, followed by one or more repetitions of the same digit.

public static boolean sameDigits(int arg) {
    return Integer.toString(arg).matches("(\\d)\\1+");
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110