Code:
//libraries
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <windows.h>
//constant of array size
#define CARS_ARRAY_SIZE 4
#define MAXR 10
#define MINR 1
#define MAXC 4
#define MINC 0
#define PLAYERS 6
// this structure is pretty much called in every function I made
struct cars
{
char name[20];
char make[20];
int car_number;
char color[20];
int distance;
int RCinfo[4];
};
//prototypes
int car_pic(struct cars *carpointer, int size);
int Modify(struct cars *modpointer, int size, int CarSelect);
int readCarsTXT (struct cars *readpointer, int size);
int manual ();
int automatic();
int movement (struct cars *movementpointer, int size);
int main (void)
{
//declaring variables in array
int i=0;
int lower=0;
int upper=10;
int count=1;
int automaticmovement;
int manualmovement;
int AutoOrManual;
int CarType;
int number;
int CarColor;
char DriverName;
int manualrace;
int v1;
int v2;
int modifyorstart;
int automatic2;
int ExitFlag=0;
//opening file for arrays
FILE *fPointerCars;
srand(time(NULL));//
//Print name and explain this is a racing game
printf("RoseEllen Hoke\n This is a race car game a text base game that races 4 cars across the screen and determines \n the first second and third place.A text file (car.txt) with the name of the driver, type of \n the race car, car number, and the color of the car is given to you.\n\n");
printf("\n\n******Welcome to Nascar Soccer Mom Edition*******\n\n");
do
{
//this lets the user choose wether they want to race manually or automatic or exit
//struct cars AutoManual[CARS_ARRAY_SIZE];
printf("\n enter 1 if you want race against another user \n enter 2 if you want to race against 4 cars automatically \n enter 3 to exit");
scanf("%d",&AutoOrManual);
switch(AutoOrManual)
{
case 1:
//calls manual function
printf("You have selected Manual\n");
manual();
//CAll Function that runs that type of race
break;
case 2:
//calls automatic function
printf("You have selected Automatic\n");
automatic();
break;
case 3:
//exits game
printf("EXIT");
ExitFlag=1;
default:
//allows the user to choose again if they do not enter 1,2 or 3
printf("please enter a valid number");
break;
}
}while(ExitFlag==0);
return 0;
}
//function for automatic cars allows the user to modify there car, start the game or exit
int automatic ()
{
//array
struct cars AutoCars[4];
//variables
int CarSelection=0;
int CarModify=0;
int start=0;
int racestart=0;
int CarStart=0;
int Exitflag=0;
int modifyyourstart;
int playeroneortwo;
int results=0;
int player1;
int player2;
int total1;
//readCarsTXT()
readCarsTXT(AutoCars, CARS_ARRAY_SIZE);
CarSelection=car_pic(AutoCars,CARS_ARRAY_SIZE);
//readCarsTXT(ModifyCars, CARS_ARRAY_SIZE);
//readCarsTXT(StartCars, CARS_ARRAY_SIZE);
//CarStart=
do{
printf("press 1 if you would like to modify your car or press 2 if you would like to start the game or press 3 to exit the menu");
scanf("%d",&modifyyourstart);
switch (modifyyourstart)
{//Ask user whether they want to mod car or start or exit
case 1:
CarModify=Modify(AutoCars,CARS_ARRAY_SIZE,CarSelection);
break;
case 2:
start=AutoRunRace(AutoCars, CARS_ARRAY_SIZE);
break;
case 3:
printf("Exiting the menu");
Exitflag=1;
default:
printf("please enter a valid number");
break;
}
}while(Exitflag==0);
results=writeCarsTXT(AutoCars,CARS_ARRAY_SIZE);
scanf("%d,%d", player1,player2);
printf("First place had a time of %d\n Second place had a time of %d", total1);
}
//function for manual allows user to choose what player they are either play 1 or 2 and then allows them to modify both cars and start game
int manual()
{
//variables
int player1, player2;
int total1, total2;
time_t t;
char ans;
struct cars Race[4];
struct cars ManualRace[2];
int playerone=0;
int playertwo=0;
int CarSelection=0;
int CarModify=0;
int CarStart=0;
int Exitflag=0;
int results=0;
int modifyorstart;
int playeroneortwo;
int start=0;
int racestart=0;
readCarsTXT(Race, CARS_ARRAY_SIZE);
playerone=car_pic(Race,CARS_ARRAY_SIZE);
playertwo=car_pic(Race,CARS_ARRAY_SIZE);
//Copy the race array to the manua rae array
strcpy(ManualRace[0].name,Race[playerone].name);
strcpy(ManualRace[0].make,Race[playerone].make);
ManualRace[0].car_number=Race[playerone].car_number;
strcpy(ManualRace[0].color,Race[playerone].color);
strcpy(ManualRace[1].name,Race[playertwo].name);
strcpy(ManualRace[1].make,Race[playertwo].make);
ManualRace[1].car_number=Race[playertwo].car_number;
strcpy(ManualRace[1].color,Race[playertwo].color);
//scanf(carFilePointer,"%s %s %d %s",readpointer-> name, readpointer-> make, &readpointer->car_number, readpointer-> color);
do{
printf("press 1 if you would like to modify your car or press 2 if you would like to start the game or press 3 to exit the menu");
scanf("%d", &modifyorstart);
switch (modifyorstart)
{// Ask here whether they want to mod car or start
case 1:
printf("enter 1 if you are player one or enter 2 if you are player two");
scanf("%d", &modifyorstart);
switch(playeroneortwo)
{
case 1:
CarModify=Modify(ManualRace,CARS_ARRAY_SIZE, playerone);
break;
case 2: CarModify=Modify(ManualRace,CARS_ARRAY_SIZE, playertwo);
break;
}
// CarModify=Modify(ManualRace,CARS_ARRAY_SIZE, playerone);
break;
case 2:
start=AutoRunRace(ManualRace, CARS_ARRAY_SIZE);
break;
case 3:
printf("Exiting the menu");
Exitflag=1;
default:
printf("please enter a valid number");
break;
}
results=writeCarsTXT(ManualRace,CARS_ARRAY_SIZE);
}while(Exitflag==0);
//generates random numbers
srand(time(&t));
//generates number 0-3
player1 = (rand()% 3)+1;
player2 - (rand()% 3)+1;
total1= player1 + player2;
scanf("%d,%d", player1,player2);
printf("First place had a time of %d\n Second place had a time of %d", total1);
}
//function that creates a random number for the moves and the car
int RandGenerator(int Max, int Min)
{
int randnum=0;
//int MAXV=10;
//int MINV=1;
randnum=( rand() % (Max-Min))+ Min ;
//printf("%d\n", randnum);
return randnum;
}
//this functions runs the race by using the random number generator and picking a number defined by the constatnts at the begining
int AutoRunRace(struct cars *racer, int size)
{
int FinishFlag=0;
int RandCar;
int RandDist;
int i,j;
for(i=0;i<4;i++)
{
(racer+i)->distance=0;
}
do
{
RandCar= RandGenerator(MAXC, MINC);
RandDist= RandGenerator(MAXR, MINR);
(racer+RandCar)->distance+=RandDist;
for(i=0;i<4;i++)
{
printf("%d", (racer+i)-> car_number);
for(j=0;j<(racer+i)->distance;j++)
{
printf("*");
}
if((racer+i)->distance>=90)
{
FinishFlag=1;
}
printf("\n");
}
}while(FinishFlag==0);
}
//This fuctnio writes to the txt file raceresults
int writeCarsTXT(struct cars *resultspointer, int size)
{
int num;
FILE *resultsfptr;
resultsfptr = fopen("raceresults.txt", "w");
int i=0, results=0;
// display cars
for (i=0; i < size; i++)
{
printf("%d- %-12s %-12s %-3d %-12s\n", i+1, resultspointer-> name, resultspointer-> make, resultspointer->car_number, resultspointer-> color);
// pointer arithmetic. point to the next array element
resultspointer++;
}
scanf("%d", &results);
// assign array index to chosen_car and return
results --;
return results;
if(resultsfptr == NULL)
{
printf("no winners");
exit(1);
}
printf("\n first place is: %d\n second place is: %d\n third place is: %d\n ");
scanf("%d",&num);
fprintf(resultsfptr,"%d", num);
fclose(resultsfptr);
}
// this function allows the user to pick a car from the text file car
int car_pic(struct cars *carpointer, int size)
{
int i=0, chosen_car=0;
// display cars
for (i=0; i < size; i++)
{
printf("%d- %-12s %-12s %-3d %-12s\n", i+1, carpointer-> name, carpointer-> make, carpointer->car_number, carpointer-> color);
// pointer arithmetic. point to the next array element
carpointer++;
}
printf("\nEnter the number that correlates to what car you want");
scanf("%d", &chosen_car);
// assign array index to chosen_car and return
chosen_car --;
return chosen_car;
}
// this allows the user to modify there name, make of the car, number and color
int Modify(struct cars *modpointer, int size, int CarSelect)
{
//variables
int i= CarSelect;
char CarType1[20];
char DriverName[20];
int numberA;
int menu=0;
char CarColor1[20];
int ExitMod=0;
do{
//You should have this is a Do while and have a case to exit the modifications
printf("Please select your modification\n enter 1 for name\n enter 2 for a make\n enter 3 for a number\n enter 4 for a color\n enter 5 to exit");
scanf("%d", &menu);
switch(menu)
{
case 1:
printf("\n please enter driver name");
scanf("%s", DriverName);
strcpy((modpointer+i)->name, DriverName);
break;
case 2:
printf("please enter the type of car you want");
scanf("%s", CarType1);
strcpy ((modpointer+i)->make, CarType1);
break;
case 3:
printf("\nplease enter the number you want");
scanf("%d", numberA);
((modpointer+i)->car_number=numberA);
break;
case 4:
printf("please enter the color you want your car");
scanf("%s", &CarColor1);
strcpy((modpointer+i)->color, CarColor1);
break;
case 5:
printf("Exit modification");
ExitMod=1;
break;
default:
printf("Wrong INput");
}
}while(ExitMod==0);
}
//This function reads the cars. txt file
//and is passed from a function to a structure
int readCarsTXT(struct cars *readpointer, int size)
{
// declaring variables
int i=0;
FILE *carFilePointer;
carFilePointer = fopen("car.txt", "r");
// check existence of the file
if (carFilePointer == NULL)
{
printf("file does not exist");
}
else
{
// reads the file and assign data to array of structures
for (i=0; i < CARS_ARRAY_SIZE; i++)
{
fscanf(carFilePointer,"%s %s %d %s",readpointer-> name, readpointer-> make, &readpointer->car_number, readpointer-> color);
// pointer arithmetic. point to the next array element
readpointer++;
}
// call car_pic function
}
close(carFilePointer);
}
int results(struct cars *resultspointer, int size)
{
struct cars temp;
int x,y;
/* seed the randomizer */
srand( (unsigned)time(NULL));
int write=0;
int player1=0;
int player2;
/* sort the scores */
for(x=0;x<CARS_ARRAY_SIZE;x++){
for(y=x+1;y<size;y++){
if((resultspointer+x)->(distance)<((resultspointer+y)->(distance)))
{
temp = ((resultspointer+x)->(distance));
player[x] = ((resultspointer+x)->(distance));
player[y] = (resultspointer+y)->(distance);
}
}
}
/* display the results */
puts("Round Results:");
for(x=0;x<PLAYERS;x++)
{
printf("#%d %s, score %d\n",x+1);
((resultspointer+x)->(name));
((resultspointer+x)->(distance));
write=writeCarsTXT(CARS_ARRAY_SIZE, results);
}
}