• Log in or sign up for an account here: https://www.sythe.org/login

A simple guessing game [Source]

JVT

Newcomer
Joined
May 22, 2009
Sythe Gold
0
Messages
13
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!!!!!
 
A simple guessing game [Source]
I will try tomorrow. Will edit and not post another post after I tried.
 
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.
 
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.

I use that same code pretty much. It works perfectly. Math.random() returns a double between 0 and 1, so the cast is necessary.
 
A simple guessing game [Source]
It doesn't work as it should... take inputs of min 6 and max 8 as an example.
return (int) (Math.random() * (max - min + 1)) + min;
return (int) (1 * (8 - 6 + 1)) + 6;
return (int) (1 * 3) + 6;
return (int) 9;

It should be
Code:
return (int) (Math.random() * (max - min) + min);
 
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.
 
A simple guessing game [Source]
My bad. D:

Well done Swan.:laugh:
 
A simple guessing game [Source]
A simple guessing game [Source]
lol , nice guessing game it's looking good =]
 
A simple guessing game [Source]
I should ban you for that. OUR COLOR IS NOT EVEN THE SAME! >_________>
Haha I always think you're him for some reason...
 
A simple guessing game [Source]
Plus Null. If it did include 1, it would be soooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo rare it would not even matter.... :/
 
A simple guessing game [Source]
Runescape Gamblingrunescape gambling
Back
Top