Input from keyboard

This is a discussion on Input from keyboard within the C Programming forums, part of the General Programming Boards category; Hey, I'm trying to write a simple game (battleships) in C and am a little stuck. The information i've found ...

  1. #1
    B_B
    B_B is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Input from keyboard

    Hey,

    I'm trying to write a simple game (battleships) in C and am a little stuck. The information i've found on the internet and in books hasn't helped me a great deal so i turn to you!

    This is my code so far:

    Code:
    #include <stdio.h>
    #include <8051.h>
    
    void putchar(char c);
    int c;
    int hits;
    int array_pointer;
    int Battleship[5][5] = { {'0','0','1','1','1'}, {'1','0','0','0','0'}, {'1','0','1','1','0'}, {'0','0','0','0','0'}, {'1','1','1','1','1'}}; 
    
    void main()
    {
    	putchar;
    	printf("\x1b[2J",0);						//Clears terminal screen
    	printf("%s\n\r",&"___________________________");		//Prints title block
    	printf("%s\n\r",&"---B-A-T-T-L-E-S-H-I-P-S---");		//Prints title block
    	printf("%s\n\n\n\n\r",&"___________________________");		//Prints title block
    	printf("%s\n\n\n\r",&"Press ENTER to begin...");		//Prints title block
    	
    	printf("%s %s %s %s %s\n\r", "X", "X", "X", "X", "X");
    	printf("%s %s %s %s %s\n\r", "X", "X", "X", "X", "X");
    	printf("%s %s %s %s %s\n\r", "X", "X", "X", "X", "X");
    	printf("%s %s %s %s %s\n\r", "X", "X", "X", "X", "X");
    	printf("%s %s %s %s %s\n\r", "X", "X", "X", "X", "X");
    	}
    
    void game()
    {
    	hits = 12;
    	array_pointer = 0;
    
    
    }
    void putchar(char c)
    {
    	while(! TI);
    	TI = 0;
    	SBUF =c;
    }
    I'm hoping to get the user to press enter to begin the game, where the grid of 'X' will apear, and where any other key does nothing.

    PLEASE HELP!

    B_B

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Call getchar() function just after the printf statment. And also main should return a value of type int.

    Code:
    int main() {
    ...
    ...
    printf("%s\n\n\n\r",&"Press ENTER to begin...");		//Prints title block
    getchar();
    
    printf("%s %s %s %s %s\n\r", "X", "X", "X", "X", "X");
    ...
    ...
    return 0;
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    - putchar is a standard-lib function, NEVER override this
    - global vars are bad
    - \n\r are wrong, simple use \n
    - &"" is wrong, string literals are defined without &
    - many other beginners stuff ...

    Code:
    #include <stdio.h>
    
    int readChar() {
      char in[2];
      if( fgets( in, 2, stdin) ) return *in;
      return EOF;
    }
    
    
    #define NUMSHIPS 5
    int c;
    int hits;
    int array_pointer;
    
    void game()
    {
    	hits = 12;
    	array_pointer = 0;
    }
    int main()
    {
      char Battleship[NUMSHIPS][5] = { {' ',' ','X','X','X'}, {'X',' ',' ',' ',' '}, {'X',' ','X','X',' '}, {' ',' ',' ',' ',' '}, {'X','X','X','X','X'}}; 
      int i;
      printf("\x1b[2J",0);						//Clears terminal screen
      printf("%s\n","___________________________");		//Prints title block
      printf("%s\n","---B-A-T-T-L-E-S-H-I-P-S---");		//Prints title block
      printf("%s\n\n\n\n","___________________________");		//Prints title block
      printf("%s\n\n\n","Press ENTER to begin...");		//Prints title block
    
      readChar();
    
      for( i=0; i<NUMSHIPS; ++i )
        printf("%c %c %c %c %c\n", Battleship[i][0],Battleship[i][1],Battleship[i][2],Battleship[i][3],Battleship[i][4]);
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using inline assembly for keyboard input
    By sleventeen in forum C Programming
    Replies: 7
    Last Post: 05-10-2009, 02:31 AM
  2. Keyboard Input
    By CaliJoe in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2009, 10:51 AM
  3. Keyboard input in a Dialog
    By ksarkar in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2005, 06:39 AM
  4. Intercepting keyboard input
    By EvBladeRunnervE in forum Windows Programming
    Replies: 3
    Last Post: 01-08-2004, 09:03 AM
  5. FAQ Keyboard Input ? (C++)
    By Malikive in forum FAQ Board
    Replies: 6
    Last Post: 11-07-2001, 09:30 PM

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