您的位置:首页 > 教育 > 锐评 > 网络规划设计师正高级任职资格_wap手机网站开发_怎么推广产品最有效_军事最新消息

网络规划设计师正高级任职资格_wap手机网站开发_怎么推广产品最有效_军事最新消息

2025/1/8 21:00:42 来源:https://blog.csdn.net/qq_66767938/article/details/144780992  浏览:    关键词:网络规划设计师正高级任职资格_wap手机网站开发_怎么推广产品最有效_军事最新消息
网络规划设计师正高级任职资格_wap手机网站开发_怎么推广产品最有效_军事最新消息

目录

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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com