Sign in to follow this  

get an if statement to work

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello all! I am trying to code a program to practice using arrays and I started with this little program that I cant seem to get to working right. I'm trying to have two charters a jedi and sith fight. The player being the jedi and the computer the sith. The player chooses a number from 1, 2, or 3 and that is there move. Then the computer uses the same process but it get a random number instead for the siths action and the sth performs that action. Now my problem is that when its time for the sith to perform its action the program ends? I cant figure out why any help would be hot?
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;

int attack (int atk);
int defend (int def);
stall ();


int main ()
{
 // the array is jedi[0] = attack, jedi[1] = defend, jedi[2] = vitality, jedi[3] = wound
 int jedi[4]= {3,4,10,8};
 //for the sith see jedi
 int sith[4]= {4,3,8,10};
 int move = 0;
 int dicenumber = 0;
 //trying two different ways for bool type one with "int" and the other with "bool"
 int jedturn = 0;
 bool sithturn = 1;

 
 srand(time(NULL));
 //start of players turn.
 cout << "attack with 1? defend with 2. stall with 3."<<endl;
 cin >> move;

 switch (move)
 {
 case 1:
 attack (jedi[0]) ;
 jedturn = 1;
 break;

 case 2:
 defend (jedi[1]);
 jedturn =1;
 break;

 case 3:
 stall ();
 jedturn =1;
 break;

 default:
 cout << "please enter 1, 2 or 3."<<endl;
 break;
 }
 //end of players turn

 //start of pc turn
 //chosse random number for acticon for enemy
  //then enemy perform the action
 if (jedturn == 1)
 {
  int enemyaction = rand() % 3+1;
  
  

  switch (enemyaction)
  {
  case (1):
  cout << "blah "<< attack(sith[0]);
  break;

  case (2):
  defend (sith[1]);
  break;

  case (3):
  sithturn = 1;
  break;

  default:
  break;
  }  
 }
 else
 cout << "sith dose nothing"<<endl;
 //end of player turn

 return 0;
}

int attack (int atk)
{
 int totalattack = 0;

 totalattack = atk + rand() % 20;
 cout << totalattack<< endl;
 return totalattack;
}

int defend (int def)
{
 int totaldefend;
 totaldefend = def + 5;
 return totaldefend;
}

 stall ()
{
 cout<< "end turn" << endl;
 return 0;
}

Share this post


Link to post
Share on other sites
Looks like a great start! I'm glad you didn't decide to start making Everquest 3 first. Anyway, if you look at your program, you'll see it's doing just what you asked it to do: Player gives input, player moves, Sith move, return 0; If you haven't been introduced to the while loop yet, now is an excellent time. Consider:


bool GameIsFinished = false;

while (!GameIsFinished) // this could also be written "while (GameIsFinished == false)"
{
GetPlayerInput();
DoPlayerMove(); // have player take a move

if (A_Player_Is_Dead() == true) // if someone is dead now, the game is over
GameIsFinished = true;

DoSithMove(); // let computer move
if (A_Player_Is_Dead() == true) // see above
GameIsFinished = true;

}

cout << "Thanks for playing!";
return 0;



In this way, the game will keep going until a player is dead. Obviously the functions I used were pseudocode, by the way. :)

Share this post


Link to post
Share on other sites
good start so far :) one other thing i noticed

stall ()
{
cout<< "end turn" << endl;
return 0;
}

that function returns a value but isnt set up to do so (return 0), try

int stall()
{
cout<< "end turn" << endl;
return 0;
}

Share this post


Link to post
Share on other sites
hum a funny thing happened? I have been trying to get the line cout << "blah "<< attack(sith[0]); to show up in my program, and i couldnt until i put cout<< "sith defends" << endl; in case 2 and cout<< "sith stall" << endl; in case 3 why didn't the cout << "blah "<< attack(sith[0]); show up before?

Share this post


Link to post
Share on other sites
Most likely this is because the output buffer wasn't being flushed. When you type a line like:

cout << "My text";

The computer puts "My text" into a holding space (a "buffer") but doesn't print everything in the buffer to the screen until later, to be more efficient - if you were to put:

cout << "My text";
cout << "My text2";
cout << "My text3" << std::endl;

It could print all of it at once instead of having to get it, print it, get it, print it, get it, and print it because it's collected in the buffer. When you use std::endl; or std::flush; you tell the computer to go ahead and print the text in the buffer no matter what, causing it to be displayed.

'Least, that's my understanding of it.

Edit: I wouldn't know why putting std::endl in other cases would have that effect.

Share this post


Link to post
Share on other sites
what's up all! today I stared reading about pointers and wanted to implement them in my program. I have been doing well so far but I am coming across one problem. in the program when it comes time for the sith to attack it displays the sith actions as well as the players actions. Giving the player an extra turn and giving the player the same action as the computer. I was wondering if this has something to do with the pointers I am using if its the function I'm using or something else?

#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;

int playersmove (int *arr, bool *whoturn);
int sithturn (int *sitharr,bool *siturn);
int attack (int *atk);
int defend (int *def);
stall ();

int main ()
{
int i = 0;
//array types
int jedi[4]= {3,4,10,8};
int sith[4]= {4,3,8,10};
//boolen types
bool jediturn = true;
bool sithsturn = false;
//pointer types
bool *turn;
turn = &jediturn;
bool *pcturn;
pcturn = &sithsturn;


//function
if (jediturn == true)
{
playersmove(jedi, turn);
}

if (jediturn == false)
{
jediturn = false;
sithsturn = true;
sithturn (sith, pcturn);
}


return 0;
}

int playersmove (int *arr ,bool *whoturn)
{
int move = 0;

cout << "attack with 1? defend with 2. stall with 3."<<endl;
cin >> move;

switch (move)
{
case 1:
attack (arr);
*whoturn = false;
break;

case 2:
defend (arr+1);
*whoturn = false;
break;

case 3:
stall ();
*whoturn = false;
break;

default:
cout << "please enter 1, 2 or 3."<<endl;
break;
}

return *whoturn;
}

int sithturn(int *sitharr, bool *siturn)
{
int enmeyaction = rand() % 3;



switch (enmeyaction)
{
case (1):
cout << "the sith attacks "<< endl;
attack (sitharr);
*siturn = false ;
break;

case (2):
cout<< "sith defends" << endl;
defend (sitharr);
*siturn = false;
break;

default:
break;
}

return *siturn;
}

int attack (int *atk)
{
int totalattack = 0;
int tack = *atk;
totalattack = tack + rand () % 20 +1;
cout << "you chosse to attack " <<totalattack<< endl;

return totalattack;
}

int defend (int *def)
{
int totaldefend;
totaldefend = *def + 5;
cout << "you rasie your guard " << totaldefend << std::endl;
return totaldefend;
}

stall ()
{
cout<< "end turn" << endl;
return 0;
}

Share this post


Link to post
Share on other sites
I don't have time to answer your question, but I just have to warn your: its great to learn how pointers work, but you're going to want to use for an 'appropriate' use. You don't need to use pointers there, so it would probably be best to avoid them there. I don't really know of anything your level that requires the use of pointers; they don't become exceedingly useful(most of the time) until you get to classes.

Share this post


Link to post
Share on other sites
I agree, you shouldn't "try to use pointers". They're not a must-have or a "good thing". They're just something you unfortunately have to deal with occasionally.
So you should understand what they are, and what they can do that you can't do otherwise, and then wait until you run into a situation where you actually need them. But as long as you don't need them, stay away. [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by Xizorp
what's up all! today I stared reading about pointers and wanted to implement them in my program. I have been doing well so far but I am coming across one problem.


Zen: Yes, the problem you are coming across is that you want to implement something just because you read about it. :)

Share this post


Link to post
Share on other sites
yea after I stared using it I realized that I wouldn't need them for this program. but I just wanted to get some practice in with them. the books I have stressed the importance of pointers, especially when using arrays that pass values to functions. I don't really want to shy away from them right now. I want to get an understanding of them and was wondering when is it necessary to use them? what's a situation that requires there use?

Share this post


Link to post
Share on other sites

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this