C语言实现双人五子棋游戏
程序员文章站
2022-07-03 17:34:36
本文实例为大家分享了c语言实现双人五子棋游戏的具体代码,供大家参考,具体内容如下实现功能生成棋盘玩家1与玩家2对战,哪个玩家率先有连续5子连线,哪个玩家赢。如何实现组成:二维数组:board[row]...
本文实例为大家分享了c语言实现双人五子棋游戏的具体代码,供大家参考,具体内容如下
实现功能
生成棋盘玩家1与玩家2对战,哪个玩家率先有连续5子连线,哪个玩家赢。
如何实现
组成:
二维数组:board[row][col],定义一个row*col的棋盘。
主要逻辑:
显示棋盘,提示用户下子,下子后判断
1.显示棋盘很简单,慢慢凑棋盘就好
2. 用户下子,注意两个条件:棋子在棋盘里,下子位置未被占用。
3.判断是最难的,
方法:从下子位置的8个方向(上,下,左,右,右上,右下,左上,左下)计算相同棋子数目,然后将对角的棋子数相加,等于5说明有5子连线
主要函数中用到三个主要实现函数:
showboard(board, row, col);//展示棋盘 playermove(board, row, col, cur);//玩家下注,cur表示哪个玩家下子 judge(board, row, col);//判断5子连线 getcount(board[][col], row, col, dir)//计算方向dir相同棋子数
代码
头文件
#ifndef __fivechree_h__ #define __fivechee_h__ #include<stdio.h> #include<windows.h> #pragma warning(disable:4996) #define row 10//棋盘行数 #define col 10//棋盘列数 #define init '*'//棋盘初始化 #define player1 1 #define player2 2 #define next 3//继续往下下 #define draw 4//棋盘下满 平局 //8个方向 #define up 10 #define right_up 11 #define right 12 #define right_down 13 #define down 14 #define left_down 15 #define left 16 #define left_up 17 extern void menu(); extern void game(); #endif
main函数源文件
#include"fivechree.h" int main(){ int quit = 0; while (!quit){ menu(); int select = 0; scanf("%d", &select); switch (select){ case 1: game(); break; case 2: quit = 1; break; default: printf("enter error!\n"); break; } } printf("byebye\n"); system("pause"); return 0; }
函数定义源文件
#include"fivechree.h" static int x = 0; static int y = 0; void menu(){ printf("+---------------------+\n"); printf("+- 1.play 2.exit -+\n"); printf("+---------------------+\n"); printf("please enter your select#"); } static void showboard(int board[][col], int row, int col){//展示棋盘 o玩家1棋子,x玩家2棋子 system("cls"); for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ switch (board[i][j]){ case player1: board[i][j] = 'o'; break; case player2: board[i][j] = 'x'; break; case 0: board[i][j] = init; break; default: break; } } } printf(" "); for (int i =1; i <= row; i++){ printf("%2d ", i); } printf("\n"); for (int i = 1; i <= row; i++){ printf("%-2d", i); for (int j = 1; j <= col; j++){ printf(" %c ", board[i - 1][j - 1]); } printf("\n"); } } static void playermove(int board[][col], int row, int col, int who){//玩家下子,who 为哪个玩家下子 while (1){ printf("please enter player%d postion<x,y>#", who); scanf("%d %d", &x, &y); if (x<1 || x>row || y<1 || y>col){ //超过棋盘范围 printf("postion is error!\n"); continue; } if (board[x - 1][y - 1] == init){//判断位置是否已被下子 board[x - 1][y - 1] = who; break; } printf("postion is not empty\n"); } } static int getcount(int board[][col], int row, int col, int dir){//判断8个方向相同棋子的数目 int _x = x;//_x,_y变化,后面与x,y棋子相比较 int _y = y; int count = 0; while (1){ switch (dir){ case up: _x--; break; case down: _x++; break; case left: _y--; break; case right: _y++; break; case right_up: _x--, _y++; break; case right_down: _x++, _y++; break; case left_down: _x++, _y--; break; case left_up: _x--, _y--; break; default: break; } if (_x>=1 || _x<=row || _y>=1 || _y<=col){//棋子不能越界 if (board[x-1][y-1] == board[_x-1][_y-1]){ //printf("yes\n"); count++; } else{ //printf("no\n"); break; } } else{ return count; } } return count; } //如何判断:从下子位置的8个方向(上,下,左,右,右上,右下,左上,左下) //计算相同棋子数目,然后将对角的棋子数相加,等于5说明有5子连线 static int judge(int board[][col], int row, int col){ int count1 = getcount(board, row, col, up)\ + getcount(board, row, col, down); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = getcount(board, row, col, right_up)\ + getcount(board, row, col, left_down); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = getcount(board, row, col, right)\ + getcount(board, row, col, left); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = getcount(board, row, col, right_down)\ + getcount(board, row, col, left_up); if (count1 >= 4){ return board[x-1][y-1]; } for (int i = 0; i < row; i++){//判断棋盘是否下满 for (int j = 0; j < col; j++){ if (board[i][j] == init){ return next; } } } return draw; } void game(){ int board[row][col] = { 0 }; //memset(board, init, row*col); int result = 0; int cur = player1; showboard(board, row, col);//先展示棋盘 while (1){ //showboard(board, row, col); playermove(board, row, col, cur); showboard(board, row, col);//棋盘将board数组变化,所以要在判断前将数组变化 result = judge(board, row, col); if (result != next){ break; } cur = (cur == player1 ? player2 : player1);//三目表达式,注意不是 player1 ? player2 : player1 } showboard(board, row, col); switch (result){ case 'o': printf("player1 win!\n"); break; case 'x': printf("player2 win!\n"); break; case draw: printf("tie game!\n"); break; default: //printf("%c\n", result); printf("bug\n"); break; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。