qualche giorno fa avevo iniziato un programmino del "tris" in c++ per console. Funzionava normalmente, l'unico problema era che il turno della CPU veniva gestito da dei numeri randominci (int i= rand()...), il che rendeva il giochino troppo "facile" per così dire, così ho provato a rendere il turno della Cpu un po' più elaborato, ma sono in un punto di stallo dal quale non riesco ad uscire, a volte funziona, poi si blocca e non gioca il turno.
grazie in anticipo
- Codice: Seleziona tutto
/*tris*/
#include <iostream>
#include "tris.h"
using namespace std;
int main(){
char c,r;
schermata();
if((c=getch())) clearscr();
do{
cout << "benvenuto" << endl;
cout << 7 << " | " << 8 <<" | " << 9 << endl;
cout << "----------" << endl;
cout << 4 << " | " << 5 << " | " << 6 << endl;
cout << "----------" << endl;
cout << 1 << " | " << 2 <<" | " << 3 << endl;
resmat();
while (!vittoria()){
gioca();
clearscr();
stampamat();
if (vittoria()) break;
randturn();
clearscr();
stampamat();}
cout << "partita terminata!!" << endl;
cout << "giocare ancora?[s/n]"; cin >> r;}
while(r!='n');
}
questa è l'implementazione delle varie funzioni:
- Codice: Seleziona tutto
/*cpp*/
#include <iostream>
#include <stdio.h>
#include <termios.h>
#include "tris.h"
using namespace std;
char mat[3][3];
void clearscr(){
const char* command = "clear";
system(command);}
void sleep(int milliseconds)
{
clock_t time_end;
time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
while (clock() < time_end)
{
}
}
void schermata(){
cout << "############################################# " << endl;
cout << "# # " << endl;
cout << "# ######### ##### # ###### # " << endl;
cout << "# # # # # # # " << endl;
cout << "# # # # # # # " << endl;
cout << "# # # # # # # " << endl;
cout << "# # ##### # ## # " << endl;
cout << "# # # # # # # " << endl;
cout << "# # # # # # # " << endl;
cout << "# # # # # # # " << endl;
cout << "# # # # # ###### # " << endl;
cout << "# # " << endl;
cout << "############################################# " << endl;
}
static struct termios old, new1;
void initTermios(int echo)
{
tcgetattr(0, &old);
new1 = old;
new1.c_lflag &= ~ICANON;
new1.c_lflag &= echo ? ECHO : ~ECHO;
tcsetattr(0, TCSANOW, &new1);
}
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
char getch(void)
{
return getch_(0);
}
void resmat(){
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
mat[i][j]=' ';}
}
}
bool vittoria(){
bool flag = true;
if ((mat[0][0]=='X'||mat[0][0]=='O') && mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2]) {flag = false; return true;}
if ((mat[0][2]=='X'||mat[0][2]=='O') && mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0]) {flag = false; return true;}
if(flag){
for (int i=0;i<3;i++){
if ((mat[i][0]=='X'||mat[i][0]=='O') && mat[i][0] == mat[i][1] && mat[i][1] == mat[i][2]) {i=4; return true;}
if ((mat[0][i]=='X'||mat[0][i]=='O') && mat[0][i] == mat[1][i] && mat[1][i] == mat[2][i]) {i=4; return true;}}}
return false;}
void stampamat(){
cout << '\n' << '\n' << '\n';
cout << " ************* " << endl;
cout << " * " << mat[0][0] << " | " << mat[0][1] <<" | " << mat[0][2] << " *" << endl;
cout << " * " << "----------" << "*" <<endl;
cout << " * " << mat[1][0] << " | " << mat[1][1] << " | " << mat[1][2] << " *"<< endl;
cout << " * " << "----------" << "*" <<endl;
cout << " * " << mat[2][0] << " | " << mat[2][1] <<" | " << mat[2][2] <<" *" << endl;
cout << " ************* " << endl;}
void gioca(){
int d=getch();
for (int i=0;i<3;i++){
if (d==(7+i)||d==(55+i)) {mat[0][i]='X';i=4; }}
for (int j=0;j<3;j++){
if (d==(4+j)|| d==(52+j)) {mat[1][j]='X';j=4; }}
for (int k=0;k<3;k++){
if (d==(1+k)|| d==(49+k)) {mat[2][k]='X';k=4; }}
}
bool AIrand(){ //questa funzione dovrebbe gestire il turno della CPu in modo da farle mettere la 'O' nel punto più conveniente.
if((mat[0][0]=='X'|| mat[0][0]=='O') && mat[0][0]==mat[1][1]){if(mat[2][2]!=' '){mat[2][2]='O'; return true;}else return false;}
if((mat[0][2]=='X'|| mat[0][2]=='O') && mat[0][2]==mat[1][1]){if(mat[2][0]!=' '){mat[2][0]='O'; return true;}else return false;}
if((mat[2][2]=='X'|| mat[2][2]=='O') && mat[2][2]==mat[1][1]){if(mat[0][0]!=' '){mat[0][0]='O'; return true;}else return false;}
if((mat[2][0]=='X'|| mat[2][0]=='O') && mat[2][0]==mat[1][1]){if(mat[0][2]!=' '){mat[0][2]='O'; return true;}else return false;}
for (int i=0;i<3;i++){
if((mat[i][0]=='X'|| mat[i][0]=='O') && mat[i][0]==mat[i][1]){if(mat[i][2]!=' '){mat[i][2]='O'; return true;}else return false;}
if((mat[0][i]=='X'|| mat[0][i]=='O') && mat[0][i]==mat[1][i]){if(mat[2][i]!=' '){mat[2][i]='O'; return true;}else return false;}
if((mat[i][1]=='X'|| mat[i][1]=='O') && mat[i][1]==mat[i][2]){if(mat[i][0]!=' '){mat[i][0]='O'; return true;}else return false;}
if((mat[1][i]=='X'|| mat[1][i]=='O') && mat[1][i]==mat[2][i]){if(mat[0][i]!=' '){mat[0][i]='O'; return true;}else return false;}
}
return false;
}
void randturn(){
int a,b;
if(!AIrand()){
srand(time(NULL));
do{
a = rand() % 3 + 0;
b = rand() % 3 + 0;}
while (mat[a][b] == 'X'|| mat[a][b] == 'O');
mat[a][b]='O';}
}