I need some quick help with code.
Hey I really need help figuring out this problem for my program study group. I need to show this code in my presentation. I was just wondering if anyone could help me figue it out. Heres the problem.
The Problem
Input Specification
Assume in all situations, the user enters valid input.
Many games store a player's score and allow the player to have multiple lives. The final score of playing a game is the score the player attains at the point in time that they have no lives left. Furthermore, many of these games have several rounds or boards. If a player completes a round without losing a life, rather than the opponent getting a chance to play, that player gets to continue playing. Using these general guidelines, you will design the framework for a game program without knowing any detailed information of the game. You will only be given the function prototype for a function that executes a single round of a game as well as some other specifications for how you should run the game.
the goal is to show how a good functional design can be used to split up work amongst mostly independently working programmers, an actual function will not be provided until after the due date to the assignment.
Here is the prototype of the function you will have to call that executes a single round of a game:
// Pre-condition: name is the name of the current player. numlives is a reference to
// the variable storing the number of lives left of the current player.
// score is a reference to the variable storing the score of the current
// player, and round is the round the player is currently on.
// Post-conditions: A single round of the game will be executed. The player's score
// and number of lives will be adjusted accordingly. The round
// will be completed unless the player has no more lives left.
void playround(char name[], int* numlives, int* score, int round);
You will execute a competition between two players in this mystery game. Before the game starts, ask each player their name and set their scores to 0 and their total number of lives to 5.
After this, the game begins. Allow player 1 to go first. On a particular round, if a player does NOT lose any lives, then do NOT change the turn to the next player. Instead, allow the same player to continue their turn playing the next round.
In the situation that the player does lose at least one life, the subsequent turn is taken by the other player. The only exception to either of these rules is if one player has 0 lives. If one player has 0 lives, then all turns will be given to his/her competitor till they lose all their lives.
Your main goal will be to call the playround function when appropriate, control who's turn it is and quit the game and print out the final results of both players. You should print out a status report for each player after they finish a round. (Or if they do not succeed in finishing a round and end up with 0 lives while playing a round.)
Note: This setup is DIFFERENT than standard video games where your opponent always gets to go after you lose a life. Here the change in turn is indicated by finishing a round in which you could lose more than one life. Furthermore, if you finish a round without losing a life, the turn doesn't change, but you DO have to make a new call to the playround function. It is also possible for you to finish a round, lose a life or more in the process, but not lose one at the end of the round. In this case, play advances to the other player if they have lives left.
Thanks so much guys!
What do you mean defining?
What do you mean define??
like this:
Code:
#include <stdio.h>
#DEFINE numlives = 5
#DEFINE score = 0
#DEFINE round = 1
int main(void) {
char name;
printf("Player one, what is your name?\n");
scanf(
now what do i do? how do i store a name??
how do i define name?
Start by defining this stuff:
quote:
--------------------------------------------------------------------------------
// Pre-condition: name is the name of the current player. numlives is a reference to
// the variable storing the number of lives left of the current player.
// score is a reference to the variable storing the score of the current
// player, and round is the round the player is currently on.
--------------------------------------------------------------------------------
really close now please help
OK, almost done. Here is what i havenow. The errors are posted below it. I marked the lines where the errors are with ************ followed by the line number. Hope that helps you out. please let me know how to fix the problems. Thank you guys so much for all of your help throughout this. I'm using gcc compiler in telnet on windows xp
Code:
#include <stdio.h>
#include<string.h>
char name[100] = {0};
int numlives = 5;
int score = 0;
int round = 1;
int player1lives = 5;
int player1turn = 0;
int player1score = 0;
int player2lives = 5;
int player2turn = 0;
int player2score = 0;
int templives;
void swapplayers(void);
void activeplayerstats(void); ************ line 16
void playround(char name[], int* numlives, int*score, int round); ********* line 17
int main(void) { ********** line 19
char name1[100]= {0};
char name2[100]= {0};
printf("Player one, what is your name?\n");
scanf("%s", name1);
printf("Player two, what is your name?\n");
scanf("%s", name2);
strcpy(name,name1);
round = player1turn;
numlives = player1lives;
score = player1score;
while (player1lives>0 || player2lives>0)
{
if (numlives>0)
{
templives=numlives;
playround(name, &numlives, &score,round);
}
if (numlives==0)
{
printf("Your Game is Over.\n");
}
activeplayerstats();
swapplayers( &name1, &name2); ****** line 46
}
printf("Here are the final standings:\n");
if(player1score>player2score)
{
printf("%s %d points\n", name1, player1score);
printf("%s %d points\n", name2, player2score);
}
else
{
printf("%s %d points\n", name2, player2score);
printf("%s %d points\n", name1, player1score);
}
}
void activeplayerstats(void) {
printf("%s, you now have %d points and %d lives left.\n", name,
score, numlives);
void swapplayers(char *p, char *q) {
char tmp[100]; ******** line 70
tmp = *p;
*p = *q;
*q = *tmp;
} **************** line 74
errors....
game3.c: In function 'swapplayers':
game3.c:19: parse error before '{' token
game3.c:19: declaration for parameter 'main' but no such
parameter
game3.c:17: declaration for parameter 'playround' but no such parameter
game3.c:16: declaration for parameter 'activeplayerstats' but no such parameter
game3.c:19: number of arguements doesn't match prototype
cc1: prototype declaration ********whats this??
game3.c:46: too many arguements to function 'swapplayers'
game3.c: In function 'swapplayers':
game3.c:70:incompatible types in assignment
game3.c: In function 'activeplayerstats':
game3.c:74:parse error at end of input
OK finally no compiler errors!! but correct???
I also attached the c file so u guys can look at it. Hi, have been writing a program for my comp science lab. I finaaly finished the program and there are no compiling errors. The program is a simple game. What i have to do is write the framework for the game without knowing any detailed information of the game. I am only given the function prototype for a function that executes a single round of a game as well as some other specifications for how the game should run. what im asking is, is my program doing everything it needs to be doing to be correct?? how can i check. when i first ran the program it asked for both players names correctly and the after i enter the second name it just goes to a new line. But the program is still running. What should i do. Please help.
the prototype is:
Pre-condition: name is the name of the current player. numlives
is a reference to the variable storing the number of
lives left of the current player. score is a reference
to the variable storing the score of the current
player, and round is the round the player is
currently on.
Post-conditions: A single round of the game will be executed. The
player's score and number of lives will be
adjusted accoridngly. The round will be
completed unless the player has no more lives
left.
void playround(char name[], int* numlives, int* score, int round);
Many games store a player's score and allow the player to have multiple lives. The final score of playing a game is the score the player attains at the point in time that they have no lives left. Furthermore, many of these games have several rounds or boards. If a player completes a round without losing a life, rather than the opponent getting a chance to play, that player gets to continue playing.
You will execute a competition between two players in this mystery game. Before the game starts, ask each player their name and set their scores to 0 and their total number of lives to 5.
After this, the game begins. Allow player 1 to go first. On a particular round, if a player does NOT lose any lives, then do NOT change the turn to the next player. Instead, allow the same player to continue their turn playing the next round.
In the situation that the player does lose at least one life, the subsequent turn is taken by the other player. The only exception to either of these rules is if one player has 0 lives. If one player has 0 lives, then all turns will be given to his/her competitor till they lose all their lives.
Your main goal will be to call the playround function when appropriate, control who's turn it is and quit the game and print out the final results of both players. You should print out a status report for each player after they finish a round. (Or if they do not succeed in finishing a round and end up with 0 lives while playing a round.)
Note: This setup is DIFFERENT than standard video games where your opponent always gets to go after you lose a life. Here the change in turn is indicated by finishing a round in which you could lose more than one life. Furthermore, if you finish a round without losing a life, the turn doesn't change, but you DO have to make a new call to the playround function. It is also possible for you to finish a round, lose a life or more in the process, but not lose one at the end of the round. In this case, play advances to the other player if they have lives left.
Example of Output to be produced by your code
Since your code can only really be run in conjunction with a written playround function, the following example will only explicitly list the output that shouuld be produced by your code. Where the function should produce output, I will place the words "FUNCTION OUTPUT" in that location, followed by a description of the function's output. This does NOT indicate the literal output of the function. User input is indicated by bold italics.
Player 1, what is your name?
Sarah
Player 2, what is your name?
Billy
FUNCTION OUTPUT FOR SARAH'S TURN
Sarah, you now have 100 points and 4 lives left.
FUNCTION OUTPUT FOR BILLY'S TURN
Billy, you now have 80 points and 5 lives left.
FUNCTION OUTPUT FOR BILLY'S TURN, SINCE HE DIDN'T LOSE
ANY LIVES
Billy, you now have 145 points and 3 lives left.
FUNCTION OUTPUT FOR SARAH'S TURN
Sarah, you now have 230 points and 4 lives left.
FUNCTION OUTPUT FOR SARAH'S TURN, SINCE HE DIDN'T LOSE
ANY LIVES
Sarah, you now have 380 points and 3 lives left.
FUNCTION OUTPUT FOR BILLY'S TURN
Billy, you now have 245 points and 0 lives left.
Your game is now over.
FUNCTION OUTPUT FOR SARAH'S TURN
Sarah, you now have 500 points and 2 lives left.
FUNCTION OUTPUT FOR SARAH'S TURN, SINCE BILLY HAS NO LIVES LEFT.
Sarah, you now have 700 points and 2 lives left.
FUNCTION OUTPUT FOR SARAH'S TURN
Sarah, you now have 825 points and 1 life left.
FUNCTION OUTPUT FOR SARAH'S TURN
Sarah, you now have 1000 points and 0 lives left.
Here are the final standings:
Sarah 1000 points
Billy 245 points
Notes: You do not have to follow the proper pluralization of the word 'life' as indicated in the example. You may format the status reports of the players' scores as you wish. The output above is only to give you a rough example of what's expected.
heres the code::
Code:
#include <stdio.h>
#include<string.h>
char name[100] = {0};
int numlives = 5;
int score = 0;
int round = 1;
int playerlives[2] = {5,5};
int playerturn[2] = {0,0};
int playerscore[2] = {0,0};
int currentplayer = 0;
int templives;
void swapplayers(void);
void activeplayerstats(void);
void playround(char name[], int* numlives, int*score, int round);
int main() {
char name1[100]= {0};
char name2[100]= {0};
printf("Player one, what is your name?\n");
scanf("%s", name1);
printf("Player two, what is your name?\n");
scanf("%s", name2);
strcpy(name,name1);
round = playerturn[currentplayer];
numlives = playerlives[currentplayer];
score = playerscore[currentplayer];
while (playerlives[0]>0 || playerlives[1]>0)
{
if (numlives>0)
{
templives=numlives;
playround(name, &numlives, &score, round);
activeplayerstats;
}
if (numlives==0)
{
printf("Your Game is Over.\n");
}
swapplayers();
}
printf("Here are the final standings:\n");
if(playerscore[0]>playerscore[1])
{
printf("%s %d points\n", name1, playerscore[0]);
printf("%s %d points\n", name2, playerscore[1]);
}
else
{
printf("%s %d points\n", name2, playerscore[1]);
printf("%s %d points\n", name1, playerscore[0]);
}
return 0;
}
void activeplayerstats(void) {
printf("%s, you now have %d points and %d lives left.\n", name,
score, numlives);
}
void swapplayers(void) {
if (numlives<templives) {
if (currentplayer=0) {
currentplayer=1; }
if (currentplayer=1) {
currentplayer=0; }
}
}
void playround(char name[], int *numlives, int *score, int round){}
like 2 errors now. PLEASE HELP
ERRORS POSTED AT BOTTOM WHATS WRONG???? WHAT CAN I DO??
Code:
#include <stdio.h>
#include<string.h>
int playerlives[2] = {5,5};
int round=1;
int playerscore[2] = {0,0};
int currentplayer = 0;
int templives;
void swapplayers();
void activeplayerstats(char,int,int,int);
void playround(char name[], int* numlives, int*score, int round);
int main() {
int numlives = 5;
int score = 0;
char playername[2][100]= {0};
char name[100]= {0};
printf("Player one, what is your name?\n");
scanf("%s", name[0]);
printf("Player two, what is your name?\n");
scanf("%s", name[1]);
strcpy(name,playername[currentplayer]);
numlives = playerlives[currentplayer];
score = playerscore[currentplayer];
while (numlives>0)
{
templives=numlives;
playround(name, &numlives, &score, round);
round+=1;
activeplayerstats(name[currentplayer], score, numlives,currentplayer);
if (numlives==0)
{
printf("Your Game is Over.\n");
}
swapplayers();
}
printf("Here are the final standings:\n");
if(playerscore[0]>playerscore[1])
{
printf("%s %d points\n", name[0], playerscore[0]);
printf("%s %d points\n", name[1], playerscore[1]);
}
else
{
printf("%s %d points\n", name[1], playerscore[1]);
printf("%s %d points\n", name[0], playerscore[0]);
}
return 0;
}
void activeplayerstats( char playername[], int score[currentplayer],
int numlives, int currentplayer) {
printf("%s, you now have %d points and %d lives left.\n",
playername[currentplayer],score[currentplayer],
numlives[currentplayer]);
}
void swapplayers(int numlives,int templives, int currentplayer) {
if (numlives<templives) {
if (currentplayer==0) {
currentplayer=1; }
if (currentplayer==1) {
currentplayer=0; }
}
}
void playround(char name[], int *numlives, int *score, int round){
game3.c:65: conflicting types for `activeplayerstats'
game3.c:18: previous declaration of `activeplayerstats'
game3.c: In function `activeplayerstats':
game3.c:68: subscripted value is neither array nor pointer ]
I USe GCC compiler