C语言三子棋(附详细代码)

文章目录

  • 前言
  • 一、所需函数
  • 二、函数实现
    • 1.基本流程
    • 2.代码展示
  • 三、总结


前言

此次我们来进行三子棋的C语言编写。三子棋是在一个在3*3的方格中,将自己的三个棋子连成一条线,即可获胜。


一、所需函数

1.初始化棋盘函数(InitBoard)
2.打印棋盘函数(ShowBoard)
3.判断棋子是否连线成功函数(IsEnd)
4.玩家落子函数(PlayerMove)
5.电脑落子函数(ComputerMove)
6.菜单函数(Menu)
7.主函数


二、函数实现

1.基本流程

1).进入菜单界面
2).创建棋盘并初始化
3).打印棋盘
4).玩家落子
5).判定胜负关系
6).电脑落子
7).判定胜负关系
8).回到步骤3继续执行,直到不能继续落子


2.代码展示

采用多文件的形式展示
game.h代码如下:

#pragma once

#include
#include 
#include 
#include 

#define ROW 3
#define COL 3
#define INIT ' '
#define WHITE 'X' //玩家赢
#define BLACK 'O' //电脑赢
#define DRAW 'D'  //平局
#define NEXT 'N'  //继续

#pragma warning(disable:4996)

extern void Game();

game.c代码如下:

#include "game.h"

static void InitBoard(char board[][COL], int row, int col)     //初始化棋盘
{
     
	for (int i = 0; i < row; i++){
     
		for (int j = 0; j < col; j++){
     
			board[i][j] = INIT;
		}
	}
}
static void ShowBoard(char board[][COL], int row, int col)     //打印棋盘
{
     
	system("cls");
	printf(" ");
	for (int i = 0; i < col; i++){
     
		printf("%4d", i + 1);
	}
	printf("\n--------------\n");
	for (int i = 0; i < row; i++){
     
		printf("%-2d", i + 1); 
		for (int j = 0; j < col; j++){
     
			printf("| %c ", board[i][j]); 
		}
		printf("\n--------------\n");
	}
}
//判断棋盘当前的局面,共四种可能
//#define WHITE 'X' //玩家
//#define BLACK 'O' //电脑
//#define DRAW 'D'  //平局
//#define NEXT 'N'  //继续
static char IsEnd(char board[][COL], int row, int col){
     
	for (int i = 0; i < row; i++){
                       //判断行是否一样
		if (board[i][0] == board[i][1] && \
			board[i][1] == board[i][2] && \
			board[i][2] != INIT){
     
			return board[i][0];
		}
	}
	for (int j = 0; j < col; j++){
                       //判断列是否一样
		if (board[0][j] == board[1][j] && \
			board[1][j] == board[2][j] && \
			board[0][j] != INIT){
     
			return board[0][j];
		}
	}
	if (board[0][0] == board[1][1] && \            //判断主对角线是否一样
		board[1][1] == board[2][2] && \
		board[1][1] != INIT){
     
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && \            //判断副对角线是否一样
		board[1][1] == board[2][0] && \
		board[1][1] != INIT){
     
		return board[1][1];
	}
	for (int i = 0; i < row; i++){
                     //判断没有一样的,继续
		for (int j = 0; j < col; j++){
     
			if (board[i][j] == INIT){
     
				return NEXT;
			}
		}
	}
	return DRAW;                                  //平局
}

static void PlayerMove(char board[][COL], int row, int col)    //玩家移动
{
     
	int x = 0;
	int y = 0;
	while (1){
     
		printf("Please Enter Postion:");
		scanf("%d %d", &x, &y);
		if (x < 1 || y < 1 || x > 3 || y > 3){
     
			printf("Enter Postion Error!\n");
			continue;
		}
		if (board[x - 1][y - 1] == INIT){
     
			board[x - 1][y - 1] = WHITE;
			break;
		}
		else{
     
			printf("Postion Is Not Empty!\n");
		}
	}
}
static void ComputerMove(char board[][COL], int row, int col)   //电脑移动
{
     
	while (1){
     
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == INIT){
     
			board[x][y] = BLACK;
			break;
		}
	}
}


void Game(){
     
	char board[ROW][COL];
	InitBoard(board, ROW, COL);
	srand((unsigned long)time(NULL));
	char result = 0;
	while (1){
     
		ShowBoard(board, ROW, COL);
		PlayerMove(board, ROW, COL);
		result = IsEnd(board, ROW, COL);
		if (result != NEXT){
     
			break;
		}
		ShowBoard(board, ROW, COL);
		ComputerMove(board, ROW, COL);
		result = IsEnd(board, ROW, COL);
		if (result != NEXT){
     
			break;
		}
	}
	ShowBoard(board, ROW, COL);
	switch (result){
     
	case WHITE:
		printf("You Win!\n");
		break;
	case BLACK:
		printf("You Lose!\n");
		break;
	case DRAW:
		printf("You == Computer!\n");
		break;
	default:
		printf("BUG!\n"); 
		break;
	}
}

main.c代码如下:

#include"game.h"

void Menu(){
     
	printf("+-------------------------+\n");
	printf("|1.Play             0.Exit|\n");
	printf("+-------------------------+\n");
}
int main(){
     
	int select = 0;
	int quit = 0;
	while (!quit){
     
		Menu();
		printf("Please select 1 or 0 :");
		scanf("%d", &select);
		switch (select){
     
		case 1:
			Game();
			break;
		case 0:
			quit = 1;
			break;
		default:
			printf("Enter Error,Please Try Again.\n");
			break;
		}
	}
	printf("You are welcomed to come back next time to play!\n");
	system("pause");
	return 0;
}

三、总结

这就是C语言的三子棋了,欢迎大家来交流自己的看法。

你可能感兴趣的:(个人成长,c语言)