![]() The program should work with small changes. We could program a new user control with pictures of frogs instead of buttons. MessageBox.Show( " No more moves are possible\n", MessageBox.Show( " Congratulations! You won!", Private void aTable1_NextM( int position) These buttons share a lot of properties, so I put them in a table of buttons. The control contains a TableLayoutPanel with labels (for the position of the frog) and seven buttons. For this purpose (and for the simulation), I decided to create a UserControl called ATable. With the program, you can also play the game. In the end, I've added a simulation of the game. ![]() In TreeView, you can see how the computer makes his moves. First I've used a ListBox to view, if my solution works. I've tried to present the solution in three different ways. Private ArrayList FindAll(StringBuilder s, int k) The method TryThis takes one parameter which describes the number of the step (or the depth of the tree - for example "L 1 2" and "R 4 2" have the same step number).įirst, I declared a struct solutionData, which describes one step of the solution. The edge condition is reached when there are no more elements left in the array of all possible moves. It is a recursive method, so it is using an edge condition and a recursive call. Using the Codeįor the solution, I've programmed a method called TryThis. We also have to think that the game can't be solved. We continue this procedure until we reach the goal. Since this is not a winning one, we have to take the move "R from 5 to 4". In the third step, we explore the possible move "L from 3 to 4". So we take the alternative second move, "R from 4 to 2". With black backcolor are signed moves that were taken but cancelled because they didn't lead to the goal.Īfter the first move, "L from 2 to 3" we see, that moves "L from 1 to 2" and "L from 0 to 1" don't lead to the end of the game. With red forecolor are signed moves that were taken. In the picture, we see at the most left level the first two possible moves. We continue the procedure until we reach the goal or we have run out of possible moves. Since we exploited the first possibility and we didn't reach the goal, we take the other possible move: "R from 4 to 2". In this situation, there was only one possible move, so we must go one more step back and resume the situation: LL_LRRR. We have to resume the old situation: L_LLRRR. We don't have any possible move left and we didn't reach the goal. We can decide to take the first possibility in each step. In the starting position, there are two possible moves : "L from 2 to 3" and "R from 4 to 3". Each element has its position, its letter and new possible position. I decide to make a table containing 7 elements with indexes from 0 to 6. The solution of the problem is usually in the leaves of the tree or (in our case) the path to a leaf. The algorithm for solving the problem is a standard backtracking algorithm with which we review the nodes of a tree. If the move leads to a situation where no more "legal" moves are possible, the computer must discard the move and take the alternative. If the move is leading to the goal, the computer must remember the move and the path to reach this move. The computer must try all the possibilities in a situation. When it makes the first move, it isn't clear if this move will lead to a happy ending. The computer must make moves which lead to the goal. The problems begin when the computer solves the game. In this case, we notify the user and reset the game. It is also possible that no more moves are possible and the goal isn't reached. All we have to do is to validate the user move and check if he has reached the goal. Writing the code for playing the game is simple. The frog with an 'R' can move from right to left for one position or for two positions, if it jumps the frog with an 'L'. The frog with an 'L' can move from left to right for one position or for two positions if it jumps the frog with an 'R'.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |