设计题目:井字棋游戏设计与实现 游戏简介:一种在井字格上进行的连珠游戏,由分别代表O和X的两个游戏者轮流在格子里落子,任意三子形成一条直线,则为获胜。今天,小明发明了新玩法:①棋盘3x3大小,获胜条件不变;②当一方落下第4子仍未获胜时,则其落下的第1子会被拿掉,依次类推。

类似文章很多,但是可以拿走棋子的较少,故本文着重讲解拿走棋子部分

头文件

#include <stdio.h>
#include <windows.h>
#include<stdlib.h>//用于产生随机数//全局变量
int QP[3][3];//棋盘,1为√,2为×,0为空
int qp1[8] = { 0 }, qp2[8] = { 0 };//用于存数以便于每六步消子
int player=1;//1为√ 玩家,2为× 玩家2或电脑
int cnt1=0,cnt2=0;int judge_win();//判断输赢
void gotoxy(int x, int y);//跳转光标
void print_menu();//菜单
void print_box();//棋盘
void move_player1();//√下棋
void move_player2(int oder);//×下棋
int game(int oder);//游戏本体
void turn();//玩家状态
void updatebox();//更新棋盘
void xiaozi();//消去棋子

主函数

int main()
{int oder;system("mode con cols=90 lines=35");
begin:print_menu();scanf("%d", &oder);for (int a = 0; a < 3; a++)for (int b = 0; b < 3; b++)QP[a][b] = 0;//初始化a,bswitch (oder){case 0:return 0;break;case 1:game(oder);break;case 2:game(oder);break;default:printf("输入错误返回菜单\n");goto begin;}return 0;
}

拿走棋子:

原理:定义两个数组,用于存放已下棋子的坐标(qb1和qp2),然后用cnt来计数,当到了第四步(由于使用一维数组存数,故此时cnt=8)时,则将第一次所下棋子坐标赋值为0(代表此处没有棋子),并更新棋盘,后续步骤则用%来更新存放棋子坐标的数组(指qb1和qp2)

void xiaozi()
{if (cnt1 >= 8){QP[qp1[(cnt1) %8]][qp1[((cnt1)%8+1)]] = 0;updatebox();}if (cnt2 >= 8){QP[qp2[(cnt2) % 8]][qp2[((cnt2) % 8 + 1)]] = 0;updatebox();}
}

开始菜单

void print_menu()
{printf("请输入:\n1 玩家对战\n2 普通人机\n0 退出游戏\n");
}

打印棋盘

void print_box()
{gotoxy(26, 5);printf("        ┃        ┃        ");gotoxy(26, 6);printf("        ┃        ┃        ");gotoxy(26, 7);printf("        ┃        ┃        ");gotoxy(26, 8);printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━");gotoxy(26, 9);printf("        ┃        ┃        ");gotoxy(26, 10);printf("        ┃        ┃        ");gotoxy(26, 11);printf("        ┃        ┃        ");gotoxy(26, 12);printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━");gotoxy(26, 13);printf("        ┃        ┃        ");gotoxy(26, 14);printf("        ┃        ┃        ");gotoxy(26, 15);printf("        ┃        ┃        \n");
}

玩家1移动

void move_player1()
{do{gotoxy(60, 15);printf("                              ");gotoxy(60, 6);printf("请输入x轴坐标:      ");gotoxy(60, 7);printf("请输入y轴坐标:      ");gotoxy(60, 6);int x, y;printf("请输入x轴坐标:");scanf("%d", &x);gotoxy(60, 7);printf("请输入y轴坐标:");scanf("%d", &y);if ((x > 3) || (x < 0) || (y < 0) || (y > 3)){gotoxy(60, 15);printf("x,y的值在1到3之间,请重输");continue;}if (QP[x - 1][y - 1] != 0){gotoxy(60,15);printf("此处有棋子,请重输");continue;}else{QP[x - 1][y - 1] = 1;qp1[cnt1 %8] = x - 1;qp1[(cnt1+1)%8] = y - 1;break;}} while (1);updatebox();player = 

玩家二

void move_player2(int oder)
{if (oder == 1) //玩家{do{gotoxy(60, 15);printf("                              ");gotoxy(60, 6);printf("请输入x轴坐标:      ");gotoxy(60, 7);printf("请输入y轴坐标:      ");gotoxy(60, 6);int x, y;printf("请输入x轴坐标:");scanf("%d", &x);gotoxy(60, 7);printf("请输入y轴坐标:");scanf("%d", &y);if ((x > 3) || (x < 0) || (y < 0) || (y > 3)){gotoxy(60, 15);printf("x,y的值在1到3之间,请重输");continue;}if (QP[x - 1][y - 1] != 0){gotoxy(60, 15);printf("此处有棋子,请重输");continue;}else{QP[x - 1][y - 1] = 2;qp2[cnt2 % 8] = x - 1;qp2[(cnt2 + 1) % 8] = y - 1;break;}} while (1);}else if(oder==2){while (1){int x = rand() % 3;int y = rand() % 3;if (QP[x][y] == 0){QP[x][y] = 2;break;}}}updatebox();player = 1;
}

游戏体

int game(int oder)
{print_box();do{turn();//显示到谁move_player1();judge_win();if (judge_win() != 0)break;cnt1 += 2;xiaozi();turn();move_player2(oder);judge_win();if (judge_win() != 0)break;cnt2 += 2;xiaozi();} while (1);if (judge_win ()==1 )MessageBox(NULL, TEXT("√赢了"), TEXT("游戏结束"), 0);else if(judge_win ()==2)MessageBox(NULL, TEXT("×赢了"), TEXT("游戏结束"), 0);return 0;
}

打印游戏动态

void turn()
{gotoxy(60, 5);printf("     ");gotoxy(60, 5);if(player==1)printf("√");else if(player == 2)printf("×");gotoxy(63, 5);printf("走");
}

更新棋盘

void updatebox()
{int a, b;//用于计算打印棋子的位置for(a=0;a<3;a++)for (b = 0; b < 3; b++){if (QP[a][b] == 1){gotoxy(30 + a * 8, 6 + b * 4);printf("√");}else if (QP[a][b] == 2){gotoxy(30 + a * 8, 6 + b * 4);printf("×");}else if (QP[a][b] == 0){gotoxy(30 + a * 8, 6 + b * 4);printf("   ");}}
}

判断输赢

int judge_win()
{for (int r = 0; r < 3; r++){//√赢返回1,×赢返回2//三横三竖的判断if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 1 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 1)return 1;if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 2 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 2)return 2;}if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==1 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] ==1)return 1;if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==2 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] == 2)return 2;return 0;
}

移动光标

void gotoxy(int x, int y)
{COORD pos = { x,y };HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hout, pos);//设置光标位置,并移动到相应位置
}//

总代码

#include <stdio.h>
#include <windows.h>
#include<stdlib.h>//用于产生随机数//全局变量
int QP[3][3];//棋盘,1为√,2为×,0为空
int qp1[8] = { 0 }, qp2[8] = { 0 };//用于存数以便于每六步消子
int player=1;//1为√ 玩家,2为× 玩家2或电脑
int cnt1=0,cnt2=0;int judge_win();//判断输赢
void gotoxy(int x, int y);//跳转光标
void print_menu();//菜单
void print_box();//棋盘
void move_player1();//√下棋
void move_player2(int oder);//×下棋
int game(int oder);//游戏本体
void turn();//玩家状态
void updatebox();//更新棋盘
void xiaozi();//消去棋子int main()
{int oder;system("mode con cols=90 lines=35");
begin:print_menu();scanf("%d", &oder);for (int a = 0; a < 3; a++)for (int b = 0; b < 3; b++)QP[a][b] = 0;//初始化a,bswitch (oder){case 0:return 0;break;case 1:game(oder);break;case 2:game(oder);break;default:printf("输入错误返回菜单\n");goto begin;}return 0;
}void print_menu()
{printf("请输入:\n1 玩家对战\n2 普通人机\n0 退出游戏\n");
}void print_box()
{gotoxy(26, 5);printf("        ┃        ┃        ");gotoxy(26, 6);printf("        ┃        ┃        ");gotoxy(26, 7);printf("        ┃        ┃        ");gotoxy(26, 8);printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━");gotoxy(26, 9);printf("        ┃        ┃        ");gotoxy(26, 10);printf("        ┃        ┃        ");gotoxy(26, 11);printf("        ┃        ┃        ");gotoxy(26, 12);printf("━━━━━━━━╂━━━━━━━━╂━━━━━━━━");gotoxy(26, 13);printf("        ┃        ┃        ");gotoxy(26, 14);printf("        ┃        ┃        ");gotoxy(26, 15);printf("        ┃        ┃        \n");
}void move_player1()
{do{gotoxy(60, 15);printf("                              ");gotoxy(60, 6);printf("请输入x轴坐标:      ");gotoxy(60, 7);printf("请输入y轴坐标:      ");gotoxy(60, 6);int x, y;printf("请输入x轴坐标:");scanf("%d", &x);gotoxy(60, 7);printf("请输入y轴坐标:");scanf("%d", &y);if ((x > 3) || (x < 0) || (y < 0) || (y > 3)){gotoxy(60, 15);printf("x,y的值在1到3之间,请重输");continue;}if (QP[x - 1][y - 1] != 0){gotoxy(60,15);printf("此处有棋子,请重输");continue;}else{QP[x - 1][y - 1] = 1;qp1[cnt1 %8] = x - 1;qp1[(cnt1+1)%8] = y - 1;break;}} while (1);updatebox();player = 2;
}void move_player2(int oder)
{if (oder == 1) //玩家{do{gotoxy(60, 15);printf("                              ");gotoxy(60, 6);printf("请输入x轴坐标:      ");gotoxy(60, 7);printf("请输入y轴坐标:      ");gotoxy(60, 6);int x, y;printf("请输入x轴坐标:");scanf("%d", &x);gotoxy(60, 7);printf("请输入y轴坐标:");scanf("%d", &y);if ((x > 3) || (x < 0) || (y < 0) || (y > 3)){gotoxy(60, 15);printf("x,y的值在1到3之间,请重输");continue;}if (QP[x - 1][y - 1] != 0){gotoxy(60, 15);printf("此处有棋子,请重输");continue;}else{QP[x - 1][y - 1] = 2;qp2[cnt2 % 8] = x - 1;qp2[(cnt2 + 1) % 8] = y - 1;break;}} while (1);}else if(oder==2){while (1){int x = rand() % 3;int y = rand() % 3;if (QP[x][y] == 0){QP[x][y] = 2;break;}}}updatebox();player = 1;
}int game(int oder)
{print_box();do{turn();//显示到谁move_player1();judge_win();if (judge_win() != 0)break;cnt1 += 2;xiaozi();turn();move_player2(oder);judge_win();if (judge_win() != 0)break;cnt2 += 2;xiaozi();} while (1);if (judge_win ()==1 )MessageBox(NULL, TEXT("√赢了"), TEXT("游戏结束"), 0);else if(judge_win ()==2)MessageBox(NULL, TEXT("×赢了"), TEXT("游戏结束"), 0);return 0;
}void turn()
{gotoxy(60, 5);printf("     ");gotoxy(60, 5);if(player==1)printf("√");else if(player == 2)printf("×");gotoxy(63, 5);printf("走");
}void updatebox()
{int a, b;//用于计算打印棋子的位置for(a=0;a<3;a++)for (b = 0; b < 3; b++){if (QP[a][b] == 1){gotoxy(30 + a * 8, 6 + b * 4);printf("√");}else if (QP[a][b] == 2){gotoxy(30 + a * 8, 6 + b * 4);printf("×");}else if (QP[a][b] == 0){gotoxy(30 + a * 8, 6 + b * 4);printf("   ");}}
}void xiaozi()
{if (cnt1 >= 8){QP[qp1[(cnt1) %8]][qp1[((cnt1)%8+1)]] = 0;updatebox();}if (cnt2 >= 8){QP[qp2[(cnt2) % 8]][qp2[((cnt2) % 8 + 1)]] = 0;updatebox();}
}int judge_win()
{for (int r = 0; r < 3; r++){//√赢返回1,×赢返回2//三横三竖的判断if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 1 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 1)return 1;if (QP[r][0] == QP[r][1] && QP[r][2] == QP[r][1] && QP[r][1] == 2 || QP[0][r] == QP[1][r] && QP[2][r] == QP[1][r] && QP[1][r] == 2)return 2;}if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==1 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] ==1)return 1;if (QP[0][0] == QP[1][1] && QP[1][1] == QP[2][2] && QP[1][1] ==2 || QP[0][2] == QP[1][1] && QP[1][1] == QP[2][0] && QP[1][1] == 2)return 2;return 0;
}void gotoxy(int x, int y)
{COORD pos = { x,y };HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hout, pos);//设置光标位置,移动到
}//

本代码存在打印bug,有两段文字无法被打印

本文为sztu学生张fm的大一上作业,要交给s小乐老师,为防止作业出现雷同特此声明,为完成博客任务不被卷死,只能发文来凑数.................

代码参考: https://b23.tv/9dCyt4B

实现若想要实现人工智能可参考(本代码无):(27条消息) 关于井字棋/三子棋的伪人工智能算法的实现_DanteKing的博客-CSDN博客_井字棋ai算法

井字棋(棋子可消去(拿走))相关推荐

  1. python二维游戏示例_Python实现的井字棋(Tic Tac Toe)游戏示例

    本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码 ...

  2. python井字棋ai,python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  3. python井字棋游戏代码_Python实现的井字棋(Tic Tac Toe)游戏示例

    Python实现的井字棋(Tic Tac Toe)游戏示例 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  Python实现的井字棋(Tic Tac Toe)游戏示 ...

  4. python井字棋_python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  5. LeetCode简单题之找出井字棋的获胜者

    题目 A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上. 第一个玩家 A 总是用 "X" 作 ...

  6. 组合游戏系列5: 井字棋、五子棋AlphaGo Zero 算法实战

    来源 | MyEncyclopedia 上一篇我们从原理层面解析了AlphaGo Zero如何改进MCTS算法,通过不断自我对弈,最终实现从零棋力开始训练直至能够打败任何高手.在本篇中,我们在已有的N ...

  7. php井字游戏,python实现井字棋游戏

    #本游戏python3.4.0下编写调试,只能在windows下运行. import random import subprocess import time #定义函数 def draw_board ...

  8. C++井字棋游戏,DOS界面版

    据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /*N字棋游戏PVP版,DOS版本 ...

  9. 用TensorFlow基于神经网络实现井字棋(含代码)

    为了展示如何应用神经网络算法模型,我们将使用神经网络来学习优化井字棋(Tic Tac Toe).明确井字棋是一种决策性游戏,并且走棋步骤优化是确定的. 开始 为了训练神经网络模型,我们有一系列优化的不 ...

  10. [C++] 井字棋游戏源码

    TicTac.h 1 #define EX 1 //该点左鼠标 2 #define OH 2 //该点右鼠标 3 4 class CMyApp : public CWinApp 5 { 6 publi ...

最新文章

  1. Redis实现分布式锁的深入探究
  2. PTP4L命令手册(谷歌翻译)
  3. 过滤输入内容中是否含有特殊字符与表情
  4. 利用wget 抓取 网站网页 包括css背景图片
  5. Xcode 8 GM 编译缺失 /Users/usr/lib/libresolv.9.dylib
  6. Spring MVC表单教程
  7. android 居右属性,使用layoutDirection属性设置布局靠左或靠右
  8. java 获取季度第一天_Java获取当天、本周、本月、本季度、本年等 开始及结束时间...
  9. hello world! 这是我在博客园的第一个博客!
  10. 五分钟上手ECharts教程
  11. sm是什么职位_职位或职级ED MD SM M 是什么意思?
  12. 重大噩耗:苹果账号无法付款!(11-20更新:账单地址和卡地址一样,信用卡名字和开发者名字一致,都无法付款)
  13. 工程项目成本费用明细表_工程施工合同成本费用明细表有哪些
  14. svg 右键意见删除cvs_一种实现svg自定义鼠标右键菜单的方法
  15. java 运行配置_JAVA运行环境配置
  16. 安信可ESP32-CAM摄像头开发demo--广域网远程实时查看视频流
  17. Excel如何把同一列的内容拆分为两列?
  18. 用 typescript 做一个贪吃蛇小游戏
  19. ps 改变图层纯色与渐变色
  20. 计算机屏幕广播系统,ip网络广播终端_ip广播终端连接图_ip广播系统方案

热门文章

  1. C#使用spitter控件分割左右两个panel并可拖动
  2. AD9854的工作原理和应用电路图
  3. 点阵发光管怎么用C语言编程,LED点阵经验各种点阵驱动方法讲解
  4. 光伏电站清扫机器人_光伏电站清扫机器人
  5. 零经验小白的独游历程——U3d学习经验与教程分享
  6. SQL中DDL、DML、DQL、DCL、TCL是什么意思
  7. 用HTML+CSS做一个漂亮简单大学生校园班级网页
  8. 比亚迪秦后排座椅拆卸
  9. 进阶-第18__深度探秘搜索技术_基于slop参数实现近似匹配以及原理剖析和相关实验
  10. 远程连接服务器出现channel is not opened通道未打开