用c改了个c++的五子棋,可是无法编译成功,求问题所在,附上源码!
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
enum Chess{ space, black, white };
constint n=15;
typedef struct
{
int x;
int y;
}point,*sp;
/*函数原型*/
voidputout( Chess F[][n]);
voidsourceMarks( double F[][n]);
voidudMarks( double numbers[][n],const Chess chessStates[][n]);
intselect();
spmaxpoint( double numbers[][n]);
voidputout(point ip);
intover( point ip,Chess chessStates[][n] );
spputin();
intlegitimacy( point ip,Chess chessStates[][n]);
/*main函数*/
void main()
{
int count=1;
ChesschessStates[n][n]={space};
doublenumbers[n][n]={0};
pointchesspoint1;
pointchesspoint2;
intchoice=select();
sourceMarks( numbers);
if( count==choice)putout( chessStates);/*先手输出棋盘*/
do
{
if( count==choice)
{
printf( "please put your chess: ");
chesspoint1=*putin();
chesspoint2=chesspoint1;
while( !legitimacy( chesspoint1,chessStates) )
{
printf( "please put again: ");
chesspoint1=*putin();
}
printf( "\n ");
chessStates[chesspoint1.x][chesspoint1.y]=white;
udMarks( numbers,chessStates );
}
else
{
chesspoint1=*maxpoint( numbers );
chessStates[chesspoint1.x][chesspoint1.y]=black;
}
output( chessStates );
if( count==choice )
{
if( count!=1)
{
printf( "your coordinate is: ");
output( chesspoint2 );
printf( "\n ");
}
printf( "the point of computer is: ");
output( chesspoint1 );
printf( "\n ");
}
printf( "\n ");
count++;
}while( !over( chesspoint1,chessStates ) && count <=n*n );
printf( "---------------------------------------------\n ");
if( count> n*n )
printf( "this is a draw/n ");
else
{
if( chessStates[chesspoint1.x][chesspoint1.y]==white)
printf( "Congratulations!YOU WIN!\n ");
else printf( "Sorry.YOU LOSE!\n ");
}
printf( "----------------------------------------------\n ");
}
/*输出棋盘*/
void putout(Chess F[][n])
{
for(int a=0;a <n;a++)
{
printf( " ' ' ");
printf( " ' '%d ",a+1);
}
printf( "\n ");
for(int i=0;i <n;i++)
{
printf( "%d ' ' '| ' ",i+1);
for(int j=0;j <n;j++)
{
switch( F[i][j])
{
case black:printf( " '@ ' '| ' ");
break;
case white:printf( " 'o ' '| ' ");
break;
case space:printf( " ' ' '| ' ");
break;
}
}
}
}
/*初始化棋盘分数*/
void sourceMarks( double F[][n] )
{
for( int i=0;i <n;i++)
for( int j=0;j <n;j++)
{
int sum=0;
for( int count=1;count <=4;count++)
{
if( i-count> =0 )
sum++;
if( j-count> =0 )
sum++;
if( i+count <n )
sum++;
if( j+count <n )
sum++;
if( i-count> =0 && j-count> =0 )
sum++;
if( i+count <n && j+count <n )
sum++;
if( i-count> =0 && j+count <n )
sum++;
if( i+count <n && j-count> =0 )
sum++;
}
F[i][j]=sum;
}
}/*中心点分数最高*/
/*更新棋盘分数*/
void udMarks( double numbers[][n], const Chess chessStates[][n] )
{
Chess beginStates;
Chess lastStates;
Chess temp;
int count;
double sum;
for( int i=0; i <n; i++)
for( int j=0; j <n; j++)
if( chessStates[i][j]!=space)
numbers[i][j]=0;
else
{
sum=0;
for( count=1; count <=4; count++ )
{
if( i-count> =0 )
sum++;
if( j-count> =0 )
sum++;
if( i+count <n )
sum++;
if( j+count <n )
sum++;
if( i-count> =0 && j-count> =0 )
sum++;
if( i+count <n && j+count <n )
sum++;
if( i-count> =0 && j+count <n )
sum++;
if( i+count <n && j-count> =0 )
sum++;
}
for( int z=1; z <=2; z++)
for( int k=1; k <=8; k++ )
{
int x=i;
int y=j;
int n=0;
int mark;
if( k%2==1)
temp=space;
for( count=1; count <=n; count++)
{
if( z==1 )
{
switch( k )
{
case 1:x--; break;
case 2:x++; break;
case 3:y--; break;
case 4:y++; break;
case 5:x--; y--; break;
case 6:x++; y++; break;
case 7:x++; y--; break;
case 8:x--; y++; break;
}
}
else
{
switch( k )
{
case 1:x++; break;
case 2:x--; break;
case 3:y++; break;
case 4:y--; break;
case 5:x++; y++; break;
case 6:x--; y--; break;
case 7:x--; y++; break;
case 8:x++; y-- ; break;
}
}
if( !(x> =0 && x <n && y> =0 && y <n) )
break;
else
{
if( count==1)
{
beginStates=lastStates=chessStates[x][y];
if( temp==beginStates && temp!=space)
n++;
if( temp!=beginStates && temp!=space && beginStates!=space)
n--;
temp=beginStates;
if( beginStates==space )
mark=5;
else mark=8;
}
if( chessStates[x][y]!=lastStates && lastStates!=space )
{
if( chessStates[x][y]!=space )
n--;
break;
}
if( chessStates[x][y]!=space )
{
n++;
lastStates=chessStates[x][y];
}
if( n==4 )
break;
}
}
if( !(x> =0 && x <n && y> =0 && y <n) && n> 0)
n--;
sum=sum+(mark*pow(3,n)-mark)/2+3*n;
}
numbers[i][j]=sum;
}
/*---------------------------*/
spmaxpoint( double numbers[0][n])
{
point*tp;
intmax_number;
intmax_number=numbers[0][0];
for( int i=0;i <n;i++)
for( int j=0;j <n;j++)
if( numbers[i][j]> =max_number)
{
max_number=numbers[i][j];
}
tp=(point *)malloc( sizeof (point) );
return (tp);
}
spputin()
{
point *ip;
char i,j;
printf( "下子请输入坐标: ");
scanf(&i,&j);
while( !( i> = '1 '&&i <= '15 ')||!( j> = '1 '&&j <= '15 '))
{
printf( "输入坐标有误,请重新输入: ");
scanf(&i,&j);
}
ip=(point *)malloc( sizeof (point) );
ip-> x=int(i);
ip-> y=int(j);
return (ip);
}
/*输入下棋坐标*/
void putout( point ip)
{
printf( " '[ '%c '] ' '[ '%c '] ' ",ip.x+1,ip.y+1);
}
/*游戏开始选择先后手*/
int select()
{
char c;
printf( "Please select a choice!\n\n1.先手 2.后手\n ");
c=getchar();
while(!(c== '1 '||c== '2 '))c=getchar();
return (c);
}
/*棋子坐标合法判定*/
int iegitimacy( point ip,Chess chessStates[][n])
{
int i=ip.x;
int j=ip.y;
if( i <0 || i> =n || j <0 || j> =n || chessStates[i][j]!=space )
return (-1);
return (1);
}
/*判断输赢*/
int over( point ip,Chess chessStates[n][n] )
{
Chess temp=chessStates[ip.x][ip.y];
int count;
for( int k=1; k <=8;k++)
{
int i=ip.x;
int j=ip.y;
if( k%2==1 )count=0;
while( 1 )
{
switch( k )
{
case 1: i++; break;
case 2: i--; break;
case 3: j++; break;
case 4: j--; break;
case 5: i--; j--; break;
case 6: i++; j++; break;
case 7: i++; j--; break;
case 8: i--; j++; break;
}
if( i> =0 && i <n && j> =0 && j <n )
{
if( chessStates[i][j]==temp )
count++;
else break;
}
else break;
if( count==4 )
return (1);
}
}
return (-1);
}
[解决办法]
太多C++语法没有改过来了,不知道你对C语言熟悉多少。
举例而言:
enum Chess {...};
在C++中可以把Chess当成一个数据类型来使用,但在C中就不可以,可以用
typedef enum {...} Chess;
又如
const int n=15;
这个在C++中就是一个真正的常量,可以用于数组的声明,但在C中就不可以,可以用
enum {n = 15};
或
#define n 15;
类似的问题还有很多,你按着编译器的指示一点一点改吧。