導航:首頁 > 編程語言 > 一個簡單游戲的編程

一個簡單游戲的編程

發布時間:2024-04-02 10:43:01

A. 奼傚嚑C璇璦涓灝忔父鎴忎唬鐮侊紝綆鍗曠殑錛岃佹敞閲娿併佽阿璋浜嗐

// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24鐐規父鎴
*/
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "string.h"/*
浠庝竴鍓鎵戝厠鐗屼腑錛屼換鍙4寮犮
2-10 鎸夊叾鐐規暟璁$畻(涓轟簡琛ㄧず鏂逛究10鐢═琛ㄧず)錛孞,Q,K,A 緇熶竴鎸 1 璁$畻
瑕佹眰閫氳繃鍔犲噺涔橀櫎鍥涘垯榪愮畻寰楀埌鏁板瓧 24銆
鏈紼嬪簭鍙浠ラ殢鏈烘娊鍙栫焊鐗岋紝騫剁敤璇曟帰娉曟眰瑙c
*/void GivePuzzle(char* buf)
{
char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i<4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i<5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c=='T') return 10;
if(c>='0' && c<='9') return c - '0';
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
case 3:
return '/';
} return ' ';
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 - op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)>0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i<4; i++)
{
if( question[i] == 'T' )
strcpy(p[i], "10");
else
sprintf(p[i], "%c", question[i]);
}

switch(type)
{
case 0:
sprintf(answer, "%s %c (%s %c (%s %c %s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, "%s %c ((%s %c %s) %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, "(%s %c %s) %c (%s %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, "((%s %c %s) %c %s) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, "(%s %c (%s %c %s)) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 絳夊緟鑰冪敓瀹屾垚
int type[5]={0,1,2,3,4};//璁$畻綾誨瀷
double p[4];
double sum=0;
//
for(int i=0; i<4; i++) //寰鐜鍙栧緱鐐規暟
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i<5;i++)
{
MakeAnswer(answer,type[i],question,oper); //鑾峰彇鍙鑳界殑絳旀
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
閲囩敤闅忔満璇曟帰娉曪細灝辨槸閫氳繃闅忔満鏁板瓧浜х敓 鍔犲噺涔橀櫎鐨 緇勫悎錛岄氳繃澶ч噺鐨勬祴璇曟潵鍛戒腑鐨勮В娉
鎻愮ず錛
1. 闇瑕佽冭檻鐢ㄦ嫭鍙鋒帶鍒惰$畻嬈″簭鐨勯棶棰 姣斿傦細( 10 - 4 ) * ( 3 + A ), 瀹為檯涓婅$畻嬈″簭鐨勬暟鐩鏄鏈夐檺鐨勶細
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 闇瑕佽冭檻璁$畻緇撴灉涓哄垎鏁扮殑鎯呭喌錛( 3 + (3 / 7) ) * 7
3. 棰樼洰涓鐗岀殑浣嶇疆鍙浠ヤ換鎰忎氦鎹
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 瀛樺偍榪愮畻絎︼紝0:鍔犳硶 1:鍑忔硶 2:涔樻硶 3:闄ゆ硶
for(int i=0; i<1000 * 1000; i++)
{
// 鎵撲貢綰哥墝欏哄簭
shuffle(question);

// 闅忔満浜х敓榪愮畻絎
for(int j=0; j<3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 鍒濆嬪寲闅忔満縐嶅瓙
srand( (unsigned)time( NULL ) ); char buf1[4]; // 棰樼洰
char buf2[30]; // 瑙g瓟
printf("***************************\n");
printf("璁$畻24\n");
printf("A J Q K 鍧囨寜1璁$畻錛屽叾瀹冩寜鐗岀偣璁$畻\n");
printf("鐩鏍囨槸錛氶氳繃鍥涘垯榪愮畻緇勫悎鍑虹粨鏋滐細24\n");
printf("***************************\n\n");
for(;;)
{
GivePuzzle(buf1); // 鍑洪
printf("棰樼洰錛");
for(int j=0; j<4; j++){
if( buf1[j] == 'T' )
printf("10 ");
else
printf("%c ", buf1[j]);
} printf("\n鎸変換鎰忛敭鍙傝冪瓟妗...\n");
getch(); if( TryResolve(buf1, buf2) ) // 瑙i
printf("鍙傝冿細%s\n", buf2);
else
printf("鍙鑳芥槸鏃犺В...\n"); printf("鎸変換鎰忛敭鍑轟笅涓棰樼洰錛寈 閿閫鍑...\n");
if( getch() == 'x' ) break;
} return 0;
}

閱讀全文

與一個簡單游戲的編程相關的資料

熱點內容
進化演算法和啟發式演算法的區別 瀏覽:600
android組件是什麼 瀏覽:973
安卓手機微信怎麼同步信息 瀏覽:181
小人pdf 瀏覽:806
我的世界伺服器怎麼造好看的建築 瀏覽:307
兄弟連培訓php多少錢 瀏覽:249
1523鋪地面的演算法 瀏覽:746
linux源碼安裝php環境 瀏覽:821
ping命令用法 瀏覽:717
日本海軍pdf 瀏覽:469
哪個app有大臉特效 瀏覽:140
自己生活需要解壓嗎 瀏覽:946
考研究生演算法 瀏覽:528
java撤銷 瀏覽:442
學完單片機做點什麼好 瀏覽:312
編譯後運行不了 瀏覽:404
安卓如何設置手機鍵盤大小 瀏覽:847
室內程序員表白方式 瀏覽:694
全民去哪裡找app 瀏覽:124
表格命令CAD 瀏覽:244