You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
int X [ ] = { ' _ ' , ' _ ' , ' _ ' , ' _ ' , ' _ ' , ' _ ' , ' _ ' , ' _ ' , ' _ ' } , shunt = 1 , f ;
void map ( )
{
int i , j ;
system ( " cls " ) ;
for ( i = 0 ; i < 9 ; i + + )
{
if ( i = = 2 | | i = = 5 | | i = = 8 )
printf ( " %c \n " , X [ i ] ) ;
else
printf ( " %c| " , X [ i ] ) ;
}
}
int win ( int f , int a [ ] )
{
if ( a [ 0 ] + a [ 1 ] + a [ 2 ] = = 3 * ' O ' | |
a [ 3 ] + a [ 4 ] + a [ 5 ] = = 3 * ' O ' | | a [ 6 ] + a [ 7 ] + a [ 8 ] = = 3 * ' O ' | |
a [ 0 ] + a [ 3 ] + a [ 6 ] = = 3 * ' O ' | | a [ 1 ] + a [ 4 ] + a [ 7 ] = = 3 * ' O ' | |
a [ 2 ] + a [ 5 ] + a [ 8 ] = = 3 * ' O ' | | a [ 0 ] + a [ 4 ] + a [ 8 ] = = 3 * ' O ' | |
a [ 2 ] + a [ 4 ] + a [ 6 ] = = 3 * ' O ' )
f = 1 ;
else if ( a [ 0 ] + a [ 1 ] + a [ 2 ] = = 3 * ' X ' | |
a [ 3 ] + a [ 4 ] + a [ 5 ] = = 3 * ' X ' | | a [ 6 ] + a [ 7 ] + a [ 8 ] = = 3 * ' X ' | |
a [ 0 ] + a [ 3 ] + a [ 6 ] = = 3 * ' X ' | | a [ 1 ] + a [ 4 ] + a [ 7 ] = = 3 * ' X ' | |
a [ 2 ] + a [ 5 ] + a [ 8 ] = = 3 * ' X ' | | a [ 0 ] + a [ 4 ] + a [ 8 ] = = 3 * ' X ' | |
a [ 2 ] + a [ 4 ] + a [ 6 ] = = 3 * ' X ' )
f = 2 ;
else
f = 0 ;
return f ;
}
void shuru_number ( )
{
int a , b , number ;
scanf ( " %d " , & number ) ;
while ( number < 1 | | number > 9 | | X [ number - 1 ] ! = ' _ ' ) \
{
printf ( " 非法输入,请重新输入 \n " ) ;
scanf ( " %d " , & number ) ;
map ( ) ;
}
if ( shunt % 2 ! = 0 )
{
a = number ;
X [ a - 1 ] = ' O ' ;
map ( ) ;
shunt + + ;
}
else
{
b = number ;
X [ b - 1 ] = ' X ' ;
map ( ) ;
shunt + + ;
}
}
int timex ( )
{
int i , v ;
time_t t ;
srand ( ( unsigned ) time ( & t ) ) ;
v = rand ( ) % 10 ;
return v ;
}
int computer ( )
{
map ( ) ;
int nn ;
int v , i ;
printf ( " 先手or后手 \n 输入1or2选择 \n " ) ;
scanf ( " %d " , & nn ) ;
if ( nn = = 1 )
for ( i = 0 ; i < 5 ; i + + )
{
map ( ) ;
int a , b , number ;
scanf ( " %d " , & number ) ;
while ( number < 1 | | number > 9 | | X [ number - 1 ] ! = ' _ ' )
{
printf ( " 非法输入,请重新输入 \n " ) ;
scanf ( " %d " , & number ) ;
map ( ) ;
}
a = number ;
X [ a - 1 ] = ' O ' ;
map ( ) ;
if ( win ( f , X ) )
{
f = win ( f , X ) ;
break ;
}
if ( i < 4 )
{
v = timex ( ) ;
while ( v < 1 | | v > 9 | | X [ v - 1 ] ! = ' _ ' )
{
v = timex ( ) ;
map ( ) ;
}
X [ v - 1 ] = ' X ' ;
map ( ) ;
}
}
else
{
for ( i = 0 ; i < 5 ; i + + )
{
v = timex ( ) ;
while ( v < 1 | | v > 9 | | X [ v - 1 ] ! = ' _ ' )
{
v = timex ( ) ;
map ( ) ;
}
X [ v - 1 ] = ' X ' ;
map ( ) ;
if ( win ( f , X ) )
{
f = win ( f , X ) ;
break ;
}
if ( i < 4 )
{
map ( ) ;
int a , b , number ;
scanf ( " %d " , & number ) ;
while ( number < 1 | | number > 9 | | X [ number - 1 ] ! = ' _ ' )
{
printf ( " 非法输入,请重新输入 \n " ) ;
scanf ( " %d " , & number ) ;
map ( ) ;
}
a = number ;
X [ a - 1 ] = ' O ' ;
map ( ) ;
}
if ( win ( f , X ) )
{
f = win ( f , X ) ;
break ;
}
}
}
return 0 ;
}
int main ( )
{
int i , n ;
int q ;
f = 0 ;
printf ( " 这是一个会掉头发的井字棋游戏 \n " ) ;
printf ( " 输入‘ 1or2’ 选择 \n 1.人机对战 \n 2.玩家对战 \n " ) ;
scanf ( " %d " , & q ) ;
if ( q = = 2 )
{
map ( ) ;
printf ( " 输入数字1~9, 进行下棋哦 \n " ) ;
for ( i = 0 ; i < 9 ; i + + )
{
shuru_number ( ) ;
if ( win ( f , X ) )
{
f = win ( f , X ) ;
break ;
}
}
}
else
{
computer ( ) ;
}
if ( f = = 1 )
printf ( " O胜利 \n " ) ;
else if ( f = = 2 )
printf ( " X胜利 \n " ) ;
else
printf ( " 平局 \n " ) ;
getchar ( ) ;
return 0 ;
}