当前位置:网站首页>Mobile console Gobang (first draft of detailed design)

Mobile console Gobang (first draft of detailed design)

2022-06-11 07:12:00 lihongtao8209

1. Program code

/*1.vboard[x][y] x Represents an array row ,Y Represents an array column   The index is based on 0 2. Lines entered in the console 、 Columns are based on 1 3. The program is in Huawei NOVA7 Development 、 debugging  4.C The compiler must support UTF-8 */
#include <stdio.h>
#define SIZE 15
#define HEI 0X48 //'H'
#define BAI 0X42 //'B'
#define BLK '0'
#define NUL 0
char *pos="┼";
char *black="●";
char *white="○";
int  step=0;
int  count=0;
int  qizi[2]={
    HEI,BAI};
int  record[SIZE*SIZE]= {
    -1}; // Record 
char vboard[SIZE][SIZE+1];
int  players[2];// Record who is black and who is white 
// initialization ;
void init();
// Show chessboard 
void showConsole();
// Show virtual checkerboard 
void debugVboard();
// All at once 
void inputStep();
// Chess piece record 
void recordStep(int x,int y);
// Print chess piece records 
void printRecord();
void setValue(int x,int y,int v);
// Take a move 
int  player(int wc);
// Take turns playing chess 
void alternatePlay();
int main()
{
    
    init();
    initVboard();
    alternatePlay();
    /* setValue(15,15,HEI); setValue(14,15,BAI); showConsole(); */
    //debugVboard();
    //searchVboard();
    return 0;
}
init()
{
    
  count=0;
}
void showConsole()
{
    
    char ind[16]="123456789ABCDEF\0";
    char   *ind2="ABCDEFGHIJKLMNO";

    for(int i=0; i<SIZE; i++)
    {
    
// Print left column index 
        printf("%c",ind[i]);
// Print chess pieces 
        for(int j=0; j<SIZE; j++)
        {
       if(vboard[i][j]=='*')
                printf("%s ",pos);
            else if(vboard[i][j]=='H')
                printf("%s ",black);
            else if(vboard[i][j]=='B')
                printf("%s ",white);
            else printf("%s ",pos);
        }
        printf("\n");
    }
// Print the bottom row index 
    for(int k=0; k<SIZE; k++)
    {
    
        printf("%2c",*(ind2+k));
    }
    putchar('\n');
}

void initVboard()
{
    
    for(int i=0; i<SIZE; i++)
    {
    
        for(int j=0; j<SIZE; j++)
        {
    
            vboard[i][j]='*';
        }
        vboard[i][SIZE]=0;
    }
}

void debugVboard()
{
    
    for(int i=0; i<SIZE; i++)
    {
    
        for(int j=0; j<SIZE; j++)
        {
    
            printf("%2c",vboard[i][j]);
        }
        putchar('\n');
    }
}
// The index is based on 1
void setValue(int x,int y,int v)
{
    
    vboard[x-1][y-1]=v;
}

void printRecord()
{
    
    for(int i=0; i<SIZE; i++)
    {
    
        for(int j=0; j<SIZE; j++)
        {
       // Black chess 
            if(vboard[i][j]=='H')
            {
    
                printf("● ROW:%d COL:%d\n",i+1,j+1);
            }
            // White chess 
            if(vboard[i][j]=='B')
            {
    
                printf("○ ROW:%d COL:%d\n",i+1,j+1);
            }
        }
    }
}
// Record... In a one-dimensional array 225 Step 
void recordStep(int x,int y)
{
     
    int i;
    i=step;
    if(i<225 && record[i]==-1)
    {
    
        record[i]=15*x+y;
        printf("%d %d %d\n",step,record[i],__LINE__);
        step++;
    }
    
}
// All at once 
void inputStep()
{
    
    int whofirst=-1;
    while(whofirst<0)
    {
    
        printf("1 Bai Xian ,2 Heixian , Please select :");
        scanf("%d",whofirst);
    }
    switch(whofirst)
    {
    
    case 2:
     {
       // Default black first 
        players[0]=HEI;
        players[1]=BAI;
        break;
     }
    default:
     {
    
        players[0]=BAI;
        players[1]=HEI;
     }
    }
}
// Black and white 
int player(int wc)
{
    
    int x,y;
    printf("%d",count+1);
    if(wc==HEI) 
    printf("%srow,column:",black);
    else if(wc==BAI)
    printf("%srow,column:",white);
    else{
    }
    scanf("%d,%d",&x,&y);
    getchar();
    //printf("ascci=%d",c);
    if((x>0&&x<=15)&&(y>0&&y<=15))
    {
    
      setValue(x,y,wc);
      //recordStep(x,y);
      return 0;
    }
    else
    {
    }
    return 1;
}
// Take turns playing chess 
void alternatePlay()
{
    
   int flag=0;
   int alter=0;
   int heibai;
   while(flag==0)
   {
    
      showConsole();
      alter=count%2;
      heibai=qizi[alter];
      flag=player(heibai);
      count++;
   }
}
    

2. Console input

2.1 Enter black chess

 Enter black chess
First step , stay 15 That's ok 15 Column input black chess . The format is :15,15 enter .

2.2 Enter white

 Wait for white chess
For example 14 That's ok 15 Column enter white . The format is :14,15 enter .

原网站

版权声明
本文为[lihongtao8209]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020522382556.html