目录
C语言编程实现五子棋::
game.h
game.c
1.打印菜单
2.打印棋盘
3.玩家下棋
4.判断五子连珠
5.判断输赢
6.游戏运行
game.c完整源代码展示
test.c
C语言编程实现五子棋::
game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#define ROW 20
#define COL 20
#define PLAYER1 1
#define PLAYER2 2
#define NEXT 0
#define PLAYER1_WIN 1
#define PLAYER2_WIN 2
#define DRAW 3
enum Dir
{LEFT,RIGHT,UP,DOWN,LEFT_UP,LEFT_DOWN,RIGHT_UP,RIGHT_DOWN
};
void Menu();
void Game();
game.c
1.打印菜单
void Menu()
{printf("#############################\n");printf("####### 五子棋游戏 ######\n");printf("#######1.Play 0.Exit######\n");printf("#############################\n");printf("#############################\n");printf("Please Select:");
}
2.打印棋盘
void ShowBoard(int board[ROW][COL], int row, int col)
{//清屏system("cls");printf(" ");int i = 1;for (i = 1;i <= col;i++){printf("%3d", i);}printf("\n");for (i = 0;i < row;i++){int j = 0;printf("%2d ", i + 1);for (j = 0;j < col;j++){if (board[i][j] == 0){printf(" . ");}else if (board[i][j] == PLAYER1){printf(" # ");}else{printf(" o ");}}printf("\n");}
}
3.玩家下棋
void PlayerMove(int board[ROW][COL], int row, int col, int who)
{while (1){printf("Player[%d] Please Enter your Pos:", who);scanf("%d %d", &x, &y);if (x < 1 || x > row || y < 1 || y > col){printf("输入坐标不合法!\n");continue;}else if (board[x - 1][y - 1] != 0){printf("Pos Is Occupied!\n");continue;}else{board[x - 1][y - 1] = who;break;}}
}
4.判断五子连珠
int ChessCount(int board[ROW][COL], int row, int col,enum Dir d)
{int _x = x - 1;int _y = y - 1;int count = 0;while (1){switch (d){case LEFT:_y--;break;case RIGHT:_y++;break;case UP:_x--;break;case DOWN:_x++;break;case LEFT_UP:_x--;_y--;break;case LEFT_DOWN:_x++;_y--;break;case RIGHT_UP:_x--;_y++;break;case RIGHT_DOWN:_x++;_y++;break;default://Do Nothingbreak;}//不合法if (_x < 0 || _x > row - 1 || _y < 0 || _y > col - 1){break;}//合法if (board[x - 1][y - 1] == board[_x][_y]){count++;}else{break;}}return count;
}
5.判断输赢
int IsWin(int board[ROW][COL], int row, int col)
{//在当前位置(x,y)处int count1 = ChessCount(board, row, col, LEFT) + ChessCount(board, row, col, RIGHT) + 1;int count2 = ChessCount(board, row, col, UP) + ChessCount(board, row, col, DOWN) + 1;int count3 = ChessCount(board, row, col, LEFT_UP) + ChessCount(board, row, col, RIGHT_DOWN) + 1;int count4 = ChessCount(board, row, col, LEFT_DOWN) + ChessCount(board, row, col, RIGHT_UP)+ 1;if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5){if (board[x - 1][y - 1] == PLAYER1){return PLAYER1_WIN;}else{return PLAYER2_WIN;}}int i = 0;for (i = 0;i < row;i++){int j = 0;for (j = 0;i < col;j++){if (board[i][j] == 0){return NEXT;}}}return DRAW;
}
6.游戏运行
void Game()
{int board[ROW][COL];memset(board, 0, sizeof(board));int result = NEXT;do{ShowBoard(board, ROW, COL);PlayerMove(board, ROW, COL, PLAYER1);result = IsWin(board, ROW, COL);ShowBoard(board, ROW, COL);if (NEXT != result){break;}ShowBoard(board, ROW, COL);PlayerMove(board, ROW, COL, PLAYER2);result = IsWin(board, ROW, COL);if (NEXT != result){break;}} while (1);//用户1赢 用户2赢 平局switch (result){case PLAYER1_WIN:printf("恭喜用户1,你已经赢了!\n");break;case PLAYER2_WIN:printf("恭喜用户2,你已经赢了!\n");break;case DRAW:printf("平局,和气生财!\n");break;default:break;}system("pause");ShowBoard(board, ROW, COL);
}
game.c完整源代码展示:
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
int x = 0;
int y = 0;
void Menu()
{printf("#############################\n");printf("####### 五子棋游戏 ######\n");printf("#######1.Play 0.Exit######\n");printf("#############################\n");printf("#############################\n");printf("Please Select:");
}
//按照(x,y)作为起点,按照特定的方向,求连续相对的最大个数
int ChessCount(int board[ROW][COL], int row, int col,enum Dir d)
{int _x = x - 1;int _y = y - 1;int count = 0;while (1){switch (d){case LEFT:_y--;break;case RIGHT:_y++;break;case UP:_x--;break;case DOWN:_x++;break;case LEFT_UP:_x--;_y--;break;case LEFT_DOWN:_x++;_y--;break;case RIGHT_UP:_x--;_y++;break;case RIGHT_DOWN:_x++;_y++;break;default://Do Nothingbreak;}//不合法if (_x < 0 || _x > row - 1 || _y < 0 || _y > col - 1){break;}//合法if (board[x - 1][y - 1] == board[_x][_y]){count++;}else{break;}}return count;
}
//4种返回值 NEXT:继续 PLAYER1_WIN:用户1赢 PLAYER2_WIN:用户2赢 DRAW:平局
int IsWin(int board[ROW][COL], int row, int col)
{//在当前位置(x,y)处int count1 = ChessCount(board, row, col, LEFT) + ChessCount(board, row, col, RIGHT) + 1;int count2 = ChessCount(board, row, col, UP) + ChessCount(board, row, col, DOWN) + 1;int count3 = ChessCount(board, row, col, LEFT_UP) + ChessCount(board, row, col, RIGHT_DOWN) + 1;int count4 = ChessCount(board, row, col, LEFT_DOWN) + ChessCount(board, row, col, RIGHT_UP)+ 1;if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5){if (board[x - 1][y - 1] == PLAYER1){return PLAYER1_WIN;}else{return PLAYER2_WIN;}}int i = 0;for (i = 0;i < row;i++){int j = 0;for (j = 0;i < col;j++){if (board[i][j] == 0){return NEXT;}}}return DRAW;
}
void ShowBoard(int board[ROW][COL], int row, int col)
{//清屏system("cls");printf(" ");int i = 1;for (i = 1;i <= col;i++){printf("%3d", i);}printf("\n");for (i = 0;i < row;i++){int j = 0;printf("%2d ", i + 1);for (j = 0;j < col;j++){if (board[i][j] == 0){printf(" . ");}else if (board[i][j] == PLAYER1){printf(" # ");}else{printf(" o ");}}printf("\n");}
}
void PlayerMove(int board[ROW][COL], int row, int col, int who)
{while (1){printf("Player[%d] Please Enter your Pos:", who);scanf("%d %d", &x, &y);if (x < 1 || x > row || y < 1 || y > col){printf("输入坐标不合法!\n");continue;}else if (board[x - 1][y - 1] != 0){printf("Pos Is Occupied!\n");continue;}else{board[x - 1][y - 1] = who;break;}}
}
void Game()
{int board[ROW][COL];memset(board, 0, sizeof(board));int result = NEXT;do{ShowBoard(board, ROW, COL);PlayerMove(board, ROW, COL, PLAYER1);result = IsWin(board, ROW, COL);ShowBoard(board, ROW, COL);if (NEXT != result){break;}ShowBoard(board, ROW, COL);PlayerMove(board, ROW, COL, PLAYER2);result = IsWin(board, ROW, COL);if (NEXT != result){break;}} while (1);//用户1赢 用户2赢 平局switch (result){case PLAYER1_WIN:printf("恭喜用户1,你已经赢了!\n");break;case PLAYER2_WIN:printf("恭喜用户2,你已经赢了!\n");break;case DRAW:printf("平局,和气生财!\n");break;default:break;}system("pause");ShowBoard(board, ROW, COL);
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
int main()
{int select = 0;int quit = 0;while (quit == 0){Menu();scanf("%d", &select);switch (select){case 1:Game();break;case 0:quit = 1;printf("游戏结束\n");break;default:printf("输入错误,请重新输入!\n");break;}}return 0;
}