三子棋
用C语言来实现三子棋小游戏,我们需要先把大概思路顺一遍,在我们的test.c中把这些函数运用起来,在头文件game.h中声明函数,然后在game.c中把你声明的函数实现。
我们先要确定一个思路去完成我们的三子棋小游戏:
1.在开始游戏前,需要一个目录,让玩家选择开始游戏或者退出游戏。
2.建立一个二维数组,这个二维数组就是棋盘,将二维数组初始化。
3.将二维数组打印出来,让玩家可以看到,打印棋盘。
4.棋盘打印好了,玩家落子,并且判断玩家落子的合法性。
5.电脑随机落子。
6.判断棋盘是否下满。
7.判断游戏的输赢。
game.h中的函数声明
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define ROW 3
#define COL 3
void menu();
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 FullBoard(char board[ROW][COL], int row, int col);
char Win(char board[ROW][COL], int row, int col);
game.h实现函数
#include "game.h"
//打印目录
void menu(){
printf("\n");
printf("///1.开始游戏 0.结束游戏///\n");
printf("\n");
}
//初始化棋盘
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;
int j = 0;
for (i = 0; i < row; i++){
for (j = 0; j < col - 1; j++){
printf(" %c |",board[i][j]);
}
if (j = col - 1){
printf(" %c ", board[i][j]);
printf("\n");
}
if (i != row - 1){
for (j = 0; j < col; j++){
printf("---", board[i][j]);
if (j != (col - 1)){
printf("|");
}
}
printf("\n");
}
}
}
//玩家走
void PlayerMove(char board[ROW][COL], int row, int col){
int x = 0;
int y = 0;
while (1){
printf("请玩家落子:\n");
scanf("%d%d", &x, &y);
if ((x < row) && (x >= 0) && (y >= 0) && (y < col)){
if (board[x][y] == ' '){
board[x][y] = 'X';
break;
}
else{
printf("此坐标已经有棋子,请重新输入!\n");
}
}
else{
printf("坐标非法,请重新输入!\n");
}
}
}
//电脑走
void ComputerMove(char board[ROW][COL], int row, int col){
int x = 0;
int y = 0;
printf("电脑落子\n");
while (1){
x = rand() % row ;
y = rand() % col ;
if (board[x][y] == ' '){
board[x][y] = 'O';
break;
}
}
}
//判断是否把棋盘下满
int FullBoard(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 Win(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] == board[i][j - 1] && board[i][j + 1] == board[i][j] && board[i][j] != ' '){
return board[i][j];
}
else if (board[i][j] == board[i - 1][j] && board[i][j] == board[i + 1][j] && board[i][i] != ' ') {
return board[i][j];
}
else if (board[i][j] == board[i - 1][j - 1] && board[i][j] == board[i + 1][j + 1] && board[i][j] != ' '){
return board[i][j];
}
else if (board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i + 1][j - 1] && board[i][j] != ' '){
return board[i][j];
}
}
}
if (FullBoard(board, ROW, COL) == 1){
return 'P';
}
return ' ';
}
在test.c中使用
#include "game.h"
void game(){
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
char tmp = 0;
while (1){
PlayerMove(board, ROW, COL);
tmp = Win(board, ROW, COL);
DisplayBoard(board, ROW, COL);
if (tmp != ' '){
break;
}
ComputerMove(board, ROW, COL);
tmp = Win(board, ROW, COL);
DisplayBoard(board, ROW, COL);
if (tmp != ' '){
break;
}
}
if (tmp == 'X'){
printf("玩家获胜!\n");
}
if (tmp == 'O'){
printf("电脑获胜,你输了!\n");
}
if (tmp == 'P'){
printf("平局!\n");
}
}
int main(){
int n = 0;
srand((unsigned int) time(NULL));
do{
menu();
printf("输入你的选择:");
scanf("%d", &n);
switch (n){
case 1:
game();
break;
case 0:
printf("退出游戏!");
break;
default:
printf("非法输入,请重新选择!");
break;
}
} while (n);
system("pause");
return 0;
}
以上就是完成三子棋的代码,在判断输赢的地方可以进行优化,可以使我们的棋盘不局限于3*3的格局。