Buon pomeriggio a tutti, scrivo questo posto dopo il precedente sempre riguardante questo progetto.
Il programma mi funziona nel senso che il gioco é corretto e non da problemi.
Per quanto riguarda l'outputdella tabella, piú un numero é grande e piú si “deforma”.
Vi inserisco un'immagine di quello che intendo:
Qui sotto vi riporto il codice con la tebella "corretta":
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define FOR_WINDOWS 1
#if FOR_WINDOWS == 1
#define scanf scanf_s
#define gets gets_s
#endif
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int numbers[10] = {2, 4, 8, 16, 32, 64, 128, 256};
int directions[4] = {
72, //UP
75, //LEFT
77, //RIGHT
80 //DOWN
};
int Score = 0;
void clearScreen()
{
if (FOR_WINDOWS)
system("cls");
else
printf("\e[1;1H\e[2J");
}
int Random(int min, int max)
{
return rand() % (max + 1 - min) + min;
}
int isTableFree(int Table[4][4])
{
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (!Table[i][j])
return 1;
return 0;
}
int canMove(int Table[4][4])
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (Table[i][j] == Table[i][j + 1] || Table[i][j] == Table[i + 1][j])
return 1;
return 0;
}
void randomCoord(int coord[2])
{
int i;
for (i = 0; i < 2; i++)
coord[i] = Random(0, 3);
}
void newNum(int Table[4][4])
{
int coord[2], done = 0;
do
{
randomCoord(coord);
if (Table[coord[0]][coord[1]] == 0)
{
Table[coord[0]][coord[1]] = Random(1, 2) * 2;
done++;
}
} while (!done && isTableFree(Table));
}
void moveNumbers(int Table[4][4], int dir)
{
int i, j, k;
switch (dir)
{
case 72:
//UP
for (k = 0; k < 3; k++)
for (i = 3; i > 0; i--)
for (j = 0; j < 4; j++)
if (Table[i - 1][j] == 0)
{
Table[i - 1][j] = Table[i][j];
Table[i][j] = 0;
}
break;
case 75:
//LEFT
for (k = 0; k < 3; k++)
for (i = 0; i < 4; i++)
for (j = 3; j > 0; j--)
if (Table[i][j - 1] == 0)
{
Table[i][j - 1] = Table[i][j];
Table[i][j] = 0;
}
break;
case 77:
//RIGHT
for (k = 0; k < 3; k++)
for (i = 0; i < 4; i++)
for (j = 0; j < 3; j++)
if (Table[i][j + 1] == 0)
{
Table[i][j + 1] = Table[i][j];
Table[i][j] = 0;
}
break;
case 80:
//DOWN
for (k = 0; k < 3; k++)
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
if (Table[i + 1][j] == 0)
{
Table[i + 1][j] = Table[i][j];
Table[i][j] = 0;
}
break;
}
}
int elaborateTable(int Table[4][4], int dir)
{
int i, j, result[4][4];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
result[i][j] = Table[i][j];
moveNumbers(Table, dir);
switch (dir)
{
case 72:
//UP
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
if (Table[i][j] == Table[i + 1][j])
{
Table[i + 1][j] = 0;
Table[i][j] *= 2;
Score += Table[i][j];
}
break;
case 75:
//LEFT
for (i = 0; i < 4; i++)
for (j = 0; j < 3; j++)
if (Table[i][j] == Table[i][j + 1])
{
Table[i][j + 1] = 0;
Table[i][j] *= 2;
Score += Table[i][j];
}
break;
case 77:
//RIGHT
for (i = 0; i < 4; i++)
for (j = 3; j > 0; j--)
if (Table[i][j] == Table[i][j - 1])
{
Table[i][j - 1] = 0;
Table[i][j] *= 2;
Score += Table[i][j];
}
break;
case 80:
//DOWN
for (i = 3; i > 0; i--)
for (j = 0; j < 4; j++)
if (Table[i][j] == Table[i - 1][j])
{
Table[i - 1][j] = 0;
Table[i][j] *= 2;
Score += Table[i][j];
}
break;
}
moveNumbers(Table, dir);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (result[i][j] != Table[i][j])
return 1;
return 0;
}
void initTable(int Table[4][4])
{
int i, j, coord[2], initNumbers[2];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
Table[i][j] = 0;
initNumbers[0] = Random(1, 2) * 2;
if (initNumbers[0] == 4)
initNumbers[1] = 2;
else
initNumbers[1] = Random(1, 2) * 2;
for (i = 0; i < 2; i++)
{
randomCoord(coord);
if (Table[coord[0]][coord[1]] == 0)
Table[coord[0]][coord[1]] = initNumbers[i];
else
i--;
}
}
void printTable(int Table[4][4])
{
int i, j;
for (i = 0; i < 4; i++)
{
printf("|");
for (j = 0; j < 4; j++)
{
if (Table[i][j] != 0)
{
printf("%4d", Table[i][j]);
system("COLOR 00");
}
else
printf(" ");
printf("|");
}
printf("\n\n");
}
printf("Actual score: %d\n", Score);
}
int nextMove(int Table[4][4], int dir)
{
if (elaborateTable(Table, dir))
newNum(Table);
else if (!isTableFree(Table) && !canMove(Table))
return 1;
printTable(Table);
return 0;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void color(int x){
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, x);
}
void stampa(int frase){
if(frase == 1){
color(15);printf("\t\t .----------------. .----------------. .----------------. .----------------. \n");
printf("\t\t | .--------------. || .--------------. || .--------------. || .--------------. | \n");
printf("\t\t | | _____ | || | ____ | || | _ _ | || | ____ | | \n");
printf("\t\t | | / ___ `. | || | .' '. | || | | | | | | || | .' __ '. | | \n");
color(7);printf("\t\t | | |_/___) | | || | | .--. | | || | | |__| |_ | || | | (__) | | | \n");
printf("\t\t | | .'____.' | || | | | | | | || | |____ _| | || | .`____'. | | \n");
printf("\t\t | | / /____ | || | | `--' | | || | _| |_ | || | | (____) | | | \n");
printf("\t\t | | |_______| | || | '.____.' | || | |_____| | || | `.______.' | | \n");
color(8);printf("\t\t | | | || | | || | | || | | | \n");
printf("\t\t | '--------------' || '--------------' || '--------------' || '--------------' | \n");
printf("\t\t '----------------' '----------------' '----------------' '----------------' \n");
}else if(frase == 2){
color(7);
printf("\n\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 218, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 191);
printf("\t\t\t\t\t %c CI SI MUOVE CON LE FRECCIE %c\n", 179, 179);
printf("\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 192, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 217);
}else if(frase == 3){
printf("\n\t\t\t\t\t\t%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 218, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 191);
printf("\t\t\t\t\t\t%c PER RINIZIARE - 'R' %c\n", 179, 179);
printf("\t\t\t\t\t\t%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 192, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 217);
}else if(frase == 4){
color(15);
printf("\n\t\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 218, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 191);
printf("\t\t\t\t\t\t %c PER USCIRE - 'U' %c\n", 179, 179);
printf("\t\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 192, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 217);
}else if(frase == 5){
printf("\n\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%C\n", 218, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 191);
printf("\t\t\t\t\t %c PREMERE UN TASTO PER INIZIARE %c\n", 179, 179);
printf("\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%C\n", 192, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 217);
}else{
printf("\n\t\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 218, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 191);
printf("\t\t\t\t\t\t %c CARICAMENTO... %c\n", 179, 179);
printf("\t\t\t\t\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 192, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 217);
}
}
int main(){
int i, j, Table[4][4], dir, GameOver = 0, New = 0, cont;
char s = -33;
for(i = 0; i < 5; i++){
stampa(i + 1);
}
printf("\t\t\t\t\t ");
getch();
system("cls");
stampa(1);
color(7);
stampa(999);
printf("\t\t\t\t\t ");
color(2);
for(i=0,j;i<36;i++)
{
printf("%c",s);
j=i;
if(i % 2 != 0 && i < 20){
sleep(1);
}
}
system("cls");
do{
New = 0;
Score = 0;
srand((unsigned int)time(0));
clearScreen();
initTable(Table);
printTable(Table);
do
{
do
{
dir = _getch();
if (dir == 'n')
New = 1;
} while (dir != directions[0] && dir != directions[1] && dir != directions[2] && dir != directions[3] && !New);
if (!New)
{
clearScreen();
GameOver = nextMove(Table, dir);
}
} while (!GameOver && !New);
if (!New)
{
printf("GAME OVER!\n\nYour score is: %d\n", Score);
system("PAUSE");
printf("Continue? (Y/N)\n");
cont = _getch();
}
}while (cont == 'y' || New);
if (FOR_WINDOWS)
system("PAUSE");
return 0;
}
Scusate se non ci sono i commenti ma appunto voglio sistemare questa cosa prima di metterli.
Avete qualche idea ?
Spero possiate aiutarmi.
Grazie.