Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having an issue with having the guess, when its the correct # that is selected by the computer, displaying the gameWon() void. I first have it set to if(theNumber == 20) but when I input any number it still displays the gameWon void text that I put in there. After it lets the computer select a number but won't take the number if its correct and then nothing works

What I have tried:

Java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gom;

/**
 *
 * @author stephenwessels
 */
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
public class GOM extends JFrame implements ActionListener, KeyListener
{

    Scanner in = new Scanner(System.in);
    
    Container content = this.getContentPane();
    JTextField theGuess = new JTextField();
    JLabel bankRoll = new JLabel("100.00 Zipoids");
    JLabel d1 = new JLabel("  ");
    JLabel d2 = new JLabel("  ");
    JButton newPlayer = new JButton("New Player");
    JButton newNumber = new JButton("New Number");
    JTextField thePlayer = new JTextField();
    JTextArea theOutput = new JTextArea();
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JScrollPane scrollArea = new JScrollPane(theOutput);
    
    Random randomizer = new Random();
    String playerName = "";
    int theNumber = 20;
    int numTries = 0;
    int numGames = 0;
    double amtRemaining = 100.00;
    
    
    
    public GOM()
    {
        JLabel guess = new JLabel("Make your guess:");
        
        this.setVisible(true);
        this.setSize(500,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Guess O'Matic");

        content.add(p1, BorderLayout.NORTH);
        p1.setLayout(new BorderLayout());
        p1.add(bankRoll, BorderLayout.EAST);
        p1.add(guess, BorderLayout.WEST);
        p1.add(theGuess);
        
        content.add(p2, BorderLayout.SOUTH);
        p2.setLayout(new BorderLayout());
        p2.add(thePlayer);
        p2.add(newPlayer, BorderLayout.WEST);
        p2.add(newNumber, BorderLayout.EAST);
        
        content.add(scrollArea, BorderLayout.CENTER);
        content.add(d1, BorderLayout.WEST);
        content.add(d2, BorderLayout.EAST);
        
        newPlayer.addActionListener(this);
        newNumber.addActionListener(this);
        
        thePlayer.addKeyListener(this);
        theGuess.addKeyListener(this);
        
        theOutput.setForeground(Color.LIGHT_GRAY);
        
        
        
    }
    
    public void actionPerformed(ActionEvent e) 
    {
        JButton btn = (JButton) e.getSource();
        if(btn == newPlayer) 
        {
            this.newPlayer();
        }else 
        {
            newGame();
            thePlayer.setText(playerName + " Game # " + numGames);
            theGuess.requestFocus();
            theGuess.setEnabled(true);
            theGuess.setBackground(Color.yellow);
        }  
    }
    
     private void newGame() 
     {
         numGames++;
         theNumber = randomizer.nextInt(100);
         numTries = 0;
         displayInstructions();
         bankRoll.setText(amtRemaining-- + " Zipoids");
         updateScore();
         theOutput.setText("");
         
     }
    
    public void newPlayer()
    {
        theOutput.setText("");
        
        theOutput.setEnabled(false);
        theGuess.setEnabled(false);
        newPlayer.setEnabled(false);
        newNumber.setEnabled(false);
        
        thePlayer.setEnabled(true);
        thePlayer.setText(playerName);
    }
    
    
    
     @Override
    public void keyTyped(KeyEvent ke) 
    {
        
    }

    @Override
    public void keyPressed(KeyEvent ke) 
    {
        
    }

    @Override
    public void keyReleased(KeyEvent ke) 
    {
        JTextField tf = (JTextField) ke.getSource();
        int key = ke.getKeyCode();
        if(key == KeyEvent.VK_ENTER)
        {
            if(tf == thePlayer)
            {
                addPlayer();
            }else
            {
                newGuess();
            }
        }
    }
    
    public void addPlayer()
    {
        playerName = thePlayer.getText();
       if(playerName.equals(""))
       {
           theOutput.append("\nEnter a name to play");
           thePlayer.requestFocus();
       }else
       {
           amtRemaining = 100.00;
           numGames = 0;
           thePlayer.setEnabled(false);
           thePlayer.setBackground(Color.WHITE);
           newPlayer.setEnabled(true);
           newNumber.setEnabled(true);
           theGuess.setEnabled(true);
       }
    }
    
    private void newGuess() 
    {
        String curStr = "123";
        
        int converted = Integer.parseInt(curStr);
        numTries++;
        int curGuess = converted;
        if(theNumber == 20)
        {
            gameWon();
            bankRoll.setText(curGuess + " Zipoids");
        }else
        {
            theOutput.append("Try a higher or lower number \n");
            theGuess.requestFocus();
            theGuess.setBackground(Color.yellow);
            theGuess.selectAll();
            bankRoll.setText(amtRemaining-- + " Zipoids");
        }
        //Prevents the 'Zipoids' from going into the negatives
        if(amtRemaining == 0.0)
        {
            theOutput.append("Game Over\n");
            theOutput.setEnabled(false);
            theGuess.setEnabled(false);
            newPlayer.setEnabled(false);
            thePlayer.setEnabled(false);
        }
    }
    
    public void gameWon()
    {
        double curWinnings = 0.0;
        
        theOutput.setText("******* WINNER ********");
        theOutput.append("\nAmount of tries: " + numTries + "\n" + "Amount Wagered: " + amtRemaining * curWinnings);
        theGuess.setText("");
        theGuess.setBackground(Color.white);
        switch(numTries)
        {
            case 1:
                if(numTries == 1)
                {
                    curWinnings =  2.00;
                }
            case 2:
                if(numTries == 2)
                {
                    curWinnings = 1.75;
                }
                case 3:
                if(numTries == 3)
                {
                    curWinnings = 1.50;
                }
                case 4:
                if(numTries == 4)
                {
                    curWinnings = 1.25;
                }
                case 5:
                if(numTries == 5)
                {
                    curWinnings = 1.00;
                }
                case 6:
                if(numTries == 6)
                {
                    curWinnings = 0.75;
                }
                case 7:
                if(numTries == 7)
                {
                    curWinnings = 0.50;
                }
                case 8:
                if(numTries == 8)
                {
                    curWinnings = 0.25;
                }
                case 9:
                if(numTries == 9)
                {
                    curWinnings = 0.0;
                }
            default:
                if(numTries < 9)
                {
                    curWinnings = 0.0;
                }
        }
    }
    
    public void updateScore()
    {
        thePlayer.setText(playerName + numGames);
        bankRoll.setText(amtRemaining + " Zipoids");
    }
    
     private void displayInstructions() 
    {
        theOutput.append("Enter your name" + "\nPress ENTER to play\n");
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        GOM gui = new GOM();
    }
    
}
Posted
Updated 4-Mar-18 7:30am
v2

1 solution

You should read this: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)[^]
Quote:
I'm having an issue with having the guess,

Time to learn how to use a debugger!

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900