have a look at this:)

This is a discussion on have a look at this:) within the C Programming forums, part of the General Programming Boards category; Ok.. is it ugly to use this kind of statements down here? Like, while,for,while, and if/else if. Can you do ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    have a look at this:)

    Ok.. is it ugly to use this kind of statements down here? Like, while,for,while, and if/else if. Can you do it some other way?. Oh btw, the number of rounds you want to play doesent work. Cant figure out why.
    Please help me out here

    Code:
    int main()
    {
      int human, computer, result, statistic, antalggr, 
        ggr=0, h=0, c=0, run=0, antalrun;
               
      srand( time(0) );
      statistic = 0;
      printf("welcome to stone, paper scissor-game ...\n");
      printf("How many times do you want to play?\n");
      scanf("%d", &antalggr);
      printf("How many rounds do you want to play?\n");
      scanf("%d", &antalrun);
      while (run<antalrun) {
        for (run = 0; run<antalrun; run++) {
          while (ggr<antalggr) {
    	for (ggr = 0; ggr<antalggr; ggr++) {
    	  if (result==HUMAN_WIN) {
    	    h++;
    	  }
    	  else if(result==COMPUTER_WIN) {
    	    c++;
    	  } 
    	  human=human_choice(human);
    	  computer=computer_choice(computer);
    	  print_computer_choice();
    	  result=winner(human,computer);
    	  print_winner(result);
    	}
    	if (c>h){printf("Computer win round\n");
    	}
    	else if (h>c){
    	  printf("Human win round\n");
    	}
    	else if (h==c) {
    	  printf("Its a draw!!\n");
    	}
          }
        }
      }
      return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,438
    Sure, you can use an infinite for loop and determine the ending conditions yourself with some Ifs down at the end of the loop. If conditions for breaking the loop is true, then just do break to break the loop.
    Code:
    for (;;)
    {
    	break; /* Breaks the loop */
    }
    The while is probably unnecessary
    Code:
      while (run<antalrun) {
    Since the for loop checks for that condition and loops while it's true.

    Code:
    	  if (result==HUMAN_WIN) {
    	    h++;
    	  }
    	  else if(result==COMPUTER_WIN) {
    	    c++;
    	  }
    I'd move that down below
    Code:
    result=winner(human,computer);
    This makes more sense, doesn't it?
    Code:
    result = winner(human, computer);
    if (result == HUMAN_WIN) h++;
    else if (result == COMPUTER_WIN) c++;
    OK, sorry, couldn't resist cleaning up that messy coding style

    You're also mixing tabs and spaces - that's usually a bad idea. Use one. I recommend only tabs.
    Last edited by Elysia; 12-16-2007 at 05:17 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    hmm

    Well i need to get this play_again-function to work. Have same problem with another game..
    Just dont get it.. you cant use a while(true) and just return true or false from a function? i have tried that.
    I will give you the whole code for the game, please have a look!
    The game is working except the play_again-function-whateveritis
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<time.h>
    #include<string.h>
    
    #define MAX_TURNS 10
    #define MAX_BUFFER 6		
    #define DRAW 0
    #define COMPUTER_WIN 1
    #define HUMAN_WIN 2
    #define STONE 0
    #define SCISSOR 1
    #define PAPER 2
    #define STONE_STR "s"	
    #define SCISSOR_STR "k"
    #define PAPER_STR "p"
    
    
    
    
    
    /* ------------------- Utilities ---------------*/
    void clear_stdin();
    int human_choice(int human);
    int computer_choice(int computer);
    void print_computer_choice();
    int winner(int human,int computer);
    void print_winner(int result);
    int play_again(int svar);
    /***************************************************
     *
     *    MAIN
     *
     ***************************************************/
    
    int main()
    {
      int human, computer, result, statistic, antalggr, 
        ggr=0, h=0, c=0;
               
      srand( time(0) );
      statistic = 0;
      printf("Welcome to paper, stone, scissor-game ...\n");
      printf("How many times do you want to play?\n");
      scanf("%d", &antalggr);
          while (ggr<antalggr) {
    	for (ggr = 0; ggr<antalggr; ggr++) {
    	  if (result==HUMAN_WIN) {
    	    h++;
    	  }
    	  else if(result==COMPUTER_WIN) {
    	    c++;
    	  } 
    	  human=human_choice(human);
    	  computer=computer_choice(computer);
    	  print_computer_choice();
    	  result=winner(human,computer);
    	  print_winner(result);
    	}
    	if (c>h){printf("Computer win round\n");
    	}
    	else if (h>c){
    	  printf("Human win round\n");
    	}
    	else if (h==c) {
    	  printf("Its a draw!\n");
    	}
    	
    	/* play again? fix me! */
    	play_again(svar);
    	if (svar == true) {
    	  ;
    	}
    	else if (svar == false) {
    	  break;
    	}
           
          }
      return 0;
    }
    
    /******************************************************
     *
     *  DEFINITIONS
     * 
     ******************************************************/
    
    void clear_stdin()
    {
      while( getchar() != '\n' ){;}
    }
    
    int human_choice(int human) {
      char humanchar;
      printf("Choose stones), scissor(k) or bag(p)\n");
      printf("  : ");
    
      scanf("%c", &humanchar);
     
      switch(humanchar){
      case 's': human = STONE; break;
      case 'k': human = SCISSOR; break;
      case 'p':  human = PAPER; break;
      }
      clear_stdin();
      return human;
    }
    
    int computer_choice(computer){
      computer = rand() % 3;
      return computer;
    }
    
    void print_computer_choice(int computer) {
      char c_took;
      switch(computer){
      case 0: c_took = 's'; break;
      case 1: c_took = 'k'; break;
      case 2: c_took = 'p'; break;
      }
      printf("Computer choosed '%c'\n", c_took);
    }
    
    int winner(int human,int computer)
    
    {
     
      int result=0;  
    
      if (computer == human) {
        result = DRAW;
      }
      else if (human == STONE && computer == SCISSOR) {
        result = HUMAN_WIN;
      }
      else if (human == SCISSOR && computer == PAPER) { 
        result = HUMAN_WIN;
      }
      else if (human == PAPER && computer == STONE) { 
        result = HUMAN_WIN;
      }
      else {
        result = COMPUTER_WIN;
      }
      return result;
    }
    
    void print_winner(int result)
    {
      if(result==DRAW)
        printf("Draw!\n");
      if(result==COMPUTER_WIN)
        printf("Computer won!\n");
      if(result==HUMAN_WIN)
        printf("You won!\n");
    }
    
    int play_again(int svar) {
      char getanswer;
      printf("Do you want to play again?(j/n)\n");
      scanf("%c", getanswer);
      if (getanswer == 'j') {
        svar = true;
      }
      else if (getanswer == 'n'){
        svar = false;
      }
      return svar;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,438
    You're still mixing tabs and spaces - please, fix that! It messes up the code when put inside the dev IDE.
    Use only ONE tab.
    Code:
    	play_again(svar);
    svar is undefined. You must declare it.

    Code:
    int computer_choice(computer){
    Missing int! Turn up your warnings to max! This code is bad!

    Code:
    int play_again(int svar) {
    Function is expect to set svar to the correct value; this requires pointers.

    result, human and computer are not initialized; initialize them.

    Code:
    scanf("&#37;c", getanswer);
    scanf takes a pointer.
    Last edited by Elysia; 12-16-2007 at 06:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21