Thread: Grid / Matrix

  1. #1
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24

    Grid / Matrix

    I am making a snake game (just in command prompt) and I want to setup a grid that has kinda x and y values like a matrix.

    1 2 3 4 5 6 7 8 9 10
    1 0 0 0 0 0 0 0 0 0 0
    2 0 0 0 0 0 0 0 0 0 0
    3 0 0 0 0 0 0 0 0 0 0
    4 0 0 0 0 0 0 0 0 0 0
    5 0 0 0 0 X 0 0 0 0 0 <--- lets say the int x and y are here
    6 0 0 0 0 0 0 0 0 0 0 at 5,5
    7 0 0 0 0 0 0 0 0 0 0
    8 0 0 0 0 0 0 0 0 0 0
    9 0 0 0 0 0 0 0 0 0 0
    10 0 0 0 0 0 0 0 0 0 0

    How would i setup this matrix and move the variables around?
    --Seph

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    int matrix[10][10]={/*values*/};

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    here's a start
    Code:
    int grid[11][11]; /*A grid of 10 by 10*/
    x would be assigned to
    Code:
    grid[4][5];
    I think?
    EDIT
    Sorry thantos posted at same time And I think it would be 11 by 11 right the null byte

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int grid[10][10];
      int i, j;
      
      for (i = 0; i < 10; i++)
      {
        for (j = 0; j < 10; j++)
        {
          grid[i][j] = i*10+j;
        }
      }
      
      for (i = 0; i < 10; i++)
      {
        for (j = 0; j < 10; j++)
        {
          printf ("%2d ", grid[i][j]);
        }
        putchar('\n');
      } 
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24
    thx and two more questions, how do i display the grid( is it just printf("%c,grid[x][y]) and how do i see if the user pressed the arrow keys Left, down , right, or up?
    --Seph

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    that is how you display the grid.
    Code:
    printf("%i",grid[2][2]); /*or whatever*/
    About the arroy key
    http://www.phanderson.com/C/getkey.html
    It has a Windows program that will tell you the Hexadecimal and decimal value for anykey you hit. Only works on Windows though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM