#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;
}
29 iunie 2010
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
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))
Prolog
parent(andrew, bob).
parent(andrew, beth).
parent(bob, carlos).
parent(beth, cindy).
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
mylast(X, [X]).
mylast(X, [_|T]) :- mylast(X, T).
Determinati penultimul element al unei liste. Exemplu:
?- before_last(X, [1, 2, 3]). X = 2
before_last(X, [X,Y]).
before_last(X, [_|T]) :- before_last(X, T).
Determinati elementul de indice
k
dintr-o lista. Exemplu:?- kth(X, 3, [4, 3, 2]). X = 2
kth(X,1,[X|T]).
kth(X,K,[Y|L]) :- Z is K-1 , kth(X,Z,L).
Determinati lungimea unei liste. Exemplu:
?- len(X, [1, 2, 3]). X = 3
len(0, []).
len(X, [H|T]) :- len(Z, T) , X is Z+1.
Concatenati doua liste. Exemplu:
?- concat([1, 2], [3], L). L = [1, 2, 3]
concat([], L, L).
concat([H|T],X,[H|L]) :- concat(T, X, L).
Inversati o lista. Exemplu:
?- rev([1, 2, 3], L). L = [3, 2, 1]
put(X,L,V) :- concat(L,[X],V).
inver([],[]).
inver([H|T], L) :- inver(T,B) , put(H,B,L).
Abonați-vă la:
Postări (Atom)