How to do a parameterization in C? - c

I'm trying to find an algorithm for the game master mind with 4 numbers, where each number can be between 0 to 5, giving 1296 possibilities. With the first guess being 1,1,0,0
there are less options left.
I would like to know how to remove the options which are not suitable according to the first guess.
How to use an array(solutions) and array(current solutions)? Should I use parameterization for that?
Is there an algorithm in C to do that?
Thanks a lot for the help!

The simplest to implement is to simply loop trough all your elements and make the once that no longer work false. This might be the best idea here as looping trough 1300 elements is still quite fast however be aware that there is a faster solution in just finding which type of solutions are no longer available.

For mastermind there are multiple algorithms, see wikipedia, however for your first implementation I think they are too difficult.
You could start by using either
Thijser's idea (slightly better than brute-forcing all possibilities),
or try to emulate a human player: using that a white key-peg means correct color in wrong position and a black key-peg meaning correct color in correct position. You can write an easy recursion to take that info into account:
white-peg -> move the colors around ;
black-peg remove colors to find out which of the colors was the one that was correct-in-correct-pos.

Related

Implementing Intelligent design sort

This might be frivolous question, so please have understanding for my poor soul.
After reading this article about Intelligent Design sort (http://www.dangermouse.net/esoteric/intelligentdesignsort.html) which is in no way made to be serious in any way, I started wondering whether this could be possible.
An excerpt from article says:
The probability of the original input list being in the exact order it's in is 1/(n!). There is such a small likelihood of this that it's clearly absurd to say that this happened by chance, so it must have been consciously put in that order by an intelligent Sorter.
Let's for a second forget about intelligent Sorter, and think about possibility that random occurrences of members in array are in some way sorted. Our algorithm should determine the pattern without changing array's structure.
Is there any way to do this? Speed is not a requirement.
The implementation is very easy actually. The entire point of the article is that you don't actually sort anything. In other words, a correct implementation is a simple NOP. As my preferred language is Java, I'll show a simple in-place implementation in Java as a lambda function:
list->{}
Funny article, I had a good laugh.
If the only thing you're interested in is that whether your List is sorted, then you could simply keep an internal sorted flag (defaulted to true for an empty list) and override your add() method to check if the element you're adding fits the ordering of the List - that is, compare it to the adjacent elements and setting the sorted flag appropriately.

AS3 randomizing an array and comparing it to a string

So, I am a basic programmer in flash and this weekend I have to make a small mini game. This is where I get confused...I have 1 movieclip which has 5 labels ( each showing a different shape). I also have a dynamic text field which I have text or (a string) that will need to match the movieclip. Meaning, if the text displays circle, and the shape is circle, if you click the screen you win. if they dont match, you lose. So I am asking this in order to find out, how to create 2 arrays, randomize them then compare the value. I know how to set everything on timers and give scores, I just cant get figure this part out. AS3 and I are having a bad day. Any ideas, even pseudo code helps...or just a flow , something please ! lol thanks in advance
Regarding randomizing an array, have a look at this elaborate article at Activetuts, which specifically aims at Actionscript. It provides documented code with clear illustrations and tips. You could also check out the Fisher-Yates shuffle for some pseudo-code.
I don't quite get your question with regards to comparing the strings.. In AS3, you can use == to see if the strings are equal.

AI Minesweeper project

I need to implement Minesweeper solver. I have started to implement rule based agent.
I have implemented certain rules. I have a heuristic function for choosing best matching rule for current cell (with info about surrounding cells) being treated. So for each chosen cell it can decide for 8 surroundings cells to open them, to mark them or to do nothing. I mean. at the moment, the agent gets as an input some revealed cell and decides what to do with surrounding cells (at the moment, the agent do not know, how to decide which cell to treat).
My question is, what algorithm to implement for deciding which cell to treat?
Suppose, for, the first move, the agent will reveal a corner cell (or some other, according to some rule for the first move). What to do after that?
I understand that I need to implement some kind of search. I know many search algorithms (BFS, DFS, A-STAR and others), that is not the problem, I just do not understand how can I use here these searches.
I need to implement it in a principles of Artificial Intelligence: A modern approach.
BFS, DFS, and A* are probably not appropriate here. Those algorithms are good if you are trying to plan out a course of action when you have complete knowledge of the world. In Minesweeper, you don't have such knowledge.
Instead, I would suggest trying to use some of the logical inference techniques from Section III of the book, particularly using SAT or the techniques from Chapter 10. This will let you draw conclusions about where the mines are using facts like "one of the following eight squares is a mine, and exactly two of the following eight squares is a mine." Doing this at each step will help you identify where the mines are, or realize that you must guess before continuing.
Hope this helps!
I ported this (with a bit of help). Here is the link to it working: http://robertleeplummerjr.github.io/smartSweepers.js/ . Here is the project: https://github.com/robertleeplummerjr/smartSweepers.js
Have fun!

showing that any algorithm that accesses an array only by comparisons takes sigma(logn) steps

For this I think to properly solve it I need to show that sigma(logn) is its lower bound. I know all of the comparisons in my book run in O(nlogn), but im not sure how to form this into a concrete answer.
I think you've misread the problem: The array you are given is sorted. You are not sorting it. You are accessing it. Let's play a game. I pick a US state and you try to guess it. Every guess I will tell you if my chosen state is alphabetically before or after your guessed state. How many guesses do you need? The problem gives you a great clue with binary search.
Add this to your general algorithm toolbox: To show a lower bound is valid (but not necessarily tight), assume there exists an upper bound that is smaller, and do a proof by contradiction. For your problem, this should be doable.

MD5 code kata and BDD

I was thinking to implement MD5 as a code kata and wanted to use BDD to drive the design (I am a BDD newb).
However, the only test I can think of starting with is to pass in an empty string, and the simplest thing that will work is embedding the hash in my program and returning that.
The logical extension of this is that I end up embedding the hash in my solution for every test and switching on the input to decide what to return. Which of course will not result in a working MD5 program.
One of my difficulties is that there should only be one public function:
public static string MD5(input byte[])
And I don't see how to test the internals.
Is my approach completely flawed or is MD5 unsuitable for BDD?
I believe you chose a pretty hard exercise for a BDD code-kata. The thing about code-kata, or what I've understood about it so far, is that you somehow have to see the problem in small incremental steps, so that you can perform these steps in red, green, refactor iterations.
For example, an exercise of finding an element position inside an array, might be like this:
If array is empty, then position is 0, no matter the needle element
Write test. Implementation. Refactor
If array is not empty, and element does not exist, position is -1
Write test. Implementation. Refactor
If array is not empty, and element is the first in list, position is 1
Write test. Implementation. Refactor
I don't really see how to break the MD5 algorithm in that kind of steps. But that may be because I'm not really an algorithm guy. If you better understand the steps involved in the MD5 algorithm, then you may have better chances.
It depends on what you mean with unsuitable... :-) It is suitable if you want to document a few examples that describes your implementation. It should also be possible to have the algorithm emerge from your specifciation if you add one more character for each test.
By just adding a switch statement you're just trying to "cheat the system". Using BDD/TDD does not mean you have to implement stupid things. Also the fact that you have hardcoded hash values as well as a switch statement in your code are clear code smells and should be refactored and removed. That is how your algorithm should emerge because when you see the hard coded values you first remove them (by calculating the value) and then you see that they are all the same so you remove the switch statement.
Also if your question is about finding good katas I would recommend lokking in the Kata catalogue.

Resources