#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> int step=0; void map ()//Game menu function. {printf ("▇▇▇▇▇▇▇▇▇▇▇▇▇▇\n"); printf ("█ jigsaw puzzle game █\n"); printf ("█1. 3x3█\n "); printf ("█2. 4x4█\n "); printf ("█3. 5x5█\n "); printf ("█4. Exit █\n "); printf ("▇▇▇▇▇▇▇▇▇▇▇▇▇▇\n"); } void Fun (int *a,int t,int m,int r) {int i; int g; for (i=0;i<m;i++) {if (i%r== (r-1) && (i+t)%r==0)//Let the array not be converted across rows. Continue if (i%r==0&& (i+t)%r== (r-1)) continue; if (a[i+t]==0 && (i+t) >=0 && (i+t) <m) {g=a[i]; A[I]=A[I+T]; a[i+t]=g; step++; return; }}} void Move (int *a,int m,int R) {///up or down move function. Char ch; int i,j; WhilE (Ch=getch ()) {System ("CLS"); if (ch== ' W ') {i=-r; Fun (A,I,M,R); } else if (ch== ' s ') {i=r; Fun (A,I,M,R); } else if (ch== ' a ') {i=-1; Fun (A,I,M,R); } else if (ch== ' d ') {i=1; Fun (A,I,M,R); } else if (ch== ' 4 ') {printf ("You have quit the game, please play again!") \ n "); Exit (0); } for (i=0;i<m;i++) {if (a[i]==0) printf (""); else printf ("%2d", A[i]); if ((i+1)%r==0) Putchar (10); } printf ("Total walk:%d steps!\n", step); }} void map_x (int m,int r) {int i=1,j,a[25],n; Srand (Time (NULL)); printf ("Jigsaw game begins: \ n"); while (i<m) {N=1+rand ()% (m-1); for (j=1;j<i;j++) { if (N==a[j]) break; } if (j==i) {a[i]=n; i++; }} a[0]=0; for (i=0;i<m;i++) {if (a[i]==0) printf (""); else printf ("%2d", A[i]); if ((i+1)%r==0) Putchar (10); } printf ("Total walk:%d steps!\n", step); Move (A,M,R); } int main () {int i,n; Char ch; Map ();//Display menu. printf ("Please select the level of the game you are playing: \ n"); Ch=getch (); while (ch!= ' 1 ' && ch!= ' 2 ' && ch!= ' 3 ' &&ch!= ' 4 ') {printf ("input wrong, please re-enter: \ n"); Ch=getch (); } switch (CH) {case ' 1 ': map_x (9,3); Break Case ' 2 ': map_x (16,4); Break Case ' 3 ': map_x (25,5); Break Case ' 4 ': printf ("You have quit the game, please play again!") \ n "); Exit (0); Break } return 0; }
Pure C language Writing puzzle game