经过对上一次版本的优化,本次版本修复了之前的bug,并且美化了界面,增强了交互性。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
int t = 2, xx = 1;
int pan[20][20] = {
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6},
{7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9}
};
void menu();
void play();
void qp();
void test();
void run();
void over(int a);
void menu()//目录
{
system("cls");char cho;
printf("\n\n\t #################################\n");
printf("\t\t *五子棋小游戏*\n");
printf("\t\t *1.双人游戏*\n");
printf("\t\t *2.人机对战(暂未开放)\n");
printf("\t\t *3.游戏须知\n");
printf("\t #################################\n");
printf("\t 请选择选项,目前仅有 1 选项\n\n");
cho = _getch();
switch (cho){
case '1':run();break;
case '3':system("cls");
printf("本游戏是键盘输入坐标来落子的,白棋先落,第二次落子为黑色,依次落子。\n");
printf("若落子坐标非法则需重新落子。\n");
printf("键盘输入1直接进入游戏\n");Sleep(10000); menu();break;
default:menu(); break;
}
}
void over(int a)
{
if (a == 2) {
printf("\n\n\n\t\t恭喜恭喜,黑棋获胜!!!\n\t\t恭喜恭喜,黑棋获胜!!!\n\t\t恭喜恭喜,黑棋获胜!!!\n");
}
else if (a == 1){
printf("\n\n\n\t\t恭喜恭喜,白棋获胜!!!\n\t\t恭喜恭喜,白棋获胜!!!\n\t\t恭喜恭喜,白棋获胜!!!\n");
}
xx = 2;Sleep(99999999999);
}
void qp()//棋盘
{
int i, j, k = 0;
printf("\n\t\t五子棋小游戏\n\n ");
for (i = 0; i < 9; i++)
printf(" %d", i + 1);
for (i = 9; i < 20; i++)
printf("%d", i + 1);
printf("\n 1 ");
for (i = 0; i < 20; i++) {//printf("┌ ┬ ┐├ ┼ ┤└ ┴ ┘│ ○ ●");
for (j = 0; j < 20; j++) {
switch (pan[i][j]) {
case 1:printf("┌ "); k++; break;
case 2:printf("┬ "); k++; break;
case 3:printf("┐ "); k++;
if (k % 20 == 0) {
printf("%d\n %d ", i + 1, i + 2);
}
break;
case 4:printf("├ "); k++; break;
case 5:printf("┼ "); k++; break;
case 6:printf("┤ "); k++;
if (k % 20 == 0) {
printf("%d\n", i + 1);
if (i < 8)
printf(" %d ", i + 2);
if (i > 7)
printf(" %d", i + 2);
}
break;
case 7:printf("└ "); k++; break;
case 8:printf("┴ "); k++; break;
case 9:printf("┘ "); k++;
if (k % 20 == 0) {
printf("%d\n", i + 1);
}
break;
case 10:printf("○"); k++;
if (k % 20 == 0) {
printf("%d\n", i + 1);
}
break;
case 11:printf("●"); k++;
if (k % 20 == 0) {
printf("%d\n", i + 1);
}
break;
}
}
}
printf(" ");
for (i = 0; i < 9; i++)
printf(" %d", i + 1);
for (i = 9; i < 20; i++)
printf("%d", i + 1);
}
void play()
{
int i, j;
while (1)
{
printf("\n\n *请选择落子坐标(x,y): ");
scanf_s("%d %d", &j, &i);
printf("\n");
if (i > 0 && i < 21 && j>0 && j < 21){
if (pan[i - 1][j - 1] != 10 && pan[i - 1][j - 1] != 11){
if (t % 2 == 1){
t++;
pan[i - 1][j - 1] = 11;
break;
}
else if (t % 2 == 0){
t++;
pan[i - 1][j - 1] = 10;
break;
}
}
else if (pan[i - 1][j - 1] == 10 || pan[i - 1][j - 1] == 11){
run();
continue;
}
}
else if (i < 1 || i > 21 || j < 1 || j > 21){
run();
continue;
}
}
}
void test()
{
int i, j;
for (i = 0; i < 20; i++){
for (j = 0; j < 20; j++) {
if (pan[i][j] != 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9) {
if ( (pan[i][j] == pan[i][j + 1] && pan[i][j] == pan[i][j + 2] && pan[i][j] == pan[i][j + 3] && pan[i][j] == pan[i][j + 4])
|| (pan[i][j] == pan[i + 1][j] && pan[i][j] == pan[i + 2][j] && pan[i][j] == pan[i + 3][j] && pan[i][j] == pan[i + 4][j])
|| (pan[i][j] == pan[i + 1][j + 1] && pan[i][j] == pan[i + 2][j + 2] && pan[i][j] == pan[i + 3][j + 3] && pan[i][j] == pan[i + 4][j + 4])
|| (pan[i][j] == pan[i - 1][j + 1] && pan[i][j] == pan[i - 2][j + 2] && pan[i][j] == pan[i - 3][j + 3] && pan[i][j] == pan[i - 4][j + 4]))
{// &&pan[i][j]
if (pan[i][j] == 11) {
over(2);
}
if (pan[i][j] == 10) {
over(1);
}
}
}
}
}
if (xx == 1) {
play();
}
}
void run()
{
system("cls");
while (1)
{
qp();
test();
system("cls");
}
}
int main()
{
system("color e2");//e2 第一个字符为背景色,第二个字体为字体色,十六进制。
system("mode con cols=50 lines=32");
SetConsoleTitleA("五子棋小游戏"); //设置窗口 system("title 五子棋");
menu();
system("pause");
return 0;
}