c - Moving characters in 2d array -


i tried create 8 x 8 checkers game. trying move hyphen ' _ ' in 2d array select character 'x' want. have created if statement detecting hyphen ' _ ' seem code isn't working, need help. new programming.

#include <stdio.h>  void gameboard(char board[8][8]) {   int x, y;    for(x=0; x<8; x++)   {    for(y=0; y<8; y++)     {     printf("=---=");     }     printf("\n\n");      for(y=0;y<8;y++)     {     printf("| %c |",board[x][y]);     }     printf("\n\n");   }     for(x=0;x<8;x++)   {   printf("=---=");   }   }  void character(char board[8][8]) {  int x,y;   for(x=0;x<8;x++){    for(y=0;y<8;y++){     if(x<3){         if(x%2 == 0){               if(x%2 == 0){               board[x][y] = 'o';                }               if(y%2==1){               board[x][y]= ' ';                }        }        if(x%2 == 1){               if(y%2 == 0){               board[x][y] = ' ';                }               if(y%2 ==1){              board[x][y]= 'o';                }        }      }      if((x==3) || (x==4)){     board[x][y] = ' ';     }       if(x>4)     {             if(x%2 == 0){                   if(y%2 == 0){                   board[x][y] = 'x';                    }                   if(y%2 ==1){                   board[x][y]= ' ';                    }            }             if(x%2 == 1){                   if(y%2 == 0){                   board[x][y] = ' ';                     }                   if(y%2 ==1){                   board[x][y]= 'x';                    }            }            if(x==5 && y ==1)           {            if(x%2 == 1){                   if(y%2 == 1){                   board[x][y] = '_';                   }                  }             }             }      }    }  }   void playgame(char board[8][8]) {   int x=0, y=0, a, b, c=0,input;  char token;    printf("\n\n---start game---\n");         if(token == '_')   {     printf("please select token : ");     }   for(a=0; a<8; a++)  {   for(b=0; b<8; b++)    {     if(board[a][b] == token & c == 0)     {      x = a;      y = b;      c++;      }     }  }    printf("1 go right\n");   printf("2 go left\n");   printf("3 go left\n");   printf("4 go right\n");   printf("5 go down left\n");   printf("6 go down right\n");   printf("7 select token\n");   fflush(stdin);   scanf("%i", &input);     if(input == 1)    {     board[x][y+2] = token;     y++;     }    else if(input == 2)     {      board[x][y-2] = token;      y--;      }     else if(input == 3)      {      board[x-1][y-1] = token;      x--;      y--;      }      else if(input == 4)      {      board[x-1][y+1] = token;      x--;      y++;      }      else if(input == 5)      {       board[x+1][y-1] = token;       x++;       y--;      }      else if(input == 6)      {       board[x+1][y+1] = token;       x++;       y++;       }       else       {       board[x][y] = token;       } }   int main() {    char bx[8][8];     gameboard(bx);    playgame(bx);   return 0;  } 

you have initialize array, use switch statement calculate new position based on input.

save old position , swap content of new cell of old cell in saved position.

int main() {     char board[8][8];     int x, y;     (x = 0; x < 8; x++)         (y = 0; y < 8; y++)             board[x][y] = '.';     board[0][0] = '_';      int xpos = 0;     int ypos = 0;     while (1)     {         system("cls||clear");         //print board         (y = 0; y < 8; y++)         {             (x = 0; x < 8; x++)                 printf("%c", board[x][y]);             printf("\n");         }         printf("menu\n 1 left \n2 right \n3 \n4 down \n");          int savex = xpos;         int savey = ypos;         int move = 0;         scanf("%d", &move);         char c;         while ((c = getchar()) != '\n' && c != eof);          switch (move)         {         case 1: if (xpos > 0) xpos--; break;         case 2: if (xpos < 7) xpos++; break;         case 3: if (ypos > 0) ypos--; break;         case 4: if (ypos < 7) ypos++; break;         }          //swap position:         board[savex][savey] = '.';         board[xpos][ypos] = '_';     }     return 0; } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -