Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 644 645 [646] 647 648 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887853 times)

MoonyTheHuman

  • Bay Watcher
  • I think the DEC VAX hates me.
    • View Profile
    • hellomouse

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9676 on: June 04, 2016, 10:12:24 pm »

Random question, but in what language(s) could the thread title be syntactically correct?
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9677 on: June 04, 2016, 10:14:00 pm »

Good question...
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #9678 on: June 04, 2016, 10:18:02 pm »

Easy. Python.

Code: [Select]
class Class:
    def isCoder(self):
        return True

def post():
    print("Hello Observable Universe")

self = Class()
if self.isCoder(): post() #Programming Thread

It, uh, needs some setup though.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9679 on: June 04, 2016, 10:18:33 pm »

Heh.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9680 on: June 05, 2016, 05:28:32 pm »

Okay so I got my diceroller project working. Then I tried to roll multiple dice at the same time but that failed. Which is fine I was happy to where I got it but now I want to tackle OOP programming and I was wondering if someone could give me a simple summary of how that goes?

I did OOP once with a bank account idea in my Java class but ultimately I failed, passing only because my proffesor was such a nice guy. I really want to learn this skill so I prove to myself that programming really is for me.

Plan/Algorithm:

Make an abstract object called Dice. Said object will have the trait sides using Int.

Then make D6, D10, D12, and etc dice.

Finally I want the ability to 'roll' them using a random generator and to roll multiple dice at the same time.

EX: roll 6 D6 dice that give me 6 numbers.
~~~~~~~~~~~~~~~~~~~~~~~~~~

Problem is that after 'setting up' a abstract idea I don't know how to make the constructors work.

Does the Dice and D# object blueprints need to be in the same class?

I'd also like to say that while there are EASIER ways to do the whole dice rolling thing that I want to ultimately use it to learn OOP style programming. I feel it in my bones that if I can't code in this style, then by the time I reach Uni then everything I've worked for becomes null.
Logged

frostshotgg

  • Bay Watcher
  • It was a ruse
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9681 on: June 05, 2016, 05:38:12 pm »

How did you do it before? If you did it the way most of us were suggesting it should be a relatively simple modification, but we need to know what you have to work with.
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9682 on: June 05, 2016, 05:42:50 pm »

It's in my laptop and my Wifi dosen't work. I'll post it later today but if I remember it was something like this:

1. main asks for you to pick 1,2,3.
2. 1 and 2 forces the pc to go to a 'diceroller' class. Then return an answer (which was a random number).
3. Display number and do it all again until user hits 3.

I know that's not descriptive but that's all I remember. No ACTUAL code.

Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9683 on: June 05, 2016, 06:29:35 pm »

Quote
Problem is that after 'setting up' a abstract idea I don't know how to make the constructors work.

Well, let's look at just this one thing then.

First up, what is a constructor? It's a function that is run when you instantiate an object. The rules are:

- a constructor function has the same name as the class
- a constructor function has the return type omitted (actually, it returns the created object)
- you can make more than 1 constructor, as long as each one takes different parameters

e.g. for a class called "Dice", any function called "Dice" inside it is by definition treated as a constructor. Here is a really basic Dice class. I omitted the "roll dice" code, but included two constructors:

class Dice
{
    int sides;

    Dice()
    {
        sides = 6;
    }

    Dice(int _sides)
    {
        sides = _sides;
    }
};

and that's pretty much it. You call the Dice constructor from outside the class:

main()
{
    Dice FirstDice; // since we didn't include a parameter, it calls the "Dice()" constructor, and sets sides to 6 by default

    Dice SecondDice(8); // give me an 8-sided dice.

    Dice ThirdDice(6); // ask for a 6-sided dice explicitly
}

Pretty much just focus on understanding what's happening here for now. If there are more than 1 constructor, then the program chooses 1 to call based on the parameters you've included when creating the Dice objects in main().
« Last Edit: June 05, 2016, 06:39:46 pm by Reelya »
Logged

frostshotgg

  • Bay Watcher
  • It was a ruse
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9684 on: June 05, 2016, 06:35:39 pm »

Minor correction (if I remember Java syntax right):
You can't just set sides to that value there because of scope or something. At the very least, it's a style thing. It should be
Code: [Select]
    Dice(int _sides)
    {
        this.sides = _sides;
    }
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9685 on: June 05, 2016, 06:41:42 pm »

It's not needed. It also is going off the topic of how to make a constructor. 3man75 is a beginner, the basic stuff is confusing him already so we shouldn't bring in anything except the bare minimum to get him started. What I wrote wasn't strictly Java, but it's code that will compile pretty much as-is on several languages such as C++/C# and Java.
« Last Edit: June 05, 2016, 06:51:07 pm by Reelya »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9686 on: June 05, 2016, 08:21:44 pm »

Okay taking a look at my code now.

How did you do it before? If you did it the way most of us were suggesting it should be a relatively simple modification, but we need to know what you have to work with.

This is mostly just an FYI but...

Spoiler (click to show/hide)
Logged

frostshotgg

  • Bay Watcher
  • It was a ruse
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9687 on: June 05, 2016, 08:45:19 pm »

Okay so the mental shift you're going to want to have here is to change what you view the DiceRoller class as from "Collection of functions" to "Separate thing that you tell to do things" if that makes sense. You want to construct a DiceRoller, and then that object persists and has a value, pretend that you're creating a physical object in virtual space, in this case a die.

You'll want your DiceRoller class to have a variable to store whatever side of the die is facing up, as well as a function to roll the die which changes the variable, and a function to read what that value is. The theory is that you construct persistent objects, and then you tell it to change and it holds some permanence so that you can refer to it from multiple different functions or loops or whatever and it will be the same.

Spoiler: Example Pseudocode (click to show/hide)

Also you may want to use flash drives in the future instead of manually typing stuff like that. Just a thought. :P
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9688 on: June 05, 2016, 08:45:54 pm »

@3man75, to learn how to adapt the code to a class, you should probably remove all the user-interaction, simplify it in main() as much as possible, then you can see how making objects is just an extension of the thinking involved in making functions. I'm going to write this in C++ style for brevity, but you can adjust it for Java style:

Let me show some steps to "evolve" a class from the bare code. The "code" here is only two lines. 1 to initialize the Random object, and another to store a random number from 1 to 6 in a variable called "result".

Code: [Select]
void main()
{
   Random rng = new Random();
   int result = rng.nextInt(6) + 1;
}

The first thing you can do is package this in a function so that it can be re-used:

Code: [Select]
int Roll()
{
   Random rng = new Random();
   return rng.nextInt(6) + 1;
}

void main()
{
   int result = Roll();
}

The next level is to have a function parameter for the "sides":

Code: [Select]
int Roll(int sides)
{
   Random rng = new Random();
   return rng.nextInt(sides) + 1;
}

void main()
{
   int result = Roll(6); // pass value "6" as the sides of the dice for the roll
}

To turn this into a class you just wrap a class block around the function:

Code: [Select]
class Dice
{
   int Roll(int sides) // this is the same function as above, we've just packaged it in an object called "Dice"
   {
      Random rng = new Random();
      return rng.nextInt(sides) + 1;
   }
}

void main()
{
   Dice dice; // make a dice so we can call it
   int result = dice.Roll(6); // still passing 6 as the "sides" each time we roll
}

The last step is where we start actually using some OO specifics. Since objects can remember things, we can make a variant of the Dice object that remembers how many sides it has:

Code: [Select]
class Dice
{
   int sides; // "data slot" to remember how many sides there are

   Dice(int _sides) // constructor, which takes a temp value called "_sides"
   {
      sides = _sides; // copy the temp value to the permanent one
   }

   int Roll() // we don't need to pass a "sides" value any more, because a Dice object remembers how many sides it has
   {
      Random rng = new Random();
      return rng.nextInt(sides) + 1;
   }
}

void main()
{
   Dice dice(6); // make a dice with 6 sides
   int result = dice.Roll(); // roll our six-sided dice
}

Finally, we can notice that we're making a "new Random()" every time we roll the dice. This is not good, because a random object can be called over and over. We can make the variable "Random rng" be part of the class itself, and only initialize it when we create our Dice object:

Code: [Select]
class Dice
{
   int sides; // "data slot" to remember how many sides there are
   Random rng; // our random number generator is stored for this class

   Dice(int _sides) // constructor, which takes a temp value called "_sides"
   {
      sides = _sides; // copy the temp value to the permanent one
      rng = new Random(); // set up the rng only when the class is created
   }

   int Roll() // we don't need to pass a "sides" value any more, because a Dice object remembers how many sides it has
   {
      // we don't need to create a new rng here anymore each time we Roll
      return rng.nextInt(sides) + 1;
   }
}

void main()
{
   Dice dice(6); // make a dice with 6 sides
   int result = dice.Roll(); // roll our six-sided dice
}


You should be able to see how all these sections of code actually do the exact same thing, but they build on each other to become more modular, more general, and "object oriented".
« Last Edit: June 05, 2016, 09:49:14 pm by Reelya »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9689 on: June 05, 2016, 09:17:04 pm »

Minor correction (if I remember Java syntax right):
You can't just set sides to that value there because of scope or something. At the very least, it's a style thing. It should be
Code: [Select]
    Dice(int _sides)
    {
        this.sides = _sides;
    }
That's not a style thing I've ever done, but then again, syntax highlighting is a handy thing.  It's perfectly valid to refer to class variables inside methods so long as they're not trying to refer to instance variables within static methods none of which concerns 3man75 right now so I'm not going to get into that.

Also, for crying out loud 3man75, you only need the one Scanner, it's reuseable!

A cursory review:
Code: [Select]

Scanner ChoiceONE = new Scanner (System.in);
Scanner ChoiceSixSides = new Scanner (System.in);
Scanner howMany = new Scanner (System.in);
//You do not need three Scanners all pointed at System.in.  I dare say that it might cause issues to do so.

int indicator = 0;
//Dead variable, remove it.

System.out.println("Welcome to Dice Roller. Which dice would you like to throw?");
//Hey, where's the menu options?  The user doesn't know the right input unless they've ALREADY GONE THROUGH THE PROGRAM.  This is not a good thing.

int whichOptionInMenu = ChoiceONE.nextInt();

do {
if (whichOptionInMenu == 1) {

System.out.println("How many 6 sided dice do you want to roll?");

int throwerOne = howMany.nextInt();

DiceRoller mydice = new DiceRoller (throwerOne);

System.out.println(throwerOne() + " is what the dice rolled. \n");/*throwerOne is not a method, it is an integer, you cannot treat it like a method.  Also, you would print the number of dice and not the result even if you fixed that issue.  You want to call a method of your DiceRoller object that returns an integer.*/
}

if (whichOptionInMenu == 2) {

DiceRoller mydice = new DiceRoller();

mydice.DiceRollerTWO(whichOptionInMenu);
//Well, you have the theory of calling a method on an object, you just need to use it right.
}

System.out.println("");
System.out.println("Welcome back to Dice Roller. Which dice would you like to throw now? ");
System.out.println("");
System.out.println("Click option 1: for D6 or a six sided dice." + "Click Option 2: For D10 or 10 sided dice" + "Click Option 3: To quit the program.");//Also, the verb you're looking for isn't 'click'.

Which OptionInMenu = ChoiceONE.nextInt();
} While (whichOptionInMenu != 3);


CoiceONE.close();
//Misspelling.

//You forgot the close-bracket for the class.

======

Public class DiceRoller {
Random D6Generator = new Random();
Scanner howmany = new Scanner (System.in);

System.out.println("How many 6 sided dice do you want to roll?");
//Uhhh...No.  This does not go here.  At all.  Ever.
int throwerOne = howMany.nextInt();
//Ditto.
int sexSidedDice = D6Generator.nextInt(6) + 1;
//What is this FOR?
}
//Bracket mismatch.  The rest of your code won't compile.
Public Public Int DiceRollerTWO (int x) {
//Not valid syntax.
//Although if you took out one of the 'public' s it would be.
//You need to actually do something with your parameter x here.  Or not ask for it.
Random D10 Generator = new Random();
//As an aside, why all these Randoms?  Math.random() works well enough for these purposes.  And you really do only need the one, as Reelya said.

int tenSidedDice = D10Generator.nextInt(10) + 1;

System.out.println("You got: " + tenSidedDice);
//No.  No no no.  This is what you put around the method call, not something you put in the method itself.

return tenSidedDice;
}

}

Please, for the love of Armok, get an IDE and code in it.  Most of these mistakes are the kind that would send red squiggly lines of error all down the screen.

This is not addressing how to do what you want to do.  This is just what was wrong with your original code.

@Reelya:You don't need to wait until the constructor to initialize the Random, by the way.  Just initializing it in the class variable declarations is valid.  Oh wait.  You wrote it in C++.

That's not helping with Java.  I'll come back to this tomorrow when I can computer as opposed to phone.
« Last Edit: June 05, 2016, 10:00:04 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.
Pages: 1 ... 644 645 [646] 647 648 ... 796