Sign in to follow this  
dvchwi

Help taking on more advanced problems.

Recommended Posts

Hello, I have recently found myself in a rut with my coding skills. I can pretty much write up programs for simple type problems (basic searching, sorting, some basic games such as guessing games and hangman, projectile motion simulations, etc.) without any sort of trouble at all. However, I am having diffuculty progressing beyond this level. For example, I am trying to write a progam the reads a list of words from a file and creates a word search puzzle from them. I am having a great amount of diffuculting solving this problem, and am even unsure of the approach to take. Another problem I have run into is creating AI for a Tic-Tac-Toe game. I have yet to figure out a really rigorous way to have the computer play so that it always draws against a competent player (or against itself, for that matter). Overall, I am having difficulty formulating these larger into simple, clean programs (or any type of program, for that matter. Any suggestions on how to escape this rut? Thanks!

Share this post


Link to post
Share on other sites
I don't have an aproach to suggest for the first problem. But in general, you have to think, how would you do it without the computer, then teach the computer how to do that.

For Tic Tac Toe, the aproach should be the same. If you play enough tic tack toe, you will quickly discover that tbere is a stratagy that will always lead to a win or draw. Just goodle tic tac toe and play against the computer a few times.

Share this post


Link to post
Share on other sites
It sounds like you're fine with programming, but run into problems when working out the design of your applications. Take a step back from the coding for a bit, and write out with a pen and paper how you'd like your application to be designed. You should take into account of what your user sees, as well as how it would work in the backend. What classes will you need? How do the classes interact? Can you apply any patterns to your design?

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
It sounds like you're fine with programming, but run into problems when working out the design of your applications. Take a step back from the coding for a bit, and write out with a pen and paper how you'd like your application to be designed. You should take into account of what your user sees, as well as how it would work in the backend. What classes will you need? How do the classes interact? Can you apply any patterns to your design?


Your statement about deciding what classes I need reminded me of another issue I have. I almost never use classes when coding. I learned C as my first language, but quickly caught onto Python when I realized how much more effective it was for pretty much any application. However, OO design has never "clicked" with me, and I tend to write everything pretty procedurally based. This is another symptom of my larger problem of being unable to take on larger projects effectively. Should I make an active effort to learn OO design techniques? Any good books on the topic, or general software engineering?

Share this post


Link to post
Share on other sites
Ok, I've made some progress. I am working on my Grid class, which will contain the actual puzzle itself. I have added methods to initilize the grid to a certain size, and methods to check if rows, columns, or diagonals are full, since I know this will be important later on. Now, do I want to place the logic for adding a word to the grid into the class itself, or do I want to have an outside function work with the class to add the word. To clarify, if I let the class handle all the logic, adding a word would look like this:

myGrid.addword(word)


Otherwise, I would have another set of logic on the outside using the methods in the class to determine the proper positioning of the word, and pass all those location parameters to the class instance to fill in the puzzle. I'm inclined to make the logic internalized, but it also occurs to me making a more generalized grid class would be more useful for reusing it later on (possible extending it for a board game like tic-tace-toe, checkers, or chess). Any thoughts or advice on the standard practice here?

Share this post


Link to post
Share on other sites
Hey, if may I add something, don't oyu think you are taking a step that is too long? I mean, these things you are trying aren't as trivials as they seems to be... I'm not shure were to begin when making an Tic-Tac-Toe AI (you know what, I think I'm gona try to make one!).

But hey, maybe with more effort, you might actually make it, who knows... anyway, good luck with that.

Share this post


Link to post
Share on other sites
Quote:
Original post by algumacoisaqualquer
Hey, if may I add something, don't oyu think you are taking a step that is too long? I mean, these things you are trying aren't as trivials as they seems to be... I'm not shure were to begin when making an Tic-Tac-Toe AI (you know what, I think I'm gona try to make one!).

But hey, maybe with more effort, you might actually make it, who knows... anyway, good luck with that.
If you try to make one you'll need to figure out where to begin. And that would be by learning how to play Tic-Tac-Toe well. It doesn't take long to figure it out.

Convercely you could write a program that will learn how to play Tic-Tac-Toe. Something that will play randomly at first, but eliminate loosing combinations. If you can then figure out how to make the compute equate symetrical moves, the computer will be able to learn very quickly. (like how playing a peice in the top left corner on the second move is the same a playing any other corner.

Share this post


Link to post
Share on other sites
You real question boils down to a design problem. How much functionality do you implement in your grid class vs. building an intermediate class that holds that functionality. If the idea of seperating the solution into multiple classes seems too foreign, I would implement all of the functionality in the grid class and then revisit it at a later time until the "light" bulb goes off.

From a design perspective you should build in a layer of abstraction from the UI and rules/logic.

A good book to get a better understanding on designs is "Design Patterns: Elements of Reusable Object-Oriented Software" ISBN := 0-201-63361-2

Share this post


Link to post
Share on other sites
Ok, I'll check that out. I've decided to simplify things a bit and make a battleship game instead. I can wrap my ahead around what needs to be done for that much more easily, and should get me some good practice.

Share this post


Link to post
Share on other sites
it's true, the problem likely doesn't have anything to do with your code or knowledge of the language you're using, but instead having more to do with the different approaches required for large projects. For small stuff, you can just jump in and code it. Big stuff'll explode in your face if you don't plan it out in detail first.

Oh, and on the tic-tac-toe AI, you have to decide first if you want it to be beatable at all, or do you want it to function absolutely perfectly [and thus never loose]. If you want it to be unbeatable, the minimaxing is your best bet on such a small search space [tic-tac-toe isn't that complicated, and it's a small enough game that minimaxing will work well]. Incase you're unfamiliar, minimaxing is a brute-force exhaustive search of all possible future game states that can be reached from the current game state, and starting from the finished games, it throws out every solution that doesn't leave it with a victory[if possible, or a draw if victory isn't possible]. It'll play a 100% optimized game of tic-tac-toe, and a human opponent will never beat it [and it won't be able to beat itself ether, since tic-tac-toe is a draw, every time, between perfect players].

If you want to make it defeatable, i'd advise actually using the exact same method as above, only instead of picking the optimum choice every time, weight the options on how optimum they are, and make the higher weights more likely to be picked. That'll also assure it won't play the exact same game over and over, and to ramp up the difficulty, you need only make the weight difference between a bad choice and a good one, larger.

Share this post


Link to post
Share on other sites
Tic Tac Toe is easier than the word search problem. :)

First, you want to split "user interface" from "what a player can do".

Write a "Tic Tac Toe" game simulator -- it takes a series of moves and displays a Tic Tac Toe game.

Keep the part of the code that reads the series of moves seperate from the part that runs the game, and seperate from the part that displays the resulting game. Breaking your programs up into smaller chunks is very important, and having strong walls that restrict what kinds of communication can cross them makes programming a much simpler task.

You'll want a Tic Tac Toe game that can either read from a file or from user input -- that is the first stage.

The next stage is writing a Tic Tac Toe AI. The first AI you write should move randomly -- it should look at the game (using "display-like" code -- the AI should use the same INTERFACE to look at the game that your "display the game to the user" uses), pick an empty spot at random, and then tell the game engine it wants to move there.

Once you have that done, you now can think about writing a smart AI. The AI is now decoupled from the Tic Tac Toe game -- the AI only depends on the Tic Tac Toe "game player" interface and the "game display" interface.

To write a smart AI, you need to think about ways that a person plays Tic Tac Toe.

Some players memorize simple rules to win at Tic Tac Toe, some players examine the possible moves the opponent can do, the possible responses, and determine what move is best.

One path or the other may be easier. AI research has provided patterns for both kinds of AI I mentioned above -- but first you have to have a game framework for the AI to play in!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this