C++剪刀石头布游戏设计详解与心得

4星 · 超过85%的资源 需积分: 50 54 下载量 90 浏览量 更新于2024-11-26 4 收藏 87KB DOC 举报
本篇文章主要介绍了如何使用C++语言设计一款剪刀石头布游戏。设计目标是让学生熟练掌握C++编程基础,特别是面向对象编程思想,并能应用到实际问题中。以下是文章的主要知识点和设计过程概述: 1. **知识点与设计思路**: - **继承与派生**:在设计中,运用了继承与派生的概念,创建了两个类,一个是游戏规则的基类,另一个是游戏内容的派生类。派生类(如Player)从基类(如Game)中继承特性,实现游戏规则的抽象,而具体的游戏逻辑则在派生类中实现。 - **函数**:为了保持代码清晰和复用性,采用了函数来封装相对独立的功能,比如用户输入处理、游戏判断和结果输出。 - **控制结构**:游戏流程包括条件判断(如用户输入检查)、分支(根据用户选择决定游戏步骤)和循环(重复游戏直到用户选择退出),这些结构确保了程序的灵活性和执行效率。 - **类与对象**:数据封装体现在类的设计上,外部只能通过接口函数访问和操作数据,避免直接修改游戏状态,保证了数据的安全性和一致性。 2. **程序设计流程**: - 多文件工程结构:将源代码分解为多个文件,提高代码组织和维护性。 - 游戏规则类与游戏内容类:定义两个类,分别负责规则说明和游戏逻辑。 - 电脑出拳:使用randGame()函数根据游戏轮次生成随机选择。 - 用户交互:接受玩家输入,判断其选择并响应,支持查看结果、帮助或退出。 3. **心得体会**: - 通过课程设计,作者体验到了C++编程的乐趣,不仅巩固了理论知识,还了解到计算机基础知识,并对计算机体系结构有了更深入的理解。 - 从范例程序出发,通过实践操作,增强了编程能力和解决问题的能力。 本文详细介绍了C++剪刀石头布游戏的设计过程,展示了面向对象编程的核心概念,并分享了作者在课程设计中的学习心得。通过阅读和理解这个设计,读者不仅能掌握C++编程技巧,还能领悟到面向对象编程的思想精髓。
354 浏览量
简易推箱子 #include "Map.h" #include <conio.h> int XDest = 4; int YDest = 4; void CMap::Init() { int i,j; CPoint cPoint; CSprite *pSphte; CPlayer cPlay; CBox cBox; CDest cDest; int XPlayer = 4; int YPlayer = 6; int XBox = 4; int YBox = 5; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (i == 0 || j == 0 || i == 9 || j == 9) { cPoint.SetPoint(i, j); pSphte = new CWall(cPoint); m_nMap[i][j] = pSphte; } else if (i == XPlayer && j == YPlayer) { cPoint.SetPoint(i,j); pSphte = new CPlayer(cPoint); m_nMap[i][j] = pSphte; } else if (i == XBox && j == YBox) { cPoint.SetPoint(i,j); pSphte = new CBox(cPoint); m_nMap[i][j] = pSphte; } else if (i == XDest && j == YDest) { cPoint.SetPoint(i,j); pSphte = new CDest(cPoint); m_nMap[i][j] = pSphte; } else { cPoint.SetPoint(i, j); pSphte = new CSpace(cPoint); m_nMap[i][j] = pSphte; } } cout<<endl; } } void CMap::Draw() { int i,j; for (i=0; i<10; i++) { for (j=0; j<10; j++) { cout<<m_nMap[i][j]->GetSprite(); } cout<<endl; } } void CMap::Logic() { int n = getch(); //获取输入的数字 char ch = char(n);//将获取到的数字进行强制类型转换 CPlayer *pPlayer = GetPlayer(); int XPlayer = pPlayer->GetX(); int YPlayer = pPlayer->GetY(); CBox *pBox = GetBox(); int XBox = pBox->GetX(); int YBox = pBox->GetY(); CDest *pDest = GetDest(); switch(ch) { case 'a': { int yPlayer = pPlayer->GetY(); YPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer++; YBox++; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'd': { int yPlayer = pPlayer->GetY(); YPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { YPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int yBox = pBox->GetY(); YBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { YPlayer--; YBox--; } CSprite *pB = m_nMap[pBox->GetX()][yBox]; m_nMap[pBox->GetX()][yBox] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetY(YBox); m_nMap[pBox->GetX()][yBox]->SetY(yBox); } CSprite *pP = m_nMap[pPlayer->GetX()][yPlayer]; m_nMap[pPlayer->GetX()][yPlayer] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetY(YPlayer); m_nMap[pPlayer->GetX()][yPlayer]->SetY(yPlayer); } break; case 'w': { int xPlayer = pPlayer->GetX(); XPlayer--; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer++; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox--; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer++; XBox++; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; case 's': { int xPlayer = pPlayer->GetX(); XPlayer++; if ("※" == m_nMap[XPlayer][YPlayer]->GetSprite()) { XPlayer--; } else if (m_nMap[XPlayer][YPlayer]->GetSprite() == "■" || m_nMap[XPlayer][YPlayer]->GetSprite() == "●") { int xBox = pBox->GetX(); XBox++; if (m_nMap[XBox][YBox]->GetSprite() == "※") { XPlayer--; XBox--; } CSprite *pB = m_nMap[xBox][pBox->GetY()]; m_nMap[xBox][pBox->GetY()] = m_nMap[XBox][YBox]; m_nMap[XBox][YBox] = pB; pBox->SetX(XBox); m_nMap[xBox][pBox->GetY()]->SetX(xBox); } CSprite *pP = m_nMap[xPlayer][pPlayer->GetY()]; m_nMap[xPlayer][pPlayer->GetY()] = m_nMap[XPlayer][YPlayer]; m_nMap[XPlayer][YPlayer] = pP; pPlayer->SetX(XPlayer); m_nMap[xPlayer][pPlayer->GetY()]->SetX(xPlayer); } break; default: break; } if ((pPlayer->GetX() == XDest && pPlayer->GetY() == YDest) || (pBox->GetX() == XDest && pBox->GetY() == YDest)) { int i,j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { m_nMap[i][j]->SetSprite(" "); } } } } else if (m_nMap[XDest][YDest]->GetSprite() == " ") { m_nMap[XDest][YDest]->SetSprite("◎"); } if (m_nMap[XDest][YDest]->GetSprite() == "■") { m_nMap[XDest][YDest]->SetSprite("●"); // cout<<"游戏结束"<<endl; } if (pBox->GetX() != XDest || pBox->GetY() != YDest) { pBox->SetSprite("■"); } } CPlayer *CMap::GetPlayer(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "♀") { return (CPlayer *)m_nMap[i][j]; } } } } CBox *CMap::GetBox(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "■" || m_nMap[i][j]->GetSprite() == "●") { return (CBox *)m_nMap[i][j]; } } } } CDest *CMap::GetDest(void) { int i, j; for (i = 0; i < 10; i++) { for (j =0; j < 10; j++) { if (m_nMap[i][j]->GetSprite() == "◎") { return (CDest *)m_nMap[i][j]; } } } }