2012-11-17 00:55:58 +01:00
|
|
|
#ifndef _TRIS_H_
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
#define _TRIS_H_
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Librerie Principali*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <limits.h>
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Colori ANSI*/
|
|
|
|
#define NERO "\033[22;30m"
|
|
|
|
#define BLU "\033[34m"
|
|
|
|
#define GIALLO "\033[33m"
|
|
|
|
#define BIANCO "\033[37m"
|
|
|
|
#define ROSSO "\033[31m"
|
|
|
|
#define RESET "\033[0m"
|
|
|
|
#define GRASSETTO "\033[1m"
|
|
|
|
#define SOTTOLINEATO "\033[4m"
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Definizioni*/
|
|
|
|
#define random(x) rand() % x
|
|
|
|
#define randomize srand((unsigned)time(NULL))
|
|
|
|
#define M 3
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Funzione specifiche degli os*/
|
|
|
|
#ifdef __WIN32__
|
|
|
|
|
|
|
|
#define os 0
|
|
|
|
#include <conio.h>
|
|
|
|
#define clear "cls"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define os 1
|
|
|
|
#define clear "clear"
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
int mgetchar(void);
|
|
|
|
int mgetchar(){
|
2012-11-19 16:54:12 +01:00
|
|
|
char buf=0;
|
|
|
|
struct termios old={0};
|
|
|
|
if(tcgetattr(0,&old)<0)
|
|
|
|
perror("tcsetattr()");
|
|
|
|
old.c_lflag&=~ICANON;
|
|
|
|
old.c_lflag&=~ECHO;
|
|
|
|
old.c_cc[VMIN]=1;
|
|
|
|
old.c_cc[VTIME]=0;
|
|
|
|
if(tcsetattr(0,TCSANOW,&old)<0)
|
|
|
|
perror("tcsetattr ICANON");
|
|
|
|
if(read(0,&buf,1)<0)
|
|
|
|
perror("read()");
|
|
|
|
old.c_lflag|=ICANON;
|
|
|
|
old.c_lflag|=ECHO;
|
|
|
|
if(tcsetattr(0,TCSADRAIN,&old)<0)
|
|
|
|
perror("tcsetattr ~ICANON");
|
|
|
|
return (buf);
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Prototipi*/
|
|
|
|
void vuota(void);
|
|
|
|
void stampa(void);
|
|
|
|
int controlla(int giocatore);
|
|
|
|
void spinner(int tempo);
|
|
|
|
int minimax(int giocatore, int profondita);
|
|
|
|
void facile(void);
|
|
|
|
void medio(void);
|
|
|
|
void difficile(void);
|
|
|
|
void pausa(void);
|
|
|
|
void leggimossa(void);
|
|
|
|
void aspetta(int t);
|
|
|
|
void beep(int t);
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Variabili globali*/
|
|
|
|
char a='a',c;
|
|
|
|
int i,k;
|
|
|
|
int giocatore,scelta,mossa;
|
|
|
|
int tabella[M][M];
|
2012-11-19 16:54:12 +01:00
|
|
|
int tastierino=0;
|
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
/*Funzioni*/
|
|
|
|
void stampa(){
|
2012-11-19 16:54:12 +01:00
|
|
|
int i,k;
|
|
|
|
printf("\n\n\n");
|
|
|
|
for(i=0;i<M;i++) {
|
|
|
|
printf(RESET" ");
|
|
|
|
for(k=0;k<M;k++) {
|
|
|
|
if(tabella[i][k]==1)
|
|
|
|
printf(GIALLO" O ");
|
|
|
|
else {
|
|
|
|
if(tabella[i][k]==2)
|
|
|
|
printf(ROSSO" X ");
|
|
|
|
else
|
|
|
|
printf(BIANCO" . ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
int controlla(int giocatore){
|
2012-11-19 16:54:12 +01:00
|
|
|
for(i=0;i<M;i++){
|
|
|
|
if(tabella[i][i]==giocatore&&tabella[i+1][i+1]==giocatore&&tabella[i+2][i+2]==giocatore)
|
2012-11-17 00:55:58 +01:00
|
|
|
return 1;
|
2012-11-19 16:54:12 +01:00
|
|
|
if(tabella[i+2][i]==giocatore&&tabella[i+1][i+1]==giocatore&&tabella[i][i+2]==giocatore)
|
2012-11-17 00:55:58 +01:00
|
|
|
return 1;
|
2012-11-19 16:54:12 +01:00
|
|
|
}
|
|
|
|
for(i=0;i<M;i++){
|
|
|
|
for(k=0;k<M;k++)
|
|
|
|
if(tabella[i][k]==giocatore&&tabella[i][k+1]==giocatore&&tabella[i][k+2]==giocatore)
|
|
|
|
return 1;
|
|
|
|
for(k=0;k<M;k++)
|
|
|
|
if(tabella[k][i]==giocatore&&tabella[k+1][i]==giocatore&&tabella[k+2][i]==giocatore)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int minimax(int giocatore, int profondita){
|
2012-11-19 16:54:12 +01:00
|
|
|
if(controlla(2))
|
|
|
|
return INT_MAX;
|
|
|
|
if(mossa==M*M)
|
|
|
|
return 0;
|
|
|
|
int res,tmp;
|
|
|
|
if(giocatore==1){
|
|
|
|
res=1;
|
|
|
|
for(i=0;i<M;i++){
|
|
|
|
for(k=0;k<M;k++){
|
|
|
|
if(!tabella[i][k]){
|
|
|
|
tabella[i][k]=1;
|
|
|
|
if(controlla(1)&&!profondita==20){
|
|
|
|
tabella[i][k]=0;
|
|
|
|
return INT_MIN;
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
else if((tmp=minimax(2,profondita-1))<res)
|
2012-11-17 00:55:58 +01:00
|
|
|
res=tmp;
|
2012-11-19 16:54:12 +01:00
|
|
|
else
|
|
|
|
res-=2;
|
2012-11-17 00:55:58 +01:00
|
|
|
tabella[i][k]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
res=-1;
|
|
|
|
for(i=0;i<M;i++){
|
|
|
|
for(k=0;k<M;k++){
|
|
|
|
if(!tabella[i][k]){
|
|
|
|
tabella[i][k]=2;
|
|
|
|
if(controlla(2))
|
|
|
|
res+=2;
|
|
|
|
else if((tmp=minimax(1,profondita-1))>res)
|
2012-11-17 00:55:58 +01:00
|
|
|
res=tmp;
|
2012-11-19 16:54:12 +01:00
|
|
|
tabella[i][k]=0;
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
return res;
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void facile() {
|
2012-11-19 16:54:12 +01:00
|
|
|
int a,b;
|
|
|
|
do{
|
|
|
|
randomize;
|
|
|
|
a=random(M);
|
|
|
|
b=random(M);
|
|
|
|
}while(tabella[a][b]==1||tabella[a][b]==2);
|
|
|
|
tabella[a][b]=2;
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void medio(){
|
2012-11-19 16:54:12 +01:00
|
|
|
int max=INT_MIN,mi=1,mk=1,t;
|
|
|
|
for(i=0;i<M;i++){
|
|
|
|
for(k=0;k<M;k++){
|
|
|
|
if(!tabella[i][k]){
|
|
|
|
tabella[i][k]=2;
|
|
|
|
t=minimax(1, 10);
|
|
|
|
if(t>max){
|
|
|
|
max=t;
|
|
|
|
mi=i;
|
|
|
|
mk=k;
|
|
|
|
}
|
|
|
|
tabella[i][k]=0;
|
|
|
|
}
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stampa();
|
2012-11-19 16:54:12 +01:00
|
|
|
tabella[mi][mk]=2;
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void difficile(){
|
|
|
|
int max=INT_MIN,mi=1,mk=1,t;
|
2012-11-19 16:54:12 +01:00
|
|
|
for(i=0;i<M;i++){
|
|
|
|
for(k=0;k<M;k++){
|
|
|
|
if(!tabella[i][k]){
|
|
|
|
tabella[i][k]=2;
|
|
|
|
t=minimax(1,20);
|
|
|
|
if(t>max){
|
|
|
|
max=t;
|
|
|
|
mi=i;
|
|
|
|
mk=k;
|
|
|
|
}
|
|
|
|
tabella[i][k]=0;
|
|
|
|
}
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
tabella[mi][mk]=2;
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void pausa() {
|
|
|
|
do{
|
2012-11-19 16:54:12 +01:00
|
|
|
getchar();
|
|
|
|
}while ((c= getchar()) != '\n' && c!= EOF);
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void vuota() {
|
2012-11-19 16:54:12 +01:00
|
|
|
for(i=0;i<M;i++) {
|
|
|
|
for(k=0;k<M;k++)
|
|
|
|
tabella[i][k]=0;
|
|
|
|
}
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void leggimossa(){
|
2012-11-19 16:54:12 +01:00
|
|
|
int tasto=0;
|
|
|
|
fflush(stdin);
|
|
|
|
if(tastierino==1){
|
|
|
|
do{
|
|
|
|
tasto=0;
|
|
|
|
fflush(stdin);
|
|
|
|
if(scelta==2)
|
|
|
|
printf(GIALLO" Fai la tua mossa:\n"RESET BIANCO);
|
|
|
|
else
|
|
|
|
printf(GIALLO" Gioca il giocatore %d.\n Fai la tua mossa:\n"RESET BIANCO,giocatore);
|
|
|
|
if(os)
|
|
|
|
tasto=mgetchar();
|
|
|
|
else
|
|
|
|
do{
|
|
|
|
tasto=getch();
|
|
|
|
}while(kbhit());
|
|
|
|
switch(tasto){
|
|
|
|
case 49:{
|
|
|
|
i=2;
|
|
|
|
k=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 50:{
|
|
|
|
i=2;
|
|
|
|
k=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 51:{
|
|
|
|
i=2;
|
|
|
|
k=2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 52:{
|
|
|
|
i=1;
|
|
|
|
k=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 53:{
|
|
|
|
i=1;
|
|
|
|
k=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 54:{
|
|
|
|
i=1;
|
|
|
|
k=2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 55:{
|
|
|
|
i=0;
|
|
|
|
k=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 56:{
|
|
|
|
i=0;
|
|
|
|
k=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 57:{
|
|
|
|
i=0;
|
|
|
|
k=2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if(tabella[i][k]==1||tabella[i][k]==2||i<0||k<0||i>=M||k>=M){
|
2012-11-17 00:55:58 +01:00
|
|
|
system(clear);
|
2012-11-19 16:54:12 +01:00
|
|
|
stampa();
|
2012-11-17 00:55:58 +01:00
|
|
|
printf(GRASSETTO ROSSO"\n Mossa non valida,riprova.\n\n"RESET BIANCO);
|
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
}while(tabella[i][k]==1||tabella[i][k]==2||i<0||k<0||i>=M||k>=M);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
do{
|
|
|
|
if(scelta==2)
|
|
|
|
printf(GIALLO" Tocca a te.\n Inserisci coordinate della mossa"ROSSO"[Riga,colonna]:\n" RESET BIANCO);
|
|
|
|
else
|
|
|
|
printf(GIALLO" Gioca il giocatore %d.\n Inserisci coordinate della mossa"ROSSO"[Riga,colonna]:\n" RESET BIANCO,giocatore);
|
|
|
|
scanf("%d,%d",&i,&k);
|
|
|
|
if(tabella[i][k]==1||tabella[i][k]==2||i<0||k<0||i>=M||k>=M){
|
2012-11-17 00:55:58 +01:00
|
|
|
system(clear);
|
|
|
|
stampa();
|
2012-11-19 16:54:12 +01:00
|
|
|
printf(GRASSETTO ROSSO"\n\n Coordinate non valide,riprova.\n\n\n"RESET BIANCO);
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
}while(tabella[i][k]==1||tabella[i][k]==2||i<0||k<0||i>=M||k>=M);
|
|
|
|
system(clear);
|
|
|
|
}
|
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void aspetta(int t){
|
2012-11-19 16:54:12 +01:00
|
|
|
time_t Ti,Tf;
|
|
|
|
time(&Ti);
|
|
|
|
do{
|
|
|
|
time(&Tf);
|
|
|
|
}while(difftime(Tf,Ti)<t);
|
2012-11-17 00:55:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spinner(int tempo) {
|
2012-11-19 16:54:12 +01:00
|
|
|
char spinner[] = "/-\\|";
|
2012-11-17 00:55:58 +01:00
|
|
|
printf(GIALLO"\n ");
|
2012-11-19 16:54:12 +01:00
|
|
|
for (int i=0; i<(tempo*10);i++){
|
|
|
|
putchar(spinner[i%4]);
|
|
|
|
fflush(stdout);
|
|
|
|
usleep(200000);
|
|
|
|
putchar('\b');
|
|
|
|
}
|
2012-11-17 00:55:58 +01:00
|
|
|
printf(RESET);
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
void beep(int t){
|
|
|
|
for(i=0;i<t*100;i++)
|
|
|
|
putchar('\a');
|
|
|
|
}
|
2012-11-19 16:54:12 +01:00
|
|
|
|
2012-11-17 00:55:58 +01:00
|
|
|
#endif
|