| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 312 人关注过本帖
标题:求指点 const 使用问题
只看楼主 加入收藏
lanskyxti
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2014-8-29
结帖率:80%
收藏
已结贴  问题点数:6 回复次数:2 
求指点 const 使用问题
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#include<string.h>

const int SIZE=6;
const char comp_c='@';
//char const player_c='O';
const char player_c='O';//程序报错起始位置

void display(char board[][SIZE]);
int valid_moves(char board[][SIZE],bool moves[][SIZE],char player);
void make_move(char board[][SIZE],int row,int col,char player);
void computer_move(char board[][SIZE],bool moves[][SIZE],char player);
int best_move(char board[][SIZE],bool moves[][SIZE],char player);
int get_score(char board[][SIZE],char player);
int main(void)
{
    char board[SIZE][SIZE]={0};
    bool moves[SIZE][SIZE]={false};
    int row=0;
    int col=0;
    int no_of_games=0;
    int no_of_moves=0;
    int invalid_moves=0;
    int comp_score=0;
    int user_score=0;
    char y=0;
    int x=0;
    char again=0;
    bool next_player=true;
   
    do
    {
         next_player=!next_player;
         no_of_moves=4;
         for(row=0;row<SIZE;row++)
           for(col=0;col<SIZE;col++)
             board[row][col]=' ';
         board[SIZE/2-1][SIZE/2-1]=board[SIZE/2][SIZE/2]='O';
         board[SIZE/2-1][SIZE/2]=board[SIZE/2][SIZE/2-1]='@';
         /*int mid=SIZE/2;
         board[mid-1][mid-1]=board[mid][mid]=player_c;
         board[mid-1][mid]=board[mid][mid-1]=comp_c;*/
        
         do
         {
           display(board);
           if(next_player=!next_player)
           {
             if(valid_moves(board,moves,player_c))
             {
                 for( ; ; )
                {
                 printf("Please enter your move (row column):");
                 scanf("%d%c",&x,&y);
                 y=tolower(y)-'a';
                 x--;
                 if(x>=0&&y>=0&&x<SIZE&&y<SIZE&&moves[x][y])
                 {
                     make_move=(board,x,y,player_c);//程序报错,const char无法转换成void
                    no_of_moves++;
                    break;
                 }
                 else
                   printf("Not a valid move,try again.\n");
                }
             }
             else
             if(++invalid_moves<2)
             {
                 printf("\nYou have to pass,press return");
                 scanf("%c",&again);
             }
             else
               printf("\nNeither of us can go ,so the game is over.\n");
             }
           else
           {
             if(valid_moves(board,moves,'@'))
             {
               invalid_moves=0;
               computer_move(board,moves,'@');
               no_of_moves++;
             }
             else
             {
               if(++invalid_moves<2)
                printf("\nI have to pass,your go\n");
               else
               printf("\nNeither of us can go,so the game is over.\n");
             }
           }     
         }while(no_of_moves<SIZE*SIZE&&invalid_moves<2);
         display(board);
         comp_score=user_score=0;
         for(row=0;row<SIZE;row++)
           for(col=0;col<SIZE;col++)
           {
             comp_score +=board[row][col]==comp_c;
             user_score +=board[row][col]==player_c;
           }
         printf("The final score is:\n");
         printf("Computer %d\n User %d\n\n",comp_score,user_score);
         printf("Do you want to play again (y/n):");
         scanf("%c",&again);
    }while(tolower(again)=='y');
    printf("\nGoodbye\n");
    //system("pause");
    return 0;
}
void display(char board[][SIZE])
{
     int row=0;
     int col=0;
     char col_label='a';
     printf("\n");
     for(col=0;col<SIZE;col++)
        printf(" %c",col_label+col);
     printf("\n");
     for(row=0;row<SIZE;row++)
     {
        printf(" +");
        for(col=0;col<SIZE;col++)
          printf("---+");
        printf("\n%2d|",row+1);
        for(col=0;col<SIZE;col++)
          printf(" %c |",board[row][col]);
        printf("\n");
     }
     printf(" +");
     for(col=0;col<SIZE;col++)
       printf("---+");
     printf("\n");
}
int valid_moves(char board[][SIZE],bool moves[][SIZE],char player)
{
    int rowdelta=0;
    int coldelta=0;
    int x=0;
    int y=0;
    int no_of_moves=0;
    int row=0;
    int col=0;
    char opponent=(player==player_c)?comp_c:player_c;
    for(row=0;row<SIZE;row++)
       for(col=0;col<SIZE;col++)
          moves[row][col]=false;
         
    for(row=0;row<SIZE;row++)
       for(col=0;col<SIZE;col++)
       {
          if(board[row][col] !=' ')
            continue;
          for(rowdelta=-1;rowdelta<=1;rowdelta++)
            for(coldelta=-1;coldelta<=1;coldelta++)
            {
               if(row+rowdelta<0||row+rowdelta>=SIZE||col+coldelta<0||col+coldelta>=SIZE||(rowdelta==0&&coldelta==0))
               continue;
               if(board[row+rowdelta][col+coldelta]==opponent)
               {
                 x=row+rowdelta;
                 y=col+coldelta;
                 for(;;)
                 {
                    x +=rowdelta;
                    y +=coldelta;
                    if(x<0||x>=SIZE||y<0||y>=SIZE)
                      break;
                    if(board[x][y]==' ')
                      break;
                    if(board[x][y]==player)
                    {
                      moves[row][col]=true;
                      no_of_moves++;
                      break;
                    }
                 }
               }
            }
       }
    return no_of_moves;
}
void make_move(char board[][SIZE],int row,int col,char player)
{
     int rowdelta=0;
     int coldelta=0;
     int x=0;
     int y=0;
    
     char opponent=(player==player_c)?comp_c:player_c;
     board[row][col]=player;
     for(rowdelta=-1;rowdelta<=1;rowdelta++)
       for(coldelta=-1;coldelta<=1;coldelta++)
       {
         if(row+rowdelta<0||row+rowdelta>=SIZE||col+coldelta<0||col+coldelta>=SIZE||(rowdelta==0&&coldelta==0))
           continue;
         if(board[row+rowdelta][col+coldelta]==opponent)
         {
           x=row+rowdelta;
           y=col+coldelta;
           for(;;)
           {
             x +=rowdelta;
             y +=coldelta;
             if(x<0||x>=SIZE||y<0||y>=SIZE)
               break;
             if(board[x][y]==' ')
               break;
             if(board[x][y]==player)
             {
               while(board[x-=rowdelta][y-=coldelta]==opponent)
               board[x][y]=player;
               break;
             }
           }
         }
       }
}
void computer_move(char board[][SIZE],bool moves[][SIZE],char player)
{
     int best_row=0;
     int best_col=0;
     int new_score=0;
     int score=100;
     int row=0;
     int col=0;
     char temp_board[SIZE][SIZE];
     bool temp_moves[SIZE][SIZE];
     char opponent=(player==player_c)?comp_c:player_c;
     for(row=0;row<SIZE;row++)
       for(col=0;col<SIZE;col++)
       {
         if( !moves[row][col])
           continue;
         memcpy(temp_board,board,sizeof(temp_board));
         make_move(temp_board,row,col,player);
         valid_moves(temp_board,temp_moves,opponent);
         new_score=best_move(temp_board,temp_moves,opponent);
         if(new_score<score)
         {
           score=new_score;
           best_row=row;
           best_col=col;
         }
       }
     make_move(board,best_row,best_col,player);
}
int get_score(char board[][SIZE],char player)
{
    int score=0;
    int row=0;
    int col=0;
    char opponent=(player==player_c)?comp_c:player_c;
    for(row=0;row<SIZE;row++)
       for(col=0;col<SIZE;col++)
       {
         score -=board[row][col]==opponent;
         score +=board[row][col]==player;
       }
    return score;
}
int best_move(char board[][SIZE],bool moves[][SIZE],char player)
{
    char opponent=(player==player_c)?comp_c:player_c;
    char new_board[SIZE][SIZE]={0};
    int score=0;
    int new_score=0;
    int row=0;
    int col=0;
    for(row=0;row<SIZE;row++)
       for(col=0;col<SIZE;col++)
       {
         if(!moves[row][col])
         continue;
         memcpy(new_board,board,sizeof(new_board));
         make_move(new_board,row,col,player);
         new_score=get_score(new_board,player);
         if(score<new_score)
           score=new_score;
       }
    return score;
}
像我这个应该怎样去改,const有什么注意使用的规定呢?
搜索更多相关主题的帖子: color 
2014-11-03 23:58
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
const的位置不同,意义也不一样,你注释掉的那行当然报错。

授人以渔,不授人以鱼。
2014-11-04 00:20
书生等待
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:280
专家分:689
注 册:2013-2-22
收藏
得分:6 
make_move=(board,x,y,player_c);//程序报错,const char无法转换成void
我vs2010编译也是这句报错,但是报错是赋值操作符左操作数为表达式 error C2659: '=' : function as left operand
make_move是函数名,怎么给他赋值的
2014-11-04 09:22
快速回复:求指点 const 使用问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026789 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved