03 iulie 2010

Exemple simple in Java folosind swing

// afisarea directorului curent

import javax.swing.*;
import java.io.*;

public class ShowDir {

static JFrame f = new JFrame ();
static JTextField tf = new JTextField (20);
public static void main (String args[]) {
JPanel pane = new JPanel ();
File dir = new File (".");
pane.add (new JLabel ("Director curent"));
tf.setText (dir.getAbsolutePath());
tf.setEditable (false);
pane.add(tf);
f.setContentPane(pane);
f.setSize(300,100);
f.setVisible (true);
}

}

/* . Program care afiseaza un obiect JFileChooser(). Se va apela metoda
"showOpenDialog" cu parametrul "null" si se va afisa numele fisierului selectat
atunci cand rezultatul metodei este zero. Cum se poate repeta selectarea unui
fisier si afisarea lui, intr-o aceeasi executie a programului ?
Sa se adauge un camp text pentru afisarea numelui fisierului selectat.
Continutul unui JTextField se stabileste cu metoda "setText".
Se vor folosi succesiv FlowLayout si BorderLayout.*/

import javax.swing.*;
import java.util.*;
import java.awt.*;

public class Chooser extends JFrame {

JFileChooser chooser;
public Chooser(String titlu) {
super(titlu);
setLayout(new FlowLayout());
setSize(300,300);

chooser=new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal==0)
System.out.println(chooser.getSelectedFile().getName());
}
public static void main (String [] arg) {
new Chooser("Select");

}
}

29 iunie 2010

X si 0

#include"stdio.h"
#include"stdlib.h"

#define B_SIZE 3
#define WIN 10000
#define LOSS -10000
#define DRAW 0
#define OCHAR(ch) ((ch=='X')?'O':'X')

char gameboard[3][3] =
{' ',' ',' ',
' ',' ',' ',
' ',' ',' '
};

int curX,curY;
int nodesSearched;

void clrscr(void)
{
#ifdef __linux__
system("clear");
#else
system("CLS");
#endif
}

// Draws the game board accourding to the accupancy matrix
void DrawBoard(char board[3][3])
{
printf("\n 0 1 2\n +-+-+-+\n");
for(int i=0; i < B_SIZE;i++)
{
printf("%d ",i);
for(int j=0; j < B_SIZE;j++)
printf("|%c",board[i][j]);
printf("|\n +-+-+-+\n");
}
}

// Evaluates the win
int CheckStatus(char board[3][3],char ch)
{
int i,j,ord;
for(i=0; i < B_SIZE;i++)
{
ord=0;
for(j=0; j < B_SIZE;j++)
{
if(board[i][j]==ch) ord++;
else break;
}
if(ord==3) return 1;
}
ord=0;
for(i=0; i < B_SIZE;i++)
{
ord=0;
for(j=0; j < B_SIZE;j++)
{
if(board[j][i]==ch) ord++;
else break;
}
if(ord==3) return 1;
}
ord=0;
for(i=0;i < B_SIZE;i++)
if(board[i][i]==ch) ord++;
if(ord==3) return 1;
else ord=0;
for(i=0; i < B_SIZE;i++)
if(board[i][B_SIZE-i-1]==ch) ord++;
if(ord==3) return 1;
return 0;
}

int Negamax(char board[3][3],int occ,int depth,char ch)
{
int score, max=LOSS;
int i,j;
nodesSearched++;
if(CheckStatus(board,ch)==1) return WIN*(10-depth); //*(10-depth) is there so we choose the shortest path to victory
else if(CheckStatus(board,OCHAR(ch))==1) return LOSS*(10-depth);
else if(occ>=9) return DRAW;
for(i=0; i < B_SIZE;i++)
for(j=0;j < B_SIZE;j++)
if(board[j][i]==' ')
{
board[j][i]=ch;
score=-Negamax(board,occ+1,depth+1,OCHAR(ch));
board[j][i]=' ';
if(score > max)
{
max=score;
if(depth==0)
{
curX=i;
curY=j;
}
}
}
return max;
}

int AlphaBeta(char board[3][3],int occ,int depth,int alpha,int beta,char ch)
{
int score,bestscore=LOSS;
int i,j;
nodesSearched++;
if(CheckStatus(board,ch)==1) return WIN*(10-depth); //*(10-depth) is there so we choose the shortest path to victory
else if(CheckStatus(board,OCHAR(ch))==1) return LOSS*(10-depth);
else if(occ>=9) return DRAW;
for(i=0; i < B_SIZE;i++)
for(j=0; j < B_SIZE;j++)
if(board[j][i]==' ')
{
board[j][i]=ch;
score=-AlphaBeta(board,occ+1,depth+1,-beta,-alpha,OCHAR(ch));
board[j][i]=' ';
if(score > beta) return score; // beta cutoff
if(score > bestscore)
{
bestscore=score;
if(depth==0)
{
curX=i;
curY=j;
}
if(score>alpha) alpha=score;
}
}
return bestscore;
}

int main()
{
int x,y,occ=0;
char mych='X',winner=0;
do
{
clrscr();
DrawBoard(gameboard);
printf("Previous move explored %d nodes\nIntroduce (x,y) coordinates:\n",nodesSearched);
scanf("%d",&y);
scanf("%d",&x);
if(x>=B_SIZE || x<0>=B_SIZE || y<0 || gameboard[y][x]!=' ') continue;

gameboard[y][x]=mych;
if(CheckStatus(gameboard,mych))
{
winner=mych;
continue;
}
if((++occ)>=9) break;

nodesSearched=0;
//Negamax(gameboard,occ,0,OCHAR(mych));
AlphaBeta(gameboard,occ,0,LOSS*11,WIN*11,OCHAR(mych));

gameboard[curY][curX]=OCHAR(mych);
if(CheckStatus(gameboard,OCHAR(mych)))
{
winner=OCHAR(mych);
continue;
}
if((++occ)>=9) break;

}while(!winner);
clrscr();
DrawBoard(gameboard);
printf("\n\nWinner is %c",winner);
return 0;
}

27 iunie 2010

Exercitii Prolog

% daca o lista e palindroma

is_palindrome(L) :- reverse(L,L).

% daca un numar este prim
radacina(X, Y):-
N is Y*Y, N =< X, X mod Y =:= 0.
radacina(X, Y):-
Y < X, Y1 is Y + 1, radacina(X, Y1).
prim(X):-
Y is 2, X > 1 , \+radacina(X, Y).


% duplicarea elem unei liste
dupli([],[]).
dupli([X|Xs],[X,X|Ys]) :- dupli(Xs,Ys).

se aplica: dupli([1,2,3], L).


% o lista care contine toate elementele intre 2 limite

range(I,I,[I]).
range(I,K,[I|L]) :- I < K, I1 is I + 1, range(I1,K,L).

se aplica: range(1,5, L).


altele

Quicksort (Haskell)

module Quick_sort where

partition p [] = ([],[])
partition p (x:rest) = if p x then (x:l,r) else (l,x:r)
where (l,r) = partition p rest
{-
sau
partition p (x:rest)
| p x = (x:l,r)
| otherwise = (l,x:r)
where (l,r)= partition p rest

-}

-- Varianta 1
qsort [] = []
qsort (x:ls) = qsort l ++ [x] ++ qsort r
where (l,r) = partition (<= x) ls

-- Varianta 2
qs [] = []
qs (x:ls) = qs l ++ [x] ++ qs r
where l = [y | y <- ls, y <= x]
r = [y | y <- ls, y > x]

-- Varianta 3
qss [] = []
qss (x:ls) = let l = [y | y <- ls, y <= x]
r = [y | y <- ls, y > x]
in qss l ++ [x] ++ qss r

Problema reginelor in Clips

; Placing N non-attacking queens on a NxN chessboard
; ==================================================
; CGA 2009

(deftemplate board (slot id) (slot free_line) (multislot queens))
; chessboard = (board (id an_id) (free_line l) (queens [1 c1 ] [ 2 c2 ] ... [ N cN ] ))

(defrule read_size
=>
(printout t "Board size: ")
(bind ?n (read))
(assert (board (id (gensym)) (free_line ?n) (queens))
(solutions 0))
(if (> ?n 0) then (assert (column ?n))))

(defrule lines_and_columns
(column ?n&~1)
=>
(assert (column (- ?n 1))))

(defrule place_queen
(board (id ?id) (free_line ?l&~0) (queens $?q))
(column ?c)
(not (board (id ?id)
(queens $? [ ?lq ?cq&:(or (= ?cq ?c) (= (- ?lq ?l) (abs (- ?cq ?c)))) ] $?)))
=>
(assert (board (id (gensym)) (free_line (- ?l 1)) (queens [ ?l ?c ] $?q))))

(defrule solution
?f <- (board (free_line 0) (queens $?q))
?g <- (solutions ?c)
=>
(retract ?f ?g)
(assert (solutions (+ ?c 1)))
(printout t $?q crlf))

(defrule print_count
(declare (salience -10))
(solutions ?n)
=>
(printout t ?n " solutions" crlf))