Replacing Underscores In Array Implementation Of Hangman Game

Jun 5, 2014

For an array implementation of a Hangman game I have created an array to hold the game board progress. It is initialized as "_ _ _ " where the underscores represent the number of letter in the word to be guessed. I have written the following method to replace underscores with a correct guess. It is functioning correctly in that it is replacing the underscore with a correct character guess, but it is only replacing the first time this letter appears in the word. I.e. for "greed" guessing "e" would only replace the first e: _ _ e _ _how I may be able to fix this issue.

// Updates gameboard from "_" to current guess if guess is correct
if (isCorrectGuess(move)==true){
if(inWinningState()==false){
guessProgress[charLocation] = guess;}
return true;}

View Replies


ADVERTISEMENT

Create Hangman Game With GUI?

Apr 18, 2015

I am trying to create a Hangman game with a gui. i already have buttons for the letters, each with its own action listener. I'm having difficulties creating the gallows and man.

I have a class called guiTest, which contains all of the letters. This is essentially what i have. Can I add the image to this class, or do I need to create a new class that extends JFrame?

Java Code: import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;

[code]....

View Replies View Related

Program A Hangman Game

Jan 18, 2014

i'm started to program a hangman game i Java not javascript i ecplice. the code so i can move forword just take the code and program it s it works.

import java.util.*;
public class GameLogic {
private boolean isStarted;
private int numberOfGuesses;
private int maxGuesses;
private List<String> guesses;
private String currentWord;

[code]....

View Replies View Related

Create A Very Basic Hangman Type Of Game

Oct 12, 2014

You have to create a very basic hangman type of game. Open Notepad and create a list of at least ten words in a text file. Save the file as a .txt file and use it to read words for the game. In this game you do not have to show pictures but you do need to keep track of the amount of times that the user has guessed wrong. When the game starts, show the amount of characters in the word represented as dashes and ask the user to type in a letter. if the user guesses correctly show the word again with the correct letter filled in the correct positions(s) and if the user correctly guessed the word, show a message. If the user guesses wrong, keep track of the body parts that needs to be reduced with every wrong guess and inform the user as well, also show a message.

I'm struggling with reading the file, showing the amount of parts left and asking the user to play again. Here is the code:

package hangman;

/**
*
* @author
*/
import java.nio.*;
import java.io.*;
import java.util.*;
public class HangMan {

[code]...

View Replies View Related

Hangman Game Using Class - It Keeps On Bringing Up Exception Error

Apr 23, 2015

I wrote a program that would play a game of Hangman. It plays the game but it keeps on bringing up a exception error. This is my class code

import java.util.ArrayList;
class Assignment2Q4
{
private final ArrayList<Character> guessList = new ArrayList<>();
private final char[] charArray;
private final String secretWord;
private int guesses;

[Code] ....

This is the output that is given:

Enter the secret word:
pizza
Guess a letter:
d
?????
Guess a letter:

[Code] .....

Also when I guess the first letter of the word correctly it stops the program without going further on with the guessing.

Like for ex. P????

View Replies View Related

Hangman 2 Player Game - User Cannot Guess More Than 6 Times

May 27, 2014

I am creating a hangman code. So the game i am creating is 2 player, first player enters the word and second player guesses. i made the code but i don't know how to restrict the first player from adding random characters into the secret word because for my project the word can only contain letters. so i need making the code so the player 1 can only enter letters.

My code is below:

/**
* Auto Generated Java Class.
*/
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
String player1,player2;
int wrongguessamount = 0; //keeps the count of the amount of wrong guesses
int correctguessamount = 0; //keeps count of the amount of correct guesses
int maxincorrect=6;//makes sure the user cant guess more than 6 times

[Code] .....

View Replies View Related

Multi-word Hangman Game - Spaces Not Displayed When Program Run

Apr 9, 2015

I created a simple Hangman game. It functions correctly, the only issue is when the file contains a phrase (2 or more words), spaces are not displayed when the program is run. For example, if the phrase was Java Programming Forums.

It would appear as _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _.

I want there to be spaces in between the words. How could I fix this? What would I need to add to my code?

import java.util.Scanner;
import java.io.*;
 public class hangman{
public static void main (String[] args) throws FileNotFoundException{
Scanner kb = new Scanner(System.in);
String guess;
String guesses = "";
int wrongGuess = 8;

[Code] ....

View Replies View Related

Hangman Game - How To Scan The String For Input Supplied By User

Dec 9, 2014

My assignment is to create a hangman game. One of my first difficulties with it is how to scan the string for an input supplied by the user.

Here is my method:

//Guesses that character c is in the secret word.
//Updates all instance variables accordingly,
//and returns true if c is present in the secret word.
public boolean guessCharacter(char c){
}

View Replies View Related

Hangman Type Game - How Many Times Each Guessed Letter Occurs Within The Word

Jul 31, 2014

The game runs fine, however I have to include how many times each letter that is guessed occurs within the word. For example, if the guess is e, the program should print out: The character e occurs in 3 positions. How would I go about doing this??

/*
* A program that plays simple word guessing game. In this game the user provides a list of words to the program.
* The program randomly selects one of the words to be guessed from this list.
* The player then guesses letters in an attempt to figure out what the hidden word might be.
* The number of guesses that the user takes are tracked and reported at the end of the game.
*/

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Hangman Game - No Graphics Show Up On Eighth Guess (Blank Frame)

Feb 5, 2015

I'm having some problems with the graphics of my hangman game. The graphic that's supposed to show up on the first guess (the hangman pole) doesn't show up until guess number 2. And on the eighth guess, no graphics show up (I just get a blank frame).

import java.awt.*;
import javax.swing.*;
public class HangmanFigure extends JPanel {
private int guesses;
private Image background;
public HangmanFigure() {
super();

[Code] .....

View Replies View Related

Replacing Characters In Array?

Mar 7, 2014

Is that the only way to code the JOptionPane? Seems rather similar to Sys.out

--- Update ---

I had more than this, but post was deleted somehow and this was the gist of it:

for(i = 0 ; i < charArray.length; i++){
if(Character.isDigit(charArray(i))){
address.replace("6", "*");
address.replace("7", "*");

Is there a better way to scan a character array for digits and replace any number in the array with another character ("*") in this case? The array is the address, " 1234 Runner Road " .

View Replies View Related

Replacing Old With New Values In Array

May 24, 2014

how to replace the values in my array with the results of my function factorial.

public static void main(String[] args) {
//this is my main function:
int[] array = {5,4,3,2,1};
int i = 0;
System.out.print("results: ");
for (i = 0; i < array.length; i++){
System.out.print(factorial(array[i]));

[code]....

So, what I'm trying to do is change the contents of the array "array" into their factorial value. So, they should be replaced with {120,24,6,2,1}. then add those using linear sum but that's a different story.

View Replies View Related

2048 Game Implementation In Java - Rectangle In GameBox

Apr 4, 2014

I want to implement the 2048 game in Java.

I made the game logics, but now I need to implement the interface.

I made 2 classes GameBox and ContainerBox that extends JComponent.

I painted in GameBox a rectangle and now I want to initialize a matrics of GameBoxes in ContainerBox so I used:

GameBox GB[][] = new GameBox[4][4];

I am thinking about drawing the Boxes in something like this:

for(int i=0;i<4;i++)

for(int j=0;j<4;j++)

but I don't know how to draw those rectangles in the for...

View Replies View Related

Replacing Null Values Within Multidimensional Array

Feb 18, 2014

I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).

I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?

View Replies View Related

Array Based Implementation Of A Stack - Generic Array Creation

Oct 10, 2014

So I have this stack. I'm writing out all the operations and what not but I'm having trouble bypassing this "generic array creation" problem. I'm meant to be creating an array based implementation of a stack and from my research from google and my various attempts at things, I have not found a solution that works.

In addition; I have all the operations written that I need except for one final one. And that is clear(). clear() is meant to empty the array, essentially it is a popAll() method. Then all I need to do is set up so I can print out the arrays and I should be able to handle everything else.

StackInterface:

/**
An interface for the ADT stack.
*/
public interface StackInterface<T>
{
/** Adds a new entry to the top of this stack.
@param newEntry an object to be added to the stack */
public void push(T newEntry);

/** Removes and returns this stackÕs top entry.
@return either the object at the top of the stack or, if the stack is empty before the operation, null
*/
public T pop();

[Code] ....

View Replies View Related

Tic Tac Toe Game 2D Array

Apr 13, 2014

I know a lot of people taking beginning java courses have posted questions about creating a tic tac toe game, but here is another one. Below is my code. I can't seem to get the X's and O's to show up on the board or to determine a winner.

import java.util.*;
public class Trial3
{
public static char turn;
public static int count;
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
char[][]b=new char[3][3];
boolean flag=false;
count=0;

[code]....

View Replies View Related

Replacing And Checking Character In String?

Jul 24, 2014

so my task is to write a code which would ask user to input the year as integer and first three letters of the month with first being an upper case letter. Than the program would determine the number of days for given year and month.

Currently I have a brain crash on how to check, if the user has provided the month with first character being upper Case. If not, than the program would automatically correct the character. Problem starts at line 17.

import java.util.Scanner;

public class DaysOfMonth4_17 {
public static void main (String[] args) {
//Initiate scanner
Scanner input = new Scanner (System.in);
//Ask for year input and use is as INT
System.out.println("Enter the year");

[code]...

View Replies View Related

DAO Based Application - Replacing Old Textfile

Apr 16, 2014

I'm currently trying to build a DAO based application where you use a text file as a data source. It have worked out well until I tried to delete lines from the file.

public void delete(DTOBil dtobil) {
try{
reader = Files.newBufferedReader(Paths.get("databilar.txt"),charset);
writer = Files.newBufferedWriter(Paths.get("temp.txt"), charset, StandardOpenOption.CREATE_NEW);
String line = null;
while((line = reader.readLine()) != null){

[Code] ....

I've managed to fill out the temp file with everything except the line I wanted to remove, but when I try to replace the original file with the temp file it won't work. It casts the error: "temp.txt -> databilar.txt".

I've also tried to use the renameTo method without any success...

View Replies View Related

Hangman In Java With Eclipse

Apr 21, 2015

I'm trying to finish this game of hangman for java in the program eclipse. Currently when I run the program I get this in the text field:

in randomword
in string2Array[Ljava.lang.String;@22998b08

v
i
d
e
o
g
a
m
e
s
New Game
Enter a letter: s
s
???????????

After I try entering a letter it doesn't prompt for the user to enter another, nor does it change the ? mark when the user guesses correctly.

Here's the code :

import java.util.Scanner;
public class Hangman {
int NUMBER_STRIKES = 7;
int strikes = 0;
String userguess= " ";
private ArrayFunctions func = new ArrayFunctions();
String currentWord;

[Code] ....

View Replies View Related

Memory Game 2D Array

Oct 3, 2014

So I have that traditional memory game homework assignment and for some reason I can't figure out how to put the codes in order. I can't figure out the arrangement to make it work. So far I have

import java.util.Random;
import java.util.Scanner;
public class MemoryGame {

[Code].....

View Replies View Related

Regex Replacing Entire Text Rather Than Match

Feb 14, 2014

With the code below, I am trying to replace all regex matches for visa cards within a given text file.

My first test was with a text "new3.txt" exclusively containing the visa test card 4111111111111111. My objective was to replace the card with "xxxx-xxxx-xxxx-xxxx". This was successful.

However, when modifying the text file to include other characters and text before and after (ex: " qwerty 4111111111111111 adsf zxcv"), it gives mixed results. Although it successfully validates the match, it replaces the whole text in the file, rather than replacing solely the match.

When trying this search and replace with words (rather than a regex match), it does not have this behavior. What am I missing?

import java.io.*;
import java.util.regex.*;
public class BTest
{
//VISA Test
private final static String PATTERN = "(?s).*4[0-9]{12}(?:[0-9]{3})?.*";
public static void main(String args[])
{
try

[Code]...

View Replies View Related

StringBuilder - Replacing Each Letter Without Reverse Tool

May 28, 2014

How can I replace each letter for example "abcde" to "edcba" with StringBuilder only and without the reverse tool. This is what I tried:

StringBuilder str=new StringBuilder("abcde");
int indexBegin=0;
int indexEnd=4;
for(int i=0;i<str.length();i++){
str.setCharAt(i, str.charAt(indexEnd));
indexEnd--;
}
System.out.println(str);

The output is:edcde with i understand why its wrong ,the last two letters already swap so it didnt take from the original str.

View Replies View Related

Constructors - How To Put 3 Values Of Each Variable Without Replacing Last Inputted One

Oct 2, 2014

Java Code:

public class Puppy{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public void setAge( int age ){
puppyAge = age;

[Code] ....

How do I put 3 values of the each variable without replacing the last inputted one?

Like when I input "Tommy" and input another name "Gerald", "Tommy" won't be replaced by "Gerald" when I input again.

View Replies View Related

Replacing String In Case Of Exact Match Only

Oct 15, 2014

I have 2 strings

String value ="/abc_12_1/abc234/abc/filename.txt";
String src="abc"
Sring tgt=xyz

Now I have to replace the string in variable value in case of exact match; I am trying to do the following :

if(value.contains(src)
{
value.replaceall(src,tgt);
 }

Now the problem here is it replaces all the occurrence of abc in the string value and I get the below output as :

value=""/xyz_12_1/xyz234/xyz/filename.txt";

However my requirement is only in the case the value exactly matches with source the replacement shd happen. I am expecting the output like this :

String value ="/abc_12_1/abc234/xyz/filename.txt";

Also the above code is in a function which will be called multiple times and the values will keep  on changing. However the target and source will remain the same always.

View Replies View Related

Hangman Program To Show Graphics

Nov 22, 2014

So I am trying to make my hangman program to show graphics. Here is what I have so far. How do I make my variables in here into a graphics program?

import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
public class Hangman2 extends JFrame
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
Hangman2 gp = new Hangman2();

[code]....

View Replies View Related

Hangman - Main Class Missing

Mar 31, 2015

I am having an issue with this code. It says that I'm missing the main class.  
 
import java.util.Random;
import java.util.Scanner;
public class Assignment5Hangman
{
public class GetData
{
private Scanner input;
public GetData()

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved