I have a program that a friend wrote for me in Java, however I'm trying to put it in C++ and I don't know Java myself. Does anyone know somewhere that I can get it translated? Or maybe someone who has time to translate it themselves?

It all looks vaguely familiar, but I'm not sure of a lot of the corresponding commands while translating.

This is basically what I have,

System.out.println("Welcome to the game of Pig!");
System.out.println();

gameOver = false;
while (!gameOver)

{
    // HUMAN'S TURN
    turnScore = 0;
    turnOver = false;
    do
    {
        dice = (int) (Math.random() * 6) + 1;
        
        System.out.println("You rolled: " + dice);
        if (dice == 1)
        {
            System.out.println("You lose your turn!" +
                         " Your total is " + scoreHuman);
            turnScore = 0;
            turnOver = true;
        }
        else
        {
            turnScore += dice;
            System.out.println("Your turn score is "
                    + turnScore + " and your " +
                    "total score is " + scoreHuman);
            System.out.println("If you hold, you will have " +
                    (turnScore + scoreHuman) + " points.");
            System.out.println("Enter 'r' to roll again, " +
                    "'s' to stop.");
            s = keyboard.next();
            if (s.equals("s"))
              turnOver = true;
            }
        } while (!turnOver);

        // Add in any points the human got
        scoreHuman += turnScore;
        System.out.println("Your score is " + scoreHuman);
        // Check for human winning
        if (scoreHuman >= 100)
        {
            System.out.println("YOU WIN!");
            gameOver = true;
            return;          // Exit immediately if win
        }
//Computer Turn - basic order as above

I would really appreciate it!

Recommended Answers

All 3 Replies

I don't see any difficult stuff that would get you confused: System.out.println() is used to print whatever you pass as argument and then changes line. The '+' symbol, when applied to String, concatenates:

var1 = 5;
var1 = 10;
"aaaa" + var1 -> aaaa5
"aaaa " + (var1 + var2) + " bbbbb" -> aaaa 15 bbbbb
"aaaa " + var1 + var2 + " bbbbb" -> aaaa 510 bbbbb

Math.random() : Generates a random double number between [0,1)

And the keyboard.next() probably reads from the input.
Also if you are unfamiliar with java but know C, then you can read that program try to understand what it does and how the algorithm works and write one in C. You don't have to copy and replace every single line and command from java to C. It cannot be done

thanks for the reply, the main issue I'm having is understanding what is a variable, and what is a command.

For instance, "scoreHuman" I would assume is a variable, but in C++, you must define it as a variable before your loop, and then refer back to it when you need it. Do you not have to do this in Java?

this block specifically I'm having a hard time with,

turnScore = 0;
    turnOver = false;
    do
    {
        dice = (int) (Math.random() * 6) + 1;
 
        System.out.println("You rolled: " + dice);
        if (dice == 1)
        {
            System.out.println("You lose your turn!" +
                         " Your total is " + scoreHuman);
            turnScore = 0;
            turnOver = true;

scoreHuman, turnScore, turnOver, dice, gameOver, are they all int? maybe bool or char?

Of course you have to declare everything before you use it. Java is similar to C++.
I don't remember if it has Objects. I think it does. Anyway, when you said that you got that java code, I assumed that it worked and it was well written, meaning that all variables were declared.

As far as their types you need to look at the code and see how they are used. For example, of course the gameOver is boolean since the code says: gameOver = false; at some point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.