Ok, to busy in live, and i don’t have time to update my blog 🙁
So, today i got some exam with C Programming and turn out bad
here some code i work on in the exam, a little bit messy code 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <stdio.h> void main() { int xn, yn, i, j, x[4], y[4], done = 0, jalan, gerakan[100], jml = 0, temp; scanf("%d %d", &xn, &yn); int papan[xn][yn]; for(i = 0; i < 4;i++) { scanf("%d %d", &x[i], &y[i]); } scanf("%d", &jalan); i = 0; while(jalan > 0) { temp = (jalan/10)*10; gerakan[i] = jalan - temp; jalan = temp/10; jml++; i++; } for(i = 0;i < yn;i++ ) { for(j = 0; j < xn; j++) { papan[j][i] = 0; } } for(i = jml-1; i >= 0;i--) { if(gerakan[i] == 1) { if((x[0] + 1) < xn) { x[3] = x[2]; x[2] = x[1]; x[1] = x[0]; y[3] = y[2]; y[2] = y[1]; y[1] = y[0]; x[0]++; } } else if(gerakan[i] == 2) { if((y[0] + 1) < yn) { x[3] = x[2]; x[2] = x[1]; x[1] = x[0]; y[3] = y[2]; y[2] = y[1]; y[1] = y[0]; y[0]++; } } else if(gerakan[i] == 3) { if((x[0] - 1) >= 0) { x[3] = x[2]; x[2] = x[1]; x[1] = x[0]; y[3] = y[2]; y[2] = y[1]; y[1] = y[0]; x[0]--; } } else if(gerakan[i] == 4) { if((y[0] - 1) >= 0) { x[3] = x[2]; x[2] = x[1]; x[1] = x[0]; y[3] = y[2]; y[2] = y[1]; y[1] = y[0]; y[0]--; } } } printf("\n"); for(i = 0; i < 4; i++) { papan[x[i]][y[i]] = 1; } for(i = 0;i < yn;i++ ) { for(j = 0; j < xn; j++) { printf("%d ", papan[j][i]); } printf("\n"); } } |
So first input the size of the matrix x X y
and then put the snake coordinate x y
put the snake body coordinate
and the last one is for the tail
and then put some integer value from 1 to 4
the rule is 1 to go right, 2 to go down, 3 to go left, and 4 to go up
I hope this helpful to who ever read this 😀