Simple guessing game continuing with any other letter other than the declared variable Y/N
By : Begia
Date : March 29 2020, 07:55 AM
it should still fix some issue You have an accidental semicolon after that if statement and before your break.
|
Simple guessing game that allows user to play again after correctly guessing the random number?
By : Splixz
Date : March 29 2020, 07:55 AM
wish helps you since my english is not good, i will give you the code ( modifed from yours). code :
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int random_num;
int guessGame(int guessed_num) {
if (guessed_num == random_num) {
printf("Correct! That's the number.\n");
return 1;
} else if (guessed_num < random_num) {
printf("Too low. Guess again.\n");
return 0;
} else {
printf("Too high. Guess again.\n");
return 0;
}
}
int main() {
char loop;
while (1) {
int guessed_num = 0;
srand(time(NULL));
random_num = rand() % 50 + 1;
printf("I have a number between 1-50.\n");
printf("Can you guess what it is?\n");
printf("Enter your initial guess.\n");
while (1) {
scanf("%d", &guessed_num);
int returnValue = guessGame(guessed_num);
if (returnValue == 1) {
break;
}
}
printf("You wanna do it again? ");
getchar();
scanf("%c", &loop);
if (loop == 'n') {
break;
}
}
return 0;
}
|
Letter guessing game Java
By : Michael
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have been working on a java guessing game for letters (a-z)! However i have created the game perfectly by using the number 1-26, but i cannot figure out how to convert each integer to a letter ie a = 1, b = 2,....z = 26! code :
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Guess the Letter");
String myLetter=scan.nextLine();
//get the letter of myLetter variable then convert to Uppercase
char enteredLetter=Character.toUpperCase(myLetter.charAt(0));
//26 only because the characters array starts with index 0
char[] characters ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//I had created a parrallel array symbolizing int value of each letter
int[] range={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
//this variable convert user input to one of the array element of range
int userInputToInt=0;
//this variable is for knowing what int[] range array element must the value of userInputToInt fall
int userInputControlLoop=0;
char randomLetter=characters[(int)(Math.random()*26)];
// get the random input of computer convert it to int
int computerInputToInt=0;
//this loop is for getting the int value of randomLetter input by the computer
for(int i=0;i<characters.length;++i)
{
if(randomLetter==characters[i])
{
computerInputToInt=range[i];
}
}
//this loop is for getting the int value of user inputted letter
for(char i:characters)
{
if(enteredLetter==i)
{
userInputToInt=range[userInputControlLoop];
}
++userInputControlLoop;
}
//test the entered letter of user
if(enteredLetter==randomLetter)
{
System.out.println("Correct Guess");
System.out.println("The letter is:"+randomLetter);
}
//test the entered letter of user if greater than computer input
else if(userInputToInt>computerInputToInt)
{
System.out.println("Incorrect Guess");
System.out.println("The letter is too high");
System.out.println("The letter is:"+randomLetter);
}
//test the entered letter of user if lesser than computer input
else if(userInputToInt<computerInputToInt)
{
System.out.println("Incorrect Guess");
System.out.println("The letter is too low");
System.out.println("The letter is:"+randomLetter);
}
}
|
Java guessing letter game
By : user6670856
Date : March 29 2020, 07:55 AM
it helps some times Hi im currently creating a guessing letter game! So far i have made it so you guess between the numbers 1 and 26 using the java.util.random class. However this random number should be converted into the corresponding number within the alphabet! ie a=1 etc..This is where my problem is i dont know how to convert this randomly generated number into a letter! This needs to be related to the integer as i have to able to tell the user if they are above the letter in the alphabet or to low based on the guess they entered! One more thing is that the users guess will be a letter also! Below is my code! Any help will be greatly appreciated! code :
Random random = new Random();
char c = (char) (random.nextInt(26) + 'a');
|
Python guessing game with clues
By : user2985033
Date : March 29 2020, 07:55 AM
I wish this helpful for you This is the closest I could get to what you asked for. The comments on your original question are worth a read in my opinion but I think this does exactly what you asked for in the question. code :
print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))
count = 0
last_distance = -1
while user_input is not 41 and count < 4:
count = count + 1
how_close_to_answer = (41 - user_input)
how_close_to_answer = how_close_to_answer.__abs__()
if how_close_to_answer <= 5 and last_distance > 5:
user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
elif last_distance == -1:
if 5 < how_close_to_answer < 20:
user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
elif how_close_to_answer >= 20:
user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
elif how_close_to_answer <= 5:
user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
else:
if how_close_to_answer < last_distance:
if how_close_to_answer <= 5:
user_input = int(input(f'Hotter. Remaining guesses {5 - count} '))
else:
user_input = int(input(f'Warmer. Remaining guesses {5 - count} '))
elif how_close_to_answer > last_distance:
user_input = int(input(f'Colder. Remaining guesses {5 - count} '))
last_distance = how_close_to_answer
if user_input is not 41:
print('You Lose!')
else:
print('You Win!')
print(f"It took you {count + 1} guesses to get this correct.")
|