C++實(shí)現(xiàn)簡單24點(diǎn)游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡單24點(diǎn)游戲的具體代碼,供大家參考,具體內(nèi)容如下
隨機(jī)生成4個代表撲克牌牌面的數(shù)字字母,程序自動列出所有可能算出24的表達(dá)式,用擅長的語言(C/C++/Java或其他均可)實(shí)現(xiàn)程序解決問題。
程序風(fēng)格良好(使用自定義注釋模板)
列出表達(dá)式無重復(fù)。
以下為源代碼
#include<iostream>
#include<string>
#include <stdlib.h>
#include<time.h>
using namespace std;
char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' };
char buf[4];
double nums[4];
char ope[4] = { '+','-','*','/' };
void cre()//生成
{
int i = 0;
int j;
cout << "生成的四張牌面為:";
srand((unsigned)time(0));
for (i = 0; i<4; i++)
{
j =rand() % 13;
buf[i] = card[j];
}
cout << buf[0] << ";" << buf[1] << ";" << buf[2] << ";" << buf[3] << "。" << endl;
for (i = 0; i<4; i++)
{
if (buf[i] == 'A') nums[i] = 1;
else if(buf[i] == '2') nums[i] = 2;
else if (buf[i] == '3') nums[i] = 3;
else if (buf[i] == '4') nums[i] = 4;
else if (buf[i] == '5') nums[i] = 5;
else if (buf[i] == '6') nums[i] = 6;
else if (buf[i] == '7') nums[i] = 7;
else if (buf[i] == '8') nums[i] = 8;
else if (buf[i] == '9') nums[i] = 9;
else if (buf[i] == '10') nums[i] = 10;
else if (buf[i] == 'J') nums[i] = 11;
else if (buf[i] == 'Q') nums[i] = 12;
else if (buf[i] == 'K') nums[i] = 13;
}
}
double calcute(double a, double b, char index)
{
if (index == '+') return a + b; //若為+,則返回相應(yīng)結(jié)果
else if (index == '-') return a - b;
else if (index == '*') return a*b;
else if (index == '/')
if (b != 0)
return a / b; //只有當(dāng)分母不為0時,返回結(jié)果
}
void exh()//窮舉計算
{
double temp[3], tem[2]; //第一個符號放置后,經(jīng)過計算后相當(dāng)于剩下三個數(shù),這個數(shù)組用于存儲這三個數(shù)
double sum; //求得的和
int judge = 0; //判斷是否找到一個合理的解
for (int i = 0; i < 4; i++) //第一次放置的符號
{
for (int j = 0; j < 4; j++) //第二次放置的符號
{
for (int k = 0; k < 4; k++) //第三次放置的符號
{
for (int m = 0; m < 3; m++) //首先計算的兩個相鄰數(shù)字,共有3種情況,相當(dāng)于括號的作用
{
if (nums[m + 1] == 0 && ope[i] == '/') break;
temp[m] = calcute(nums[m], nums[m + 1], ope[i]);
temp[(m + 1) % 3] = nums[(m + 2) % 4];
temp[(m + 2) % 3] = nums[(m + 3) % 4]; //先確定首先計算的兩個數(shù)字,計算完成相當(dāng)于剩下三個數(shù),按順序儲存在temp數(shù)組中
for (int n = 0; n < 2; n++) //三個數(shù)字選出先計算的兩個相鄰數(shù)字,兩種情況,相當(dāng)于第二個括號
{
if (temp[n + 1] == 0 && ope[j] == '/') break;
tem[n] = calcute(temp[n], temp[n + 1], ope[j]);
tem[(n + 1) % 2] = temp[(n + 2) % 3]; //先確定首先計算的兩個數(shù)字,計算完成相當(dāng)于剩下兩個數(shù),按順序儲存在temp數(shù)組中
if (tem[1] == 0 && ope[k] == '/') break;
sum = calcute(tem[0], tem[1], ope[k]); //計算和
if (sum == 24) //若和為24
{
judge = 1; //判斷符為1,表示已求得解
if (m == 0 && n == 0)
cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl;
else if (m == 0 && n == 1)
cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl;
else if (m == 1 && n == 0)
cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl;
else if (m == 1 && n == 1)
cout << nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << ")" << ope[j] << nums[3] << ")=" << sum << endl;
else if (m == 2 && n == 0)
cout << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl;
else if (m == 2 && n == 0)
cout << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl; //m=0,1,2 n=0,1表示六種括號放置可能,并按照這六種可能輸出相應(yīng)的格式的計算式
}
}
}
}
}
}
if (judge == 0)
cout << "這四張撲克牌無法找到一個合理的解" << endl; //如果沒有找到結(jié)果,符號位為0
}
int main()
{
int i;
int select = 1;
cout<< " ################################################" << endl
<< " # #" << endl
<< " # 歡迎進(jìn)入24點(diǎn)游戲 #" << endl
<< " # #" << endl
<< " ################################################" << endl;
while (select)
{
cout<< " ################################################" << endl
<< " # #" << endl
<< " # 是否開始游戲 #" << endl
<< " # #" << endl
<< " # 0.是 1.否 #" << endl
<< " # #" << endl
<< " ################################################" << endl;
cout << "請輸入你的選擇(0或1):";
cin >> i;
switch (i)
{
case 0:
cre();
exh();
break;
case 1:
select = 0;
break;
default:
cout << "請在0和1之間選擇!" << endl;
}
}
return 0;
}

效果圖1

效果圖2
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Matlab實(shí)現(xiàn)動態(tài)表白圖的繪制
這篇文章主要利用Matlab實(shí)現(xiàn)繪制獨(dú)特的表白動圖,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定的幫助,感興趣的小伙伴可以了解一下2022-05-05
淺談C++內(nèi)存分配及變長數(shù)組的動態(tài)分配
下面小編就為大家?guī)硪黄獪\談C++內(nèi)存分配及變長數(shù)組的動態(tài)分配。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
C++實(shí)現(xiàn)獲取時間戳和計算運(yùn)行時長
這篇文章主要為大家詳細(xì)介紹了如何使用C++實(shí)現(xiàn)獲取時間戳和計算運(yùn)行時長功能,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-12-12
Cocos2d-x UI開發(fā)之CCControlColourPicker控件類使用實(shí)例
這篇文章主要介紹了Cocos2d-x UI開發(fā)之CCControlColourPicker控件類使用實(shí)例,本文代碼中包含大量注釋來講解CCControlColourPicker控件類的使用,需要的朋友可以參考下2014-09-09
C語言 structural body結(jié)構(gòu)體詳解用法
C 數(shù)組允許定義可存儲相同類型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是 C 編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許您存儲不同類型的數(shù)據(jù)項(xiàng),結(jié)構(gòu)用于表示一條記錄,假設(shè)您想要跟蹤圖書館中書本的動態(tài),您可能需要跟蹤每本書的下列屬性2021-10-10
C++字符數(shù)組、字符數(shù)組指針和string類
這篇文章主要介紹了C++字符數(shù)組、字符數(shù)組指針和string類,string是一個類而不是基本數(shù)據(jù)類型,數(shù)組不含有處理函數(shù),下面更多詳細(xì)內(nèi)容,需要的小伙伴可以參考下面文章2022-03-03

