How do I declare constants in SWI-Prolog? - prolog

I have created a basic adventure game in prolog. The player starts with 0 points, and I want the points to increase as the player progresses in the game. How do I do this?
Pardon me if my question sounds stupid, I'm only a beginner in Prolog.

You can add facts at run time using:
asserta(...)
assertz(...)
asserta adds a fact on the top, so using the cut i think you can hide the previous value.

Related

How to resolve a specific matrix (undirected structured homogeneous graph) containing binary elements

A friend asked me to do a program for him :
The program needed is for resolving a "puzzle game" composed by 10 torches, arranged like a 2x5 (or 5x2) matrix. The goal is to resolve the puzzle by having all torches alight, but here is the difficulty : when you change the state of a torch, adjacent torches have their state changed too (only adjacent, not the ones in diagonals).
So it is like I have a specific matrix which contain binary elements that I need to resolve by turning all elements to True (or "alight"), but elements are linked with adjacent elements like an undirected structured homogeneous graph ("mesh" graph).
Here is some picture to figure how the puzzle works :
Exemple of initial pattern
Puzzle Mechanics
Here is where I need some help/advice/clue :
First, how can I solve this kind of puzzle in a clever way (already done it by trying all possibilities, but when I need more than 5 "step", it takes really too much time to calculate), using maths.
Then, which programming langage should I use ? Which one would fit the best to this kind of processing ? (I want to do an interface where user enter an initial pattern, then the program search the solution and give the step to resolve it by giving coords and eventualy all steps in pictures on the interface. I will surely post it on a forum for my friend and his game mates.)
I'm still searching, but have not found anything that could help me to effectively resolve that kind of puzzle yet.
Thanks a lot for any help and contribution.
PS : sorry for any bad use of english.
You can solved this problem by using BruteForce approach.
There are only 10 torches and you just need to press each torch exactly once. So it will be 2^10 = 1024 combination.
For the programming language you can use: Python, C/C++, Java or C#.
Any programming language can solved this problem.

Developing a Checkers (Draughts) engine, how to begin?

I'm a relatively inexperienced programmer, and recently I've been getting interested in making a Checkers game app for a school project. I'm not sure where I can start (or if I should even attempt) at creating this. The project I have in mind probably wouldn't involve much more than a simple AI & a multiplayer player mode.
Can anyone give some hints / guidance for me to start learning?
To some extent I agree with some of the comments on the question that suggest 'try something simpler first', but checkers is simple enough that you may be able to get a working program - and you will certainly learn useful things as you go.
My suggestion would be to divide the problem into sections and solve each one in turn. For example:
1) Board representation - perhaps use an 8x8 array to represent the board. You need to be able to fill a square with empty, white piece, black piece, white king, black king. A more efficient solution might be to have a look at 'bit-boards' in which the occupancy of the board is described by a set of 64-bit integers. You probably want to end up with functions that can load or save a board state, print or display the board, and determine what (if anything ) is at some position.
2) Move representation - find a way to calculate legal moves. Which pieces can move and where they can move to. You will need to take into account - moving off the edges of the board, blocked moves, jumps, multiple jumps, kings moving 'backwards' etc. You probably want to end up with functions that can calculate all legal moves for a piece, determine if a suggested move is legal, record a game as a series of moves, maybe interface with the end user so by mousing or entering text commands you can 'play' a game on your board. So even if you only get that far then you have a 'product' you can demonstrate and people can interact with.
3) Computer play - this is the harder part - You will need to learn about minimax, alpha-beta pruning, iterative deepening and all the associated guff that goes into computer game AI - some of it sounds harder than it actually is. You also need to develop a position evaluation algorithm that measures the value of a position so the computer can decide which is the 'best' move to make. This can be as simple as the naive assumption that taking an opponents piece is always better than not taking one, that making a king is better than not making one, or that a move that leaves you with more future moves is better than one that leaves you with less choices for your next move. In practice, even a very simple 'greedy' board evaluation can work quite well if you can look 2-3 moves ahead.
All in all though, it may be simpler to look at something a little less ambitious than checkers - Othello is possibly a good choice and it is not hard to write an Othello player that can thrash a human who hasn't played a lot of the game. 3D tic-tac-toe, or a small dots-and-boxes game might be suitable too. Games like these are simpler as there are no kings or boundaries to complicate things, all (well most) moves are legal and they are sufficiently 'fun' to play to be a worthwhile software demonstration.
First let me state, the task you are talking about is a lot larger then you think it is.
How you should do it is break it down into very small manageable pieces.
The reasons are
Smaller steps are easier to understand
Getting fast feed back will help inspire you to continue and will help you fix things as they go wrong.
As you start think of the smallest step possible of something to do. Here are some ideas of parts to start:
Make a simple title screen- Just the title and to hit a key for it to
go away.
make the UI for an empty checkerboard grid.
I know those sound like not much but those will probably take much ore time than you think.
then add thing like adding the checkers, keeping the the gameboard data etc.,
Don't even think about AI until you have a game that two players can play with no UI.
What you should do is think about: what is the smallest increment I can do and add that, add that and then think about what the next small piece is.
Trust me this is the best way about going about it. If you try to write everything at once it will never happen.

Algorithmic solution to Minesweeper

I am trying to make the minesweeper solver. As you know there are 2 ways to determine which fields in minefield are safe to open, or to determine which fields are mined and you need to flag it. First way to determine is trivial and we have something like this:
if (number of mines around X – current number of discovered mines around X) = number of unopened fields around X then
All unopened fields around X are mined
if (number of mines around X == current number of discovered mines around X) then
All unopened fields around X are NOT mined
But my question is: What about situation when we can't find any mined or safe field and we need to look at more than 1 field?
http://img541.imageshack.us/img541/4339/10299095.png
For example this situation. We can't determine anything using previous method. So i need a help with algorithm for these cases.
I have to use A* algorithm to make this. That is why i need all possible safe states for next step in algorithm. When i find all possible safe states i will add them to the current shortest path and depending on heuristic function i will sort list of paths and choose next field that needs to be opened.
Awesome problem, before you get too excited though, please read NP Completeness and Minesweeper, as well as the accompanying presentation which develops some good worst case examples and how a human might solve them. Nevertheless, in expectation we most likely won't hit a time barrier, if we use basic pruning and heuristics.
The question of generating the game is asked here: Minesweeper solving algorithm. There is a very cool post on algebraic methods. You can also give backtracking a try (i.e. take a guess and see if that invalidates things), similar to the case where local information is not enough for something like sudoku. See this great discussion about this technique.
As #tigger said this is not a problem that can be solved with a simple set of rules. Minesweeper is a good example where backtracking algorithms such as DPLL is useful. With something as simple as propositional logic, you can implement a very efficient solver for minesweeper. I am not sure if you are familiar with AI reasoning & logic inference - If not, you might want to have a look at the book "Artificial Intelligence - A Modern Approach" by Stuart Russel and Peter Norvig. For quick reference of DPLL and propositional logic, search "wumpus world propositional logic" on Google.

Othello (Reversi) game, flipping pieces, Prolog

I am implementing Othello game in Prolog. The game board is represented as list of lists.
I am facing a problem with flipping pieces after making a move.
My strategy is to look in all 8 directions from position where I placed my piece (say black),
and find the enclosing black piece and flip every white piece between my pieces.
So, I have 8 separate predicates to do that.
The problem is I call them sequentially after I make a move, and if any one of these predicates fails, the whole thing fails.
Is there any way to get around this? Or maybe my approach is wrong?
Maybe you should try to OR the predicates?
I know I wrote this for a CS class when studying at uni, I hope your not using stackoverflow to cheat on your assignments... ;)
As suggested by Cari Norum I just make my predicates never fail. So if one fails, I just make it return the current board state. That seems to work.

being able to solve google code jam problem sets [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
This is not a homework question, but rather my intention to know if this is what it takes to learn programming. I keep loggin into TopCoder not to actually participate but to get the basic understand of how the problems are solved. But to my knowledge I don't understand what the problem is and how to translate the problem into an algorithm that can solve it.
Just now I happen to look at ACM ICPC 2010 World Finals which is being held in china. The teams were given problem sets and one of them is this:
Given at most 100 points on a plan with distinct x-coordinates,
find the shortest cycle that passes through each point exactly once,
goes from the leftmost point always to the right until it reaches the
rightmost point, then goes always to the left until it gets back to the
leftmost point. Additionally, two points are given such that the the path
from left to right contains the first point, and the path from right to
left contains the second point. This seems to be a very simple DP: after
processing the last k points, and with the first path ending in point a
and the second path ending in point b, what is the smallest total length
to achieve that? This is O(n^2) states, transitions in O(n). We deal
with the two special points by forcing the first path to contain the first
one, and the second path contain the second one.
Now I have no idea what I am supposed to solve after reading the problem set.
and there's an other one from google code jam:
Problem
In a big, square room there are two point light sources:
one is red and the other is green. There are also n circular pillars.
Light travels in straight lines and is absorbed by walls and pillars.
The pillars therefore cast shadows: they do not let light through.
There are places in the room where no light reaches (black), where only
one of the two light sources reaches (red or green), and places where
both lights reach (yellow). Compute the total area of each of the four
colors in the room. Do not include the area of the pillars.
Input
* One line containing the number of test cases, T.
Each test case contains, in order:
* One line containing the coordinates x, y of the red light source.
* One line containing the coordinates x, y of the green light source.
* One line containing the number of pillars n.
* n lines describing the pillars. Each contains 3 numbers x, y, r.
The pillar is a disk with the center (x, y) and radius r.
The room is the square described by 0 ≤ x, y ≤ 100. Pillars, room
walls and light sources are all disjoint, they do not overlap or touch.
Output
For each test case, output:
Case #X:
black area
red area
green area
yellow area
Is it required that people who program should be should be able to solve these type of problems?
I would apprecite if anyone can help me interpret the google code jam problem set as I wish to participate in this years Code Jam to see if I can do anthing or not.
Thanks.
It is a big mistake to start from hard problems. Many World Finals problems are too hard for lots of experienced programmers, so it is no surprise that it is also too hard for someone new.
As others have said, start with much easier problems. I am assuming you know the basics of programming and can write code in at least one programming language. Try problems from Division-2 problemsets on TopCoder, and Regional/Qualifying rounds of ACM ICPC. Find out the easy problems from sites like SPOJ, UVa and Project Euler (there are lists of easy problems available online) and solve them. As you solve, also read up on the basics of algorithms and computer science. TopCoder is a great resource since they have lots of tutorials and articles and also allow you to view other people's solutions.
IMHO, becoming a better programmer in general takes a lot of practice and study. There is no shortcut. You cannot assume that you are some sort of hero programmer who can just jump in and solve everything. You just have to accept that there is a long way to go, and start at the beginning.
You should start with much easier problems. Try looking for regionals, or even get the problems from the local schools contest.
You need to have a large view of general programming, from data structures to algorithms. Master a basic programming language, one that the answers are accepted.
You cannot start learning programming by doing competitions. If you don't know any programming at all, the first programs you will write are things like "hello world", fibonacci and ackermann. Starting with things like TopCoder is like learning to drive using a formula one car. It doesn't work that way.
In short, you have to know some basic techniques that are used to develop this kinds of problems. Knowing dynamic programming, backtracking algorithms, searching, etc, helps you a lot when solving the problems.
This one from Google Code Jam is actually pretty hard, and involves computational geometry algorithms. How to solve it is detailed here: http://code.google.com/codejam/contest/dashboard?c=311101#s=a&a=5
I've been working Google Code Jam problems for the past week or so, and I think they are great exercises. The key is to find problems that stretch your abilities a little, but aren't the ones that make you want to give up. Google Code Jam problems range widely in difficulty!
I recommend starting with the ones under "Where should I start" here:
http://code.google.com/codejam/contests.html
And then explore all of the competition round 1 problems. If those are too easy move up to the other rounds.
The thing I really like about code jam is that you can use pretty much any language you want, and you can get feedback from their automated judge. If you run out of Code Jam problems check out some of the other sites that others have mentioned.
You are very right JesperE. JPRO, go back to the basics and get it settled from there. Your ability to compete and win programming contests depends on just two things:
Your deep understanding of the language you are using and
your mental flexibility with the language.
Read Problem Solving Through Problems by Larson.
It's for mathematics but I find it extremely useful for solving algorithm problems.

Resources