井子棋承载了每个人孩童时的美好时光,小到书本、纸张,大到课桌、墙壁,总能找到井字棋盘的痕迹。今天我们就来实际操作一番,用C语言完成一个简单的井字棋游戏,让我们一起重温美好。
棋盘如下:
**功能描述:**棋盘共分为九个格子,一方执“O”为棋,一方执“X”为棋,双方依次选择格子。己方棋子率先连成三子的获胜,若棋盘占满仍未分胜负,则打成平局。
具体功能实现:
1.在页面选择玩家vs玩家,或玩家vs电脑
2.玩家下棋时,输入对应格子的坐标
3.电脑下棋时,使用随机值选择坐标
4.每下一步,棋盘更新一次
5.当率先有三颗相同棋子横向或纵向或斜向连成线,则为赢,游戏结束
6.若棋盘下满后,仍未有赢家则为平局,游戏结束
7.游戏结束后返回菜单,选择继续游戏或退出
具体各函数构建:
1、菜单函数:当玩家打开程序,首先弹出一个菜单,供玩家选择,直接使用printf打印即可。
void Menu() {
printf("********欢迎来到三子棋游戏室********\n");
printf("************************************\n");
printf("***********1、开始游戏**************\n");
printf("***********2、退出游戏**************\n");
printf("************************************\n");
}
2、清空棋盘:用二维数组表示棋盘时,先创建好char 类型的二维数组,此时需要将数组中的各元素全变为“ ”(空格),已便于下一步打印棋盘。
void InitBoard(char arr[][COL], int row, int col) {
int i, j;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
arr[i][j] = ' ';
}
}
3、打印棋盘:当二维数组清空后,直接将二维数组个元素并配合“----------”“|”利用循环逐一打印,实现棋盘的打印
void ShowBoard(char arr[][COL], int row, int col) {
int i;
printf(" | 1 | 2 | 3 |\n");
for (i = 0; i < row; i++) {
printf("----------------\n");
printf(" %d | %c | %c | %c |\n", i+1, arr[0][i], arr[1][i], arr[2][i]);
}
}
4、玩家落子:打印棋盘后,该玩家输入相应的坐标落子,判断玩家选择的位置无棋子赋值即可
void WhiteJudge(char arr[][COL], int row, int col) {
printf("请选择你要下棋子的坐标");
int x = 0, y = 0;
reselect:
scanf("%d%d", &x, &y);
if (arr[x-1][y-1] == ' ')
arr[x-1][y-1] = WHITEBOARD;
else {
printf("你选择的格子已有棋子,请重选:");
goto reselect;
}
}
5、电脑落子:玩家落子结束后,电脑通过设定范围内的随机值选择坐标,同样判断该位置无棋子时,方可赋值
void BlackJudge(char arr[][COL],int row, int col) {
while (1) {
int h = rand() % row;
int l = rand() % col;
if (arr[h][l] == ' ') {
printf("电脑落子:%d %d\n", h + 1, l + 1);
arr[h][l] = BLACKBOARD;
break;
}
}
printf("\n");
}
6、赢家判断:在横向,纵向,斜向三方向下,判断是否有三颗相同棋子连成一条线,若有则执该棋者为赢家
char WinJudge(char arr[][COL], int row, int col) {
int i, j=0;
for (i = 0; i < 3; i++) {
if (arr[i][j] != ' '&&arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2])
return arr[i][0];
}
for (i = 0; i < 3; i++) {
if (arr[0][i] != ' '&&arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])
return arr[0][i];
}
if (arr[0][0] != ' '&&arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2]) {
return arr[0][0];
}
if (arr[2][0] != ' '&&arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2]) {
return arr[2][0];
}
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
if (arr[i][j] == ' ')
return 'N';
}
return 'D';
}
7、Game函数:主要函数写完毕后,用Game函数整体联系起来
int Game() {
char result;
int row = 3, col = 3;
char board[ROW][COL];
InitBoard(board, ROW, COL);
while (1) {
ShowBoard(board, ROW, COL);
WhiteJudge(board, ROW, COL);
result = WinJudge(board, ROW, COL);
switch (result) {
case 'O':
ShowBoard(board, ROW, COL);
printf("恭喜你赢了!\n");
return 0;
case 'X':
ShowBoard(board, ROW, COL);
printf("很抱歉你输了!\n");
return 0;
case 'D':
ShowBoard(board, ROW, COL);
printf("你和电脑是平局!\n");
return 0;
default:
break;
}
printf("\n");
ShowBoard(board, ROW, COL);
Sleep(500);
srand((unsigned long)time(NULL));
BlackJudge(board, ROW, COL);
result = WinJudge(board, ROW, COL);
switch (result) {
case 'O':
ShowBoard(board, ROW, COL);
printf("恭喜你赢了!\n");
return 0;
case 'X':
ShowBoard(board, ROW, COL);
printf("很抱歉你输了!\n");
return 0;
case 'D':
ShowBoard(board, ROW, COL);
printf("你和电脑是平局!\n");
return 0;
default:
break;
}
}
}
头文件函数声明:
为防止跨文件计算机无法找到函数和变量,在头文件中将函数统一声明
#ifndef _CHESS_H
#define _CHESS_H
//三子棋
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define ROW 3
#define COL 3
#define BLACKBOARD 'X'
#define WHITEBOARD 'O'
int Game();
void Menu();
void InitBoard(char arr[][COL],int row,int col);
void ShowBoard(char arr[][COL],int row,int col);
void WhiteJudge(char arr[][COL],int row,int col);
void BlackJudge(char arr[][COL],int row,int col);
char WinJudge(char arr[][COL],int row,int col);
#endif
函数定义:
#include"chessh
void Menu() {
printf("********欢迎来到三子棋游戏室********\n");
printf("************************************\n");
printf("***********1、开始游戏**************\n");
printf("***********2、退出游戏**************\n");
printf("************************************\n");
}
void InitBoard(char arr[][COL], int row, int col) {
int i, j;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
arr[i][j] = ' ';
}
}
void ShowBoard(char arr[][COL], int row, int col) {
int i;
printf(" | 1 | 2 | 3 |\n");
for (i = 0; i < row; i++) {
printf("----------------\n");
printf(" %d | %c | %c | %c |\n", i+1, arr[0][i], arr[1][i], arr[2][i]);
}
}
void WhiteJudge(char arr[][COL], int row, int col) {
printf("请选择你要下棋子的坐标");
int x = 0, y = 0;
reselect:
scanf("%d%d", &x, &y);
if (arr[x-1][y-1] == ' ')
arr[x-1][y-1] = WHITEBOARD;
else {
printf("你选择的格子已有棋子,请重选:");
goto reselect;
}
}
void BlackJudge(char arr[][COL],int row, int col) {
while (1) {
int h = rand() % row;
int l = rand() % col;
if (arr[h][l] == ' ') {
printf("电脑落子:%d %d\n", h + 1, l + 1);
arr[h][l] = BLACKBOARD;
break;
}
}
printf("\n");
}
char WinJudge(char arr[][COL], int row, int col) {
int i, j=0;
for (i = 0; i < 3; i++) {
if (arr[i][j] != ' '&&arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2])
return arr[i][0];
}
for (i = 0; i < 3; i++) {
if (arr[0][i] != ' '&&arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])
return arr[0][i];
}
if (arr[0][0] != ' '&&arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2]) {
return arr[0][0];
}
if (arr[2][0] != ' '&&arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2]) {
return arr[2][0];
}
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
if (arr[i][j] == ' ')
return 'N';
}
return 'D';
}
int Game() {
char result;
int row = 3, col = 3;
char board[ROW][COL];
InitBoard(board, ROW, COL);
while (1) {
ShowBoard(board, ROW, COL);
WhiteJudge(board, ROW, COL);
result = WinJudge(board, ROW, COL);
switch (result) {
case 'O':
ShowBoard(board, ROW, COL);
printf("恭喜你赢了!\n");
return 0;
case 'X':
ShowBoard(board, ROW, COL);
printf("很抱歉你输了!\n");
return 0;
case 'D':
ShowBoard(board, ROW, COL);
printf("你和电脑是平局!\n");
return 0;
default:
break;
}
printf("\n");
ShowBoard(board, ROW, COL);
Sleep(500);
srand((unsigned long)time(NULL));
BlackJudge(board, ROW, COL);
result = WinJudge(board, ROW, COL);
switch (result) {
case 'O':
ShowBoard(board, ROW, COL);
printf("恭喜你赢了!\n");
return 0;
case 'X':
ShowBoard(board, ROW, COL);
printf("很抱歉你输了!\n");
return 0;
case 'D':
ShowBoard(board, ROW, COL);
printf("你和电脑是平局!\n");
return 0;
default:
break;
}
}
}
main函数:
#include"chess.h"
int main() {
next:
Menu();
int choice;
int m;
printf("请输入您的选择:");
again:
scanf("%d", &choice);
switch (choice) {
case(1):
while (1) {
Game();
printf("是否要再来一局? 1、再来一局 2、返回菜单\n");
scanf("%d", &m);
if (m == 2)
goto next;
else if (m != 1) {
printf("输入错误,三秒后自动退出!");
Sleep(3000);
break;
}
}
break;
case(2):
break;
default:
printf("输入错误,请重新输入:\n");
goto again;
}
}
至此井字棋代码以全部阐述,快去和你的小伙伴一起玩耍吧。