Question: The code provide below is a project of word scramble in C++ Please kindly convert it into Java language #include #include #include #include using namespace
The code provide below is a project of word scramble in C++
Please kindly convert it into Java language
#include
#include
#include
#include
using namespace std;
/**
This is a game where the letters of a word are scrambled and the player has to guess the word.
Game play: There are two modes of play - single player and two player.
In single player mode - the computer will scramble a word and ask the player to figure out what the unscrambled word is.
In two player mode - two players will trade off providing a word for the computer to scramble and for the other player to unscramble.
Since both players are at the same computer, one player will need to look away from the screen as the other player enters the word for the computer to scramble.
*/
void word_scramble()
{
bool game_over, single_player_game_over, two_player_game_over, found_word, word_picked, input_error;
int i, word_length, swap_position_1, swap_position_2, word_choice, number_of_words, games_played;
int player1_score, player2_score, player1_game_count, player2_game_count, player1_tries, player2_tries;
string unscrambled_word, scrambled_word, guess, response, remainder, player, player1, player2;
char temp_letter;
const int WORD_COUNT = 10;
string word_list[WORD_COUNT] = {"dictionary", "sunrise", "basketball", "snowboarding", "school", "texting", "walking", "bicycle", "sleeping", "breakfast"};
bool word_used[WORD_COUNT], player_one;
game_over = false;
while(!game_over)
{
system("CLS");
srand(12345);
for (i=0; i < WORD_COUNT; i++)
word_used[i] = false;
/* Display the game logo */
cout << " W W W OOO RRRR DDD ";
cout << " W W W O O R R D D ";
cout << " W W W O O RRRR D D ";
cout << " W W W O O R R D D ";
cout << " WW WW OOO R R DDD ";
cout << " SSSS CCC RRRR AA MM MM BBBB L EEEE ";
cout << " S C R R A A M M M B B L E ";
cout << " SSSS C RRRR AAAA M M M BBBBB L EEEE ";
cout << " S C R R A A M M M B B L E ";
cout << " SSSS CCC R R A A M M M BBBB LLLLL EEEE ";
cout << "Game play: There are two modes of play - single player and two player. ";
cout << " In single player mode - the computer will scramble a word and ask you ";
cout << " to figure out what the unscrambled word is. ";
cout << " In two player mode - two players will trade off providing a word for the ";
cout << " computer to scramble and for the other player to unscramble. Since both ";
cout << " players are at the same computer, one player will need to look away from ";
cout << " the screen as the other player enters the word for the computer to scramble. ";
do {
cout << "Would you like to play Single Player Mode (s) or Two Player Mode (t)? ";
cout << "(s or t, 0 to quit): ";
cin >> response;
getline(cin, remainder);
} while (!(response == "s" || response == "S" || response == "t" || response == "T" || response == "0"));
if (response == "s" || response == "S")
/* Single Player Mode */
{
player1_score = 0;
player1_tries = 0;
games_played = 0;
single_player_game_over = false;
while (!single_player_game_over)
{
/* randomly pick word from list not picked yet*/
word_picked = false;
word_choice = rand() % WORD_COUNT;
while (!word_picked)
{
if (word_used[word_choice] == true)
{
word_choice += 1;
if (word_choice == WORD_COUNT-1) word_choice = 0;
}
else
{
word_picked = true;
word_used[word_choice] = true;
}
}
games_played +=1;
cout << " Please guess the word (0 to quit)." << " ";
unscrambled_word = word_list[word_choice];
scrambled_word = unscrambled_word;
found_word = false;
/* Scramble letters in word*/
word_length = unscrambled_word.length();
while (scrambled_word == unscrambled_word)
{
for (i=0; i < 100; i++)
{
swap_position_1 = rand() % word_length;
swap_position_2 = rand() % word_length;
temp_letter = scrambled_word[swap_position_1];
scrambled_word[swap_position_1] = scrambled_word[swap_position_2];
scrambled_word[swap_position_2] = temp_letter;
}
}
/* start game, ask user to unscramble word or let user quit*/
guess = "";
while (!(found_word || guess == "0"))
{
cout << " " << scrambled_word << " " << " ";
cin >> guess;
getline(cin, remainder);
player1_tries += 1;
if (guess == "0")
cout << "Ok. You have decided to skip this word. The correct answer was - " << unscrambled_word << ". ";
else if (guess == unscrambled_word)
{
cout << " Congratulations! You have found the word. ";
player1_score += 1;
found_word = true;
}
else
cout << " Please try again. You have not found the word (or 0 to quit). ";
}
cout << "You have solved " << setw(2) << player1_score << " out of " << games_played << " words with " << player1_tries;
if (player1_tries == 1) cout << " try. ";
else cout << " tries. ";
cout << "Would you like to play single player mode again? (y/n) ";
cin >> response;
getline(cin, remainder);
single_player_game_over = true;
if (response == "y" || response == "Y")
single_player_game_over = false;
if (games_played >= WORD_COUNT)
{
single_player_game_over = true;
cout << " You have played all the words. Exiting single player mode. ";
system ("PAUSE");
}
}
}
else if (response == "t" || response == "T")
/* Two Player Mode */
{
two_player_game_over = false;
player_one = true;
player1_score = 0;
player2_score = 0;
player1_game_count = 0;
player2_game_count = 0;
player1_tries = 0;
player2_tries = 0;
cout << " Player 1 - enter your name: ";
cin >> player1;
getline(cin, remainder);
player1[0] = toupper(player1[0]);
cout << " Player 2 - enter your name: ";
cin >> player2;
getline(cin, remainder);
player2[0] = toupper(player2[0]);
while (!two_player_game_over)
{
guess = "";
/* set current player who enters the word to scramble */
if(player_one) player = player1;
else player = player2;
input_error = true;
while (input_error)
{
input_error = false;
cout << " " << player << " - please enter a word to scramble (0 to quit): ";
cin >> unscrambled_word;
getline(cin, remainder);
if (unscrambled_word == "0")
two_player_game_over = true;
else
{
for(i=0; i < unscrambled_word.length(); i++)
{
if(!isalpha(unscrambled_word[i]))
{
input_error = true;
cout << "Please enter Letters Only for the Word!";
}
}
}
}
if (!two_player_game_over)
{
/* Scramble letters in word*/
word_length = unscrambled_word.length();
scrambled_word = unscrambled_word;
for (i=0; i < scrambled_word.length(); i++)
scrambled_word[i] = tolower(scrambled_word[i]);
for (i=0; i <= 100; i++)
{
swap_position_1 = rand() % word_length;
swap_position_2 = rand() % word_length;
temp_letter = scrambled_word[swap_position_1];
scrambled_word[swap_position_1] = scrambled_word[swap_position_2];
scrambled_word[swap_position_2] = temp_letter;
}
/* Display prompt with the scrambled word but without solution showing*/
system("CLS");
cout << " " << player << " has entered the word which is shown below with scrambled letters. ";
/* start game, ask user to unscramble the word or to let the user quit*/
player_one = !player_one;
/* set current player to the player who guesses the unscrambled word */
if(player_one)
{
player = player1;
player1_game_count += 1;
}
else
{
player = player2;
player2_game_count += 1;
}
cout << player << ", please guess the word (0 to give up)." << " ";
found_word = false;
while (!(found_word || guess == "0"))
{
cout << " " << scrambled_word << " " << " ";
cin >> guess;
getline(cin, remainder);
if(player_one) player1_tries += 1;
else player2_tries += 1;
if (guess == "0")
cout << " Ok. You have decided to skip this word. The correct answer was - " << unscrambled_word << ". ";
else if (guess == unscrambled_word)
{
cout << " Congratulations! " << player << ", you have found the word. ";
if(player_one) player1_score += 1;
else player2_score += 1;
found_word = true;
}
else
cout << " Please try again. " << player << ", you have not found the word (or 0 to quit). ";
}
cout << "The score so far is: ";
cout << left << setw(15) << player1 << "- has solved " << setw(2) << player1_score << " out of " << player1_game_count << " words with " << player1_tries;
if (player1_tries == 1) cout << " try. ";
else cout << " tries. ";
cout << left << setw(15) << player2 << "- has solved " << setw(2) << player2_score << " out of " << player2_game_count << " words with " << player2_tries;
if (player2_tries == 1) cout << " try. ";
else cout << " tries. ";
cout << "Would you like to continue two player mode? (y/n) ";
cin >> response;
getline(cin, remainder);
two_player_game_over = true;
if (response == "y" || response == "Y")
two_player_game_over = false;
}
}
}
else
game_over = true;
}
}
int main()
{
word_scramble();
system("PAUSE");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
