//C语言 三子棋 小游戏
//棋盘为3x3大小,电脑棋子 X ,玩家 O ;
//用命令窗口模拟3子棋,玩家与电脑轮流下,先连成3子判赢;无路可走判平局
#include<stdio.h>
#include<string.h>
#include<windows.h>
int judge_is_moved(char arr[][12], int place);
void print_chessboard(char arr[][12])//输出棋盘
{
int i = 0, j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 12; j++)
{
if (arr[i][j] == ‘\0‘)
printf("\n");
else
printf("%c", arr[i][j]);
}
}
}
void chess_move(char arr[][12],int place)//选手棋盘落子
{
switch (place)
{
case 1:
arr[0][2] = ‘O‘;
break;
case 2:
arr[0][6] = ‘O‘;
break;
case 3:
arr[0][10] = ‘O‘;
break;
case 4:
arr[2][2] = ‘O‘;
break;
case 5:
arr[2][6] = ‘O‘;
break;
case 6:
arr[2][10] = ‘O‘;
break;
case 7:
arr[4][2] = ‘O‘;
break;
case 8:
arr[4][6] = ‘O‘;
break;
case 9:
arr[4][10] = ‘O‘;
break;
}
print_chessboard(arr);
}
void cp_move(char arr[][12])//电脑计算、落子
{
//judge_is_moved(arr, i) ==1
int place=0;
int i=0;
if(arr[0][2]!=‘ ‘&&judge_is_moved(arr, 1) ==1)
{
place=1;
}
else{
for(i=1;i<10;i++)
{
if((i<4)&&(arr[0][2]!=‘O‘)&&(arr[0][6]!=‘O‘)
&&(arr[0][10]!=‘O‘)&&(judge_is_moved(arr, i) ==1))
{
place=i;
break;
}
else if((i>3)&&(i<7)&&(arr[2][2]!=‘O‘)&&(arr[2][6]!=‘O‘)
&&(arr[2][10]!=‘O‘)&&(judge_is_moved(arr, i) ==1))
{
place=i;
break;
}
else if((i>6)&&(arr[4][2]!=‘O‘)&&(arr[4][6]!=‘O‘)
&&(arr[4][10]!=‘O‘)&&(judge_is_moved(arr, i) ==1))
{
place=i;
break;
}
else
if(judge_is_moved(arr, i) ==1)
{
place=i;
break;
}
}
}
switch (place)
{
case 1:
arr[0][2] = ‘X‘;
break;
case 2:
arr[0][6] = ‘X‘;
break;
case 3:
arr[0][10] = ‘X‘;
break;
case 4:
arr[2][2] = ‘X‘;
break;
case 5:
arr[2][6] = ‘X‘;
break;
case 6:
arr[2][10] = ‘X‘;
break;
case 7:
arr[4][2] = ‘X‘;
break;
case 8:
arr[4][6] = ‘X‘;
break;
case 9:
arr[4][10] = ‘X‘;
break;
}
print_chessboard(arr);
}
int judge_end(char arr[][12])//判断游戏结束:0:无路可走 1:玩家赢 2:电脑赢
{
if ( ( arr[0][2] == ‘O‘&&arr[0][6] == ‘O‘&&arr[0][10]==‘O‘)
||( arr[2][2] == ‘O‘&&arr[2][6] == ‘O‘&&arr[2][10]==‘O‘)
||( arr[4][2] == ‘O‘&&arr[4][6] == ‘O‘&&arr[4][10]==‘O‘)
||( arr[0][2] == ‘O‘&&arr[2][6] == ‘O‘&&arr[4][10]==‘O‘)
||( arr[0][10] == ‘O‘&&arr[2][6] == ‘O‘&&arr[4][2]==‘O‘)
||( arr[0][2] == ‘O‘&&arr[2][2] == ‘O‘&&arr[4][2] ==‘O‘)
||( arr[0][6] == ‘O‘&&arr[2][6] == ‘O‘&&arr[4][6] ==‘O‘)
||( arr[0][10] == ‘O‘&&arr[2][10] == ‘O‘&&arr[4][10] ==‘O‘))
return 1;
if ( (arr[0][2] == ‘X‘&&arr[0][6] == ‘X‘&&arr[0][10] == ‘X‘)
|| (arr[2][2] == ‘X‘&&arr[2][6] == ‘X‘&&arr[2][10] == ‘X‘)
|| (arr[4][2] == ‘X‘&&arr[4][6] == ‘X‘&&arr[4][10] == ‘X‘)
|| (arr[0][2] == ‘X‘&&arr[2][6] == ‘X‘&&arr[4][10] == ‘X‘)
||( arr[0][10] == ‘X‘&&arr[2][6] == ‘X‘&&arr[4][2]==‘X‘)
||( arr[0][2] == ‘X‘&&arr[2][2] == ‘X‘&&arr[4][2] ==‘X‘)
||( arr[0][6] == ‘X‘&&arr[2][6] == ‘X‘&&arr[4][6] ==‘X‘)
||( arr[0][10] == ‘X‘&&arr[2][10] == ‘X‘&&arr[4][10] ==‘X‘))
return 2;
if ( (arr[0][2] != ‘ ‘&&arr[0][6] != ‘ ‘&&arr[0][10] != ‘ ‘)
&&(arr[2][2] != ‘ ‘&&arr[2][6] != ‘ ‘&&arr[2][10] != ‘ ‘)
&&(arr[4][2] != ‘ ‘&&arr[4][6] != ‘ ‘&&arr[4][10] != ‘ ‘))
return 0;
return -1;
}
int judge_is_moved(char arr[][12],int place)//判断落子位置是否空闲
{
switch (place)
{
case 1:
if (arr[0][2] == ‘ ‘)
return 1;
else
return 0;
case 2:
if (arr[0][6] == ‘ ‘)
return 1;
else
return 0;
case 3:
if (arr[0][10] == ‘ ‘)
return 1;
else
return 0;
case 4:
if (arr[2][2] == ‘ ‘)
return 1;
else
return 0;
case 5:
if (arr[2][6] == ‘ ‘)
return 1;
else
return 0;
case 6:
if (arr[2][10] == ‘ ‘)
return 1;
else
return 0;
case 7:
if (arr[4][2] == ‘ ‘)
return 1;
else
return 0;
case 8:
if (arr[4][6] == ‘ ‘)
return 1;
else
return 0;
case 9:
if (arr[4][10] == ‘ ‘)
return 1;
else
return 0;
default:
return 0;
}
}
int main()
{
char arr[5][12]={" | | ","---|---|---",
" | | ","---|---|---",
" | | "} ;
printf("***************** welcome to play!****************\n");
while(1)
{
int i=0;
char arr[5][12] = { " | | ", "---|---|---",
" | | ", "---|---|---",
" | | " };
printf("是否开始游戏?(0:否,1:是)");
flag1: scanf("%d",&i);
if(i==0)
{
printf("谢谢使用!\n");
return 0;
}
else if(i!=0&&i!=1)
{
printf("输入有误!请重新选择:");
goto flag1;
}
else
{
int j = 0;
print_chessboard(arr);
printf("游戏开始!选择先手还是后手(0:先,1:后)");
flag2: scanf("%d", &j);
if (j != 0 && j != 1)
{
printf("输入有误!请重新选择:");
goto flag2;
}
while (1)
{
int place = 0;
if (j == 0)
{
printf("请落子(输入1--9):");
flag3: scanf("%d", &place);
if (place<1 || place>9 || judge_is_moved(arr,place)==0)
{
printf("落子位置不合法!重新输入:");
goto flag3;
}
else
{
int end = -1;
chess_move(arr, place);
end = judge_end(arr);
if (end ==0)
{
printf("游戏结束!无路可走!平局!");
break;
}
if (end == 1)
{
printf("游戏结束!你赢了!");
break;
}
if (end == 2)
{
printf("游戏结束!你输了!");
break;
}
printf("-----等待电脑落子\n");
Sleep(1000);
cp_move(arr);
end=-1;
end = judge_end(arr);
if (end ==0)
{
printf("游戏结束!无路可走!平局!");
break;
}
if (end == 1)
{
printf("游戏结束!你赢了!");
break;
}
if (end == 2)
{
printf("游戏结束!你输了!");
break;
}
}
}
if (j == 1)
{
int end = -1;
printf("-----等待电脑落子\n");
Sleep(1000);
cp_move(arr);
end = judge_end(arr);
if (end == 0)
{
printf("游戏结束!无路可走!平局!");
break;
}
if (end == 1)
{
printf("游戏结束!你赢了!");
break;
}
if (end == 2)
{
printf("游戏结束!你输了!");
break;
}
printf("请落子(输入1--9):");
flag4: scanf("%d", &place);
if (place<1 || place>9 || judge_is_moved(arr,place) == 0)
{
printf("落子位置不合法!重新输入:");
goto flag4;
}
else
{
chess_move(arr, place);
end = judge_end(arr);
if (end == 0)
{
printf("游戏结束!无路可走!平局!");
break;
}
if (end == 1)
{
printf("游戏结束!你赢了!");
break;
}
if (end == 2)
{
printf("游戏结束!你输了!");
break;
}
}
}
}
}
}
return 0;
}原文地址:http://shaungqiran.blog.51cto.com/10532904/1678516