Coding Challenge Day 26- Problem 1: Word Guess Game

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 26: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: Write a program which simulates the Word Guess Game

Sample:

Welcome to Wordguess Game

**** * ***

Please Enter Single Enter: T
T*** * ***
Please Enter Single Enter: e
Te** * ***
Please Enter Single Enter: c
Tec* * ***
Please Enter Single Enter: A
Tec* * A**
Please Enter Single Enter: r
Tec* * Ar*
Please Enter Single Enter: t
Tec* * Art
Please Enter Single Enter: T
Tec* * Art
Please Enter Single Enter: w
Tec* * Art
Please Enter Single Enter: n
Tec* n Art
Please Enter Single Enter: h
Tech n Art

Solution:

package Games;

import java.util.Scanner;

public class Hangman {
	
	static String phrase= "Tech n Art";
	static String list="";
	static int a=0;
	
	public static void display(){
		for(int i=0; i<phrase.length(); i++){
			String s= phrase.substring(i, i+1);
			if(s.equals(" ")){
				System.out.print(" ");   
				a++;
			}  else if (list.indexOf(s)==-1){
				System.out.print("*");
			} else {
				System.out.print(s);
				a++;
			}
		}
	}
	
	public static void add_to_list(String p){
		list+=p;
		display();
	}
	
	public static void main (String [] args){
		Scanner obj= new Scanner (System.in);
		String input="";
		System.out.println("Welcome to Wordguess Game");
		display();
		
		while(!input.equals("stop")){
			System.out.print("\nPlease Enter Single Enter: ");
			input=obj.nextLine();
			if(!input.equals("stop"))
			   add_to_list(input);
		}
		
		System.out.println("Answer: " +phrase);
		System.out.println("Thanks for playing!!");
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: