小游戏
三子棋大家应该都不陌生吧
三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉棋、一条龙、井字棋等。
我们今天来一个c语言三子棋的进阶,n子棋的实现过程
1.我们首先可以创建一个三子棋.c源文件,从main函数开始,作为代码运行入口是前提开始写代码。
2.在创建一个game.c源文件来实现游戏的具体过程代码。
3再来一个game.h头文件来包含声明自己写的函数。
游戏规则:
在一个棋盘上,两方选手对弈,先将三个棋连成一条线的一方获胜(包括斜线)
下面来具体看下实现代码
三子棋.c
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void menu(void)
{printf("*********************\n");printf("*******1. play*******\n");printf("*******0. quit*******\n");printf("*********************\n");
}void game()
{int cow = 0, rol = 0;scanf("%d %d", &cow, &rol);char board[COW][ROL];init_board(board, cow, rol); //初始化数组(棋盘)print_board(board, cow, rol); //打印棋盘 while (1){play_game(board, cow, rol); //玩家下print_board(board, cow, rol);//判断输赢 返回'#'玩家赢 ‘*’电脑赢,‘P’平局,‘C’继续游戏char ret = re_board(board, cow, rol);if ( ret == '#'){printf("玩家赢了\n");break;//}else if (ret == 'C'){printf("电脑下棋\n");}else if(ret == 'P'){printf("平局\n");break;}comp_game(board, cow, rol); //电脑下print_board(board, cow, rol);if( re_board(board, cow, rol)== '*'){printf("电脑赢了\n");break;}else if (re_board(board, cow, rol) == 'C'){printf("玩家下棋\n");}else if(re_board(board, cow, rol) == 'P'){printf("平局\n");break;}}
}int main()
{srand(time(NULL));int input = 0;do{menu();scanf("%d", &input);switch (input){case 1:game();break;case 0:printf("已退出游戏\n");break;default:printf("输入错误\n");break;}} while (input);return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void init_board(char board[COW][ROL], int cow, int rol)
{for(int i=0; i<cow; i++){for(int j=0; j<rol; j++){board[i][j] = ' ';}}
}void print_board(char board[COW][ROL], int cow, int rol)
{for(int i=0; i<cow; i++){for(int j=0; j<rol; j++){printf(" %c ", board[i][j]);if(j < rol-1){printf("|");}}printf("\n");for(int j=0; j<rol; j++){if(i < cow - 1){printf("---");//}if (i < cow - 1 && j < rol - 1){printf("|");}}printf("\n");}
}void play_game(char board[COW][ROL], int cow, int rol)
{printf("请输入下的坐标\n");int x = 0, y = 0;while (1){scanf("%d %d", &x, &y);if (x >= 1 && x <= cow && y >= 1 && y <= rol){if (board[x - 1][y - 1] = ' '){board[x - 1][y - 1] = '#';break;}else{printf("坐标被占用,请重新输入\n");}}else{printf("非法坐标\n");}}
}void comp_game(char board[COW][ROL], int cow, int rol)
{while (1){int x = rand() % cow;int y = rand() % rol;if (board[x][y] == ' '){board[x][y] = '*';break;}}
}char re_board(char board[COW][ROL], int cow, int rol)
{//行判断int count1 = 0;int count2 = 0;for(int i=0; i<cow; i++){for(int j=0; j<rol - 1; j++){if ((board[i][j]) == (board[i][j + 1]) && board[i][j] == '#'){count1++;}if (board[i][j] == board[i][j + 1] && board[i][j] == '*'){count2++;} } if(count1 == (rol - 1)){return '#';}if(count2 == (rol - 1)){return '*';}}//列判断int count3 = 0;int count4 = 0;for(int j=0; j<rol; j++){for(int i=0; i<cow - 1; i++){if (board[i][j] == board[i + 1][j] && board[i][j] == '#'){count3++;}if (board[i][j] == board[i + 1][j] && board[i][j] == '*'){count4++;} } if(count3 == (cow - 1)){return '#';}if(count4 == (cow - 1)){return '*';}}//对角线判断int count5 = 0;int count6 = 0;for(int i=0,j=0; (i < cow-1 && j < rol-1); i++,j++){if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '#'){count5++;}if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*'){count6++;} }if(count5 == cow-1){return '#';}if(count6 == cow-1){return '*';}//对角线2判断int count7 = 0;int count8 = 0;for(int i=0,j=2; (i < cow-1 && j > 0); i++,j--){if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '#'){count7++;}if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '*'){count8++;}}if(count7 == cow-1){return '#'; } if(count8 == cow-1){return '*'; }//判断平局int count = 0;for(int i=0; i<cow; i++){for(int j=0; j<rol; j++){if(board[i][j] != ' '){count++;}}}if(count == (cow*rol)){return 'P';}//继续游戏return 'C';
}
game.h
#include<stdio.h>#define COW 3
#define ROL 3
//void init_board(char board[COW][ROL], int cow, int rol);void print_board(char board[COW][ROL], int cow, int rol);void play_game(char board[COW][ROL], int cow, int rol);void comp_game(char board[COW][ROL], int cow, int rol);char re_board(char board[COW][ROL], int cow, int rol);
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689428972a246708.html
评论列表(0条)