在中国象棋C++手把手教学中,我们将会深入探讨如何利用C++编程语言来实现一个功能完备的中国象棋游戏。这个过程不仅能够帮助你掌握C++的基本语法和面向对象编程概念,还能够让你理解游戏逻辑和人工智能(AI)的设计。下面我们将详细讲解涉及的知识点。 C++是编程的基础,它是一种静态类型的、编译式的、通用的、大小写敏感的、不仅支持过程化编程,也支持面向对象编程的语言。在创建中国象棋游戏时,你需要熟悉C++的类、对象、函数、指针等核心概念,以便构建游戏结构。 1. **类和对象**:中国象棋的棋子和棋盘都可以抽象为类。每个棋子类可以包含其位置、类型、状态等属性,并提供移动规则的函数。棋盘类则用来存储所有棋子的位置信息,实现落子、检查合法性等功能。 2. **面向对象编程**:C++的继承和多态特性在游戏设计中尤为重要。你可以创建一个棋子基类,然后为不同的棋子(如兵、炮、车、马、象、士、将)创建派生类,每个派生类都有自己的特殊行为。多态性使得你可以用统一的方式处理不同类型的棋子。 3. **数据结构**:为了高效地管理棋盘,可以使用二维数组或自定义的数据结构来表示棋盘。同时,链表或队列可以用于记录每步棋的记录,便于回溯或重播。 4. **函数**:编写一系列的函数来实现游戏的逻辑,比如判断棋局合法性、计算下一步可能的走法、判断胜负等。 5. **输入/输出**:使用C++的iostream库处理用户输入和游戏结果的输出,例如接收玩家的落子指令,显示当前棋盘状态。 6. **错误处理**:在编程过程中,合理的错误处理机制是必不可少的。例如,当玩家尝试在非法位置落子时,程序应能给出相应的提示。 7. **人工智能(AI)**:为了让电脑能够自动下棋,我们需要设计一个AI算法。初级的AI可以采用基于规则的方法,如深度优先搜索配合简单的评估函数;进阶的AI则可能涉及到蒙特卡洛树搜索(MCTS)或其他更复杂的算法。 8. **调试技巧**:在开发过程中,学会使用调试工具如gdb,理解和使用断点、单步执行、查看变量值等功能,对于找出并修复bug至关重要。 9. **版本控制**:使用Git进行版本控制,可以帮助你跟踪代码的修改历史,方便协作和回滚到特定版本。 通过实践这个项目,你不仅能够巩固C++编程技能,还能提升解决问题和逻辑思维的能力。阅读《代码的力量 C C 中国象棋程序入门与提高 蒋鹏等.pdf》和《中国象棋.ppt》这两份资料,将有助于你深入理解上述知识点,并逐步构建出自己的中国象棋游戏。
2025-07-07 14:57:57 98.81MB AI 中国象棋
1
中国象棋的C++代码 #include "chess_zn.h" QTcpSocket * Chess_ZN::client = new QTcpSocket; QUndoStack * Chess_ZN::undoStack = new QUndoStack(); int Chess_ZN::second = 120; bool Chess_ZN::isTurn = false; Chess_ZN::Chess_ZN(QWidget *parent) : QWidget(parent) { init(); initElse(); } void Chess_ZN::initElse(){ treeitem = 1; timer=new QTimer; portmap=0; isConn = true; start = false; isTimer = false; isSearch = false; connect(timer,SIGNAL(timeout()),this,SLOT(stopWatch())); connect(wigettree[1],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(getInfo(QTreeWidgetItem*))); connect(wigettree[0],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(connectToHost_PK(QTreeWidgetItem*))); connect(client,SIGNAL(connected()),this,SLOT(connected())); //连接一旦断开 connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError ))); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); peer = new PeerManager(this); peer->setServerPort(10001); items=wigettree[1]->currentItem(); item_pk=wigettree[0]->currentItem(); item_pk_info=wigettree[0]->currentItem(); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[8], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[9], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[10], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[11], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[0], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[1], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[2], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[3], SLOT(setEnabled(bool))); timer->start(1000); createUndoView(); isChoose = true; tableeditor=new TableEditor("users"); } void Chess_ZN::createUndoView() { undoView = new QUndoView(undoStack); undoView->setWindowTitle(tr("Command List")); //undoView->show(); undoView->setAttribute(Qt::WA_QuitOnClose, false); } void Chess_ZN::init(){ createActions(); createMenus(); menubar = new QMenuBar; for (int i = 0; i < 4; i++) { action1[i]->setMenu(menu[i]); menubar->addAction(action1[i]); } for (int i = 0; i < 4; i++) { hblayout[i] = new QHBoxLayout; } for (int i = 0; i < 13; i++) { vblayout[i] = new QVBoxLayout; } for (int i = 0; i < 8; i++) { if(i<4){ vblayout[1]->addWidget(boxs[i]); }else if(i>=4 && i<6){ vblayout[2]->addWidget(boxs[i]); }else{ vblayout[3]->addWidget(boxs[i]); } } for (int i = 1; i < 4; i++) { hblayout[0]->addLayout(vblayout[i]); } initwidgets(); vblayout[0]->addWidget(menubar); vblayout[0]->addLayout(hblayout[0]); this->setLayout(vblayout[0]); } void Chess_ZN::createActions(){ action1[0]= new QAction(utf("游戏设置"),this); action1[1]= new QAction(utf("文件"),this); action1[2]= new QAction(utf("操作"),this); action1[3]= new QAction(utf("关于"),this); action2[0]= new QAction(utf("新建服务器"),this); action2[0]->setShortcut(tr("Ctrl+N")); action2[1]= new QAction(utf("搜索服务器"),this); action2[1]->setShortcut(tr("Ctrl+F")); action2[2]= new QAction(utf("游戏设置"),this); action2[2]->setShortcut(tr("Ctrl+G")); action2[3]= new QAction(utf("退出"),this); action2[3]->setShortcut(tr("Ctrl+Q")); action2[4]= new QAction(utf("打开"),this); action2[4]->setShortcut(tr("Ctrl+O")); action2[5]= new QAction(utf("保存"),this); action2[5]->setShortcut(tr("Ctrl+S")); action2[6]= new QAction(utf("另存为"),this); action2[6]->setShortcut(tr("Ctrl+E")); action2[7]= new QAction(utf("载入棋盘"),this); action2[7]->setShortcut(tr("Ctrl+L")); action2[8]= new QAction(utf("悔棋"),this); action2[8]->setShortcut(tr("Ctrl+D")); action2[8]->setEnabled(false); action2[9]= new QAction(utf("求和"),this); action2[9]->setShortcut(tr("Ctrl+P")); action2[9]->setEnabled(false); action2[10]= new QAction(utf("认输"),this); action2[10]->setShortcut(tr("Ctrl+T")); action2[10]->setEnabled(false); action2[11]= new QAction(utf("重来"),this); action2[11]->setShortcut(tr("Ctrl+B")); action2[11]->setEnabled(false); action2[12]= new QAction(utf("关于"),this); action2[12]->setShortcut(tr("Ctrl+A")); for (int i = 0; i < 12; i++) { connect(action2[i],SIGNAL(triggered()),this,SLOT(allAtions())); } connect(undoStack, SIGNAL(indexChanged (int )), this, SLOT(undonext(int ))); } void Chess_ZN::undonext(int dex){ QString ssup = undoStack->text(dex-1); QString ssdown = undoStack->text(dex); ssdown = ssdown.left(ssdown.indexOf(".")); ssup = ssup.left(ssup.indexOf(".")); if(ssup.compare(ssdown)==0){ undoStack->undo(); } } void Chess_ZN::createMenus(){ for (int i = 0; i < 4; i++) { menu[i] = new QMenu; } for (int i = 0; i < 4; i++) { menu[0]->addAction(action2[i]); } for (int i = 4; i < 8; i++) { menu[1]->addAction(action2[i]); } for (int i = 8; i < 12; i++) { menu[2]->addAction(action2[i]); } menu[3]->addAction(action2[12]); boxs[0] = new QGroupBox(utf("对手信息")); boxs[1] = new QGroupBox(utf("我的信息")); boxs[2] = new QGroupBox(utf("系统信息")); boxs[3] = new QGroupBox(utf("聊天对象")); boxs[4] = new QGroupBox(utf("棋逢对手")); boxs[5] = new QGroupBox(utf("你想干嘛")); boxs[6] = new QGroupBox(utf("玩家列表")); boxs[7] = new QGroupBox(utf("消息栏")); for (int i = 0; i < 6; i++) { formLayout[i] = new QFormLayout; } } void Chess_ZN::initwidgets(){ scene = new DiagramScene(client,this); QPixmap pmap(":/Resources/chessbg.png"); QBrush pixmapBrush(pmap.scaled(pmap.width(),pmap.height())); scene->setBackgroundBrush(pixmapBrush); scene->setSceneRect(QRect(0, 0, pmap.width(),pmap.height())); scene->initItem(0); graview =new QGraphicsView(scene); graview->setFixedSize(pmap.width()+5,pmap.height()+5); connect(scene, SIGNAL(itemMoved(DiagramItem *, const QPointF &)), this, SLOT(itemMoved(DiagramItem *, const QPointF &))); button[0]=new QPushButton(utf("悔棋")); button[1]=new QPushButton(utf("求和")); button[2]=new QPushButton(utf("认输")); button[3]=new QPushButton(utf("重来")); button[4]=new QPushButton(utf("邀请对战")); button[5]=new QPushButton(utf("排行榜")); button[6]=new QPushButton(utf("刷新信息")); button[7]=new QPushButton(utf("远程求助")); button[8]=new QPushButton(utf("请对方确认")); button[9]=new QPushButton(utf("清空消息")); button[10]=new QPushButton(utf("发送消息")); button[11]=new QPushButton(utf("请对方认可")); button[12]=new QPushButton(utf("加入服务器")); button[13]=new QPushButton(utf("退出服务器")); button[14]=new QPushButton(utf("开始")); button[0]->setEnabled(false); button[1]->setEnabled(false); button[2]->setEnabled(false); button[3]->setEnabled(false); button[4]->setEnabled(false); button[13]->setEnabled(false); for (int i= 0; i < 15; ++i) { connect(button[i],SIGNAL(clicked()),this,SLOT(allButtons())); } ////棋逢对手 vblayout[4]->addWidget(graview); boxs[4]->setLayout(vblayout[4]); ////你想干嘛 hblayout[1]->addWidget(button[14]); for (int i = 0; i < 4; i++) { hblayout[1]->addWidget(button[i]); } boxs[5]->setLayout(hblayout[1]); ////玩家列表 wigettree[0] = new QTreeWidget; wigettree[0]->setColumnCount(3); wigettree[1] = new QTreeWidget; wigettree[1]->setColumnCount(3); QStringList slist; slist << utf("状态") << utf("用户名") << utf("IP地址"); QStringList slistoo; slistoo << utf("服务器") << utf("信息") << utf("IP地址"); wigettree[0]->setHeaderLabels(slist); wigettree[1]->setHeaderLabels(slistoo); formLayout[1]->addRow(button[4],button[5]); formLayout[1]->addRow(button[6],button[7]); formLayout[4]->addRow(button[12],button[13]); vblayout[5]->addWidget(wigettree[1]); vblayout[5]->addLayout(formLayout[4]); vblayout[5]->addWidget(wigettree[0]); vblayout[5]->addLayout(formLayout[1]); boxs[6]->setLayout(vblayout[5]); ////消息栏 edit[1]=new QTextEdit; line[2]=new QLineEdit; edit[1]->setReadOnly(true); formLayout[3]->addRow(button[9],button[10]); vblayout[6]->addWidget(edit[1]); vblayout[6]->addWidget(line[2]); vblayout[6]->addLayout(formLayout[3]); boxs[7]->setLayout(vblayout[6]); ////对手信息 label[0] = new QLabel(utf("NONE")); label[1] = new QLabel(utf("NULL")); vblayout[7]->addWidget(label[0]); vblayout[7]->addWidget(label[1]); boxs[0]->setLayout(vblayout[7]); ////我的信息 label[2] = new QLabel(utf("NONE")); label[3] = new QLabel(utf("NULL")); vblayout[8]->addWidget(label[2]); vblayout[8]->addWidget(label[3]); boxs[1]->setLayout(vblayout[8]); ////系统信息 QLabel *laba = new QLabel(utf("系统提示:")); QLabel *labb = new QLabel(utf("请选择:")); radio[2]=new QRadioButton(utf("红方先走")); radio[3]=new QRadioButton(utf("黑方先走")); radio[4]=new QRadioButton(utf("红子")); radio[5]=new QRadioButton(utf("黑子")); vblayout[9]->addWidget(laba); vblayout[9]->addWidget(new QLabel(utf("1.黑方准备"))); vblayout[9]->addWidget(new QLabel(utf("2.红方开始"))); vblayout[9]->addWidget(new QLabel(utf("3.红方先走"))); vblayout[10]->addWidget(labb); vblayout[10]->addWidget(radio[4]); vblayout[10]->addWidget(radio[5]); vblayout[10]->addWidget(button[11]); hblayout[3]->addLayout(vblayout[9]); hblayout[3]->addLayout(vblayout[10]); label[4]=new QLabel(utf("120秒倒计时(您有120秒考虑时间)")); label[5]=new QLabel(utf("XX方剩余时间为: ")); lcd = new QLCDNumber; lcd->setNumDigits(4); formLayout[5]->addRow(label[5],lcd); vblayout[11]->addLayout(hblayout[3]); vblayout[11]->addWidget(label[4]); vblayout[11]->addLayout(formLayout[5]); boxs[2]->setLayout(vblayout[11]); boxs[2]->setEnabled(false); ////聊天对象 line[0] = new QLineEdit; line[1] = new QLineEdit; QLabel lab(utf("聊天对象")); radio[0]=new QRadioButton(utf("对手(发送信息时直接Enter)")); radio[0]->setEnabled(false); radio[1]=new QRadioButton(utf("指定IP:")); formLayout[2]->addRow(radio[1],line[0]); line[0]->setInputMask("000.000.000.000;"); edit[0]=new QTextEdit; edit[0]->setReadOnly(true); vblayout[12]->addWidget(&lab); vblayout[12]->addWidget(radio[0]); vblayout[12]->addLayout(formLayout[2]); vblayout[12]->addWidget(edit[0]); vblayout[12]->addWidget(line[1]); boxs[3]->setLayout(vblayout[12]); connect(line[1],SIGNAL(returnPressed()),this,SLOT(returnPressed())); } void Chess_ZN::allAtions(){ QAction *ac =qobject_cast(sender()); QString ss = ac->text(); if(ss.compare(utf("新建服务器"))==0){ server = new ChessServer(this); server->show(); }else if(ss.compare(utf("取消服务器"))==0){ server->closex(); wigettree[1]->clear(); wigettree[0]->clear(); treeitem = 1; action2[0]->setText(utf("新建服务器")); }else if(ss.compare(utf("搜索服务器"))==0){ bool ok; int i = QInputDialog::getInteger(this, utf("请输入"),utf("服务器端口"), 9999, 0, 65535, 1, &ok); if(ok){ portmap = i ; peer->sendBroadcastDatagram(1); } }else if(ss.compare(utf("悔棋"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 5; client->write(byte); }else if(ss.compare(utf("求和"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 9; client->write(byte); }else if(ss.compare(utf("认输"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 11; client->write(byte); }else if(ss.compare(utf("重来"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 13; client->write(byte); } } void Chess_ZN::allButtons(){ QPushButton *ac =qobject_cast(sender()); QString ss = ac->text(); if(ss.compare(utf("刷新信息"))==0){ portmap = 9999; peer->sendBroadcastDatagram(1); }else if(ss.compare(utf("加入服务器"))==0){ connectToHost(); }else if(ss.compare(utf("退出服务器"))==0){ client->close(); items=wigettree[1]->currentItem(); button[12]->setEnabled(true); button[13]->setEnabled(false); button[4]->setEnabled(false); peer->sendBroadcastDatagram(3); //peer->sendBroadcastDatagram(1); }else if(ss.compare(utf("邀请对战"))==0){ send_pk_vs(); }else if(ss.compare(utf("请对方认可"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 3; if(radio[4]->isChecked()){ in << 0; }else{ in << 1; } client->write(byte); }else if(ss.compare(utf("悔棋"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 5; client->write(byte); }else if(ss.compare(utf("准备"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 7; client->write(byte); button[14]->setEnabled(false); button[11]->setEnabled(false); isTurn = false; }else if(ss.compare(utf("开始"))==0){ if(start){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 8; client->write(byte); isTimer = true; scene->whoturn(1); button[14]->setEnabled(false); isTurn = true; }else{ QMessageBox::information(this,utf("对弈信息"),utf("您好,对手还没有准备好,请稍等..."),QMessageBox::Ok); } }else if(ss.compare(utf("求和"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 9; client->write(byte); }else if(ss.compare(utf("认输"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 11; client->write(byte); }else if(ss.compare(utf("重来"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 13; client->write(byte); }else if(ss.compare(utf("清空消息"))==0){ edit[1]->clear(); }else if(ss.compare(utf("发送消息"))==0){ QString s=line[2]->text(); if(s.trimmed().compare("")!=0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 16; in << QHostInfo::localHostName(); in << s; peer->returnUdp()->writeDatagram(byte.data(),byte.size(),QHostAddress::Broadcast,10001); line[2]->clear(); } }else if(ss.compare(utf("远程求助"))==0){ send_pk_vs_info(); }else if(ss.compare(utf("排行榜"))==0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 16; client->write(byte); } } void Chess_ZN::send_pk_vs_info(){ item_pk_info=wigettree[0]->currentItem(); if(!item_pk_info){ item_pk_info=wigettree[0]->currentItem(); if(!item_pk_info) return; } QString ip=item_pk_info->text(0); if(ip.compare("wait...")==0){ QMessageBox::information(this,utf("连接信息"),utf("您好,连接成功"),QMessageBox::Ok); } } void Chess_ZN::send_pk_vs(){/////////----------------邀请对战 item_pk=wigettree[0]->currentItem(); if(!item_pk){ item_pk=wigettree[0]->currentItem(); if(!item_pk) return; } QString ip=item_pk->text(2); QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 1 << ip ; client->write(byte); } void Chess_ZN::connectToHost_PK(QTreeWidgetItem*item){ if(isChoose){ item_pk=wigettree[0]->currentItem(); if(!item_pk){ item_pk=wigettree[0]->currentItem(); if(!item_pk) return; } bool bo = false; if(!client->isOpen()){ button[4]->setEnabled(false); return; } QString ip=item_pk->text(2); for (int i= 0; i < peer->ipAddresses.size(); ++i) { if(ip.compare(peer->ipAddresses.at(i).toString())==0){ bo = true; break; } } if(bo){ button[4]->setEnabled(false); }else{ button[4]->setEnabled(true); } } } void Chess_ZN::getInfo(QTreeWidgetItem*item){ if(!item){ item=wigettree[1]->currentItem(); if(!item) return; } QString ip=item->text(2); peer->sendBroadcastDatagram(ip); items=wigettree[1]->currentItem(); } void Chess_ZN::connectToHost(){ if(!items){ items=wigettree[1]->currentItem(); if(!items) return; } QString ip=items->text(2); conip = ip; client->connectToHost(ip,portmap); button[12]->setEnabled(false); button[13]->setEnabled(true); button[4]->setEnabled(false); } void Chess_ZN::connected(){ QString name=QHostInfo::localHostName(); QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); int flag=0; in << flag << name; client->write(byte); QMessageBox::information(this,utf("连接信息"),utf("您好,连接成功"),QMessageBox::Ok); } void Chess_ZN::error(QAbstractSocket::SocketError socketError){ QMessageBox::information(this,utf("连接信息"),utf(tr("对不起,连接失败: %1.").arg(client->errorString())),QMessageBox::Ok); wigettree[1]->clear(); wigettree[0]->clear(); treeitem = 1; } void Chess_ZN::readyRead(){ QByteArray data=client->readAll(); QDataStream out(data); int flag=0; out >> flag; if(flag == 0){ ///接受对战方 QString Aip; QString name; out >> Aip; out >> name; int i=QMessageBox::question(this,utf("连接"),utf(Aip+"----请您对弈!!!"),QMessageBox::Yes,QMessageBox::No); QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in<<100; if(i==QMessageBox::Yes){ label[0]->setText(name); label[1]->setText(Aip); label[2]->setText(QHostInfo::localHostName()); label[3]->setText(peer->ipAddresses.at(0).toString()); scene->addItems(); scene ->initItem(1); button[14]->setText(utf("准备")); button[4]->setEnabled(false); isChoose = false; radio[0]->setEnabled(true); radio[0]->setChecked(true); boxs[2]->setEnabled(true); radio[5]->setChecked(true); in<<1; client->write(byte); }else{ in<<-1; client->write(byte); } }else if(flag == 1){///发起对战方 int ok; out >> ok; if(ok == 1){ QString Aip; QString name; out >> Aip; out >> name; QMessageBox::information(this,utf("对战信息"),utf("您好,对方接受挑战"),QMessageBox::Ok); label[0]->setText(name); label[1]->setText(Aip); label[2]->setText(QHostInfo::localHostName()); label[3]->setText(peer->ipAddresses.at(0).toString()); scene->addItems(); scene ->initItem(0); button[14]->setText(utf("开始")); button[4]->setEnabled(false); isChoose = false; boxs[2]->setEnabled(true); radio[4]->setChecked(true); radio[0]->setEnabled(true); radio[0]->setChecked(true); }else if(ok == -1){ QMessageBox::information(this,utf("对战信息"),utf("您好,对方拒绝挑战"),QMessageBox::Ok); } }else if(flag ==2){///棋子交换信息 int mapValue; QPoint point; out >> mapValue >> point; doWithInfo(mapValue,point); scene->whoturn(1); isTurn = true; second=120; }else if(flag == 3){ int se; out >> se; QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in<<4; if(scene->r_or_b == 0){ if(se ==0 ){ int i=QMessageBox::question(this,utf("换棋"),utf("对方想换棋,您是否同意?"),QMessageBox::Yes,QMessageBox::No); if(i==QMessageBox::Yes){ scene ->initItem(1); button[14]->setText(utf("准备")); radio[5]->setChecked(true); in<write(byte); }else{ in<write(byte); } } }else{ if(se ==1 ){ int i=QMessageBox::question(this,utf("换棋"),utf("对方想换棋,您是否同意?"),QMessageBox::Yes,QMessageBox::No); if(i==QMessageBox::Yes){ scene ->initItem(0); button[14]->setText(utf("开始")); radio[4]->setChecked(true); in<write(byte); }else{ in<write(byte); } } } }else if(flag == 4){ QString se; out >> se; if(se.compare("ok")==0){ QMessageBox::information(this,utf("换棋信息"),utf("您好,对方同意换棋"),QMessageBox::Ok); if(scene->r_or_b == 0){ scene ->initItem(1); button[14]->setText(utf("准备")); }else{ scene ->initItem(0); button[14]->setText(utf("开始")); } }else{ QMessageBox::information(this,utf("换棋信息"),utf("您好,对方不同意换棋"),QMessageBox::Ok); } }else if(flag == 5){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 6; int i=QMessageBox::question(this,utf("悔棋"),utf("对方想悔棋,您是否同意?"),QMessageBox::Yes,QMessageBox::No); if(i==QMessageBox::Yes){ if(scene->r_or_b == 0){ if(undoStack->text(undoStack->index()-1).contains("RED")){ undoStack->undo(); undoStack->undo(); }else{ undoStack->undo(); } }else if(scene->r_or_b == 1){ if(undoStack->text(undoStack->index()-1).contains("BLACK")){ undoStack->undo(); undoStack->undo(); }else{ undoStack->undo(); } } in<write(byte); scene->whoturn(0); second=120; }else{ in<write(byte); } }else if(flag == 6){ QString se; out >> se; if(se.compare("ok")==0){ scene->whoturn(1); second=120; QMessageBox::information(this,utf("悔棋信息"),utf("您好,对方同意悔棋"),QMessageBox::Ok); if(scene->r_or_b == 0){ //QMessageBox::information(this,QString::number(undoStack->index()-1),undoStack->text(undoStack->index()-1),QMessageBox::Ok); if(undoStack->text(undoStack->index()-1).contains("RED")){ undoStack->undo(); }else{ undoStack->undo(); undoStack->undo(); } }else if(scene->r_or_b == 1){ if(undoStack->text(undoStack->index()-1).contains("BLACK")){ undoStack->undo(); }else{ undoStack->undo(); undoStack->undo(); } } }else{ QMessageBox::information(this,utf("悔棋信息"),utf("您好,对方不同意悔棋"),QMessageBox::Ok); } }else if(flag == 7){ button[11]->setEnabled(false); QMessageBox::information(this,utf("对手信息"),utf("您好,对手已准备好,开始吧!!!"),QMessageBox::Ok); start = true; }else if(flag == 8){ isTimer = true; }else if(flag == 9){ int i=QMessageBox::question(this,utf("求和"),utf("对方想求和,您是否同意?"),QMessageBox::Yes,QMessageBox::No); QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 10; if(i==QMessageBox::Yes){ initAgain(); in << 1; }else{ in << 0; } client->write(byte); }else if(flag == 10){ int ff; out >>ff; if( ff ==1 ){ QMessageBox::information(this,utf("求和信息"),utf("对方同意求和!!!"),QMessageBox::Ok); initAgain(); }else{ QMessageBox::information(this,utf("求和信息"),utf("对方不同意求和!!!"),QMessageBox::Ok); } }else if(flag == 11){ int i=QMessageBox::question(this,utf("认输"),utf("对方想认输,您是否同意?"),QMessageBox::Yes,QMessageBox::No); QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 12; if(i==QMessageBox::Yes){ initAgain(); in << 1; }else{ in << 0; } client->write(byte); }else if(flag == 12){ int ff; out >>ff; if( ff ==1 ){ QMessageBox::information(this,utf("认输信息"),utf("对方同意您认输!!!"),QMessageBox::Ok); initAgain(); }else{ QMessageBox::information(this,utf("认输信息"),utf("对方不同意您认输!!!"),QMessageBox::Ok); } }else if(flag == 13){ int i=QMessageBox::question(this,utf("重来"),utf("对方想重来,您是否同意?"),QMessageBox::Yes,QMessageBox::No); QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 14; if(i==QMessageBox::Yes){ initAgain(); in << 1; }else{ in << 0; } client->write(byte); }else if(flag == 14){ int ff; out >>ff; if( ff ==1 ){ QMessageBox::information(this,utf("重来信息"),utf("对方同意重来!!!").append(QString::number(scene ->selectedItems().isEmpty())),QMessageBox::Ok); initAgain(); }else{ QMessageBox::information(this,utf("重来信息"),utf("对方不同意重来!!!"),QMessageBox::Ok); } }else if(flag == 15){ QString name; QString ss; out >> name; out >> ss; edit[0]->append(name+":\n "+ss); }else if(flag == 16){ int size; out >> size; if(size>0){ QStringList list; list << "name" << "time" << "wins" <<"ip"; tableeditor->widget->setRowCount(size); tableeditor->widget->setHorizontalHeaderLabels(list); QVariant name; QVariant time; QVariant wins; QVariant ip; for(int i=0;i> name; QTableWidgetItem * item1 = new QTableWidgetItem; item1->setText(name.toString()); out >> time; QTableWidgetItem * item2 = new QTableWidgetItem; item2->setText(time.toString()); out >> wins; QTableWidgetItem * item3 = new QTableWidgetItem; item3->setText(wins.toString()); out >> ip; QTableWidgetItem * item4 = new QTableWidgetItem; item4->setText(ip.toString()); tableeditor->widget->setItem(i,0,item1); tableeditor->widget->setItem(i,1,item2); tableeditor->widget->setItem(i,2,item3); tableeditor->widget->setItem(i,3,item4); } tableeditor->show(); }else{ QMessageBox::information(this,utf("排行榜"),utf("不好意思,没有记录!!!"),QMessageBox::Ok); } }else if(flag == 17){ initAgain(); } } void Chess_ZN::doWithInfo(int mapValue,QPoint point){ scene->index++; int x = 8- point.x(); int y = 9- point.y(); int xl = DiagramScene::xl; int yl = DiagramScene::yl; int xw = DiagramScene::xw; int yh = DiagramScene::yh; if(scene->r_or_b == 0){ if(scene->map[y][x]!=0){ //scene->removeItem(scene->diaitemR[(scene->map[y][x])-20-1]); QUndoCommand *deleteCommand = new DeleteCommand(scene->index,scene->map, scene->diaitemR[(scene->map[y][x])-20-1]); undoStack->push(deleteCommand); } DiagramItem *diagramItem = scene->diaitemB[mapValue-20-1]; int oldx = diagramItem->localX(); int oldy = diagramItem->localY(); scene->diaitemB[mapValue-20-1]->setPos(xl+xw*x,yl+yh*y); undoStack->push(new MoveCommand(scene->index,scene->map,diagramItem,QPointF(xl+xw*oldx,yl+yh*oldy))); /*int oldx = scene->diaitemB[mapValue-20-1]->localX(); int oldy = scene->diaitemB[mapValue-20-1]->localY(); scene->diaitemB[mapValue-20-1]->setPos(xl+xw*x,yl+yh*y); scene->diaitemB[mapValue-20-1]->setLocalX(x); scene->diaitemB[mapValue-20-1]->setLocalY(y); scene->map[y][x]=(mapValue-20); scene->map[oldy][oldx]=0;*/ }else{ if(scene->map[y][x]!=0){ //scene->removeItem(scene->diaitemB[(scene->map[y][x])-20-1]); QUndoCommand *deleteCommand = new DeleteCommand(scene->index,scene->map,scene->diaitemB[(scene->map[y][x])-20-1]); undoStack->push(deleteCommand); } DiagramItem *diagramItem = scene->diaitemR[mapValue-20-1]; int oldx = diagramItem->localX(); int oldy = diagramItem->localY(); scene->diaitemR[mapValue-20-1]->setPos(xl+xw*x,yl+yh*y); undoStack->push(new MoveCommand(scene->index,scene->map,diagramItem,QPointF(xl+xw*oldx,yl+yh*oldy))); /* int oldx = scene->diaitemR[mapValue-20-1]->localX(); int oldy = scene->diaitemR[mapValue-20-1]->localY(); scene->diaitemR[mapValue-20-1]->setPos(xl+xw*x,yl+yh*y); scene->diaitemR[mapValue-20-1]->setLocalX(x); scene->diaitemR[mapValue-20-1]->setLocalY(y); scene->map[y][x]=(mapValue-20); scene->map[oldy][oldx]=0;*/ } } QString Chess_ZN::utf(QString s){ QByteArray ba = s.toLatin1(); const char * schar = ba.data(); QString ss=s.fromLocal8Bit(schar); return ss; } void Chess_ZN::stopWatch(){ if(isTimer){ if(scene->r_or_b == 0){ if(scene->diaitemR[0]->flags() == QGraphicsItem::ItemIsMovable){ label[5]->setText(utf("红方剩余时间为: ")); }else{ label[5]->setText(utf("黑方剩余时间为: ")); } }else{ if(scene->diaitemB[0]->flags() == QGraphicsItem::ItemIsMovable){ label[5]->setText(utf("黑方剩余时间为: ")); }else{ label[5]->setText(utf("红方剩余时间为: ")); } } lcd->display(--second); if(second == 0){ scene->whoturn(0); //QMessageBox::information(this,utf("通知..."),QString::number(isTurn),QMessageBox::Ok); if(isTurn){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 17; client->write(byte); initAgain(); QMessageBox::information(this,utf("通知..."),utf("不好意思,按规定您已经弃权认输了!!!"),QMessageBox::Ok); } } } //peer->sendBroadcastDatagram(1); } void Chess_ZN::initAgain(){ for (int i= 0; i < 16; ++i) { scene->removeItem(scene->diaitemB[i]); scene->removeItem(scene->diaitemR[i]); } start = false; second = 120; isTimer = false; isChoose = true; scene ->initItem(scene->r_or_b); scene ->whoturn(0); undoStack->clear(); label[5]->setText(utf("XX剩余时间为: ")); lcd->display(0); button[14]->setEnabled(true); button[11]->setEnabled(true); scene->addItems(); scene->initItem(scene->r_or_b); } void Chess_ZN::returnPressed(){ QString ss = line[1]->text(); if(ss.trimmed().compare("")!=0){ QByteArray byte; QDataStream in (&byte,QIODevice::ReadWrite); in << 15; in << QHostInfo::localHostName(); in << ss; if(radio[0]->isChecked()){ client->write(byte); }else if(radio[1]->isChecked()){ peer->returnUdp()->writeDatagram(byte.data(),byte.size(),QHostAddress(line[0]->text()),10001); } edit[0]->append(QHostInfo::localHostName()+":\n "+ss); line[1]->clear(); } } void Chess_ZN::itemMoved(DiagramItem *movedItem,const QPointF &oldPosition) { //undoStack->push(new MoveCommand(movedItem, oldPosition)); } Chess_ZN::~Chess_ZN() { }
2023-04-13 16:00:39 29.14MB chess 中国象棋
1
做课程设计C语言小程序代码
2023-03-29 19:44:19 18KB c语言
1
有关中国象棋的C#源代码请大家仔细阅读并下载相信对大家一定会有很大的帮助 愿大家学习愉快工作愉快事业呢顺心
1
unity小游戏源码中国象棋C#语言开发。完整的源拿来学习研究很不错也可以可直接运营。
2022-01-10 21:09:46 140.97MB unity小游戏源码中国象棋C#
这是一个中国象棋的C++源代码,方便同学们学习一下C++的技术,以及各类的灵活运用。加深对vc的mfc控件的理解
2021-12-01 13:55:15 11.05MB 中国象棋 C++
1
C语言的中国象棋, 自己编写的, 上个学期的 project , syracuse university ECS 102 。
2021-06-06 23:52:48 22KB 中国象棋, C语言
1
中国象棋c语言源代码中国象棋c语言源代码
2021-06-02 13:52:50 178KB 中国象棋c语言源代码
1
用C++实现了网络象棋对战的开发,对于游戏设计、网络Socket通信都有很大借鉴作用,所有代码编译通过。
2021-05-12 16:29:52 414KB 中国象棋 C++ 网络游戏 代码
1
基于FunCode平台下c++开发的中国象棋小游戏,资源包含完整的vs2010版本工程,代码有详细注释,易于理解。适用于初学者学习和采纳。
2019-12-21 22:13:31 26.98MB 中国象棋c++
1