C语言实现五子棋
项目的各个函数VS源码
提示:这里只运用了数组,函数的内容
提示:以下是本篇文章正文内容
一、构思整个程序
五子棋首先要有几个关键的功能:
1. 构建棋盘
2. 玩家下棋
3. 电脑下棋
4. 判断输赢
二、功能实现
1.棋盘初始化
通过观察棋盘发现棋盘是由分割符以及空格实现,实际上棋盘的空格就是之后要下棋的位置,空格代表这个位置的初始化,即让每一个char类型元素初始化为空格,再通过一些处理显示棋盘。
代码如下(示例):
void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } } } void DispalyBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { //打印数据 //printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]); int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); //打印分割信息 //printf("---|---|---\n"); if (i < row - 1) { int j = 0; for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } }
2.玩家下棋VS电脑下棋
首先我们通过坐标的输入实现了定位下棋的位置,而电脑下棋我们利用随机数rand(),以及srand()进行操作,而随机数的根本来源于时间戳的变化
代码如下(示例):
void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋:>\n"); while (1) { printf("请输入坐标:>"); scanf("%d %d", &x, &y); //坐标范围合法的判断 if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("坐标被占用,不能下棋,请选择其他位置\n"); } } else { printf("坐标非法,请重新输入\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑下棋:>\n"); int x = 0; int y = 0; while (1) { x = rand() % row;//0~2 y = rand() % col;//0~2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
3.判断输赢
如何进行判断输赢呢?
有以下几种情况:
1.*符号满足,玩家赢了
2.#符号满足,电脑赢了
3.棋盘位置占满,并且都没满足,平局
4.棋盘位置未满,并且都没满足,继续下棋
//满了就返回1 //不满 返回0 int IsFull(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } char IsWin(char board[ROW][COL], int row, int col) { //行 int i = 0; int j = 0; for (i = 0; i < row; i++) { int nums = 0; for (j = 0; j < col - 1; j++) { if (board[i][j] == board[i][j + 1]&&board[i][j] != ' ') nums++; } if (nums == col - 1) return board[i][j]; } //列 for (i = 0; i < col; i++) { int nums = 0; for (j = 0; j < row - 1; j++) { if (board[j][i] == board[j + 1][i] && board[i][j] != ' ') { nums++; } } if (nums == row - 1) { return board[j][i]; } } //前提是棋盘为正方形 //对角线 int nums = 0; for (i=0,j=0;i<row-1,j<col-1;i++,j++) { if (board[i][j] == board[i + 1][j + 1] && board[i][j] != ' ') { nums++; } } if (nums == row - 1) return board[i-1][j-1]; nums = 0;//注意 for (i = 0, j = col-1; i < row - 1, j >0; i++, j--) { if (board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ') { nums++; } } if (nums == row - 1) return board[i-1][j+1]; //没有人赢,就要平局 if (IsFull(board, row, col)) { return 'Q'; } //游戏继续 return 'C'; }
4.显示棋盘结果
这里显示的结果跟初始化的结果相比,空格的位置会被棋子覆盖
void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { int j = 0; for (j = 0; j < row; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } }
三.程序总体的实现(注:细节问题由注释解释)
1.game.h
#pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 5 #define COL 5 //初始化棋盘 void InitBoard(char board[ROW][COL], int row, int col); //打印棋盘 void DisplayBoard(char board[ROW][COL], int row, int col); //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col); //电脑下棋 //找没有下棋的随机下棋 void ComputerMove(char board[ROW][COL], int row, int col); // // 玩家赢 - '*' // 电脑赢 - '#' // 平局 - 'Q' // 继续 - 'C' // char IsWin(char board[ROW][COL], int row, int col);
2.game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } } } void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { int j = 0; for (j = 0; j < row; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } } } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋:>\n"); while (1) { printf("请输入坐标:>"); scanf("%d %d", &x, &y); //坐标范围合法的判断 if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("坐标被占用,不能下棋,请选择其他位置\n"); } } else { printf("坐标非法,请重新输入\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑下棋:>\n"); int x = 0; int y = 0; while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } int IsFull(char board[ROW][COL], int row, int col) { int i = 0, j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } char IsWin(char board[ROW][COL], int row, int col) { //行 int i = 0; int j = 0; for (i = 0; i < row; i++) { int nums = 0; for (j = 0; j < col - 1; j++) { if (board[i][j] == board[i][j + 1]&&board[i][j] != ' ') nums++; } if (nums == col - 1) return board[i][j]; } //列 for (i = 0; i < col; i++) { int nums = 0; for (j = 0; j < row - 1; j++) { if (board[j][i] == board[j + 1][i] && board[i][j] != ' ') { nums++; } } if (nums == row - 1) { return board[j][i]; } } //前提是棋盘为正方形 //对角线 int nums = 0; for (i=0,j=0;i<row-1,j<col-1;i++,j++) { if (board[i][j] == board[i + 1][j + 1] && board[i][j] != ' ') { nums++; } } if (nums == row - 1) return board[i-1][j-1]; nums = 0;//注意每次都要将其置0 for (i = 0, j = col-1; i < row - 1, j >0; i++, j--) { if (board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ') { nums++; } } if (nums == row - 1) return board[i-1][j+1]; //没有人赢,就要平局 if (IsFull(board, row, col)) { return 'Q'; } //游戏继续 return 'C'; }
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("*****************************\n"); printf("***** 1. play 0. exit *****\n"); printf("*****************************\n"); } void game() { char ret = 0; char board[ROW][COL] = { 0 }; //初始化棋盘的函数 InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); //下棋 while (1) { PlayerMove(board, ROW, COL); //判断输赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } DisplayBoard(board, ROW, COL); ComputerMove(board, ROW, COL); //判断输赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } DisplayBoard(board, ROW, COL); } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else { printf("平局\n"); } DisplayBoard(board, ROW, COL); } int main() { srand((unsigned int)time(NULL));//设置随机数的生成起点 int input = 0; do { menu();//打印菜单 printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误!\n"); break; } } while (input); return 0; }
最终结果演示:
四.总结
通过以上的代码,是对数组以及函数的一种锻炼,是新手非常适合做的小程序,除了处理这些逻辑关系之外,更要清楚随机数如何运用以及其与时间戳之间的配合关系,以上总结,希望小伙伴们能够理解的更加深刻。码字不易,多多支持!