First of all this is an assignment (game) which i have to submit.
I have develop so far of this game but there are some instructions/conditions (functions) i need to complete.. but i dont understand them to do coding. So anyone please help me with this coding. Please run the coding then you will understand the game.
Instructions (which i cannot do);
• The occurrence of integer 1 in the entire board should not be exceeding 20% of the board’s size
(eg: For 5 x 5 board, the occurrence of integer 1 should not be more than 5 times)
• The largest integer in the entire board should occur only once.
• Two steps will be counted (double-penalty) when the player lands at the same square for the second time onward
• The information of previous players is stored in a plain text data file. The information of all previous players should be displayed at the beginning of the game. User / player will also be prompted to key in their information to be stored in the text data.
Code:
/*File: Assignment
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<ctype.h>
#define NROW 5
#define NCOL 5
typedef struct
{
int a[NROW][NCOL], x, y, random, count, limit, px, py;
int b[NROW][NCOL];
char name[20], direct, new;
}game;
void disp_arr(game arr);
void logic(game *arr);
int main(void)
{
game arr;
printf ("\n******************WELCOME TO NUMBER PATH GAME******************\n");
printf("\n\nPlease key in your name :");
scanf("%s", &arr.name);
printf("\n\n***Good day %s, let's start a new game. All the best!!!***\n\n", arr.name);
getch();
srand( (int) time(NULL));
do
{
for (arr.x = 0; arr.x < NROW; arr.x++)
{
for (arr.y = 0; arr.y < NCOL; arr.y++)
{
arr.random = rand() % (4 - (1) + 1) + 1;
arr.a[arr.x][arr.y] = arr.random;
}
}
arr.px = 0;
arr.py = 0;
disp_arr(arr);
arr.count = 0;
do
{
system ("cls"); /* command on some operating systems that clears the screen.*/
printf("\n\t\t\t*******GAME PLAY******\n\n");
disp_arr(arr);
printf("\nNext move:\nPress 2 - Down\nPress 4 - Left\nPress 6 - Right\nPress 8 - Up\nPress Q to Give up...\n\nEnter Direction/Choice: ");
fflush(stdin);
arr.direct = toupper(getch());
logic(&arr);
arr.count++;
}
while (arr.direct != 'Q' && (arr.px != NROW - 1 || arr.py != NCOL - 1));
/*Winning or loosing & requsting for new game*/
if (arr.px == NROW - 1 || arr.py == NCOL - 1)
{
system ("cls");
printf("\n\t\t\t******** RESULTS ********\n\n");
printf("\n\nCongratulation! you won the game!\n");
disp_arr(arr);
printf("\nNumber of steps taken: %i\n\n", arr.count);
}
else
{
system ("cls");
printf("\n\t\t\t******** GAME OVER ********\n\n");
printf("\n\nYou have given up...\n\n");
}
printf("Do you want to play a new game? (Y/N): ");
arr.new = toupper(getch());
}
while (arr.new == 'Y');
return 0;
}
void disp_arr(game arr)
{
int x, y;
printf("\n");
arr.a[NROW-1][NCOL-1] = 0;
arr.b[NROW][NCOL] = 0;
for (x = 0; x < NROW; x++)
{
for (y = 0; y < NCOL; y++)
{
if (x == arr.px && y == arr.py)
printf("(%2i )", arr.a[x][y]);
else
printf(" %2i ", arr.a[x][y]);
}
printf("\n\n");
}
}
void logic(game *arr)
{
if (arr->direct == '8' && arr->px >= (arr->a[arr->px][arr->py]))
arr->px = arr->px - arr->a[arr->px][arr->py];
else if (arr->direct == '2' && (5 - arr->px) > (arr->a[arr->px][arr->py]))
arr->px = arr->px + arr->a[arr->px][arr->py];
else if (arr->direct == '4' && arr->py >= (arr->a[arr->px][arr->py]))
arr->py = arr->py - arr->a[arr->px][arr->py];
else if (arr->direct == '6' && (5 - arr->py) > (arr->a[arr->px][arr->py]))
arr->py = arr->py + arr->a[arr->px][arr->py];
}