We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I'm trying to print the letters in this game in the correct spaces but it's not working properly. Any suggestion would be helpful. Since this is an assignment I would appreciate it if direct solutions are not provided instead pointing me to the right direction or hints would be great! Thanks! And here's my code..
import javax.swing.JOptionPane;
String[] myWords = {"hello", "desktop", "computer", "python", "network", "database", "loops", "phone", "laptop", "memory"}; // Array containing the words for the game.
char[] selectedWord, wrongGuesses, rightGuesses, currentWord; //selectedWord is the array that stores the word selected randomly for the game, wronGuesses store the wrong letters, rightGuesses stores the right letter and currentWord stores the current letter of the word that the user typed in.
int correctNum, incorrectNum;//correctNum is the total number of Input that has been guessed correctly and incorrectNum is the total number of input that has been guessed incorrectly
int[] letterIndex;//position of letter in an array
void setup(){
background(255);//white background
selectedWord = getWord().toCharArray();//stores the randomly selected string from myWords and stored in selectedWord in char format
rightGuesses = new char[10];
wrongGuesses = new char[7];
correctNum = 0;
incorrectNum = 0;
int wordLength = selectedWord.length;
currentWord = new char[wordLength];
for(int i = 0; i < wordLength; i++) {
currentWord[i] = ' ';
}
letterIndex = new int[wordLength];
size(500,500);
smooth();
drawBase();//draws the base structure of the game
drawLetterSpaces();//draws the spaces for the place where the letters needs to be printed
}
void draw(){
process();//the main process
}
//the base structure of the game which is the hangman stand
void drawBase(){
strokeWeight(5);
line(100, 300, 200, 300);
line(150, 300, 150, 50);
line(150, 50, 300, 50);
line(300, 50, 300, 100);
line(150, 150, 250, 50);
}
//draws a head
void drawHead(){
ellipse(300, 125, 50, 50);
}
//draws the body
void drawBody(){
line(300, 150, 300, 225);
}
//draws the left leg
void drawLeftLeg() {
line(300, 225, 270, 265);
}
//draws the right leg
void drawRightLeg() {
line(300, 225, 330, 265);
}
//draws the left arm
void drawLeftArm() {
line(300, 175, 270, 185);
}
//draws the right arm
void drawRightArm() {
line(300, 175, 330, 185);
}
//generates a random index number for the myWord array
int letterIndexGenerator(){
return int(random(myWords.length));//generates random index for myWord array
}
//selects the string from the randomly generated index of myWord array using letterIndexGenerator function
String getWord(){
return myWords[letterIndexGenerator()];
}
//draws the spaces for the letters
void drawLetterSpaces(){
int xVal1 = 50;//starting position of the first space
int xVal2 = 80;//ending position of the first space
for(int i = 0; i < selectedWord.length; i++){ //loop to draw the required number of spaces for the selected word from getWord function
line(xVal1, 400, xVal2, 400);
xVal1 += 50;//draws the starting position of the next spaces after 50 places gap
xVal2 += 50;//draws the ending position of the next spaces after 50 places gap
}
}
//gets the input from user
char userInput(){
String input = JOptionPane.showInputDialog("Please enter a character: ");
if(input.length() == 0){//if input is blank ask again for input
JOptionPane.showMessageDialog(null, "Invalid Input. Please try again!");
return userInput();
}
else if(input.length() > 1){//if more than one letter is input than count only the first letter
return input.charAt(0);
}
else{
return input.charAt(0);
}
}
//fills the letters input by user in the correct spaces provided
void fillLetters(char ch, int[] indexes) {
for(int i = 0; i < indexes.length; i++) {
if (indexes[i] == 0) {
textSize(50);
fill(0);
text(ch, 50, 400);
}
else if (indexes[i] == 2) {
textSize(50);
fill(0);
text(ch, 100, 400);
}
else if (indexes[i] == 3) {
textSize(50);
fill(0);
text(ch, 150, 400);
}
else if (indexes[i] == 4) {
textSize(50);
fill(0);
text(ch, 200, 400);
}
else if (indexes[i] == 5) {
textSize(50);
fill(0);
text(ch, 250, 400);
}
else if (indexes[i] == 6) {
textSize(50);
fill(0);
text(ch, 300, 400);
}
else if (indexes[i] == 7) {
textSize(50);
fill(0);
text(ch, 350, 400);
}
else if (indexes[i] == 8) {
textSize(50);
fill(0);
text(ch, 400, 400);
}
else if (indexes[i] == 9) {
textSize(50);
fill(0);
text(ch, 450, 400);
}
else if (indexes[i] == 10) {
textSize(50);
fill(0);
text(ch, 500, 400);
}
}
}
//checks if character has been previously input by user
boolean checkExistChar(char input, char[] array){
for(int i=0; i< array.length;i++){
if(array[i] == input)
return true;
}
return false;
}
//checks if user has gussed all letter correctly
boolean checkWin(){
if(selectedWord.length == correctNum){
return true;
}
else{
return false;
}
}
//checks if user has reched the limit of incorrect guesses
boolean checkLost(){
if(incorrectNum == 7){
return true;
}
else{
return false;
}
}
//prints the wront letters that has been guessed in the console sectionand for each wrong guess draws a body part of the hangman using decisionMaker function
void printWrongGuesses(){
println("Number of mistakes : " + incorrectNum);
for(int i = 0; i < incorrectNum; i++){
System.out.print(wrongGuesses[i] + " ");
decisionMaker();
}
println();
}
//checks the incorrectNum variable for the number of incorrect guesses and draws the parts of hangman based on that
void decisionMaker(){
if(incorrectNum == 1){
drawHead();
}
else if(incorrectNum == 2){
drawBody();
}
else if(incorrectNum == 3){
drawLeftArm();
}
else if(incorrectNum == 4){
drawLeftArm();
}
else if(incorrectNum == 5){
drawRightArm();
}
else if(incorrectNum == 6){
drawLeftLeg();
}
else if(incorrectNum == 7){
drawRightLeg();
}
}
//the main process
void process() {
char input = userInput();//the input from user
if(checkLost()){//if game lost display a text and exit
JOptionPane.showMessageDialog(null, "You have lost! Exiting the game! The word was " + new String(selectedWord));
System.exit(0);
}
else if(checkWin()){//if game won dispay text and exit
JOptionPane.showMessageDialog(null, "You have won the game.");
System.exit(0);
}
if(checkExistChar(input, rightGuesses)){//if user input a letter which is already present in rightGuesses array display text and ask for input again
JOptionPane.showMessageDialog(null, "You have repeated the letter "+input+"! Please try again!");
}
else if(checkExistChar(input, wrongGuesses)){//if user input a letter which is already present in wrongGuesses array then display text and ask for input again
JOptionPane.showMessageDialog(null, "You have repeated the letter "+input+"! Please try again!");
}
else if(checkExistChar(input, selectedWord)){//if user input is present in the selectedWord array, store the input letter in rightGuesses and currentWord, increment correctNum by 1 and print letter in the spaces provided
for(int i = 0; i < selectedWord.length; i++){
if(selectedWord[i] == input){
rightGuesses[correctNum] = input;
correctNum++;
currentWord[i] = input;
letterIndex[i] = i;
fillLetters(input, letterIndex);
}
}
}
else if(!checkExistChar(input, selectedWord)){//if input letter is wrong the store it in wrongGuesses array, increment incorrectNum by 1 and call printWrongGuesses function to print wrong letters in console and draw the hangman parts
wrongGuesses[incorrectNum] = input;
incorrectNum++;
printWrongGuesses();
}
}
Answers
Do you mean letters on the lines below:
_ _ _ _ _ _ ?
Is the position wrong in y or in x direction?
Which lines numbers are relevant?
did you look a textAlign with 2 parameters?
i think the problem is in the logic rather than in the text
what does fillLetters() do? what a re the inputs? why is there a 0 and a 2 but no 1?
(basically if you play it like hangman the second lot of correct letters will overwrite the first lot of correct letters). and this is what fillLetters is meant to do. but i'm not sure what the inputs are or why there's a loop.
i would do something like
)
Thank you I will try that out and see what happens. Will post the updated code if it works properly.