Computer Science Canada Console Connect 4 |
Author: | Insectoid [ Sat Jan 05, 2013 6:21 pm ] | ||
Post subject: | Console Connect 4 | ||
I haven't written any code in a while, and inspired by this thread, I decided to make my own. I've explained how it works in the comments. Now, I'm not entirely sure if I've covered all of the win conditions. I think I did (except the draw), but if you find a broken play, let me know.
|
Author: | Insectoid [ Sat Jan 05, 2013 8:10 pm ] | ||
Post subject: | Re: Console Connect 4 | ||
All right, now the game will end if there's a draw. At least, I'm pretty sure it will. Hopefully I didn't break something else along the way.
|
Author: | Zren [ Sun Jan 06, 2013 12:13 pm ] | ||
Post subject: | RE:Console Connect 4 | ||
That win condition looks unholy. Why bother checking the whole board? If you're guaranteed a board without a win condition at the beginning, you only have to check the win conditions relative to the played piece. Something like:
A crazier way to do it would be to fire rays in opposite directions and get the distance the two rays travelled. The sum() + 1 >= 4 then you win! I like how you did the tie game condition, even if you abused the hell out of the for statement. |
Author: | Insectoid [ Sun Jan 06, 2013 12:30 pm ] |
Post subject: | RE:Console Connect 4 |
Hmm, I dunno how I missed that solution for the win condition. "Hur dur, player 2 can't win on player 1's turn, but let's check all the pieces that can't possibly win anyway". As for the for loops, I love doing that. It's completely illegible, sure, but it's so much fun to write. It's always fun to look at your old code and think, how the hell did I do that? Unless you actually have to use that code. Then it sucks. |
Author: | Insectoid [ Sun Jan 06, 2013 6:54 pm ] | ||
Post subject: | Re: Console Connect 4 | ||
All right. I've re-written my win function (currently under win_). It's still a little long, and still a little messy, but it's much better. I'm using a half-adder to iterate over 3 vectors that I pass to another function that counts the consecutive like tokens on that vector. I still need a special case for the (-1, 1) vector because the adder can't return a -1. My for loops are getting beyond ridiculous now. I've kinda just started putting the loop body into the incrementor, which isn't really cool; it's just a mess. I don't really like using commas in for loops. I don't think I'm gonna do that anymore.
|
Author: | Insectoid [ Sun Jan 06, 2013 7:32 pm ] | ||
Post subject: | RE:Console Connect 4 | ||
Blah. I relented and switched to an array of vectors (sort of).
|
Author: | Zren [ Thu Jan 10, 2013 11:11 pm ] | ||
Post subject: | RE:Console Connect 4 | ||
Finally got around to deciphering your crazy algorithms. Is ^= different from != ? Ah, so it is. http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Bitwise XOR assignment a ^= b a = a ^ b Hmmm. So win_ origionally ran like so:
You could calculate the 4 vectors iteratively with: (x = 1, y = -1; x >= 0; x -= y == 1, y += y < 1) The second one was pretty interesting as I've never dealt with pointers in such a fashion. |
Author: | Insectoid [ Fri Jan 11, 2013 9:08 am ] | ||
Post subject: | RE:Console Connect 4 | ||
Quote: The second one was pretty interesting as I've never dealt with pointers in such a fashion.
It's a pretty common way of iterating over an array in C (common enough that my prof taught it, anyway), the only difference being that I incremented by 2s. Also, I just realized that I forgot to comment out the special case that no longer is a special case. Your for loop is excellent and I'm going to use it. I never thought to add/subtract a boolean operation to an integer. That's a neat trick that I'm going to remember. So, here's the semi-final product. Semi-final because it's done unless I find something else cool to do with it.
|
Author: | QuantumPhysics [ Sun Feb 03, 2013 9:37 pm ] |
Post subject: | RE:Console Connect 4 |
I like the structure of your for statements. Almost professional |
Author: | Insectoid [ Sun Feb 03, 2013 10:17 pm ] | ||||
Post subject: | RE:Console Connect 4 | ||||
I think I'd have an aneurism if I saw anything like this in production code. Also, this for loop:
|
Author: | Tony [ Mon Feb 04, 2013 3:52 am ] |
Post subject: | Re: Console Connect 4 |
Insectoid @ Sun Jan 06, 2013 6:54 pm wrote: All right. I've re-written my win function (currently under win_).
Source control. Get yourself a GitHub account. Use it. It's wonderful. More elaborate explanation: you can branch (create a derivative copy) of your project and experiment there. If the new attempt at the win function ends up being garbage, just trash the entire thing and check out the copy of the code from any point in the past. If the new function is a success, then merge the changes back into trunk/mainline/main/whatever-you-call-authoritative-working-version. Source control is not just for team projects -- I've been using git for personal school assignments. |
Author: | Insectoid [ Mon Feb 04, 2013 8:39 am ] |
Post subject: | RE:Console Connect 4 |
I dunno. I don't write code often anymore (though I really should) since I'm busy with other things right now. I never write large projects either, and most changes can be written and tested in half an hour. If it's a major change, I'll just make a new copy of the file. The last thing I want to do while writing something is to waste time figuring out my source control. That's also why I don't use complicated bloated IDEs. Too much crap going on. All I need is syntax highlighting. Anything else just gets in the way. |