UP | HOME

The loneliness Game

Hello !!, I hope you are doing great you amazing person whoever you are, and I really appreciate you reading my little C programming adventure. Soo basically I wanted to blog about a little game I made when bored, and figured out it would be a great way to optimize it, and learn new stuff too by documenting the process!

The concept :

Basically the player is faced with a NxM field made up with the sign "-" and the player is denoted by the symbol "+", there are also Bonuses "B" which add 1 to your score, Traps "T", that remove one from your score, and Dead "D" which resets the score to 0. I will go into more of the specifics later but for now this is how it works, and the controls are Basic WASD bindings, though i may go for a HJKL style later.

The code :

  1: #include <stdio.h>
  2: #include <stdlib.h>
  3: int main(int argc, char *argv[]) {
  4:     char input,map[5][5] = {
  5: 	{'-', '-', '-', '-', '-'},
  6: 	{'-', '-', '-', '-', '-'},
  7: 	{'-', '-', '-', '-', '-'},
  8: 	{'-', '-', '-', '-', '-'},
  9: 	{'-', '-', '-', '-', '-'}
 10:     };
 11:     int stop=0,i=0,moves=0,score=0,pos[2] = {2, 2};
 12:     int bonus[2];
 13:     int trap[2] ;
 14:     int death[2];
 15:     map[pos[0]][pos[1]] = '+';
 16:     do{
 17:     bonus[0] = arc4random_uniform(5); bonus[1] = arc4random_uniform(5);
 18:     trap[0] = arc4random_uniform(5); trap[1] = arc4random_uniform(5);
 19:     death[0] = arc4random_uniform(5); death[1] = arc4random_uniform(5);
 20:     }while((bonus[0] == trap[0] && bonus[1] == trap[1]) || (bonus[0] == death[0] && bonus[1] == death[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (bonus[0] == pos[0] && bonus[1] == pos[1]) || (trap[0] == pos[0] && trap[1] == pos[1]) || (death[0] == pos[0] && death[1] == pos[1]));
 21:     map[bonus[0]][bonus[1]] = 'B';
 22:     map[trap[0]][trap[1]] = 'T';
 23:     map[death[0]][death[1]] = 'D';
 24:     do{
 25:     printf("Map:\n");
 26:     for (int i = 0; i < 5; i++) {
 27: 	for (int j = 0; j < 5; j++) {
 28: 	    printf("%c ", map[i][j]);
 29: 	}
 30: 	printf("\n");
 31:     }
 32:     printf("Score: %d\n", score);
 33:     printf("Moves: %d\n", moves);
 34:     printf("Enter a direction (w,a,s,d) or c to quit: ");
 35:     scanf(" %c", &input);
 36: // pos[0] updown pos[1] lr
 37:     if (input == 'w') {
 38: 	printf("Moving up\n");
 39: 	map[pos[0]][pos[1]] = '-';
 40: 	if (pos[0] == 0) {
 41: 	    pos[0] = 4;
 42: 	}
 43: 	else {
 44: 	    pos[0]--;
 45: 	}
 46:     } else if (input == 'a') {
 47: 	printf("Moving left\n");
 48: 	map[pos[0]][pos[1]] = '-';
 49: 	if (pos[1] == 0) {
 50: 	    pos[1] = 4;
 51: 	}
 52: 	else {
 53: 	    pos[1]--;
 54: 	}
 55:     } else if (input == 's') {
 56: 
 57: 	printf("Moving down\n");
 58: 	map[pos[0]][pos[1]] = '-';
 59: 	if (pos[0] == 4) {
 60: 	    pos[0] = 0;
 61: 	}
 62: 	else {
 63: 	    pos[0]++;
 64: 	}
 65:     } else if (input == 'd') {
 66: 	printf("Moving right\n");
 67: 	map[pos[0]][pos[1]] = '-';
 68: 	if (pos[1] == 4) {
 69: 	    pos[1] = 0;
 70: 	}
 71: 	else {
 72: 	    pos[1]++;
 73: 	}
 74:     } else if (input == 'c') {
 75: 	printf("Quitting\n");
 76:     } else {
 77: 	printf("Invalid input\n");
 78:     }
 79:     map[pos[0]][pos[1]] = '+';
 80:     if (pos[0] == bonus[0] && pos[1] == bonus[1]) {
 81: 	score++;
 82: 	do{
 83: 	bonus[0]= arc4random_uniform(5);
 84: 	bonus[1]= arc4random_uniform(5);
 85: 	}while((bonus[0] == trap[0] && bonus[1] == trap[1]) || (bonus[0] == death[0] && bonus[1] == death[1]) || (bonus[0] == pos[0] && bonus[1] == pos[1]));
 86:     }
 87:     if (pos[0] == trap[0] && pos[1] == trap[1]) {
 88: 	score--;
 89: 	do{
 90: 	trap[0]= arc4random_uniform(5);
 91: 	trap[1]= arc4random_uniform(5);
 92: 	}while((trap[0] == bonus[0] && trap[1] == bonus[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (trap[0] == pos[0] && trap[1] == pos[1]));
 93: 	}
 94:     if (pos[0] == death[0] && pos[1] == death[1]) {
 95: 	score = 0;
 96: 	do{
 97: 	death[0]= arc4random_uniform(5);
 98: 	death[1]= arc4random_uniform(5);
 99: 	}while((death[0] == bonus[0] && death[1] == bonus[1]) || (death[0] == trap[0] && death[1] == trap[1]) || (death[0] == pos[0] && death[1] == pos[1]));
100:     }
101:     if (score % 3 == 0 && score != 0 && stop == 0) {
102: 	map[death[0]][death[1]] = '-';
103: 	do{
104: 	death[0]= arc4random_uniform(5);
105: 	death[1]= arc4random_uniform(5);
106: 	}while((death[0] == bonus[0] && death[1] == bonus[1]) || (death[0] == trap[0] && death[1] == trap[1]) || (death[0] == pos[0] && death[1] == pos[1]));
107: 	stop = 1;
108:     }
109:     else if (score % 3 != 0) {
110:     stop = 0;
111:     }
112:     if (moves % 5 == 0 && moves != 0) {
113: 	do{
114: 	    map[trap[0]][trap[1]] = '-';
115: 	trap[0]= arc4random_uniform(5);
116: 	trap[1]= arc4random_uniform(5);
117: 	}while((trap[0] == bonus[0] && trap[1] == bonus[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (trap[0] == pos[0] && trap[1] == pos[1]));
118: 
119:     }
120:     map[bonus[0]][bonus[1]] = 'B';
121:     map[trap[0]][trap[1]] = 'T';
122:     map[death[0]][death[1]] = 'D';
123:     moves++;
124:     }while(input != 'c');
125:     return 0;
126: }
127: 

Let's go step by step and see what we can fix or improve, to start off, line 4 to 10 can be reduced to 7 or 8 lines (which will be beneficial later too)

 4: int n=5,m=5;
 5: char input,map[50][50];
 6:     for (int i = 0; i < n; i++) {
 7: 	for (int j = 0; j < m; j++) {
 8: 	    map[i][j] = '-';
 9: 	}
10:     }
11: 

For now at least, n and m are hardcoded to 5, but this will change later. And I picked 50x50 as a max size because why not

Of course we have the usual inits on line 11, though since we are using variables instead of hardcoding 5, we will have to find the center by ourselves

11: int stop=0,i=0,moves=0,score=0,pos[2] = {n/2,m/2};

This is getting better, of course we then initialize the coordinates of bonus, trap, and death, and set the player as a '+' in the field.

Here comes the line 17-21, where it generates a random coordinate for the aforementioned pickups, and do that until there is no conflict between eachother and the player) here we will need to change it a tiny bit.

17: bonus[0] = arc4random_uniform(n); bonus[1] = arc4random_uniform(m);
18: trap[0] = arc4random_uniform(n); trap[1] = arc4random_uniform(m);
19: death[0] = arc4random_uniform(n); death[1] = arc4random_uniform(m);

Looking good so far!!, We then have line 21-23 which also shows the pickups as their respective symbols in the map.

The main interactive program starts here, which will learn at least one time and stop if the received input is a 'c', it starts with a nested for loop on line 26 up to 31 to show the content of the map, nothing fancy, just some matrix stuff. we need to change the 5 though!

26: for (int i = 0; i < n; i++) {
27:       for (int j = 0; j < m; j++) {
28: 	  printf("%c ", map[i][j]);
29:       }
30:       printf("\n");
31:   }
32: 

We show the score and the moves too, which at the start of the game are set to 0. and we prompt the user for a direction. Note here the space before the %c, this basically allows for the program to not choke on newlines and also even if the user writes multiple keys at the same time, they will still be done, like ww will make the player move twice up.

After that we have some logic which should also be changed to account for the n and m changes yet again

37: if (input == 'w') {
38:     printf("Moving up\n");
39:     map[pos[0]][pos[1]] = '-';
40:     if (pos[0] == 0) {
41: 	pos[0] = n-1;
42:     }
43:     else {
44: 	pos[0]--;
45:     }
46: } else if (input == 'a') {
47:     printf("Moving left\n");
48:     map[pos[0]][pos[1]] = '-';
49:     if (pos[1] == 0) {
50: 	pos[1] = m-1;
51:     }
52:     else {
53: 	pos[1]--;
54:     }
55: } else if (input == 's') {
56: 
57:     printf("Moving down\n");
58:     map[pos[0]][pos[1]] = '-';
59:     if (pos[0] == n-1) {
60: 	pos[0] = 0;
61:     }
62:     else {
63: 	pos[0]++;
64:     }
65: } else if (input == 'd') {
66:     printf("Moving right\n");
67:     map[pos[0]][pos[1]] = '-';
68:     if (pos[1] == m-1) {
69: 	pos[1] = 0;
70:     }
71:     else {
72: 	pos[1]++;
73:     }
74: } else if (input == 'c') {
75:     printf("Quitting\n");
76: } else {
77:     printf("Invalid input\n");
78: }

What this achieves is the "teleportation effect" whenever you are at the border of the screen!

Now we fix things from line 80 to the end of the program, aka replacing ever occurrence of 5 with n or m

 80: if (pos[0] == bonus[0] && pos[1] == bonus[1]) {
 81:     score++;
 82:     do{
 83:     bonus[0]= arc4random_uniform(n);
 84:     bonus[1]= arc4random_uniform(m);
 85:     }while((bonus[0] == trap[0] && bonus[1] == trap[1]) || (bonus[0] == death[0] && bonus[1] == death[1]) || (bonus[0] == pos[0] && bonus[1] == pos[1]));
 86: }
 87: if (pos[0] == trap[0] && pos[1] == trap[1]) {
 88:     score--;
 89:     do{
 90:     trap[0]= arc4random_uniform(n);
 91:     trap[1]= arc4random_uniform(m);
 92:     }while((trap[0] == bonus[0] && trap[1] == bonus[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (trap[0] == pos[0] && trap[1] == pos[1]));
 93:     }
 94: if (pos[0] == death[0] && pos[1] == death[1]) {
 95:     score = 0;
 96:     do{
 97:     death[0]= arc4random_uniform(n);
 98:     death[1]= arc4random_uniform(m);
 99:     }while((death[0] == bonus[0] && death[1] == bonus[1]) || (death[0] == trap[0] && death[1] == trap[1]) || (death[0] == pos[0] && death[1] == pos[1]));
100: }
101: if (score % 3 == 0 && score != 0 && stop == 0) {
102:     map[death[0]][death[1]] = '-';
103:     do{
104:     death[0]= arc4random_uniform(n);
105:     death[1]= arc4random_uniform(m);
106:     }while((death[0] == bonus[0] && death[1] == bonus[1]) || (death[0] == trap[0] && death[1] == trap[1]) || (death[0] == pos[0] && death[1] == pos[1]));
107:     stop = 1;
108: }
109: else if (score % 3 != 0) {
110: stop = 0;
111: }
112: if (moves % 5 == 0 && moves != 0) {
113:     do{
114: 	map[trap[0]][trap[1]] = '-';
115:     trap[0]= arc4random_uniform(n);
116:     trap[1]= arc4random_uniform(m);
117:     }while((trap[0] == bonus[0] && trap[1] == bonus[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (trap[0] == pos[0] && trap[1] == pos[1]));
118: 
119: }

Aaaaand this should be it

Author: Crystal

Created: 2024-05-03 Fri 20:47

Validate