Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by registered user b1913293 ( 6 years ago )
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#define MAX_M 100
#define MAX_N 100
typedef struct {
int m, n; //kich thuoc bang
int A[MAX_M][MAX_N];
int r, c; // hang va cot cua o trong
} State;
int swap(int *a, int *b){
int c;
c = *a;
*a = *b;
*b = c;
return 0;
}
void print_state(State s){
int i,j,k;
printf(" ");
for(i = 0; i <= s.n; i++){
if(i == 0){
printf("%c%c%c%c",201,205,205,205);
}
else if(i == s.n-1){
printf("%c%c%c%c%c%c\n",205,205,205,205,205,187);
}
else if (i > 0 && i < s.n-1) printf("%c%c%c%c",205,205,205,205);
}
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
if(j == 0){
printf(" %c",186);
}
if(i == s.r && j == s.c){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),255); // To mau o trong trung voi mau nen
printf("... ");
if(j == s.n-1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),241);
printf("%c",186);
}
}
else{
if((i %2 == 0 && j %2 == 0) || (i %2 != 0 && j %2 != 0) ){ // to mau xen ke tung o
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),95);
printf("%3d ",s.A[i][j]);
if(j == s.n-1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),241);
printf("%c",186);
}
}
else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),207);
printf("%3d ",s.A[i][j]);
if(j == s.n-1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),241);
printf("%c",186);
}
}
}
}
printf("\n");
}
printf(" ");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),241);
for(i = 0; i <= s.n; i++){
if(i == 0){
printf("%c%c%c%c",200,205,205,205);
}
else if(i == s.n-1){
printf("%c%c%c%c%c%c\n",205,205,205,205,205,188);
}
else if (i > 0 && i < s.n-1) printf("%c%c%c%c",205,205,205,205);
}
}
int is_finished(State s){
int i,j,flag = 1;
for (i = 0; i < s.m; i++){
for (j = 0; j < s.n; j++){
if (s.A[i][j] != (i*s.n+j+1))
flag = 0;
}
}
return flag;
}
int up(State s,State *n){
int i,j;
if(s.r == 0) return 0;
else if(s.r > 0){
swap(&s.A[s.r][s.c],&s.A[s.r-1][s.c]);
s.r -= 1;
n->m = s.m;
n->n = s.n;
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
n->A[i][j] = s.A[i][j];
}
}
n->r = s.r;
n->c = s.c;
}
return 1;
}
int down(State s, State *n){
int i,j;
if(s.r == s.m-1)
return 0;
else if(s.r < s.m){
swap(&s.A[s.r][s.c],&s.A[s.r+1][s.c]);
s.r ++;
n->m = s.m;
n->n = s.n;
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
n->A[i][j] = s.A[i][j];
}
}
n->r = s.r;
n->c = s.c;
}
return 1;
}
int left(State s, State *n){
int i,j;
if(s.c == 0)
return 0;
else if(s.c > 0){
swap(&s.A[s.r][s.c],&s.A[s.r][s.c-1]);
s.c --;
n->m = s.m;
n->n = s.n;
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
n->A[i][j] = s.A[i][j];
}
}
n->r = s.r;
n->c = s.c;
}
return 1;
}
int right(State s, State *n){
int i,j;
if(s.c == s.n-1)
return 0;
else if(s.c < s.n){
swap(&s.A[s.r][s.c],&s.A[s.r][s.c+1]);
s.c++;
n->m = s.m;
n->n = s.n;
for(i = 0; i < s.m; i++){
for(j = 0; j < s.n; j++){
n->A[i][j] = s.A[i][j];
}
}
n->r = s.r;
n->c = s.c;
}
return 1;
}
void init_state(State *S){
State N;
int i;
srand((int)time(0));
N = *S;
for(i=0; i<100000; i++){
if( rand() %4 +1 == 1 && up(*S,&N));
else if( rand() %4 +1 == 2 && down(*S, &N));
else if( rand() %4 +1 == 3 && left(*S, &N));
else if( rand() %4 +1 == 4 && right(*S, &N));
*S = N;
}
}
void intd(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),250);
printf("\n %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%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c\n",178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178);
printf(" %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c\n",178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),252);
printf(" %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%c%c%c%c\n",178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),241);
printf(" %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c %c%c\n",178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178);
printf(" %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%c%c %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c\n\n",178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178,178);
}
int main(){
State S;
State N;
FILE *f;
int mode,x,y;
PlaySound(TEXT("D:\\sound WAV\\open.wav"), NULL, SND_ASYNC);
intd(); // in tieu de
printf("SELECT MODE\n");
printf("1. SO EASY:\n");
printf("2. EASY:\n");
printf("3. NORMAL:\n");
printf("4. HARD:\n");
scanf("%d", &mode);
if(mode == 1){
x = 2;
y = 3;
}
else if(mode == 2){
x = 3;
y = 3;
}
else if (mode == 3){
x = 4;
y = 4;
}
else if (mode == 4){
x = 8;
y = 8;
}
int i, j;
S.m = x;
S.n = y;
for(i = 0; i < S.m; i++){
for(j = 0; j < S.n; j++){
S.A[i][j] = (i*S.n+j+1);
}
}
S.r = S.m-1;
S.c = S.n-1;
init_state(&S);
system("cls");
intd(); // in tieu de
print_state(S);
while(1){
int flag = 1;
int orr = 0;
if(is_finished(S)){
PlaySound(TEXT("D:\\sound WAV\\victory2.wav"), NULL, SND_ASYNC);
sleep(4);
printf("You win!");
sleep(3);
break;
}
char c = getch();
PlaySound(TEXT("D:\\sound WAV\\beep.wav"), NULL, SND_ASYNC);
if(c == -32){
c = getch();
if(c == 72){
system("cls"); //mui ten len
if(down(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 80){ //mui ten xuong
system("cls");
if(up(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 77){ //mui ten sang phai
system("cls");
if(left(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 75){ // mui ten sang trai
system("cls");
if(right(S,&N)){
S = N;
}
else flag = 0;
}
}
else if(c == 83 || c == 83+32){
system("cls");
if(up(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 87 || c == 87+32){
system("cls");
if(down(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 68 || c == 68+32){
system("cls");
if(left(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 65 || c == 65+32){
system("cls");
if(right(S,&N)){
S = N;
}
else flag = 0;
}
else if(c == 27){
system("cls");
PlaySound(TEXT("D:\\sound WAV\\to_be.wav"), NULL, SND_ASYNC);
sleep(2);
while(1){
if(i %2 == 0)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);
printf("\n\n\n\n");
printf(" lose lose lose loseloselose loseloselose loseloselose lose\n");
printf(" lose lose lose loseloselose loseloselose lose lose\n");
printf(" lose lose lose lose lose lose loselose loseloselose lose\n");
printf(" lose lose lose lose lose lose loseloselose loseloselose lose\n");
printf(" loseloselose loselose loseloselose loselose lose\n");
printf(" loseloselose loselose loseloselose loseloselose loseloselose lose\n");
Sleep(500);
system("cls");
i++;
}
sleep(2);
break;
}
else{
system("cls");
orr = 1 ;
}
intd();
print_state(S);
if(flag == 0)
printf("Illegal move.");
if(orr == 1){
PlaySound(TEXT("D:\\sound WAV\\fail.wav"), NULL, SND_ASYNC);
printf("Unknown command, please enter: w, a, s, d, or esc");
}
}
return 0;
}
Revise this Paste