Thread: [Asteria 3.0] Trivia System

Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1. #1 [Asteria 3.0] Trivia System 
    Registered Member
    Optimum's Avatar
    Join Date
    Apr 2012
    Posts
    3,570
    Thanks given
    871
    Thanks received
    1,745
    Rep Power
    5000
    This is what we are adding:


    279 rewards


    first off go to com asteria game world and add a package called 'event.trivia'
    add all of the following classes
    Code:
    package com.asteria.game.world.event.trivia;
    
    import java.util.List;
    import java.util.Random;
    
    import com.asteria.game.World;
    import com.asteria.game.character.player.Player;
    import com.asteria.game.item.Item;
    
    /**
     * Handles the logics of the trivia and stores
     * @author Zack/Optimum
     *
     */
    public class TriviaBot 
    {
    
    	/**
    	 * The trivia question
    	 */
    	private TriviaQuestion triviaQuestion;
    	
    	/**
    	 * Object for a trivia typing question
    	 */
    	private TriviaTyping triviaTyping;
    	
    	/**
    	 * Checks if trivia is running or not
    	 */
    	private boolean triviaRunning;
    	
    	/**
    	 * Checks if the trivia question is a ::####### question
    	 */
    	private boolean isTypingQuestion;
    	
    	/**
    	 * Gets the trivia question object
    	 * @return - the trivia question object
    	 */
    	public TriviaQuestion getTriviaQuestion()
    	{
    		return triviaQuestion;
    	}
    	
    	/**
    	 * Gets the trivia typing object
    	 * @return - the trivia typing object
    	 */
    	public TriviaTyping getTriviaTyping()
    	{
    		return triviaTyping;
    	}
    	
    	/**
    	 * Checks if the trivia is running
    	 * @return - true if the trivia is running
    	 */
    	public boolean isRunning()
    	{
    		return triviaRunning;
    	}
    	
    	/**
    	 * Checks if the trivia is a typing question
    	 * @return - true if the trivia is a typing question
    	 */
    	public boolean isTypingQuestion()
    	{
    		return isTypingQuestion;
    	}
    	
    	/**
    	 * Generates a new trivia question
    	 */
    	public void generateNewTrivia() 
    	{
    		triviaRunning = true;
    		Random rnd = new Random();
    		
    		if(rnd.nextInt(2) == 0) 
    		{
    			isTypingQuestion = false;
    			triviaQuestion = TriviaQuestion.getRandomQuestion();
    			World.message("[@red@TRIVIA BOT@bla@] "+triviaQuestion.getQuestion() + "...");
    			World.message("[@red@TRIVIA BOT@bla@] Answer like ::answer [answer here] for a reward");
    		} 
    		else 
    		{
    			isTypingQuestion = true;
    			triviaTyping = new TriviaTyping();
    			triviaTyping.generateFullKey();
    			World.message("[@red@TRIVIA BOT@bla@] First person to type @red@::" + triviaTyping.getKey() + " @bla@for a reward!");
    		}
    	}
    	
    	private Random rnd = new Random();
    	
    	/**
    	 * Gives the player a reward
    	 * @param target - the targeted player
    	 */
    	public void giveReward(Player target) 
    	{
    		resetTrivia();
    		int amount = (1 + rnd.nextInt(70));
    		
    		List<TriviaReward> rewards = TriviaReward.generateRewards(amount);
    		
    		for(TriviaReward reward : rewards) 
    		{
    			int randomAmount = 0;
    			if(reward.getRandomAmount() != 0) 
    			{
    				randomAmount = rnd.nextInt(reward.getRandomAmount());
    			}
    			target.getInventory().add(new Item(reward.getItemId(), reward.getLowestAmount() + randomAmount));
    		}
    		World.message("[@red@TRIVIA BOT@bla@] " + target.getFormatUsername() + " has won "+ amount+ " rewards from the trivia!");
    	}
    	
    	/**
    	 * Resets the trivia
    	 */
    	private void resetTrivia()
    	{
    		triviaRunning = false;
    		triviaQuestion = null;
    		triviaTyping = null;
    	}
    }
    Code:
    package com.asteria.game.world.event.trivia;
    
    import com.asteria.task.Task;
    
    /**
     * Handles how often the trivia runs
     * 60 = every hour
     * @author Zack/Optimum
     *
     */
    public class TriviaEvent extends Task 
    {
    
    	/**
    	 * Object for a new trivia bot
    	 */
    	public static TriviaBot trivia = new TriviaBot();
    	
    	/**
    	 * constructor for TriviaEvent
    	 * Sets the timer for the task
    	 */
    	public TriviaEvent() 
    	{
    		super(30, true);
    	}
    
    	/**
    	 * Executes a new trivia
    	 */
    	@Override
    	public void execute() 
    	{
    		trivia.generateNewTrivia();
    	}
    }
    Code:
    package com.asteria.game.world.event.trivia;
    
    import java.util.Random;
    
    /**
     * Stores all of the trivia questions and possible
     * answers
     * @author Zack/Optimum
     *
     */
    public enum TriviaQuestion 
    {
    	
    	Q1("What does rsps stand for?", "runescape private server"),
    	Q2("Question 2", "pos answer 1", "pos answer 2");
    	
    	/**
    	 * The question string
    	 */
    	private String question;
    	
    	/**
    	 * All the possible answers
    	 */
    	private String[] answers;
    	
    	/**
    	 * Constructor for TriviaQuestions
    	 * @param question - the question
    	 * @param answers - the answers
    	 */
    	private TriviaQuestion(String question, String... answers) 
    	{
    		this.question = question;
    		this.answers = answers;
    	}
    	
    	/**
    	 * Gets the question
    	 * @return - the question
    	 */
    	public String getQuestion() 
    	{
    		return question;
    	}
    	
    	/**
    	 * gets the answers
    	 * @return - the answer
    	 */
    	public String[] getAnswers() 
    	{
    		return answers;
    	}
    	
    	/**
    	 * Gets a random question
    	 * @return - the random question
    	 */
    	public static TriviaQuestion getRandomQuestion() 
    	{
    		Random rnd = new Random();
    		return values()[rnd.nextInt(values().length)];
    	}
    	
    	/**
    	 * Checks if the player attempt is correct
    	 * @param attempt - the players attempt
    	 * @return - true
    	 * 				players attempt is correct
    	 * 			 false
    	 * 				players attempt is wrong
    	 */
    	public boolean isCorrect(String attempt) 
    	{
    		for(String answer : TriviaEvent.trivia.getTriviaQuestion().getAnswers()) 
    		{
    			if(attempt.equalsIgnoreCase(answer)) 
    			{
    				return true;
    			}
    		}
    		return false;		
    	}
    	
    }
    Code:
    package com.asteria.game.world.event.trivia;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    /**
     * Class contains all of the rewards
     * @author Zack/Optimum
     *
     */
    public enum TriviaReward 
    {
    	
    	/**
    	 * Common
    	 */
    	C1(554, 10, 50, TriviaRewardRarity.COMMON),
    	C2(555, 10, 50, TriviaRewardRarity.COMMON),
    	C3(556, 10, 50, TriviaRewardRarity.COMMON),
    	C4(557, 10, 50, TriviaRewardRarity.COMMON),
    	C5(43, 10, 25, TriviaRewardRarity.COMMON),
    	
    	/**
    	 * Uncommon
    	 */
    	U1(1333, 1, 0, TriviaRewardRarity.UNCOMMON),
    	U2(1079, 1, 0, TriviaRewardRarity.UNCOMMON),
    	U3(1127, 1, 0, TriviaRewardRarity.UNCOMMON),
    	U4(1163, 1, 0, TriviaRewardRarity.UNCOMMON),
    	U5(1201, 1, 0, TriviaRewardRarity.UNCOMMON),
    	
    	/**
    	 * Rare
    	 */
    	R1(1187, 1, 0, TriviaRewardRarity.RARE),
    	R2(4587, 1, 0, TriviaRewardRarity.RARE),
    	R3(4087, 1, 0, TriviaRewardRarity.RARE),
    	R4(6967, 1, 0, TriviaRewardRarity.RARE),
    	
    	
    	/**
    	 * Very rare
    	 */
    	V1(1038, 1, 0, TriviaRewardRarity.VERY_RARE),
    	V2(1040, 1, 0, TriviaRewardRarity.VERY_RARE),
    	V3(1042, 1, 0, TriviaRewardRarity.VERY_RARE),
    	V4(1044, 1, 0, TriviaRewardRarity.VERY_RARE),
    	V5(1046, 1, 0, TriviaRewardRarity.VERY_RARE),
    	V6(1048, 1, 0, TriviaRewardRarity.VERY_RARE);
    	
    	/**
    	 * Variables
    	 */
    	private int itemId, lowestAmount, randomAmount;
    	
    	private TriviaRewardRarity rarity;
    
    	/**
    	 * Constructor for TriviaReward
    	 * @param itemId - The item id
    	 * @param lowestAmount - Lowest amount to give the player
    	 * @param highestAmount - Highest amount to give to a player
    	 * @param rarity - the rarity level
    	 */
    	private TriviaReward(int itemId, int lowestAmount, int randomAmount, TriviaRewardRarity rarity) 
    	{
    		this.itemId = itemId;
    		this.lowestAmount = lowestAmount;
    		this.randomAmount = randomAmount;
    		this.rarity = rarity;
    	}
    	
    	/**
    	 * Generates a random number
    	 */
    	private static Random rnd = new Random();
    	
    	/**
    	 * Gets the item id
    	 * @return - the item id
    	 */
    	public int getItemId()
    	{
    		return itemId;
    	}
    	
    	/**
    	 * Gets the lowest possible amount
    	 * @return - the possible amount
    	 */
    	public int getLowestAmount()
    	{
    		return lowestAmount;
    	}
    	
    	/**
    	 * Gets the random amount to give
    	 * @return
    	 */
    	public int getRandomAmount()
    	{
    		return randomAmount;
    	}
    	
    	/**
    	 * Generates a random reward list.
    	 * The list size will be determined by the amount param
    	 * @param amount - the amount of rewards
    	 * @return - the reward list
    	 */
    	public static List<TriviaReward> generateRewards(int amount) 
    	{
    		List<TriviaReward> temp = new ArrayList<>();
    		temp.clear();
    		
    		while(temp.size() != amount) 
    		{
    			int index = rnd.nextInt(values().length);
    			int random = (1 + rnd.nextInt(300));
    			
    			TriviaReward currentReward = values()[index];
    			
    			if(random % currentReward.rarity.getRarity() == 0) 
    			{
    				temp.add(currentReward);
    			} 
    		}
    		return temp;
    	}
    	
    }
    Code:
    package com.asteria.game.world.event.trivia;
    
    /**
     * The different types of rarity for rewards
     * @author Zack/Optimum
     *
     */
    public enum TriviaRewardRarity 
    {
    	
    	COMMON(1),
    	UNCOMMON(15),
    	RARE(50),
    	VERY_RARE(250);
    	
    	/**
    	 * The rarity
    	 */
    	private int rarity;
    	
    	/**
    	 * Constructor for TriviaRewardRarity
    	 * @param rarity - the rarity
    	 */
    	private TriviaRewardRarity(int rarity)
    	{
    		this.rarity = rarity;
    	}
    	
    	/**
    	 * Gets the rarity
    	 * @return - the rarity
    	 */
    	public int getRarity()
    	{
    		return rarity;
    	}
    	
    }
    Code:
    package com.asteria.game.world.event.trivia;
    
    import java.util.Random;
    
    /**
     * Handles the creation of a new random key used for
     * the player command
     * @author Zack/Optimum
     *
     */
    public class TriviaTyping 
    {
    
    	/**
    	 * Will store the final key
    	 */
    	private StringBuilder key = new StringBuilder();
    	
    	/**
    	 * A temperary key to store all the characters and numbers
    	 */
    	private StringBuilder tempKey = new StringBuilder();
    	
    	/**
    	 * The length of the key
    	 */
    	private static final int KEY_LEN = 8;
    	
    	/**
    	 * Used for selecting random indexes of the {@link tempKey}
    	 */
    	private Random rnd = new Random();
    	
    	/**
    	 * Generated the temperary key and the whole key
    	 */
    	public void generateFullKey()
    	{
    		generateTempKey();
    		generateTypingKey();
    	}
    	
    	/**
    	 * Generates the temperary key
    	 * used for getting indexes
    	 */
    	private void generateTempKey() 
    	{
    		for(char i = 'a'; i < 'z'; i++ ) 
    		{
    			tempKey.append(i);
    		}
    		for(char i = '0'; i < '9'; i++ )
    		{
    			tempKey.append(i);
    		}
    	}
    	
    	/**
    	 * Generates the final key that will be used 
    	 * for the command
    	 */
    	private void generateTypingKey() 
    	{
    		key.delete(0, key.length());
    		
    		for(int i = 0; i < KEY_LEN; i++) 
    		{
    			int index = rnd.nextInt(tempKey.length());
    			key.append(tempKey.charAt(index));
    		}
    	}
    	
    	/**
    	 * Gets the current key
    	 * @return - the key
    	 */
    	public String getKey() 
    	{
    		return key.toString();
    	}
    	
    }
    now in server.java find
    Code:
    TaskHandler.submit(new MinigameHandler());
    and under that add this:
    Code:
    TaskHandler.submit(new TriviaEvent());
    now go to commands.groovy

    and add under
    Code:
    String[] cmd = context.text
    add this:

    Code:
    if(TriviaEvent.trivia.isTypingQuestion() == true && TriviaEvent.trivia.isRunning()) {
    			if(cmd[0].equalsIgnoreCase(TriviaEvent.trivia.getTriviaTyping().getKey())) {
    				TriviaEvent.trivia.giveReward(player)
    				return;
    			}
        	}
    now below this
    Code:
    switch (cmd[0]) {
    add this
    Code:
    case "trivia":
    					if(TriviaEvent.trivia.getTriviaQuestion() != null) {
    						player.encoder.sendMessage(TriviaEvent.trivia.triviaQuestion.getQuestion())
    						return
    					} else if(TriviaEvent.trivia.isTypingQuestion() != null) {
    						player.encoder.sendMessage("first one to type: @red@::" + TriviaEvent.trivia.getTriviaTyping().getKey())
    						return
    					}
    					player.encoder.sendMessage("There is no trivia at the moment!")
    				break
    				
    				case "answer":
    					String attempt = ""
    					for(int i = 1; i < cmd.length; i++) {
    						if(i == cmd.length - 1)
    							attempt += (cmd[i])
    						else
    							attempt += (cmd[i] + " ")
    					}
    					if (TriviaEvent.trivia.getTriviaQuestion().isCorrect(attempt)) {
    						TriviaEvent.trivia.giveReward(player)
    					} else {
    						player.encoder.sendMessage("Wrong! Please try again!")
                		}
    					break
    then import these:
    Code:
    import com.asteria.game.world.event.trivia.TriviaBot
    import com.asteria.game.world.event.trivia.TriviaQuestion
    import com.asteria.game.world.event.trivia.TriviaEvent;
    remember to thanks the thread if you used

    Quote Originally Posted by DownGrade View Post
    Don't let these no life creeps get to you, its always the same on here. They'd rather spend hours upon hours in the rune-server spam section then getting laid! ha ha!Its honestly pathetic i haven't seen so many lowlifes in my life its actually insane i wish that this section would just vanish its probably the only way to get these people out of the community...
    PLEASE BE AWARE OF IMPOSTERS MY DISCORD ID: 362240000760348683
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Capital Investor
    Dawson's Avatar
    Join Date
    May 2015
    Posts
    25
    Thanks given
    1
    Thanks received
    2
    Rep Power
    9
    Cool codes, sir. Might be very usefull for a lot of server developpers. Good job!
    Reply With Quote  
     

  4. Thankful user:


  5. #3  


    Major's Avatar
    Join Date
    Jan 2011
    Posts
    2,997
    Thanks given
    1,293
    Thanks received
    3,556
    Rep Power
    5000
    Code:
     */
    	public TriviaQuestion triviaQuestion = null;
    	
    	/**
    	 * Object for a trivia typing question
    	 */
    	public TriviaTyping triviaTyping = null;
    	
    	/**
    	 * Checks if trivia is running or not
    	 */
    	public boolean triviaRunning = false;
    	
    	/**
    	 * Checks if the trivia question is a ::####### question
    	 */
    	public boolean isTypingQuestion = false;
    All of these assignments are pointless, as instance and static variables will be given default values (and null and false are the default values for object/boolean types). These values should also all be private, and you should be using getters/setters to encapsulate them.

    You're also mixing your brace style, having them on the same line as the preceeding expression, but also on the next line: stick to one (ideally with them on the same line, as you're writing Java).

    Use more blank lines, otherwise your code is hard to read.

    The names in TriviaReward are crap (but use of TriviaRewardRarity instead of an integer value is good, keep that up).
    Reply With Quote  
     

  6. Thankful users:


  7. #4  
    Registered Member
    Optimum's Avatar
    Join Date
    Apr 2012
    Posts
    3,570
    Thanks given
    871
    Thanks received
    1,745
    Rep Power
    5000
    Quote Originally Posted by Major View Post
    Code:
     */
    	public TriviaQuestion triviaQuestion = null;
    	
    	/**
    	 * Object for a trivia typing question
    	 */
    	public TriviaTyping triviaTyping = null;
    	
    	/**
    	 * Checks if trivia is running or not
    	 */
    	public boolean triviaRunning = false;
    	
    	/**
    	 * Checks if the trivia question is a ::####### question
    	 */
    	public boolean isTypingQuestion = false;
    All of these assignments are pointless, as instance and static variables will be given default values (and null and false are the default values for object/boolean types). These values should also all be private, and you should be using getters/setters to encapsulate them.

    You're also mixing your brace style, having them on the same line as the preceeding expression, but also on the next line: stick to one (ideally with them on the same line, as you're writing Java).

    Use more blank lines, otherwise your code is hard to read.

    The names in TriviaReward are crap (but use of TriviaRewardRarity instead of an integer value is good, keep that up).
    About the braces; its actually a habbit from c# lol tonight ill clean this up and add/change the stuff you recommended. Thanks a lot for the feedback

    Quote Originally Posted by DownGrade View Post
    Don't let these no life creeps get to you, its always the same on here. They'd rather spend hours upon hours in the rune-server spam section then getting laid! ha ha!Its honestly pathetic i haven't seen so many lowlifes in my life its actually insane i wish that this section would just vanish its probably the only way to get these people out of the community...
    PLEASE BE AWARE OF IMPOSTERS MY DISCORD ID: 362240000760348683
    Reply With Quote  
     

  8. #5  
    Registered Member

    Join Date
    Nov 2014
    Posts
    253
    Thanks given
    39
    Thanks received
    146
    Rep Power
    248
    Quote Originally Posted by Major View Post
    Code:
     */
    	public TriviaQuestion triviaQuestion = null;
    	
    	/**
    	 * Object for a trivia typing question
    	 */
    	public TriviaTyping triviaTyping = null;
    	
    	/**
    	 * Checks if trivia is running or not
    	 */
    	public boolean triviaRunning = false;
    	
    	/**
    	 * Checks if the trivia question is a ::####### question
    	 */
    	public boolean isTypingQuestion = false;
    All of these assignments are pointless, as instance and static variables will be given default values (and null and false are the default values for object/boolean types). These values should also all be private, and you should be using getters/setters to encapsulate them.

    You're also mixing your brace style, having them on the same line as the preceeding expression, but also on the next line: stick to one (ideally with them on the same line, as you're writing Java).

    Use more blank lines, otherwise your code is hard to read.

    The names in TriviaReward are crap (but use of TriviaRewardRarity instead of an integer value is good, keep that up).
    Don't see why this is so important. I mean surely conventions and such are useful, and indisputable, yet it really fails to make the code any better. Now before I get the "OMG but conventions ughghh READABILOTY zzzz" crew, I'll explain myself: you're missing much more important aspects of the code, just by using proper access modifiers or adding more spaces doesn't make the functionality of the code any better, its would be the same terrible code but now with better conventions (yippee)! Yes ladies and gentleman, its far more important that your code actually work, be easy to use (proper conventions are part of this aspect, but not even a large chunk), & scalable than if variable x is public or private or if the names in your enum are good enough (suprise, suprise). Now this isn't that much of a personal attack but your post was a good example of what I've been seeing: people ripping code apart for the lack of regard for 100% proper conventions, while ignoring the blaring problems staring them in the face.

    OT: Your TriviaReward enum - terribly redundant. You need to do something about that. I mean personally for something like this I wouldn't use enum constants and just go with loading from a file

    class TriviaReward: id, minAmount, maxAmount, rarity

    This will give you a lot more flexibility in grouping the numbers, so you don't need to specify rarity EVERY TIME, i.e.

    common: {
    id1 (nothing, default args considered 1-1)
    id2: 1-2 (your highestAmount currently is misleading, you should do it so that your rng does highest - lowest, so when you add lowest + generated you can get BETWEEN lowest and highest)

    };
    and so on

    Your TriviaTyping should just use a constant: static String CHARS = "abcdefghijklmnopqrstuvwxyz123456789" and you can add an infinite # of chars (not infinite but you get the point)


    This will be problematic:
    Code:
    	public static boolean isCorrect(String attempt) {
    		for(TriviaQuestion t : values()) {
    			for(String answer : t.getAnswers()) {
    				if(attempt.equalsIgnoreCase(answer)) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    If the answer to any of your questions is "rome", I could type in "rome" as the answer to ANY question and it would be correct. Think about it

    Finally, your getRewards is interesting, though not wrong I do think it could be nicer. The way you do it now: you scroll through every reward, check if the random number modulus something is equal to 0, and then if the rarity matches the required rarity, then wallah!. Instead you could generate a number, and each rarity has its own discrete set of numbers. I.e. (also give the TriviaRarity enum some use)

    COMMON(Integer.MAX_VALUE)
    UNCOMMON(80)
    RARE(5)
    VERY_RARE(1)

    static Rarity forRand(int randomNumber) {
    for(int i = values().length - 1; i >= 0; i++)
    if(randomNumber < values()[i].val)
    return r;
    }

    That way if you did lets say random (2000), it would be a 2/2001 (assuming you include 0) chance to get very rare, and so on

    Rarity rarity = Rarity.forRandom(rng.nextInt(1000))
    List items = map.get(rarity) //map all rarities to rewards, this is another way you could do this
    Reward r = items.getRandom...

    and do that for all amounts (I just find it nicer)
    Reply With Quote  
     

  9. Thankful user:


  10. #6  


    Major's Avatar
    Join Date
    Jan 2011
    Posts
    2,997
    Thanks given
    1,293
    Thanks received
    3,556
    Rep Power
    5000
    Quote Originally Posted by Intrice Joe View Post
    Don't see why this is so important. I mean surely conventions and such are useful, and indisputable, yet it really fails to make the code any better. Now before I get the "OMG but conventions ughghh READABILOTY zzzz" crew, I'll explain myself: you're missing much more important aspects of the code, just by using proper access modifiers or adding more spaces doesn't make the functionality of the code any better, its would be the same terrible code but now with better conventions (yippee)! Yes ladies and gentleman, its far more important that your code actually work, be easy to use (proper conventions are part of this aspect, but not even a large chunk), & scalable than if variable x is public or private or if the names in your enum are good enough (suprise, suprise). Now this isn't that much of a personal attack but your post was a good example of what I've been seeing: people ripping code apart for the lack of regard for 100% proper conventions, while ignoring the blaring problems staring them in the face.
    I reject the idea that adhering to conventional guidelines does not improve code. My post is intentionally about writing neat code, not about redesigning it. You are more than welcome to promote other ideas about improving the code in your own posts, but please don't treat my own suggestions as if they should be the same as yours. I certainly agree that there are other important aspects to consider, and that I only commented on one aspect, but so did you.

    Quote Originally Posted by Intrice Joe View Post
    OT: Your TriviaReward enum - terribly redundant. You need to do something about that. I mean personally for something like this I wouldn't use enum constants and just go with loading from a file
    Sure, splitting data from code is usually a good idea.

    Quote Originally Posted by Intrice Joe View Post
    class TriviaReward: id, minAmount, maxAmount, rarity

    This will give you a lot more flexibility in grouping the numbers, so you don't need to specify rarity EVERY TIME, i.e.

    common: {
    id1 (nothing, default args considered 1-1)
    id2: 1-2 (your highestAmount currently is misleading, you should do it so that your rng does highest - lowest, so when you add lowest + generated you can get BETWEEN lowest and highest)

    };
    and so on
    Well you can do this by overloading the enum constructor, so it's not exactly an advantage of using a file.

    Quote Originally Posted by Intrice Joe View Post
    Your TriviaTyping should just use a constant: static String CHARS = "abcdefghijklmnopqrstuvwxyz123456789" and you can add an infinite # of chars (not infinite but you get the point)
    Not sure what this means


    Quote Originally Posted by Intrice Joe View Post
    This will be problematic:
    Code:
    	public static boolean isCorrect(String attempt) {
    		for(TriviaQuestion t : values()) {
    			for(String answer : t.getAnswers()) {
    				if(attempt.equalsIgnoreCase(answer)) {
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    If the answer to any of your questions is "rome", I could type in "rome" as the answer to ANY question and it would be correct. Think about it
    Good spot

    Please use code tags more thoroughly
    Reply With Quote  
     

  11. #7  
    Registered Member

    Join Date
    Nov 2014
    Posts
    253
    Thanks given
    39
    Thanks received
    146
    Rep Power
    248
    Quote Originally Posted by Major View Post
    I reject the idea that adhering to conventional guidelines does not improve code. My post is intentionally about writing neat code, not about redesigning it. You are more than welcome to promote other ideas about improving the code in your own posts, but please don't treat my own suggestions as if they should be the same as yours. I certainly agree that there are other important aspects to consider, and that I only commented on one aspect, but so did you.

    Well you can do this by overloading the enum constructor, so it's not exactly an advantage of using a file.
    What I see here, very often, are "neat-freaks", who spend posts upon posts talking about how to properly use names, or when to encapsulate, or things of this sort. Your post simply addressed such issues of naming and data hiding. That's easy (by that I mean its just textbook, sure you can point it out on every thread, but it really requires none of your own intuition - might as well link them to your cleaner code thread).

    Design is far (I know this might be suprising for you), far more important than simple conventions. Now I don't mean completely TERRIBLE conventions, but if seen here and there you must focus on the big picture.

    Here's a good analogy: If you encounter a paragraph that makes absolutely no sense, should your only comment be:
    "Please indent the first line, add a comma here and fix this spelling mistake" - but at the end of the day it is still a terrible paragraph with a poor thought process and possibly invalid logic. Does the proper spelling make it look better? Sure. Is it important? I would say so. Is it more important, however, than having a concise and logical structure? No. Spelling mistakes and such come along the way, they can be fixed as they come, but the only way to truely fix something is to attack the core (before it gets out of control, naming can always be fixed but people can get too deep into their current design to want to redesign it).

    E.G.. I develop an essay on my plan of investigation specified in lets say paragraph 1 which outlines what in my mind is a good design, but it would stunt me 3/4 the way through. Now I either rewrite my essay or make it progressively worse and worse by either deterring from the structure or by continuing the flawed structure. Good thing I have my periods in the right place though!

    Summary: I wouldn't say conventions are not important, they're simply secondary to good design. You can post your convention things all you want, just weigh their importance properly - in the real world a few problems with conventions here and there just aren't that big a deal.


    -Second part I was saying you should use a file because it would faciliate in the creation of the system. Not really something to put in an enum (and there are a ton of ways to go about it)
    Reply With Quote  
     

  12. #8  
    Registered Member
    Optimum's Avatar
    Join Date
    Apr 2012
    Posts
    3,570
    Thanks given
    871
    Thanks received
    1,745
    Rep Power
    5000
    Yea thanks intrice Joe for the spot with the loop and for all the feedback

    Quote Originally Posted by DownGrade View Post
    Don't let these no life creeps get to you, its always the same on here. They'd rather spend hours upon hours in the rune-server spam section then getting laid! ha ha!Its honestly pathetic i haven't seen so many lowlifes in my life its actually insane i wish that this section would just vanish its probably the only way to get these people out of the community...
    PLEASE BE AWARE OF IMPOSTERS MY DISCORD ID: 362240000760348683
    Reply With Quote  
     

  13. #9  
    Donator


    Join Date
    Sep 2007
    Age
    27
    Posts
    2,426
    Thanks given
    125
    Thanks received
    505
    Rep Power
    386
    Quote Originally Posted by Major View Post
    All of these assignments are pointless, as instance and static variables will be given default values (and null and false are the default values for object/boolean types). These values should also all be private, and you should be using getters/setters to encapsulate them.
    A lot of IDEs kick up a fuss if you reference a variable which hasn't necessarily been set (specifically, a variable which wasn't assigned in ALL constructors).
    Reply With Quote  
     

  14. #10  


    Major's Avatar
    Join Date
    Jan 2011
    Posts
    2,997
    Thanks given
    1,293
    Thanks received
    3,556
    Rep Power
    5000
    Code:
    				String attempt = ""
    					for(int i = 1; i < cmd.length; i++) {
    						if(i == cmd.length - 1)
    							attempt += (cmd[i])
    						else
    							attempt += (cmd[i] + " ")
    					}
    use StringJoiner

    Code:
    /**
    	 * Object for a new trivia bot
    	 */
    	public static TriviaBot trivia = new TriviaBot();
    Shouldn't be static

    Quote Originally Posted by donar fabian View Post
    A lot of IDEs kick up a fuss if you reference a variable which hasn't necessarily been set (specifically, a variable which wasn't assigned in ALL constructors).
    Neither eclipse nor IDEA do by default...

    Quote Originally Posted by Intrice Joe View Post
    - waste of reading -
    Sure, I can see that you didn't actually read my post, particularly the parts where I stated that "there are other important aspects to consider" and the intentions of my post. I can only assume your waste of a post here is because you somehow misinterpreted my reply as a personal attack, which I'm not really sure how. Also the fact that you have to include snide remarks like "I know this might be surprising for you" only shows that your own argument is weak (and also that you have clearly made a complete leap from two posts on this thread instead of anything else I've said).

    Sure you can stick the stuff in a file, which is great because:
    • You have to ship a parser too.
    • You have the code overhead from that parser (e.g. testing - although as you didn't mention this at all, i'll tell you that testing is important, although I know this might be surprising for you).
    • Errors in the file are a runtime error instead of a compile-time error (which would not be the case if they were stored in an enum).


    So I wouldn't call the original choice "ignoring the blaring problems staring them in the face."

    Sticking stuff in a file is separating code from data sure, but at some point you're going to have to combine the two. This code has about a dozen enumerators (not exactly code bloat), which leads me to believe that you are one of the truly pitiful programmers that are dogmatic about "best practices" even in the face of common sense, and that is sure as hell worse than being a "neat-freak".
    Last edited by Major; 05-20-2015 at 10:46 AM.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Trivia system !
    By blaxe in forum Tutorials
    Replies: 9
    Last Post: 09-15-2014, 11:58 AM
  2. [Asteria] Basic pickpocket system
    By Jamili in forum Snippets
    Replies: 7
    Last Post: 06-12-2014, 05:03 AM
  3. Help With The Trivia System
    By Formality in forum Help
    Replies: 1
    Last Post: 12-29-2013, 09:08 PM
  4. 718 trivia system
    By alz3emz in forum Help
    Replies: 16
    Last Post: 12-02-2013, 11:33 PM
  5. [PI] Trivia System by DarkInfantry
    By DarkInfantry in forum Snippets
    Replies: 1
    Last Post: 07-16-2013, 10:59 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •