Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 4.74 KB | None | 0 0
  1. carta(c(R,S)) :- ranks(LR), naipe(LS), member(R,LR), member(S,LS).
  2.    
  3. ranks([2,3,4,5,6,dama,valete,rei,bisca,a]).
  4.  
  5. naipe([copas,paus,espadas,ouros]).
  6.  
  7. mesmoNaipe(c(_,S),c(_,S)).
  8.  
  9.     %base de dados para o predicado cartaMaisAlta().
  10.     valor(carta(dama,_),7):-!.
  11.     valor(carta(valete,_),8):-!.
  12.     valor(carta(rei,_),9):-!.
  13.     valor(carta(bisca,_),10):-!.
  14.     valor(carta(a,_),11):-!.
  15.     valor(carta(X,_),X):-!. integer(X), X>=2, X=<6.
  16.  
  17. %pontuação de cada carta. Depois de terminar o jogo
  18.     pontos(carta(dama,_),2):-!.
  19.     pontos(carta(valete,_),3):-!.
  20.     pontos(carta(rei,_),4):-!.
  21.     pontos(carta(bisca,_),10):-!.
  22.     pontos(carta(a,_),11):-!.
  23.     pontos(carta(X,_),0) :- integer(X), X>=2 , X=<6.
  24.  
  25.     %esta a falhar nas cartas que nao sao numeros
  26.     cartaMaisAlta(R1,R2,R1) :- ranks(R), member(R1,R), valor(carta(R1,_),C1), valor(carta(R2,_),C2), C1>=C2.
  27.     cartaMaisAlta(R1,R2,R2) :- ranks(R), member(R1,R), valor(carta(R1,_),C1), valor(carta(R2,_),C2), C2>=C1.
  28.  
  29.    
  30.  
  31.   play:- baralho(L), trunfo(L,T), writeln(T), delete(L,T,L1), distribui(L1,M1,M2,L2),
  32.     %player(M1,H1,C1), player(M2,H2,C2), hand(H1), hand(H2), contador(C1),contador(C2), nextPlayer(M1,M2),
  33.     aux_play([2,play,L2,M1,M2,[],[],_,T]).
  34.    
  35.     trunfo(L,T):- reverse(L,[T|_]).  
  36.     %nextPlayer(M1,M2).
  37.     %player(H,C):-
  38.     %aux_play()
  39.     aux_play([_,_,[],[],[],S1,S2,_,T]):-
  40.     (drawGame(S1,S2),S=draw;winGame(S1,S2),S=lose;winGame(S2,S1),S=win).  
  41.     aux_play([_,draw,_,_,_,_,_,_,_]):-writeln("Draw").
  42.     aux_play([_,lose,_,_,_,_,_,_,_]):-writeln("Human wins").
  43.     aux_play([_,win,_,_,_,_,_,_,_]):-writeln("Minimax wins").
  44.    
  45. % levar sempre tudo que importa com esta funcao (baralho, maos dos jogadores,trunfo, stacks)
  46.     aux_play([1,play,L,M1,M2,S1,S2,(RLP,NLP),(RT,NT)]):- writeln("-----------------"),
  47.     writeln("Computer plays:"),
  48.     %minimax([1,play,L,M1],[P,S,L1,M3],J), writeln(J),
  49.     writeln("PC playing"),
  50.     writeln("Na mao:"),
  51.     writeln(M1),
  52.     writeln("Inserir carta"), flush_output,
  53.     read((R,N)),
  54.     newHand((R,N),M1,M3),
  55.  
  56.     %%% P1 nao jogou trunfo e P2 jogou trunfo
  57.     ((N == NT, not(NLP == NT), addStack((RLP,NLP),(R,N),S1,S3), S4 = S2);
  58.  
  59.      %%% P1 jogou trunfo e P2 nao jogou trunfo
  60.      (NLP == NT, not(N==NT), addStack((RLP,NLP),(R,N),S2,S4), S3 = S1);
  61.  
  62.      %%% Nao jogaram trunfo mas sao de naipes diferentes => o primeiro que jogou fica com a palha toda
  63.      (not(NLP==N), not(N==NT), not(NLP==NT), addStack((RLP,NLP),(R,N),S2,S4), S3 = S1);
  64.  
  65.      %%% Nao jogaram trunfo mas sao de naipes iguais => ver quem jogou carta de maior rank
  66.      (N==NLP, not(N==NT), not(NLP==NT), (cartaMaisAlta(R,RLP,R), addStack((RLP,NLP),(R,N),S1,S3), S4 = S2);
  67.                                         (cartaMaisAlta(R,RLP,RLP), addStack((RLP,NLP),(R,N),S2,S4), S3 = S1));
  68.  
  69.      %%% ambos jogaram trunfo => ver quem jogou carta de maior rank
  70.      (N==NT, NLP==NT, (cartaMaisAlta(R,RLP,R), addStack((RLP,NLP),(R,N),S1,S3), S4 = S2) ;
  71.                       (cartaMaisAlta(R,RLP,RLP), addStack((RLP,NLP),(R,N),S2,S4), S3 = S1))), % verificar quem ganhou a mao e adicionar a stack
  72.    
  73.     writeln("Stack Jogador 1 (PC):"), writeln(S3),
  74.     writeln("Stack Jogador 2 (Humano):"), writeln(S4),
  75.     %writeln(M3), % mao depois de jogar, antes de receber cartas
  76.     %writeln(M2), % ^
  77.     givecards(L,M3,M2,L2,M5,M4),
  78.     %writeln(M5), % mao depois de receber cartas, algo errado com o givecards
  79.     %writeln(M4), % ^
  80.     aux_play([2,play,L2,M5,M4,S3,S4,_,(RT,NT)]).
  81.    
  82.     aux_play([2,play,L,M1,M2,S1,S2,_,T]):-
  83.     writeln("Human playing"),
  84.     writeln("Na mao:"),
  85.     writeln(M2),
  86.     writeln("Inserir carta"), flush_output,
  87.     read((R,N)),
  88.     newHand((R,N),M2,M),
  89.     aux_play([1,play,L,M1,M,S1,S2,(R,N),T]).
  90.  
  91.    
  92.     addStack((R,N),(R1,N1),S1,S2):- append([(R,N),(R1,N1)],S1,S2).  
  93.     :-use_module(library(random)).
  94.     baralho(L2) :- ranks(L1), allcards(L1,_,L), random_permutation(L,L2).
  95.     %shuffle(L1, L):-random_permutation(L1,L).
  96.     %givecards(baralho,P1,P2,restoBaralho,cartas P1, cartas P2).
  97.     givecards([X,Y|L1],M1,M2,L1,[X|M1],[Y|M2]).
  98.    
  99.     %lista de todas as cartas(produto cartesiano das listas ranks e naipe)
  100.     allcards([],_,[]).
  101.     allcards([X|L1],L2,R) :- naipe(L2), aux_cards(X,L2,L3),
  102.                              allcards(L1,L2,L4), append(L3,L4,R).
  103.     aux_cards(_,[],[]).
  104.     aux_cards(X,[Y|L1],[(X,Y)|L2]) :-aux_cards(X,L1,L2).
  105.  
  106.     :-use_module(library(lists)).
  107.     %numero de cartas por jogador = 3
  108.     %distribui(baralho, cartas P1, cartas P2, resto do baralho)
  109.     distribui([A,B,C,D,E,F|L1],[A,B,C],[D,E,F],L1).
  110.     newHand((R,N),M,M2):- member((R,N),M), delete(M,(R,N),M2),!. % not finished
  111.     %newHand((R,N),M,M2):-not(member((R,N),M)), writeln("Essa carta nao existe na tua mao").
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement