C语言三子棋游戏

一.前言

  今天我们来用c语言写一个三子棋的小游戏,相信大家都玩过,也就是谁先三字连成一线谁赢,其中‘ * ’代表的是你下的子,‘ #’是电脑下的子,这个游戏的难度只有低级难度,如果想挑战更难的可以自己改动,同时,这个只能是跟电脑对战,想和自己朋友一起,自己也可以稍微修改。话不多说,马上进入游戏思路环节。

在进入游戏思路时,由于我们要写一个游戏,使用的函数比较多,这里我们进行分开包装

1.game.cpp(用于游戏的实现)

2.test.cpp(测试游戏,也就是主函数在其中)

3.game.h(自定义函数的头文件)

这里的game.h相当于stdio.h,可以理解为你用scanf函数需要用头文件stdio.h,你用你自定义的函数也要用到game.h,当然你自定义函数必须在game.h中。

如果写在一个页面上,比如卸载test.cpp中,那么你到后期后非常混乱,而且别人想修改都不知道从哪里入手,这也是分开包装的好处,希望大家理解。

二.游戏的思路

在进入代码内容时,我们得理清楚思路,不至于等会写的时候手慢脚乱。游戏大致分为两个部分。

1.游戏目录

任何游戏,都得有进入页面,这也是第一考虑的

2.实现游戏的过程

这个部分也是最重要的,同时也是游戏的核心。在第二部分,后面后慢慢细分。

下面我们进入具体的代码实现环节

三.代码实现以及讲解

这就是第一步,游戏的选择和界面,具体的内容在代码块中已经详细说明了

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include"game.h"
using namespace std;
//菜单
void menu() {
	cout << "*****************************" << endl;
	cout << "*********  1.paly   *********" << endl;
	cout << "*********  2.exit   *********" << endl;
	cout << "*****************************" << endl;
}
int main()
{
	srand((unsigned long)time(NULL));
	int input = 0;
	//无论什么情况上来直接打印菜单
	do {
		menu();
		cout << "请选择:>";
		cin >> input;
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			cout << "退出游戏";
			break;
		default:
			cout << "选择错误,请重新选择";
			break;
		}
	} while (input);//如果输入是0,直接退出循环,不再继续游戏
	return 0;
}
    打印完选择页面,也就是菜单后,我们需要对游戏的具体内容进行讨论,大家可以想一想要进行下棋的游戏最需要的是什么, 对就是我们的棋盘,棋盘用来保留我们下过棋的位置,这很容易就想到了我们需要数组去实现, 那我们就需要去创建一个数组,这个数组的返回值我们可以用char类型去规定,因为我们想用‘ * ‘和’ # ‘去表示我们的棋子。
    前期思路就是这些下面我们直接用代码去跟大家说明
void game() {
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);//初始化棋盘
	
	DisplayBoard(board, ROW, COL);//打印棋盘
	int a = 0;
	//进入循环,因为我们不可能只下一步棋
	while (1) {
		//玩家下棋
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);//玩家下完以后进行打印,如果没有这一步,当你赢了以后,棋盘不会打印,看起来不舒服
	    a = WinBoard(board, ROW, COL);//每当你下一个子,都会进行输赢判断,赢了就直接跳出循环
		if (a != 4)
			break;
		
		ComputerMove(board, ROW, COL);//电脑下棋也是同理
		DisplayBoard(board, ROW, COL);
		//判断输赢
		a = WinBoard(board, ROW, COL);//WinBoard是一个int类型的函数,返回值用a去接收
		if (a != 4)
			break;
		
	}//后面为谁赢了,打印谁赢了
	if (a == 1)
		cout << "玩家赢" << endl << endl<< endl;
	else if (a == 2)
		cout << "电脑赢" << endl << endl << endl;
	else
		cout << "平局" << endl << endl << endl;
}

这些代码都是在game()函数中去实现的,而game()函数又在main函数中,请仔细看main函数的实现过程

上面只是表层的代码实现,下面进行深层的代码实现
这些具体代码实现都是放在game.cpp中去实现的
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col) {//函数的初始化
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			board[i][j] = ' ';
		}
	}
}
void DisplayBoard(char board[ROW][COL], int row, int col) {//棋盘的打印
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout << " " << board[i][j] << " ";
			if (j < col-1) {
				cout << '|';
			}
		}
		cout << endl;
		if (i < row - 1) {
			for (int j = 0; j < col; j++) {
				cout << "---";
				if (j < col - 1) {
					cout << "|";
				}
			}
			cout << endl;
		}
	}
}
void PlayerMove(char board[ROW][COL], int row, int col) {
	int x, y;
	cout << "玩家下棋" << endl;
	while (1) {
		cout << "输入时,中间用空格隔开:>";
		cin >> x >> y;
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3) {
			if (board[x-1][y-1] == ' ') {
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				cout << "该位置已被填充";
		}
		else
			cout << "输入错误,请重新输入" << endl;

	}
}
void ComputerMove(char board[ROW][COL], int row, int col) {
	int x = 0;
	int y = 0;
	cout << "电脑下棋:" << endl;
	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) {
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			if (board[i][j] == ' ') {
				return 0;
			}
		}
	}
	return 1;
}


int WinBoard(char board[ROW][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] == '*') {
	
			return 1;
		}
		if (board[i][0]== board[i][1] && board[i][1]== board[i][2] && board[i][2] == '#') {
			
			return 2;
		}
	}
	for (int j = 0; j < col; j++) {
		if (board[0][j]== board[1][j] && board[1][j]== board[2][j] && board[2][j] == '*') {
			
			return 1;
		}
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == '*') {
			
			return 2;
		}
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == '*') {
		
		return 1;
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == '*') {
		
		return 1;
	}

	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == '#') {
		
		return 2;
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == '#') {
		
		return 2;
	}
	if (IsFull(board, row, col)) {
		return 5;
	}
	return 4;
}

这些是代码的详细过程,其中最为重要的点就是棋盘的打印

 首先我们要想棋盘是什么样子,我这个设计的是大概形状是 ---|---|---分为三行,同时下划线---表示是的一个空格一个我们输入的数据和一个空格,又由于我们下划线在数据的下面,所以我们应当分两行来写,还有就是最右边是没有’ | '的,棋盘的最下面也是没有---下划线的,所以我们要对其进行条件判断,具体代码如下

void DisplayBoard(char board[ROW][COL], int row, int col) {
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout << " " << board[i][j] << " ";
			if (j < col-1) {
				cout << '|';
			}
		}
		cout << endl;
		if (i < row - 1) {
			for (int j = 0; j < col; j++) {
				cout << "---";
				if (j < col - 1) {
					cout << "|";
				}
			}
			cout << endl;
		}
	}
}

 棋盘创建以后,我们就需要对其进行玩家的下棋和电脑的下棋,但是我们需要注意的是,下棋必须要在有空格的地方下棋,下面我们分别对玩家下棋和电脑下棋进行细节的讨论

1.玩家下棋时我们不能要求每个玩游戏的人都是程序员,所以在判断坐标时应该对输入的坐标减一,还有就是我们上面的要在是空格的地方落子,如果不规定会导致把电脑的落子吞点,导致BUG

2.电脑下棋需要随机,所以我们需要用两个头文件stdlib.h,time.h,这两个头文件。还有上面的空格地方才能落子,坐标问题不用考虑。

 下面用代码进行说明

void PlayerMove(char board[ROW][COL], int row, int col) {
	int x, y;
	cout << "玩家下棋" << endl;
	while (1) {
		cout << "输入时,中间用空格隔开:>";
		cin >> x >> y;
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3) {
			if (board[x-1][y-1] == ' ') {
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				cout << "该位置已被填充";
		}
		else
			cout << "输入错误,请重新输入" << endl;

	}
}
void ComputerMove(char board[ROW][COL], int row, int col) {
	int x = 0;
	int y = 0;
	cout << "电脑下棋:" << endl;
	while (1) {
		
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ') {
			board[x][y] = '#';
			
			break;
		}
	}
}

 下棋以后,我们需要判断输赢,这里有   1.玩家赢,2.电脑赢,3平局,4继续。

这里我们用1表示玩家赢,用2表示电脑赢,3表示平局(其实这里什么都可以,因为不是赢或者输就是平局了),4表示继续。

这里说明一下赢的条件,要么就是每一行都是一样的字符,要么就是一列都是一样的字符,还有就是两条对角线一个的字符会赢

平局的条件就是棋盘全部占满的情况下面看代码

 

int WinBoard(char board[ROW][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] == '*') {
	
			return 1;
		}
		if (board[i][0]== board[i][1] && board[i][1]== board[i][2] && board[i][2] == '#') {
			
			return 2;
		}
	}
	for (int j = 0; j < col; j++) {
		if (board[0][j]== board[1][j] && board[1][j]== board[2][j] && board[2][j] == '*') {
			
			return 1;
		}
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == '*') {
			
			return 2;
		}
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == '*') {
		
		return 1;
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == '*') {
		
		return 1;
	}

	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == '#') {
		
		return 2;
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == '#') {
		
		return 2;
	}
	if (IsFull(board, row, col)) {
		return 3;
	}
	return 4;
}

 最后面的IsFull()是用来判断是否是平局的

nt IsFull(char board[ROW][COL], int row, int col) {
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			if (board[i][j] == ' ') {
				return 0;
			}
		}
	}
	return 1;
}

如果有空格就直接返回0,继续落子,返回1说明满了

最后

	a = WinBoard(board, ROW, COL);//WinBoard是一个int类型的函数,返回值用a去接收
		if (a != 4)
			break;
		
	}//后面为谁赢了,打印谁赢了
	if (a == 1)
		cout << "玩家赢" << endl << endl<< endl;
	else if (a == 2)
		cout << "电脑赢" << endl << endl << endl;
	else
		cout << "平局" << endl << endl << endl;
}

返回值用a接收,用a的值去判断谁赢谁输,这里方法不一,也可以用字符去判断

下面是game.h的代码块

#pragma once
#define ROW 3
#define COL 3
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
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);
int WinBoard(char board[ROW][COL], int row, int col);

这里要说一下,这里把所以的头文件放到game.h中,其他地方可以不放,我上面只是为了好看

其他地方只需要引用game.h就行

四.总结

上面就是全都的代码实现,可能有小许细节没有讲到,还请大家指正。

  • 37
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,设计三子棋游戏可以分为以下几个步骤: 1. 定义棋盘:通常使用一个三行三列的二维数组来表示棋盘,用数字0表示空位,用数字1表示玩家1下的棋子,用数字2表示玩家2下的棋子。 2. 实现落子功能:用户输入行列坐标,程序判断该位置是否为空位,如果是则将该位置设置为当前玩家下的棋子。 3. 判断胜负:每次玩家下完棋后,程序需要判断是否有一方取得胜利,可以通过遍历棋盘来实现。 4. 实现人机对战:可以使用随机数来模拟电脑的落子,也可以使用一些简单的算法来提高电脑的智能程度。 下面是一个简单的C语言三子棋游戏代码示例,供参考: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> int chessboard[3][3]; // 棋盘 int check_win(int player) // 判断胜负 { int i, j; for (i = 0; i < 3; i++) { if (chessboard[i][0] == player && chessboard[i][1] == player && chessboard[i][2] == player) { return 1; } if (chessboard[0][i] == player && chessboard[1][i] == player && chessboard[2][i] == player) { return 1; } } if (chessboard[0][0] == player && chessboard[1][1] == player && chessboard[2][2] == player) { return 1; } if (chessboard[0][2] == player && chessboard[1][1] == player && chessboard[2][0] == player) { return 1; } return 0; } void print_chessboard() // 打印棋盘 { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { if (chessboard[i][j] == 0) { printf(" "); } else if (chessboard[i][j] == 1) { printf("X"); } else if (chessboard[i][j] == 2) { printf("O"); } if (j < 2) { printf("|"); } } printf("\n"); if (i < 2) { printf("-+-+-\n"); } } } int main() { int player = 1; // 玩家先手 int row, col; srand((unsigned)time(NULL)); // 初始化随机数生成器 printf("Welcome to the Tic-Tac-Toe game!\n"); printf("Player 1: X\n"); printf("Player 2: O\n"); while (1) { printf("Player %d's turn.\n", player); if (player == 1) { printf("Please input the row and column numbers to place your chess(X).\n"); scanf("%d%d", &row, &col); if (chessboard[row][col] != 0) { printf("This place has been taken, please choose another place.\n"); continue; } chessboard[row][col] = 1; } else { printf("Thinking...\n"); while (1) { row = rand() % 3; col = rand() % 3; if (chessboard[row][col] == 0) { break; } } chessboard[row][col] = 2; } print_chessboard(); if (check_win(player)) { printf("Player %d wins!\n", player); break; } if (player == 1) { player = 2; } else { player = 1; } } return 0; } ``` 注意:以上代码仅作为参考,可能存在一些漏洞和不足之处,需要在实际使用中进行完善。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值