I'm reading some of next-year's homework for formal contracts and I'm not understanding this.
[img]http://puu.sh/8Dobs.png[/img]
The ensures clause is weird. Length of sequence s2 is equal to length of sequence s1 1 - 1, and...
Gibberish.
So right now, after finally finishing the Javascript course in Codecademy, I'm just trying to do a little test with adding objects to arrays:
[code]var contactList = [];
function addContact(name, isMale, age){
var brandNewPerson = true;
var newPerson = {
name: name,
isMale: isMale,
age: age
};
for (var i = 0;i < contactList.length;i++){
if (contactList[i] == newPerson) {
brandNewPerson = false;
}
}
if (brandNewPerson === false){
console.log("This person is already in the list!");
}
else{
contactList[contactList.length] = newPerson;
console.log("Added the person!");
}
}
function listContact(){
var printGender;
for (var i = 0;i < contactList.length;i++)
{
switch(contactList[i].isMale){
case true:
printGender = "Male";
break;
case false:
printGender = "Female";
break;
}
console.log(contactList[i].name + ", " + printGender + ", Age " + contactList[i].age);
}
}[/code]
What it should do is when I run the addContact() function, it first checks to see if any objects in array matches up with what's in the addContact(), and if there is, it gives a prompt and doesn't add in the newly made object.
i.e.
[code]addContact("Simon Belmont", true, 23);
addContact("Dracula", true, 9999999);
addContact("Simon Belmont", true, 23); //Should print out "This person is already in the list!" and not add it.[/code]
But for some reason, whenever I add in a duplicate, it still accepts it, so if I were to do the above example and use listContact():
[code]Simon Belmont, Male, Age 23
Dracula, Male, Age 9999999
Simon Belmont, Male, Age 23[/code]
Anyone know what's going on there?
You have three ='s.
[editline]7th May 2014[/editline]
Then again I know nothing of Javascript. Is it a thing?
[QUOTE=Shadow187(FP);44750052]You have three ='s.
[editline]7th May 2014[/editline]
Then again I know nothing of Javascript. Is it a thing?[/QUOTE]
Same result, unfortunately, but from what I heard, having three ='s is exactly the same as two ='s. But for some reason, Codecademy likes to nag me about how I'm not using === at certain points (such as when trying to compare true/false), which doesn't make sense! :downs:
Using one =, on the other hand, when it comes to if or else if or the like, doesn't function the same as == or === because it's an assignment, rather than a conditional expression that the latter two are.
The difference between === and ==, is that === checks both objects for class equality. 1 == "1" is true, however 1 === "1" is false.
Stackoverflow is amazing.
I don't know javascript; but from what I see [code](contactList[i] == newPerson)[/code] I would assume you would have to check each element within the var is equal such as newPerson.isMale == DEFAULT, newPerson.name == DEFAULT, ect. If I'm wrong don't hate me :( just trying to help.
[QUOTE=Proclivitas;44750135]I don't know javascript; but from what I see [code](contactList[i] == newPerson)[/code] I would assume you would have to check each element within the var is equal such as newPerson.isMale == DEFAULT, newPerson.name == DEFAULT, ect. If I'm wrong don't hate me :( just trying to help.[/QUOTE]
I actually have tried that before, whether it be newPerson.isMale, newPerson.name, and newPerson.age, but as you could imagine, it fails.
Do appreciate the help, though!
Also, just tried reversing it to make it:
[code](newPerson == contactList[i])[/code]
And including the element part with newPerson.name, etc. Didn't work, either!
So here's an interesting thought and question I'd like answered from somebody more knowledgeable than me. I rewrote AbeX300's program in Java.
[code]import components.set.Set;
import components.set.Set1L;
public final class ProgramWithIO {
private static class Person {
private String name;
private int age;
private boolean isMale;
private Person(String name, int age, boolean isMale) {
this.name = name;
this.age = age;
this.isMale = isMale;
}
private String getName() {
return this.name;
}
private int getAge() {
return this.age;
}
private boolean isMale() {
return this.isMale;
}
}
public static void addContact(Set<Person> contactList, String name,
int age, boolean isMale) {
contactList.add(new Person(name, age, isMale));
}
public static void printContacts(Set<Person> contactList) {
for (Person p : contactList) {
System.out.println("Name: " + p.getName() + "\nAge: " + p.getAge()
+ "\nGender: " + (p.isMale() ? "Male" : "Female") + "\n\n");
}
}
public static void main(String[] args) {
Set<Person> contactList = new Set1L<Person>();
addContact(contactList, "Vladimir Putin", 62, true);
addContact(contactList, "Shadow187(FP)", 19, true);
addContact(contactList, "Vladimir Putin", 62, true);
printContacts(contactList);
}
}
[/code]
Here's my result.
[code]Name: Vladimir Putin
Age: 62
Gender: Male
Name: Shadow187(FP)
Age: 19
Gender: Male
Name: Vladimir Putin
Age: 62
Gender: Male
[/code]
Now a Set in Java contains only unique elements. So how is this supposed to allow duplicates?
[editline]7th May 2014[/editline]
You can fix this by checking the individual elements of each Person object.
[code] public static void addContact(Set<Person> contactList, String name,
int age, boolean isMale) {
for (Person p : contactList) {
if ((p.isMale() == isMale) && (p.getAge() == age)
&& (p.getName().equals(name))) {
System.out.println("Duplicate contact: " + name
+ " found. Not adding.");
return;
}
}
contactList.add(new Person(name, age, isMale));
}[/code]
[code]Duplicate contact: Vladimir Putin found. Not adding.
Name: Shadow187(FP)
Age: 19
Gender: Male
Name: Vladimir Putin
Age: 62
Gender: Male
[/code]
Ahah AbeX300! I was right :P
[QUOTE=Shadow187(FP);44750201]So here's an interesting thought and question I'd like answered from somebody more knowledgeable than me. I rewrote AbeX300's program in Java.
--code--
Here's my result.
--code--
Now a Set in Java contains only unique elements. So how is this supposed to allow duplicates?
[/QUOTE]
I'm not familiar with the implementation of Set1L, but the way that Java's HashSet works (and Set1L may do the same, I don't know) is comparing objects by their hashcode. If you don't override the hashcode method provided by Object, then each object will have a hashcode generated for it (I think based on their address in memory but I could be wrong). Since it's not based on the instance methods those two objects (with name = "Vladimir Putin", etc) are not considered equal.
If you want to put Person objects into a set in Java you want to override the hashcode method (no arguments, returns an int). It's also good practice to override the equals(Object) method. Note that it takes an Object, not a Person - if you specify Person as the argument you are overloading the method, not overriding it.
Actually for HashSet it uses a hash table to organize the data, and when there are collisions (2+ objects that have the same hashcode but aren't necessarily equal) it uses the equals method to compare them.
[QUOTE=AbeX300;44750156]I actually have tried that before, whether it be newPerson.isMale, newPerson.name, and newPerson.age, but as you could imagine, it fails.
Do appreciate the help, though!
Also, just tried reversing it to make it:
[code](newPerson == contactList[i])[/code]
And including the element part with newPerson.name, etc. Didn't work, either![/QUOTE]
he's right though
[img]http://i.imgur.com/Db5wcto.gif[/img]
I'm getting a really weird issue with OpenTK.
I've created a new C# project, added OpenTK as a reference, and added this bare-bones code:
[csharp]using OpenTK;
using OpenTK.Graphics;
namespace Example
{
class Program : GameWindow
{
static void Main(string[] args)
{
using (var app = new Program()) {
app.Run();
}
}
}
}[/csharp]
However, I get an EntryPointNotFoundException from OpenTK trying to find SDL_GL_GetCurrentContext in SDL2.dll.
This is particularly odd because I'm using the exact same OpenTK binary as a project I started a couple of months ago, which still runs fine. I've tried getting the latest version of SDL2, but that hasn't helped. What could be going on?
[editline]8th May 2014[/editline]
This seems to be happening for each new project I try.
[QUOTE=Ziks;44752882]I'm getting a really weird issue with OpenTK.
I've created a new C# project, added OpenTK as a reference, and added this bare-bones code:
[csharp]using OpenTK;
using OpenTK.Graphics;
namespace Example
{
class Program : GameWindow
{
static void Main(string[] args)
{
using (var app = new Program()) {
app.Run();
}
}
}
}[/csharp]
However, I get an EntryPointNotFoundException from OpenTK trying to find SDL_GL_GetCurrentContext in SDL2.dll.
This is particularly odd because I'm using the exact same OpenTK binary as a project I started a couple of months ago, which still runs fine. I've tried getting the latest version of SDL2, but that hasn't helped. What could be going on?
[editline]8th May 2014[/editline]
This seems to be happening for each new project I try.[/QUOTE]
Can you try having Main(string[] args) without any arguments, so Main() ?
[QUOTE=Asgard;44753088]Can you try having Main(string[] args) without any arguments, so Main() ?[/QUOTE]
I can see where you are coming from, but it can find the Program.Main() entry point just fine. The issue is it not finding a particular entry point in SDL2.dll.
Just in case, I tested what you suggested and the result was identical.
I'm using curand_uniform in CUDA and it apparently returns "This function returns a sequence of pseudorandom floats uniformly distributed between 0.0 and 1.0. It may return from 0.0 to 1.0, where 1.0 is included and 0.0 is excluded." That is (0.0, 1.0]. How do i convert that to [0.0, 1.0)?
[QUOTE=Shadaez;44752267]he's right though
[img]http://i.imgur.com/Db5wcto.gif[/img][/QUOTE]
I think the "smart" == stuff in JS is just for primitive types.
[QUOTE=AbeX300;44750029]✂[/QUOTE]
newPerson and contactList[i] are two different instances of Object. Even if their properties match, which they do when you got two Simons, they're still not the same object - the first Simon is an Object in contactList[i], while the Simon you're trying to add afterwards (Mr. newPerson) is a freshly created object. Because JavaScript compares Objects by their identity and not their properties, your comparison will return false. This means that you'll have to manually compare each property in order to see whether one contact matches another.
While JavaScript doesn't let you do it, some other programming languages like Ruby actually let you see the internal ID of an object. While Ruby is more fundamentally object-oriented than JavaScript and also allows you to define your own rules for when two things are considered equal, the same concept principally applies to it: If the IDs of the Objects are equal, then the objects are equal. (The reality in Ruby is [url=http://ruby-doc.org/core-2.1.1/Object.html#method-i-eql-3F]a bit more complicated[/url] because everything is an Object there, even numbers. Comparisons therefore don't use the ID, but the "hash" of an object which is computed by the object based on the object's properties; otherwise, String literals wouldn't be comparable with "==" . Plain Objects and classes based on them without a "hash" method, like the Contact class in the following example, most likely use the object ID internally for generating the hash and nothing else, so that's why we get the same result here.)
[url=https://gist.github.com/meowy/e2a8596bf01d3ebe3242]Here[/url]'s a little Ruby script demonstrating Object equality in the way JavaScript handles it, together with an output log.
Here's your corrected (and reformatted) code:
[code]var contactList = [];
function addContact(name, isMale, age) {
var brandNewPerson = true;
var newPerson = {
name: name,
isMale: isMale,
age: age
};
for (var i = 0; i < contactList.length; i++) {
var match = contactList[i];
// This assumes that all properties need to match, not just the name.
if (match.name === newPerson.name &&
match.isMale === newPerson.isMale &&
match.age === newPerson.age) {
brandNewPerson = false;
// Bail out of the loop so we don't need to go through the rest
// of the contactList.
break;
}
}
if (brandNewPerson === false) {
console.log("This person is already in the list!");
} else {
// .push does the same as you had here before. It's just shorter
// and doesn't require you to get the length of the Array first.
contactList.push(newPerson);
console.log("Added the person!");
}
}
function listContact() {
var printGender;
for (var i = 0; i < contactList.length; i++) {
switch (contactList[i].isMale) {
case true:
printGender = "Male";
break;
case false:
printGender = "Female";
break;
}
console.log(contactList[i].name + ", " + printGender + ", Age " + contactList[i].age);
}
}[/code]
It works:
[img]http://i.imgur.com/jZq4j3U.png[/img]
[QUOTE=Ziks;44753094]I can see where you are coming from, but it can find the Program.Main() entry point just fine. The issue is it not finding a particular entry point in SDL2.dll.
Just in case, I tested what you suggested and the result was identical.[/QUOTE]
Maybe it requires an old version or you have the wrong target platform version of that dll...
But this would only happen if you somehow got the binaries mixed up or didn't set your project to the right target.
If you build from source it should just use the proper one, at least I never had to change anything.
[QUOTE=Tamschi;44754307]Maybe it requires an old version or you have the wrong target platform version of that dll...
But this would only happen if you somehow got the binaries mixed up or didn't set your project to the right target.
If you build from source it should just use the proper one, at least I never had to change anything.[/QUOTE]
I've tried rebuilding OpenTK, same result. Both the older projects that work and the new projects are targeting .NET 4.5 and Any CPU, as far as I can see they are identical in all important aspects.
[editline]8th May 2014[/editline]
I managed to break one of the working projects by swapping its OpenTK reference for the official nuget one. Maybe there's a bug with the latest version of OpenTK? Although it doesn't explain why the new project wouldn't work when I swapped its OpenTK dll with the one that worked with an older project.
[QUOTE=Ziks;44754761]
I managed to break one of the working projects by swapping its OpenTK reference for the official nuget one. Maybe there's a bug with the latest version of OpenTK? Although it doesn't explain why the new project wouldn't work when I swapped its OpenTK dll with the one that worked with an older project.[/QUOTE]
I'm pretty sure a friend of mine has this problem too. OpenTK 1.0 works fine but the latest dev build is broken, not sure if it's the same exception though
I found an even older version of OpenTK that seems to work, although it's missing some recent additions. If anyone has time, could they test the nuget version of OpenTK to see if my problem is universal?
[editline]8th May 2014[/editline]
[QUOTE=Goz3rr;44754939]I'm pretty sure a friend of mine has this problem too. OpenTK 1.0 works fine but the latest dev build is broken, not sure if it's the same exception though[/QUOTE]
Ah, I guess that confirms that then.
[editline]8th May 2014[/editline]
This is the latest version I could get to work: [url]http://www.nuget.org/packages/OpenTK/1.1.1456.5398[/url]
I have issues with my current javascript code; basically I'm making a Bomberman game. I've made a basic maze by using a collection of arrays, my issue is that the images do not seem to be placed into the canvas.
The goal is to put the softwall image on the 0's in the canvas and the hardwall image in the 1's in the canvas.
My folder structure is:
Main folder: (has the index.html)
/images
/scripts (contains the bomberman.js code)
/css (style.css in here)
Note: I didn't yet put my images into the images folder, I'll do that once I can get this thing to work. Right now they are in the main folder.
I can't seem to find my issue.
HTML: [url]http://pastebin.com/Yn1Nrdiy[/url]
CSS: [url]http://pastebin.com/sb31aVML[/url]
Javascript: [url]http://pastebin.com/2aM9GkbE[/url]
Thx :)
I like Accour's method the best (only because his looked more familiar to mine), but thanks to everyone who helped! [img]http://www.facepunch.com/fp/ratings/heart.png[/img]
Which then leads to another problem I'm having with something entirely different!
[url=http://www.codecademy.com/forum_questions/526fcb5e548c3524a5002e95]I'm currently trying to rewrite someone's Javascript code[/url] (courtesy of Leandro Gaspar) to be more customizable and bigger, and right now I'm trying to make a test code to perform actions just by typing a number in, rather than having to type it out (ignore how that isn't the case when selecting INVENTORY, STATS, and QUIT :v: ). Problem is, once I attempt to equip an item to a player, it says [i]TypeError: Cannot read property 'id' of undefined[/i], which is pretty odd to me considering the following:
[code]// Handlers
// Weapons
var weaponIndex = [];
function addWeapon(newName, newDamage){
var newWeapon = {
id: 0,
name: newName,
damageBase: newDamage,
carriedAmount: 1
};
weaponIndex[weaponIndex.length] = newWeapon;
}
addWeapon("Fists", 0);
addWeapon("Bronze Sword", 20);
addWeapon("Silver Sword", 25);
addWeapon("Gold Sword", 30);
addWeapon("Wooden Staff", 15);
addWeapon("Metal Staff", 25);
addWeapon("Nunchucks", 40);
addWeapon("Wand", 5);
addWeapon("Penultimate Wand", 100);
// Players
var playerTeam = [];
var playerFocus = playerTeam[0];
function addPlayer(newName, newHealth, newDamage){
var newPlayer = {
name: newName,
health: newHealth,
damageBase: newDamage,
equippedWeapon: weaponIndex[0],
damageTotal: newDamage
};
playerTeam[playerTeam.length] = newPlayer;
}
addPlayer("Warrior", 100, 20);
addPlayer("Monk", 75, 30);
addPlayer("Magician", 50, 10);
// Executions
function accessInventory(playerFocus){
var weaponList = 0;
var iDifference = 0;
var chosenWeapon = 0;
for (var i = 0; i < weaponIndex.length + 1; i++){
if (i !== weaponIndex.length){
if (i === 0){
console.log(i + " - " + weaponIndex[i].name);
}
else{
if (weaponIndex[i].carriedAmount !== 0){
console.log((i - iDifference) + " - " + weaponIndex[i].name + " (" + weaponIndex[i].carriedAmount + ")");
weaponIndex[i].id = (i - iDifference);
weaponList += 1;
}
else{
iDifference += 1;
}
}
}
else{
console.log("");
}
}
var itemPick = prompt("What would the " + playerTeam[playerFocus].name + " like to equip?");
if (itemPick > weaponList || itemPick < 0 || isNaN(itemPick) === true || itemPick === "" || itemPick === "null"){
alert("Invalid item!");
}
else if (weaponIndex[itemPick].carriedAmount <= 0){
if (weaponIndex[itemPick].name.substring(weaponIndex.length - 1) == "s"){
alert("You don't have " + weaponIndex[itemPick].name + "!");
}
else{
alert("You don't have a " + weaponIndex[itemPick].name + "!");
}
}
else{
for (var i = 0; i <= weaponIndex.length; i++){
if (weaponIndex[i].id == itemPick){
chosenWeapon = i;
}
}
if (playerTeam[playerFocus].equippedWeapon == weaponIndex[0]){
alert("Equipped " + weaponIndex[itemPick].name + "!");
}
else{
alert("Swapped out " + playerTeam[playerFocus].equippedWeapon.name + " for " + weaponIndex[itemPick].name + "!");
}
weaponIndex[itemPick].carriedAmount -= 1;
weaponIndex[playerTeam[playerFocus].equippedWeapon.id].carriedAmount += 1;
playerTeam[playerFocus].equippedWeapon = weaponIndex[itemPick];
playerTeam[playerFocus].damageTotal = playerTeam[playerFocus].damageBase + weaponIndex[itemPick].damageBase;
}
}
// Test Run
var gameFinish = false;
while (gameFinish === false){
var playerInput = prompt("What would you like to do, now? I mean, really, all you can do is check your INVENTORY, check your STATS, or just QUIT like a wimp.");
playerInput = playerInput.toUpperCase();
if (playerInput == "INVENTORY"){
for (var i = 0; i < playerTeam.length + 1; i++){
if (i !== playerTeam.length){
console.log(i + " - " + playerTeam[i].name);
}
else{
console.log("");
}
}
playerInput = prompt("Who should check their inventory?");
accessInventory(playerInput);
}
if (playerInput == "QUIT"){
gameFinish = true;
}
}[/code]
What's meant to happen is that when you open up the inventory, it gives a list from 0 to however many available items there are. i.e. If you have a potion, mega potion, and elixir in an array, then print it out with my method:
[code]0 - Potion (1)
1 - Elixir (2)[/code]
This indicates that you have 1 potion, no mega potions, and 2 elixirs currently held. Using a Elixir will keep the list the same, but using a Potion removes it from the list and puts Elixir (1) into the 0.
Is there a weird typo I made somewhere, or...?
Have you tried printing the value i during this loop?
[code]for (var i = 0; i <= weaponIndex.length; i++){
if (weaponIndex[i].id == itemPick){
chosenWeapon = i;
}
}[/code]
[editline]9th May 2014[/editline]
try changing:
[code]for (var i = 0; i <= weaponIndex.length; i++)[/code] to [code]for (var i = 0; i < weaponIndex.length; i++)[/code]
[editline]9th May 2014[/editline]
I'm pretty sure this is the problem as weaponIndex.length will be 1 if there is one item in it, however the index of that item is 0. Your loop continues while i <= to weaponIndex.length, which will go to 1 if there is one item in it however it's index is 0, therefore there is nothing at index 1
[QUOTE=Exi;44756669]I have issues with my current javascript code; basically I'm making a Bomberman game. I've made a basic maze by using a collection of arrays, my issue is that the images do not seem to be placed into the canvas.
The goal is to put the softwall image on the 0's in the canvas and the hardwall image in the 1's in the canvas.
My folder structure is:
Main folder: (has the index.html)
/images
/scripts (contains the bomberman.js code)
/css (style.css in here)
Note: I didn't yet put my images into the images folder, I'll do that once I can get this thing to work. Right now they are in the main folder.
I can't seem to find my issue.
HTML: [url]http://pastebin.com/Yn1Nrdiy[/url]
CSS: [url]http://pastebin.com/sb31aVML[/url]
Javascript: [url]http://pastebin.com/2aM9GkbE[/url]
Thx :)[/QUOTE]
Do you know about the developers console in browsers? Check that out, there were a few problems.
You don't want <script> tags in js files, that's for loading scripts in HTML.
You were using mapArray for playerPos before mapArray was defined.
mapArray was mispelt mapAray in the for loops,
your 2d array didn't have commas seperating the lines,
and finally your JS was running before the DOM was ready, so it couldn't find the canvas. You need to wrap the code in a window.onload or something
After fixing all that the only error I'm getting is that the images aren't found.
How could I use modern OpenGL in C# while keeping mono compatability?
SharpGL, Tao, OpenTK, PInvoke, C++/CLI Interface(If I have to)?
how slow/fast are dynamic casts in Cpp ?
Lets say i would do it for every object i have once per frame...
[QUOTE=Pappschachtel;44763655]how slow/fast are dynamic casts in Cpp ?
Lets say i would do it for every object i have once per frame...[/QUOTE]
Profile it, setup a loop, do the cast say a million times and time it, then decide that if it's too slow or not. If you don't notice it being too slow then don't worry about it. VS also has a good profiler.
gonna try to avoid those casts ...
[QUOTE=reevezy67;44761960]How could I use modern OpenGL in C# while keeping mono compatability?
SharpGL, Tao, OpenTK, PInvoke, C++/CLI Interface(If I have to)?[/QUOTE]
That very much depends on what you want to do.
SharpGL is high-level mostly, afaik.
Tao has been superseded by OpenTK and TaoClassic, the latter of which I don't know much about.
OpenTK is low level and works pretty well, but you still have to do some stuff to translate OpenGL's single-threaded state machine and resource management into something you can use comfortably from .NET.
It's effectively P/Invoke with optional error checking.
You shouldn't have to do C++/CLI, that probably would not run as well on Mono anyway since it can compile into tons of P/Invoke and possibly partially native code.
Sorry, you need to Log In to post a reply to this thread.