A simple guessing game [Source]

Discussion in 'Programming General' started by JVT, May 27, 2009.

A simple guessing game [Source]
  1. Unread #1 - May 27, 2009 at 11:46 AM
  2. JVT
    Joined:
    May 22, 2009
    Posts:
    13
    Referrals:
    0
    Sythe Gold:
    0

    JVT Newcomer

    A simple guessing game [Source]

    I just made this really quick cuz I was bored...... Its rly crap, but I was rushing... Might make it better later...

    Code:
    import java.util.*;
    
    public class GuessingGame
    {
    
    	private static int players;
    	private static int computers;
    	private static LinkedList<Player> users = new LinkedList<Player>();
    
    	public static void main(String[] args)
    	{
    		new GuessingGame();
    	}
    
    	public GuessingGame()
    	{
    		System.out.println("Welcome to JVT's Guessing Game!");
    		players = getInt("Number of players: ");
    		if (players > 10)
    		{
    			players = 10;
    			System.out
    					.println("Maximum number of players is 10. Players set to 10.");
    		}
    		computers = getInt("Number of computers: ");
    		if (computers >= players)
    		{
    			System.out
    					.println("Not everyone can be a computer! All players but one have been made computers!");
    			computers = players - 1;
    		}
    		for (int a = 0; a < players - computers; a++)
    		{
    			users.add(new Player(10, false, users.size() + 1));
    		}
    		for (int a = 0; a < computers; a++)
    		{
    			users.add(new Player(10, true, users.size() + 1));
    		}
    		System.out.println("All guesses must be 1-10");
    		Guess();
    	}
    
    	public static void Guess()
    	{
    		Scanner in = new Scanner(System.in);
    		int guess;
    		String win = "";
    		LinkedList<Player> winners = new LinkedList<Player>();
    		while (true)
    		{
    			guess = random(1, 10);
    			for (int a = 0; a < players; a++)
    			{
    				if (!users.get(a).isComputer())
    				{
    					users.get(a).setGuess(
    							getInt("Player " + a + "'s guess: "));
    				}
    				else
    				{
    					users.get(a).setGuess(random(1, 10));
    				}
    			}
    			for(int a = 0; a < players; a++)
    			{
    				if(users.get(a).getGuess() == guess)
    				{	
    					winners.add(users.get(a));
    					System.out.println("Player " + users.get(a).getNumber() + " is a winner!");
    				}
    			}
    			System.out.println("The winning guess was " + guess);
    			System.out.println("");
    		}
    	}
    
    	public static int getInt(String msg)
    	{
    		Scanner a = new Scanner(System.in);
    		System.out.print(msg);
    		while (true)
    		{
    			try
    			{
    				return a.nextInt();
    			}
    			catch (InputMismatchException e)
    			{
    				a.next();
    				System.out.print(msg);
    			}
    		}
    	}
    
    	private static int random(int min, int max)
    	{
    		return (int) (Math.random() * (max - min + 1)) + min;
    	}
    }
    
    player class:

    Code:
    
    public class Player
    {
    	private boolean computer;
    	private int cash;
    	private int guess;
    	private int number;
    	
    	public Player(int startingCash, boolean computer, int number)
    	{
    		this.computer = computer;
    		this.cash = startingCash;
    		this.number = number;
    	}
    	
    	public boolean isComputer()
    	{
    		return computer;
    	}
    	
    	public void setComputer(boolean computer)
    	{
    		this.computer = computer;
    	}
    	
    	public int getCash()
    	{
    		return cash;
    	}
    	
    	public void setCash(int cash)
    	{
    		this.cash = cash;
    	}
    	
    	public int getGuess()
    	{
    		return guess;
    	}
    	
    	public void setGuess(int guess)
    	{
    		this.guess = guess;
    	}
    	
    	public int getNumber()
    	{
    		return number;
    	}
    }
    

    I hope you like!!!!!
     
  3. Unread #2 - May 30, 2009 at 12:27 PM
  4. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    A simple guessing game [Source]

    I will try tomorrow. Will edit and not post another post after I tried.
     
  5. Unread #3 - Jun 14, 2009 at 5:25 PM
  6. samy
    Joined:
    Dec 1, 2005
    Posts:
    168
    Referrals:
    1
    Sythe Gold:
    0

    samy Active Member
    Banned

    A simple guessing game [Source]

    Nice job, however from a glance at your code, your random method wont work:

    Code:
    private static int random(int min, int max)
    	{
    		return (int) (Math.random() * (max - min + 1)) + min;
    	}
    Also, if you are using only integers then you wont need to cast it as an int.
     
  7. Unread #4 - Jun 14, 2009 at 6:00 PM
  8. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    A simple guessing game [Source]

    I use that same code pretty much. It works perfectly. Math.random() returns a double between 0 and 1, so the cast is necessary.
     
  9. Unread #5 - Jun 14, 2009 at 8:16 PM
  10. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    A simple guessing game [Source]

    It doesn't work as it should... take inputs of min 6 and max 8 as an example.
    It should be
    Code:
    return (int) (Math.random() * (max - min) + min);
    
     
  11. Unread #6 - Jun 14, 2009 at 9:18 PM
  12. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    A simple guessing game [Source]

    random()
    Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

    It will never equal 1. So. Lets do .9999999999


    equal to like 2.99999999999997... or about that... + 6


    8.99999999999999999999999999999.... cast it removes decimal.....


    8.
     
  13. Unread #7 - Jun 15, 2009 at 5:35 PM
  14. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    A simple guessing game [Source]

    My bad. D:

    Well done Swan.:laugh:
     
  15. Unread #8 - Jun 15, 2009 at 6:04 PM
  16. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    A simple guessing game [Source]

    I should ban you for that. OUR COLOR IS NOT EVEN THE SAME! >_________>
     
  17. Unread #9 - Jun 17, 2009 at 4:06 AM
  18. zoom pker
    Joined:
    Jan 23, 2009
    Posts:
    46
    Referrals:
    0
    Sythe Gold:
    0

    zoom pker Member

    A simple guessing game [Source]

    lol , nice guessing game it's looking good =]
     
  19. Unread #10 - Jun 17, 2009 at 7:46 PM
  20. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    A simple guessing game [Source]

    Haha I always think you're him for some reason...
     
  21. Unread #11 - Jun 18, 2009 at 5:39 PM
  22. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    A simple guessing game [Source]

    Plus Null. If it did include 1, it would be soooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo rare it would not even matter.... :/
     
  23. Unread #12 - Jun 18, 2009 at 8:29 PM
  24. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    A simple guessing game [Source]

    Err... Thanks?
     
< (Random)Anyone know of LUA? | Popular Conversion Tools Among People >

Users viewing this thread
1 guest


 
 
Adblock breaks this site