• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Water

Member
What do the stereotypical Java programmers writing C++ do wrong?

What tokkun said, but to sum it up in one word: "new". That's what they do wrong. Typing those three letters in C++ while thinking it's the same thing as in Java is instant disaster, and of course the stereotypical Java programmer will plaster the whole code with that. Even if the Java programmer then reads a few C++ tutorials circa 1998, does everything that is necessary to stop the raging memory leaks, doesn't forget anything and doesn't make a mistake, they are still wrong. "new" is almost always bad style in modern C++ - but doing the right things instead requires you to understand many other things about C++ that do not exist in Java.
 

Water

Member
That's not necessarily a good thing. Bugs are easier to fix the quicker they surface. Java programmers coding badly in C++ tend to do things that show up very quickly and easily. C++ programmers coding badly in Java tend to do things that fail very badly and silently. The severity of the problem is a lot lower, but the pain of tracking it down tends to be a lot worse. "catch (Exception e) {}" always makes me shiver with dread. :p
I was thinking more about C++ problems that cause things to go silently and/or randomly wrong in non-exceptional situations. Or wonderful stuff like this:
Code:
float sumOfThisLargeVector(std::vector<float> v);
that eats away performance forever since it isn't wrong as far as the result is concerned.
 

I can second Sublime Text
3
.

Yep, Sublime Text is great for whenever you don't want to use a full blown IDE.

Awesome, thanks guys.

Has anyone here had a problem with visual studio 2013 where every time you opened it, the graphics would load very choppy and you couldn't edit your code at all?


I haven't used 2013 much because of how you need to link your MSDN account, but when I did I didn't run into that.
 
So all you guys who use a Mac, what are some of your favorite tools? Not so much language specific IDEs, more like text editors, productivity tools, etc.

1. vim in a translucent Terminal with 14pt Inconsolata-dz. Put reference materials behind Terminal.

2. TextWrangler for massive multi-file find/replace.
 

Husker86

Member
I realize that this is an incredibly amateur question but...

I am almost done with my first Unity Game. The default PlayerPrefs are notoriously insecure (a user can just edit the file to make the values whatever they want).

Ideally you would use encryption, and I still might since some Unity plugins look good. However, would something like the following be just slightly more secure:

Storing a high score in the insecure PlayerPrefs: Before saving the score, I multiply it by some number, let's say 5,356. Then, when loading, I make sure the number is divisible by 5,356 and, if not, say the save file is corrupt and load a default of 0 or something.

I assume this isn't very secure, but at least the user couldn't just edit the file. They would have to see the source code to see the modifier.

Would I be wasting my time doing this?
 

oxrock

Gravity is a myth, the Earth SUCKS!
Back by popular demand, i present to you all my very early stage game filled with extremely horrible programmer art! Updates were requested however and I shan't keep anyone waiting. Please feel free to take a look and have mercy on me, haha.

Deadly Dungeoneers

XUuKJEv.gif
 

Slavik81

Member
I realize that this is an incredibly amateur question but...

I am almost done with my first Unity Game. The default PlayerPrefs are notoriously insecure (a user can just edit the file to make the values whatever they want).

Ideally you would use encryption, and I still might since some Unity plugins look good. However, would something like the following be just slightly more secure:

Storing a high score in the insecure PlayerPrefs: Before saving the score, I multiply it by some number, let's say 5,356. Then, when loading, I make sure the number is divisible by 5,356 and, if not, say the save file is corrupt and load a default of 0 or something.

I assume this isn't very secure, but at least the user couldn't just edit the file. They would have to see the source code to see the modifier.

Would I be wasting my time doing this?
Yes. You are making a game for people to play and enjoy. The player is not your adversary. You do not need to secure anything against them.

If you're saving your game state as a text file, then maybe compress it. I wouldn't bother with any further obfuscation.
 

Water

Member
Storing a high score in the insecure PlayerPrefs: Before saving the score, I multiply it by some number, let's say 5,356. Then, when loading, I make sure the number is divisible by 5,356 and, if not, say the save file is corrupt and load a default of 0 or something.

I assume this isn't very secure, but at least the user couldn't just edit the file. They would have to see the source code to see the modifier.
If the user saves 2-3 times with different scores and compares the score entries in the save files, the multiplier is pretty obvious. If you're gonna bother obfuscating the score at all, how about you do your multiplication thing, then flip some bits? (multipliedscore ^= 2863311531 for instance.) Do same thing again when reading the value. No more work for you, but would take some work to figure out.
 

Husker86

Member
Yes. You are making a game for people to play and enjoy. The player is not your adversary. You do not need to secure anything against them.

If you're saving your game state as a text file, then maybe compress it. I wouldn't bother with any further obfuscation.

I agree with you in theory, but all of the games with Leaderboards full of obviously cheated scores bothers more people than the people who get frustrated that they can't cheat (I will support the devices games program, Google Play Games, Game Center, etc.). Also, eventually, for future games, I want to store info that enables/disables in-app purchases, but obviously my previous theory on rudimentary encryption wouldn't be enough for that anyway.

Unity uses its own thing which is almost as simple as a text file, but I don't think I can compress it because it converts to the default user settings file on whatever platform you export your project to.
If the user saves 2-3 times with different scores and compares the score entries in the save files, the multiplier is pretty obvious. If you're gonna bother obfuscating the score at all, how about you do your multiplication thing, then flip some bits? (multipliedscore ^= 2863311531 for instance.) Do same thing again when reading the value. No more work for you, but would take some work to figure out.

Honestly, even that much extra work would be better than nothing. I just want to weed out the people who open the file, change the score and they're done. I like your suggestion since, like you said, it's not really anything more for me to do.

I think I'll probably just go ahead and get EasySave2 plugin though; it also comes with scripts for saving/loading to a web server which I think I'll want to use since my game will be multi-platform.

Thanks for the responses!
 

Rapstah

Member
The way Guitar Hero 2, to give an example of a game that has leaderboards and local high scores, does it is scores are only updated to the leaderboard upon actually getting the score. There's no kind of checking to see if there's a saved score higher than the leaderboard score, because ideally, if you're uploading the score to the server every time you also save it locally, and all saved scores are final, then you're only missing to upload scores if the player suddenly disconnects from the internet for some reason.

Now, Guitar Hero 2's leaderboards are still fucked because people
  1. have modded the game to give more points
  2. have memory edited the game to have a higher score upon saving than upon finishing the song
but you're never getting past this anyway.
 

Husker86

Member
The way Guitar Hero 2, to give an example of a game that has leaderboards and local high scores, does it is scores are only updated to the leaderboard upon actually getting the score. There's no kind of checking to see if there's a saved score higher than the leaderboard score, because ideally, if you're uploading the score to the server every time you also save it locally, and all saved scores are final, then you're only missing to upload scores if the player suddenly disconnects from the internet for some reason.

Now, Guitar Hero 2's leaderboards are still fucked because people
  1. have modded the game to give more points
  2. have memory edited the game to have a higher score upon saving than upon finishing the song
but you're never getting past this anyway.

That makes sense. So, they could modify the save, but it would only show on their device and not the leaderboards. That is actually how my game works anyway (uploading after round finish). Google Play Services caches these things too, I believe, so if they weren't connected at the time, it would upload later anyway.

Thanks!
 

Slavik81

Member
I agree with you in theory, but all of the games with Leaderboards full of obviously cheated scores bothers more people than the people who get frustrated that they can't cheat (I will support the devices games program, Google Play Games, Game Center, etc.). Also, eventually, for future games, I want to store info that enables/disables in-app purchases, but obviously my previous theory on rudimentary encryption wouldn't be enough for that anyway.

Unity uses its own thing which is almost as simple as a text file, but I don't think I can compress it because it converts to the default user settings file on whatever platform you export your project to.
That's a completely different problem from what I understood and it demands a completely different solution. Protecting leaderboards is very different from 'protecting' save files.

Ultimately, if your high score reporting amounts to your program saying 'this is my score, add it to the leaderboards' someone can easily look at your program, see how it says that, and simply do the same. Any approach that requires trusting the client is doomed to failure.

If you care deeply about the score board being accurate, there are options beyond just running the game on the server. For instance, listing only independently verified scores.

There's many of examples of this strategy in the real world. The world record for the hundred metre dash only includes times that were done publicly at track meets. It too would be a broken system if we trusted track runners to accurately report their times from solitary training.
 

Husker86

Member
That's a completely different problem from what I understood and it demands a completely different solution. Protecting leaderboards is very different from 'protecting' save files.

Ultimately, if your high score reporting amounts to your program saying 'this is my score, add it to the leaderboards' someone can easily look at your program, see how it says that, and simply do the same. Any approach that requires trusting the client is doomed to failure.

If you care deeply about the score board being accurate, there are options beyond just running the game on the server. For instance, listing only independently verified scores.

There's many of examples of this strategy in the real world. The world record for the hundred metre dash only includes times that were done publicly at track meets. It too would be a broken system if we trusted track runners to accurately report their times from solitary training.

Yeah, my mistake for not being completely clear. If this were to be an offline game with unlocks within the game for doing certain things, I would have taken the stance of "If someone wants to edit the file to take a shortcut, so be it".

Since Rapstah made me think of my game's mechanics, and how the leaderboards won't be affected by an edit of the save file, that's good enough for me.

If big time game companies can't prevent leaderboard cheating, I have no hope.
 
I agree with you in theory, but all of the games with Leaderboards full of obviously cheated scores bothers more people than the people who get frustrated that they can't cheat

A racing game I played on the Mac back in 2006 (Redline) would upload a checksum along with the other information so that the developer/publisher could verify and ban cheating times. I don't know more than this, this little nugget was just mentioned in passing on the game's official forum. Since this isn't a new problem, there's probably discussion about it on some game development forums.
 
So all you guys who use a Mac, what are some of your favorite tools? Not so much language specific IDEs, more like text editors, productivity tools, etc.

I just got a Mac and while I got my Windows VM set up for my dev stuff there, I'm trying to get my Mac set up for dev work.

Edit: Sorry, I know its a bit off-topic but its the best place I could think of coming without starting a new thread.

Not necessarily programming specific but I like Quicksilver and I just recently started using Cheatsheet. And yeah Sublime Text is a god send.
 

Slavik81

Member
If big time game companies can't prevent leaderboard cheating, I have no hope.
If it's any consolation, the fact that anyone would care enough about your game to bother to try to cheat is a success itself. For small-scale developers, the hardest part of the path to success is getting noticed.
 

Buggy Loop

Member
Anyone can point out a good video or site that explains step by step how i can read a binary file, and write the information found into a dynamic 2D array (that is in two individual struct..)

Its some seriously fucked up shit..
 

Jackben

bitch I'm taking calls.
Anyone can point out a good video or site that explains step by step how i can read a binary file, and write the information found into a dynamic 2D array (that is in two individual struct..)

Its some seriously fucked up shit..
What a fantastically relevant username.
 

tokkun

Member
That's not necessarily a good thing. Bugs are easier to fix the quicker they surface. Java programmers coding badly in C++ tend to do things that show up very quickly and easily. C++ programmers coding badly in Java tend to do things that fail very badly and silently. The severity of the problem is a lot lower, but the pain of tracking it down tends to be a lot worse. "catch (Exception e) {}" always makes me shiver with dread. :p

Is "catch (Exception e) {}" really an issue of C++ mindset? It seems more like an issue of lazyness that can affect someone regardless of their background. It's not like it's considered good practice to do that in C++ either.

The issue I personally run into is that my instinct is to write assertions rather than throwing runtime exceptions, without remembering that Java ignores them by default. I would love someone to explain to me why that is good default behavior.
 

Roflobear

Member
Is "catch (Exception e) {}" really an issue of C++ mindset? It seems more like an issue of lazyness that can affect someone regardless of their background. It's not like it's considered good practice to do that in C++ either.

As a Java newbie, why is it bad practice to use "catch (Exception e) {}" ?
 

Buggy Loop

Member
What a fantastically relevant username.

Sadly when im programming, my username fits me all too well... :(

Code:
struct Athlete
{
    int number;
    char name[32];
    char sport[32];
    char country[32];
}
struct ListAthletes
{
   Athlete** athletes;
   int capacityAthletes;
   int nAthletes;
}

after opening the binary file
i have inside a function something like this

ListAthletes currentlist;

file.seekg(0, ios::end);
int nElement = int(file.tellg()/sizeof(ListAthletes));
currentlist.capacityAthletes = nElement;
currentlist.nAthletes = nElement;

I attribute the dynamic memory for the 2D array.

currentList.athletes = new Athlete* [nElement];
for (int i = 0 ; i < nElement ; i++)
     currentList.athletes[i] = new Athlete  [nElement];

I reset the binary pointer to the beginning

file.seekg(0, ios::beg);
for (int i = 0 ; i < nElement ; i++)
   for (int j = 0 ; j < nElement ; j++)
    { 
          file.read((char*) &currentList.athletes[i][j].name, sizeof(currentList.athletes[i][j].name));
          file.read((char*) &currentList.athletes[i][j].sport, sizeof(currentList.athletes[i][j].sport));
          file.read((char*) &currentList.athletes[i][j].country, sizeof(currentList.athletes[i][j].country));
    }


.... can anyone point out of im totally not in the right path?
 

Dragon

Banned
Hey. Don't normally post on here, but thought I'd poke my head, say hi and ask a few questions.

I'm three months into a six month placement at a mobile app developer, and it's been great, though definitely daunting at times. A friend helped me get in, as I'd only done several Microsoft night courses and hadn't a degree. At the time of going in, I'd just a very limited experience messing around with C#.

After spending my first weeks of the placement focused on creating simple service calls in WCF to either save or pull from a database, I've been moved up to also creating some of the more simple pages of their backend sites, using MVC. I'm feeling a little out of my league and I definitely have to refer to code already written by others or ask for help a lot. I'd no experience with Javascript/Jquery, and doing pagenation and other simple tasks has had me banging my head against a wall. A week in I'm starting to feel less like I'm drowning though.

I guess I wanted to drop in here, as outside of work, programming isn't something I spend a lot of time on, but as I'm enjoying my placement and starting to take things more serious, I want to try and become more involved outside of work too and put that extra effort in, so I'm not drowning in work. It's just sometimes easier said than done though, and I still find it difficult when I come home, especially as I normally have a numb head.

Just wondering what blogs, sites, podcasts, videos or other resources you guys keep up with? Also, if anyone has any stuff specifically on MVC, I'd appreciate. I've went through a lot of the easily found tutorials, but I'm curious of there're any video channels people would recommend, that go past just the starting stuff.

Apologies for the wall of text and cheers for reading.

Hacker news is a great resource, especially for Web programming: http://news.ycombinator.com
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Sadly when im programming, my username fits me all too well... :(
.... can anyone point out of im totally not in the right path?

If you give us some more information about what you want to do with your code we might be able to help.

Also when posting code use the
Code:
 tags so it's easier to read. If you have a lot of code to show you can use [URL="http://pastebin.com/"]pastebin[/URL] as well.
 

Buggy Loop

Member
If you give us some more information about what you want to do with your code we might be able to help.

Also when posting code use the
Code:
 tags so it's easier to read. If you have a lot of code to show you can use [URL="http://pastebin.com/"]pastebin[/URL] as well.[/QUOTE]

Basically all the information about athletes, their name, their sport, their country is found in a binary file. All in the order of the struct Athelte.

I have to read the binary file. Then write the information found in it dynamically in the struct above, so i have to allocate the memory, which i guess is done by looking at out many elements are found in the binary file / sizeof(ListAthlete) .. (not sure)
 
so i have to allocate the memory, which i guess is done by looking at out many elements are found in the binary file / sizeof(ListAthlete) .. (not sure)

Are the athlete records fixed width or delimited within the file?

If the former, it is simply a matter of dividing the file size by the size of an athlete record within the file.

If it is delimited, read the file and count the delimiters, allocate the memory for the list, then read it again populating the list. There may be more optimal ways of doing it, but do it right and then do it fast.
 

tokkun

Member
I think he was referring to using "catch (Exception e) { }" in C++ code, not Java.

No, I meant both languages.

As a Java newbie, why is it bad practice to use "catch (Exception e) {}" ?

There are a relatively small number of cases where it really makes sense to arbitrarily suppress propagation of any type of exception, do nothing, and make no record of it.

If you have exceptions that you really don't need to take any action for, catch them specifically and comment why they can be ignored.

Something like this:
Code:
try { ... } catch (DontReallyCareException e) { 
  // Expected behavior during initialization. No clean-up needed.
}

The problem with catching all exceptions is that if one of the methods in your try block changes and throws a new exception type that must be handled for correctness reasons, the generic catch block will silently suppress it. It makes your code fragile to change and difficult to debug when things go wrong.

Even if you think that you can safely ignore any exceptions, you are still better off doing something like this:

Code:
try { ... } catch (WhitelistedSuppressedException e) {
  // Expected behavior
} catch (Exception e) { 
  DebugLog("Suppressed unexpected exception: " + e);
}

so it is at least possible to figure out if you are suppressing an exception you didn't expect to get.

It also may be possible that your code is being called by some other code that does care about these exceptions and is capable of handling them. In that case, clean up your state and rethrown the exception.
 

Water

Member
.... can anyone point out of im totally not in the right path?
Normally folks would not mess with raw pointers and arrays, but use normal values, smart pointers and vectors. I presume you've been specifically told to do otherwise, and the struct definitions are a given.

I don't think you are actually supposed to be reading a 2D array from the file. Based on how you try to calculate nElements, you have N things in the file, but you then try to read N^2 things. At least one of those is wrong, probably the latter. Also, it's not sizeof(ListAthletes) you want in the size calculation.
 

Roflobear

Member
The problem with catching all exceptions is that if one of the methods in your try block changes and throws a new exception type that must be handled for correctness reasons, the generic catch block will silently suppress it. It makes your code fragile to change and difficult to debug when things go wrong.

That makes a lot of sense. Thanks for clearing that up.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
No, I meant both languages.

See this is why this thread is awesome. Thanks for clearing this up.

My professor just went through exception handling in Java these past two weeks and I have done a couple assignments involving it, but the only negative to it that he provided was that it was resource intensive and would slow the program down.
He portrayed them as something that you should implement when you are expecting some errors, (user typing in invalid input or something), and he made them seem like something that needed to be caught for all programs that could deal with the performance drop.
 

RustyO

Member
Just going to throw this out there, see if anyone already has a list or spreadsheet or something kicking around, as I presume it's a fairly common pattern.

I'm working on a scenario where there is 8 possible selections, and each selection is identified via a different dec / hex value, i.e.

Code:
Selection 1 = dec 1 / hex 1
Selection 2 = dec 2 / hex 2
Selection 3 = dec 4 / hex 4
Selection 4 = dec 8 / hex 8
Selection 5 = dec 16 / hex 10
Selection 6 = dec 32 / hex 20
Selection 7 = dec 64 / hex 40
Selection 8 = dec 128 / hex 80

So depending on user selection, results would be:

Code:
Selection: Nothing = Result: dec 0 / hex 00
Selection: 1 = Result: dec 1 / hex 1
Selection: 1 & 2 = Result: dec 3 / hex 3
Selection: 1 & 2 & 6 & 8 = Result: dec 163 / hex A3
Selection: 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 = Result: dec 255 / hex FF

So just wondering if anyone has a quick list / spreadsheet of all the potential combinations of those 8 selections, and the asociated dec/hex values?
 
You've (almost) invented bit flags for yourself. Good work :)

Why do you want the pre-computed decimal-hex conversions? If it is to make a large switch statement, you're better off using masks to check which selections are made.
 

RustyO

Member
You've (almost) invented bit flags for yourself. Good work :)

Do I get a cookie? :)

Why do you want the pre-computed decimal-hex conversions? If it is to make a large switch statement, you're better off using masks to check which selections are made.

Ah, I'm doing a singular switch statement, based off the single value / masking.

I guess it's mainly for personal use / documentation (i.e. cheat sheet) as a quick sanity check? Interfacing with some hardware, so makes sense to me to have that quick double check.

Don't think it's really a problem anymore now, just wrote a quick bit of VBA to generate the possible combinations / cheat sheet. Just thought it would have been something that would be quite easy to find lying around.
 

iapetus

Scary Euro Man
So just wondering if anyone has a quick list / spreadsheet of all the potential combinations of those 8 selections, and the asociated dec/hex values?

You're asking for a binary to decimal/hex conversion sheet. Such things exist.

http://danreb.com/content/conversion-table-decimal-hexaoctal-and-binary

I haven't really needed to use anything like that since I was writing compressed graphics formats by hand back in the eighties, though, and even then you only really needed to be able to do four bits, which is easy enough to memorise - the same is true for your needs here, for reasons that should be obvious.
 

Water

Member
Do I get a cookie? :)



Ah, I'm doing a singular switch statement, based off the single value / masking.

I guess it's mainly for personal use / documentation (i.e. cheat sheet) as a quick sanity check? Interfacing with some hardware, so makes sense to me to have that quick double check.

Don't think it's really a problem anymore now, just wrote a quick bit of VBA to generate the possible combinations / cheat sheet. Just thought it would have been something that would be quite easy to find lying around.
If the switch statement in whatever language you are using can only handle int constants and not expressions, you might want to consider restructuring the code to not use a switch because using raw "magic values" isn't very maintainable or good code.
Or, if you only need a few combinations, you could name them and construct them before the switch. e.g. startEngineFlags = igniterFlag | discombobulatorFlag | fuelPumpFlag;
Regarding the individual flag values, they are also easier to get right if you generate them with 1 << i where i = 0...(#flags-1) instead of writing them out by hand.
 

iapetus

Scary Euro Man
If the switch statement in whatever language you are using can only handle int constants and not expressions, you might want to consider restructuring the code to not use a switch because using raw "magic values" isn't very maintainable or good code.
Or, if you only need a few combinations, you could name them and construct them before the switch. e.g. startEngineFlags = igniterFlag & discombobulatorFlag & fuelPumpFlag;
Regarding the individual flag values, they are also easier to get right if you generate them with 1 << i where i = 0...(#flags-1) instead of writing them out by hand.

|, presumably, not &. When dealing in bitmasks, &ing the values together is going to give 0. :)
 

Exuro

Member
I need some help with generic data types in c. I'm using the Dllist data structure from here
http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Dllists/ with node type Jval here
http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Jval/

The assignment I'm working on deals with multithreading junk which I'm fine with. I need to implement a list of "person" datatypes using the doubly linked list data type above but I'm not really familiar with these more generic lists(I;ve always just made a specific list for x assignment) so I'm kind of stuck on what I need to do to make a doubly linked list of people. I tried using a person variable in the append function casted to Jval for kicks but it spit out

"error: cast to union type from type not present in union dll_append(gList, (Jval) *p);"

Any tips on what I need to do/where I should look to figure it out wouldbe great.
 

tokkun

Member
I need some help with generic data types in c. I'm using the Dllist data structure from here
http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Dllists/ with node type Jval here
http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Jval/

The assignment I'm working on deals with multithreading junk which I'm fine with. I need to implement a list of "person" datatypes using the doubly linked list data type above but I'm not really familiar with these more generic lists(I;ve always just made a specific list for x assignment) so I'm kind of stuck on what I need to do to make a doubly linked list of people. I tried using a person variable in the append function casted to Jval for kicks but it spit out

"error: cast to union type from type not present in union dll_append(gList, (Jval) *p);"

Any tips on what I need to do/where I should look to figure it out wouldbe great.

Store the Person pointer in the void* field of the union.
 

arit

Member
I need some help with generic data types in c. I'm using the Dllist data structure from here
http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Dllists/ with node type Jval here
http://web.eecs.utk.edu/~plank/plank/classes/cs360/360/notes/Jval/

The assignment I'm working on deals with multithreading junk which I'm fine with. I need to implement a list of "person" datatypes using the doubly linked list data type above but I'm not really familiar with these more generic lists(I;ve always just made a specific list for x assignment) so I'm kind of stuck on what I need to do to make a doubly linked list of people. I tried using a person variable in the append function casted to Jval for kicks but it spit out

"error: cast to union type from type not present in union dll_append(gList, (Jval) *p);"

Any tips on what I need to do/where I should look to figure it out wouldbe great.

Looks like "lets teach our students C with structs/functions that make it look like java that we taught them the last 1-2 semesters". ...

The Jval union has a void ptr v, so something like
Code:
  person *p;
  dllist gList;
  ...
  Jval j;  
  j.v = (void*)p;
  dll_append(gList, j)

should work.
 

Exuro

Member
Looks like "lets teach our students C with structs/functions that make it look like java that we taught them the last 1-2 semesters". ...

The Jval union has a void ptr v, so something like
Code:
  person *p;
  dllist gList;
  ...
  Jval j;  
  j.v = (void*)p;
  dll_append(gList, j)

should work.

Yeah that's how I was doing it, made a small mistake, but now its working. Thanks for the help.
 
Hey guys, so I'm in an intro to CS class (learning Java) and I was wondering if anyone could help me with a basic idea of how to set this up. I'm supposed to make a TicTacToe game with either a human and a computer player or two human players. The humans I can just assume that they aren't idiots and won't try to choose the same spot but I'm not sure how to get the computer to recognize an open spot. Spots are chosen using coordinates (ABC for the X-axis and 123 for the Y-axis).

I'm guessing I just need a recursive method that checks for spots the player has filled and then fills out different ones? Any ideas how to do that? The basic assignment just requires me to make a computer that fills spots at random, I can get extra credit if I create an "AI" that is unbeatable, but I think I'll just settle for the basic one for now.

Sorry this is so ridiculously basic. I'm just starting out :p
 

Saprol

Member
The humans I can just assume that they aren't idiots and won't try to choose the same spot
A dangerous assumption. :p

Anyway, when you say you're using letters/numbers as coordinates, how are you representing the game board in your code? Do you mean you have 9 separate variables named A1, A2, A3... that hold some value indicating empty/X/O? Have you learned enough Java to know about arrays?
 
dumb java question:

for an abstract class, im adding a new method that needs to parse stuff, which is run during the initialization method of this abstract class.

problem is i learned that im not allowed to add throw parse exception to said init method.

i feel like there is another way i can do this without java compiler being pissy but i can't think of it.

:/
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Hey guys, so I'm in an intro to CS class (learning Java) and I was wondering if anyone could help me with a basic idea of how to set this up. I'm supposed to make a TicTacToe game with either a human and a computer player or two human players. The humans I can just assume that they aren't idiots and won't try to choose the same spot but I'm not sure how to get the computer to recognize an open spot. Spots are chosen using coordinates (ABC for the X-axis and 123 for the Y-axis).
The easiest way to do this is just to make two lists, one for open cells and one for closed cells. Whenever a player makes a move, take that cell out of the list of "open" cells and move it into "closed". Then have your computer just choose randomly from the list of open cells.

Also I think your professor will appreciate it if you disallow the players from choosing a filled spot.
 

usea

Member
dumb java question:

for an abstract class, im adding a new method that needs to parse stuff, which is run during the initialization method of this abstract class.

problem is i learned that im not allowed to add throw parse exception to said init method.

i feel like there is another way i can do this without java compiler being pissy but i can't think of it.

:/
Catch the exception and provide a default. What should the method return if the parse fails?
 
Top Bottom