Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <conio.h>
- #define N 5
- typedef struct robotStruct {
- int x, y;
- int score;
- int energy;
- } r;
- typedef struct mapStruct {
- int map[N][N];
- int tx, ty;
- } m;
- void fillmap(m* map) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- int tmp = rand()%10;
- if (tmp < 5) map->map[i][j] = 0;
- else if (tmp < 8) map->map[i][j] = 1;
- else map->map[i][j] = 2;
- }
- }
- }
- void init(r* rbt, m* map) {
- srand((unsigned)time(NULL));
- rbt->x = rand()%N;
- rbt->y = rand()%N;
- rbt->score = 0;
- rbt->energy = 20;
- do {
- map->tx = rand()%N;
- map->ty = rand()%N;
- } while(rbt->x == map->tx && rbt->y == map->ty);
- fillmap(map);
- }
- void render(r const rbt, m const map) {
- char tersym[3] = {'_', '~', '@'};
- for (int i = 0; i < N; ++i) {
- for (int j = 0; j < N; ++j) {
- int ter = map.map[i][j];
- if(i == rbt.y && j == rbt.x) {
- putchar('o');
- } else if (i == map.ty && j == map.tx) {
- putchar('x');
- } else {
- putchar(tersym[ter]);
- }
- }
- putchar('\n');
- }
- printf("Energy: %d\n", rbt.energy);
- printf("Score : %d\n", rbt.score);
- }
- // 0: left; 1: right; 2: up; 3: down;
- int getMove(r const rbt) {
- while(1) {
- fputs("move > ", stdout);
- char c = _getch();
- switch(c) {
- case 'a':
- if (rbt.x != 0) return 0; break;
- case 'd':
- if (rbt.x != N-1) return 1; break;
- case 'w':
- if (rbt.y != 0) return 2; break;
- case 's':
- if (rbt.y != N-1) return 3; break;
- }
- }
- }
- void YouGotAPoint() {
- puts("###################################");
- puts("### ###");
- puts("### You Got A Point!!! ###");
- puts("### ###");
- puts("###################################");
- system("pause");
- system("cls");
- }
- void update(r* rbt, m* map, int move) {
- int movedir[4][2] =
- { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
- rbt->x += movedir[move][0];
- rbt->y += movedir[move][1];
- system("cls");
- if (rbt->x == map->tx && rbt->y == map->ty) {
- YouGotAPoint();
- (rbt->score)++;
- rbt->energy = 20 - rbt->score;
- do {
- map->tx = rand()%N;
- map->ty = rand()%N;
- } while(rbt->x == map->tx && rbt->y == map->ty);
- } else {
- rbt->energy -= map->map[rbt->y][rbt->x] + 1;
- }
- }
- void YouDied() {
- puts("###################################");
- puts("### ###");
- puts("### You Died!!! ###");
- puts("### ###");
- puts("###################################");
- }
- void exitGame(r rbt) {
- system("cls");
- YouDied();
- printf("Score: %d\n", rbt.score);
- system("pause");
- }
- int main(void) {
- struct robotStruct rbt;
- struct mapStruct map;
- init(&rbt,&map);
- while (rbt.energy > 0) {
- render(rbt, map);
- update(&rbt, &map, getMove(rbt));
- }
- exitGame(rbt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement