Please, I've made this game from first principals and it's the first game ever I've ever coded, and also this is one of my first proper JavaFX applications, so if anyone could give me feedback on how to improve the code, I'd really appreciate it.
Note: The total amount of lines of code was around 2.5 - 3k so it's not just any 100 line shitty game you see on YouTube. I designed this game in a fully OOP manner, and I've made the game such that regardless of how big the grid is, the game will ALWAYS check it correctly, apart from the last part of the check which I cut short because I got pissed off.
Jun 29, 2015 06:52 AM (This post was last modified: Jun 29, 2015 07:36 AM by +Haze.)
(Jun 29, 2015 06:35 AM)csgo_player Wrote: Nice work, what algorithm does the AI use?
Thanks :),
The computer has no choice but to try the good ole bruteforce method. It first makes a copy of the board behind the scenes, it then tries each move and if a move generates a WIN it will add it to a list, it also does the same thing with the player 1's character and logs everything.
It then checks both of the results at the end, and it prioritizes winning over blocking the user.
Here's the folder that contains the main logic for the game, including the computer checking.
Look at the ComputerPlayer.Java and GridPaneLogic.Java in particular.
If you're up for a little bit of fun, you could actually program the game board into a custom JavaFX component and this will allow you to import it into scene builder and use it everywhere (you'd just need to write the methods for computer playing and picking stuff etc.). I use custom components on most projects since they allow me to prototype and generate similar things again and again. This suggestion does not imply that I'd class a game panel as a standard components of any sort but it's a fun thing to use as an example to learn custom components (I used a search engine result panel as mine when learning - yawn).
I hope this inspires more people like Reval to pick up JavaFX as a standard around here since it'd make some fantastic HF-ish desktop tools.
Jun 29, 2015 07:53 AM (This post was last modified: Jun 29, 2015 07:57 AM by +Haze.)
(Jun 29, 2015 07:16 AM)Reval Wrote: Very nice work +Haze!
I was planning to code something similar using Swing, but I think it's time to start learning and working with JavaFX.
Thank you, I appreciate it.
I would 100% recommend anybody interested in Java to go towards JavaFX. It was 100% worth my time and effort.
And there's lots of tutorials and help for it on the internet.
(Jun 29, 2015 07:32 AM)WildeThing Wrote: Looks great, well done.
If you're up for a little bit of fun, you could actually program the game board into a custom JavaFX component and this will allow you to import it into scene builder and use it everywhere (you'd just need to write the methods for computer playing and picking stuff etc.). I use custom components on most projects since they allow me to prototype and generate similar things again and again. This suggestion does not imply that I'd class a game panel as a standard components of any sort but it's a fun thing to use as an example to learn custom components (I used a search engine result panel as mine when learning - yawn).
I hope this inspires more people like Reval to pick up JavaFX as a standard around here since it'd make some fantastic HF-ish desktop tools.
Thank you.
You know after a certain point while coding the game, I realized why Game engines are used to develop games 😏 . I mean this is a simple basic game so it shouldn't matter, but next time if I would look into game engines. I was going to make the board a custom component, but I wanted it to be dynamic, I wanted the board size to change at runtime as I wish. So if I wanted 7x7 grid or 2x2 then I should do that with ease.
It was lots of fun to code this and I really hope it inspires other devs to start using JavaFX. I still got more JavaFX work to release on here too. The amazing thing is that I haven't even styled anything with CSS (properly), so if I did then woah!.
(Jun 29, 2015 07:53 AM)+Haze Wrote: Thank you, I appreciate it.
I would 100% recommend anybody interested in Java to go towards JavaFX. It was 100% worth my time and effort.
And there's lots of tutorials and help for it on the internet.
Does it work fine on Eclipse Luna now? I tried to setup everything some time ago and I had no success.
(Jun 29, 2015 08:30 AM)MrOrang Wrote: Nice program man. You did use the AlertBox or something class for the alerts right? It was added in one of the last Java updates wasnt it?
Uhm yeah, the AlertBox class was just a wrapper to the Alert class. My motivation to do that was because I came from a C# background, in C# we were able to do
MessageBox.Show();
and with JavaFX one they just make you do everything yourself so I just created a wrapper class.
(Jun 29, 2015 10:13 AM)JavaProfit Wrote: Nice job +Haze! The lack of final variables disturbs me...Just kidding, it's a good program.
Thanks dude!
I was hoping you commented, you seem like one of the members here who actually know what they're doing you know.
Anyway, yeah this final variable thing weirded me out when I saw other people on StackOverflow and Oracle using it. I thought it was strange, see I came from a C# background and I never learnt to declare a constant/final everywhere?
When do I declare it as final? Isit only when I don't intend to change it? Why declare something as final when its scope is only in a method?
(Jun 29, 2015 10:21 AM)+Haze Wrote: When do I declare it as final? Isit only when I don't intend to change it? Why declare something as final when its scope is only in a method?
Exactly. There's a good question on StackOverflow that says the same thing.
That modifier makes your code more expressive of how it's supposed to function, in my opinion.
When I started aggressively/liberally using final to describe all members and classes that I shouldn't extend, it looked a little strange...After getting used to it, it just makes sense and solidifies (almost literally) what your program does and will do in the future.
For example, some libs have made their implementations final because there's no purpose of extending them or implementing more. (I personally don't always like that, but you can always bytecode inject if you really need to extend a final class).
quick edit: Most people don't make their method parameters final, and that's usually okay.
(Jun 29, 2015 09:06 AM)+Haze Wrote: Uhm yeah, the AlertBox class was just a wrapper to the Alert class. My motivation to do that was because I came from a C# background, in C# we were able to do
MessageBox.Show();
and with JavaFX one they just make you do everything yourself so I just created a wrapper class.
Jun 29, 2015 11:40 AM (This post was last modified: Jun 29, 2015 11:43 AM by +Haze.)
(Jun 29, 2015 11:32 AM)MrOrang Wrote: Nah thats not bad lol. In swing there was a class named JOptionPane and you could just do a message box like this:
Code
JOptionPane.showMessageDialog(null, "your txt") ;
The alert class is something for JavaFX
I genuinely think that the original JavaFX were just lazy. I mean, wouldn't it make sense for them to implement something like JOptionPane.showMessageDialog(), instead of forcing programmers their own wrapper class.
Either way making your own wrapper class isn't really too much of a hassle though
(Jun 29, 2015 11:30 AM)JavaProfit Wrote: Exactly. There's a good question on StackOverflow that says the same thing.
That modifier makes your code more expressive of how it's supposed to function, in my opinion.
When I started aggressively/liberally using final to describe all members and classes that I shouldn't extend, it looked a little strange...After getting used to it, it just makes sense and solidifies (almost literally) what your program does and will do in the future.
For example, some libs have made their implementations final because there's no purpose of extending them or implementing more. (I personally don't always like that, but you can always bytecode inject if you really need to extend a final class).
quick edit: Most people don't make their method parameters final, and that's usually okay.
Hmmm that was an interesting read, I understand the concept. Using final acts as a red light to you and other programmers to let them know not to modify what you've written and to help understand your intentions.
I will most definitely work on it though, I'll try and get into the habit of using final. You see the most difficult thing for me is juggling all these programming language I know. I always have to adhere to the coding convention of each. E.g. VB, C# PHP and Java. It's quiet a task.