fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include<cmath>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. using namespace std;
  9.  
  10. //許宇和
  11. //undo not finish
  12. //can't reset
  13. //can't won
  14. stringstream ss;
  15. vector<string> split(string line){
  16. vector<string> arr;
  17. string word;
  18. istringstream iss(line);
  19. while (iss >> word) {
  20. arr.push_back(word);
  21. }
  22. return arr;
  23. }
  24. bool isint(string s){
  25. for(char ch:s){
  26. if(!isdigit(ch)){
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32.  
  33. vector<int> cells;
  34. vector<int> card;
  35. string board[75];
  36. vector<bool>check(false);
  37. void print(vector<int>&vec){
  38. for(int k=1;k<=vec.size();k++){
  39. if(vec[k-1]>=10){
  40. cout<<vec[k-1]<<" ";
  41. }else{
  42. cout<<" "<<vec[k-1]<<" ";
  43. }
  44. if(k%5==0){
  45. cout<<endl;
  46. }
  47. }
  48. cout<<endl;
  49. }
  50. void printboard(string *arr,int len){
  51. cout<<endl;
  52. for(int i=1;i<=len;i++){
  53. cout<<arr[i-1];
  54. if(i%3==0){
  55. cout<<" ";
  56. }
  57. if(i%15==0){
  58. cout<<endl;
  59. }
  60. }
  61. }
  62. void shuffle(vector<int> &arr){
  63. int now =0;
  64. for(int i=0;i<arr.size();i++){
  65. card.push_back(arr[i]);
  66. }
  67. for(int a=0;a<card.size();a++){
  68. int j=0;
  69. j=(a + rand()%(card.size()-a));
  70. now =card[a];
  71. card[a]=card[j];
  72. card[j] =now;
  73. now =0;
  74. }
  75. print(card);
  76. }
  77. int checkh(string *arr,int len){
  78. //找橫線
  79. int size =len/3;
  80. int count =sqrt(size),line =0;
  81. for(int i=0;i<count*5;i+=5){
  82. //cout<<"line begin i: "<<i<<" arr[i*3+2]: "<<arr[i*3+2]<<endl;
  83. if(arr[i*3+2]!="*"){
  84. continue;
  85. }else{
  86. //cout<<"i: "<<i<<endl;
  87. for(int j=1;j<count;j++){
  88. //cout<<"arr[(i+j)*3+2]: "<<arr[i+j*3+2]<<endl;
  89. if(arr[(i+j)*3+2]!="*"){
  90.  
  91. //cout<<"J not *: "<<j<<" "<<"i+j*3+2: "<<i+j*3+2<<"line: "<<line<<endl;
  92. break;
  93. }else if(j==count-1){
  94.  
  95. line++;
  96. //cout<<j<<" "<<line<<endl;
  97. }
  98. }
  99. }
  100. }
  101. return line;
  102. }
  103. int checkv(string *arr,int len){
  104. //找直線
  105. int size =len/3;
  106. int count =sqrt(size),line =0;
  107. for(int i=0;i<count;i++){
  108. //cout<<"line begin i: "<<i<<" arr[i*3+2]: "<<arr[i*3+2]<<endl;
  109. if(arr[i*3+2]!="*"){
  110. continue;
  111. }else{
  112. //cout<<"i: "<<i<<endl;
  113. for(int j=5;j<count*5;j+=5){
  114. //cout<<"arr[(i+j)*3+2]: "<<arr[i+j*3+2]<<endl;
  115. if(arr[(i+j)*3+2]!="*"){
  116. //cout<<"in"<<" ";
  117. //cout<<"J not *: "<<j<<" "<<"i+j*3+2: "<<i+j*3+2<<"line: "<<line<<endl;
  118. break;
  119. }else if(j==5*(count-1)){
  120.  
  121. line++;
  122. //cout<<j<<" "<<line<<endl;
  123. }
  124. }
  125. }
  126. }
  127. return line;
  128. }
  129. int checkd(string *arr,int len){
  130. //找斜線
  131. int size =len/3;
  132. int count =sqrt(size),line =0;
  133. for(int i=0;i<=count*6;i+=6){
  134. //cout<<"line begin i: "<<i<<" arr[i*3+2]: "<<arr[i*3+2]<<endl;
  135. if(arr[i*3+2]!="*"){
  136. break;
  137. }else{
  138. if(i==6*(count-1)){
  139.  
  140. line++;
  141. //cout<<j<<" "<<line<<endl;
  142. }
  143. }
  144. }
  145. for(int j=4;j<=count*4;j+=4){
  146. //cout<<"line begin i: "<<i<<" arr[i*3+2]: "<<arr[i*3+2]<<endl;
  147. if(arr[j*3+2]!="*"){
  148. break;
  149. }else{
  150. if(j==4*(count-1)){
  151.  
  152. line++;
  153. //cout<<j<<" "<<line<<endl;
  154. }
  155. }
  156. }
  157. return line;
  158. }
  159. void bingo1(){
  160. //type?for help
  161. //print welcome
  162. //ss <<10;
  163. //cout<<ss.str()[0];
  164. //cout<<endl;
  165. //cout<<ss.str()[1];
  166. //cout<<endl;
  167. cout<<"+----------------------------+"<<endl;
  168. cout<<"| BINGO |"<<endl;
  169. cout<<"| Game! |"<<endl;
  170. cout<<"| made by Edward|"<<endl;
  171. cout<<"+----------------------------+"<<endl;
  172. cout<<"type?for help"<<endl;
  173. cout<<"> ";
  174. string line;
  175. int step=0,lines=0,count =0, num=0,index= 0;
  176. int hline=0,vline=0,dline=0;
  177. while (getline(cin, line)) {
  178. vector<string> words =split(line);
  179. int len =words.size();
  180. if(len==0){
  181. cout<<"> ";
  182. continue;
  183. }
  184. int line2=0;
  185. string cmd =words[0];
  186. if(cmd =="?"){
  187. //print help
  188. cout<<"How to play this game?"<<endl;
  189. cout<<"input start to reset the bingo card"<<endl;
  190. cout<<"input end to finish the game"<<endl;
  191. cout<<"input'sel'+numbers to get lines"<<endl;
  192. cout<<"You can play with your frineds,see who is the bingo king"<<endl;
  193. cout<<"Have fun!"<<endl;
  194. }else if(cmd =="start"){
  195. //shuffle cells then print the table
  196. cout<<"this is your bingo table"<<endl;
  197. for(int i=1;i<=25;i++){
  198. cells.push_back(i);
  199. }
  200. shuffle(cells);
  201. for(int k=0;k<card.size();k++){
  202. ss << card[k];
  203. //string tmp;
  204. //cout<<"SS"<<ss.str()<<" ";
  205. //ss>>tmp;
  206. //cout<<"tmp"<<tmp;
  207.  
  208. if(card[k]>=10){
  209. board[k*3] =ss.str()[0];
  210. board[k*3+1] =ss.str()[1];
  211. board[k*3+2] =" ";
  212. }else{
  213. board[k*3] =" ";
  214. board[k*3+1] =ss.str()[0];
  215. board[k*3+2] =" ";
  216. }
  217. ss.str("");
  218. //cout<<"finsh";
  219. //cout<<board[k]<<board[k+1]<<board[k+2];//cout<<endl;
  220. }
  221. //printboard(board,75);
  222. }else if(cmd =="exit"){
  223. //stop the game
  224. cout<<"bye"<<endl;
  225. break;
  226. }else if(cmd =="sel"){
  227. //cout<<"enter"<<endl;
  228. //check numbers
  229. step++;
  230. //cout<<words[1]<<endl;
  231. if(isint(words[1])==true){
  232. num =stoi(words[1]);
  233. //cout<<num<<endl;
  234. for(int i=0;i<card.size();i++){
  235. //cout<<card[i]<<endl;
  236. if(num ==card[i]){
  237. index=i;
  238. //cout<<i<<endl;
  239. //count=9;
  240. //print();
  241. board[i*3+2] ="*";
  242. }
  243. //cout<<board[k]<<" "<<board[k+1]<<" "<<board[k+2]<<" ";
  244. }
  245. printboard(board,75);
  246. cout<<"--------------------"<<endl;
  247. hline =checkh(board,75);
  248. vline =checkv(board,75);
  249. dline =checkd(board,75);
  250. line2 =hline+vline+dline;
  251. cout<<"step: "<<step<<" "<<"lines: "<<line2<<endl;
  252. if(line2==5){
  253. cout<<"you won!"<<endl;
  254. break;
  255. }
  256. //cout<<"hline: "<< hline<<" ";
  257. // cout<<"vline: "<< vline<<" ";
  258. //cout<<"dline: "<< dline<<" ";
  259. }
  260. }else if(cmd=="undo"){
  261. //cout<<"undo"<<" "<<index<<" ";
  262. step--;
  263. board[index*3+2] =" ";
  264. printboard(board,75);
  265. cout<<"--------------------"<<endl;
  266. cout<<"step: "<<step<<" "<<"lines: "<<line2<<endl;
  267. }else{
  268. cout<<"unkuown"<<endl;
  269. }
  270. cout<<"> ";
  271. }
  272. }
  273.  
  274. void game6(){
  275. cout<<"[]"<<endl;
  276. string line;
  277. vector<int> newline;
  278. int total =0,x=0;
  279. while (getline(cin, line)) {
  280. vector<string> words =split(line);
  281. if(words[0]=="add"){
  282. if(newline.size() ==1){
  283. cout<<"invalid"<<endl;
  284. }else{
  285. total=newline[1]+newline[0];
  286. newline.clear();
  287. newline.push_back(total);
  288. total=0;
  289. }
  290. }else if(words[0] == "push"){
  291. for(int i=1;i<words.size();i++){
  292. newline.push_back(stoi(words[i]));
  293. }
  294. }else if(words[0]=="times"){
  295. total=newline[1]*newline[0];
  296. newline.clear();
  297. newline.push_back(total);
  298. total=0;
  299. }else if(words[0]=="sub"){
  300. if(newline.size() ==0){
  301. cout<<"invalid"<<endl;
  302.  
  303. }else{
  304. total=newline[newline.size()-1]-stoi(words[words.size()-1]);
  305. newline[newline.size()-1] =total;
  306. total=0;
  307. }
  308. }else if(words[0]=="exit"){
  309. x=2;
  310. cout<<"bye"<<endl;
  311. break;
  312. }else{
  313. cout<<"unknown "<<words[0]<<endl;
  314. }
  315. if(x!=2){
  316. cout<<"[";
  317. for(int a=0;a<newline.size();a++){
  318. if(a!=newline.size()-1){
  319. cout<<newline[a]<<",";
  320. }else{
  321. cout<<newline[newline.size()-1];
  322. }
  323. x=1;
  324. }
  325. cout<<"]";
  326. if(x==1){
  327. cout<<endl;
  328. x=0;
  329. }
  330. }
  331. }
  332. }
  333. void game5(){
  334. cout<<"value: 0"<<endl;
  335. string line;
  336. int x=0;
  337. int count =0;
  338. int total =0;
  339. while (getline(cin, line)) {
  340. vector<string> words =split(line);
  341. bool good =true;
  342. if(words[0]=="add"){
  343. for(int i=1;i<words.size();i++){
  344. if(isint(words[i])==false){
  345. for(int j=1;j<=count;j++){
  346. total-=stoi(words[j]);
  347. }
  348. x=1;
  349. count =0;
  350. break;
  351. }else{
  352. total+=stoi(words[i]);
  353. count++;
  354. }
  355. }
  356. }else if(words[0] == "sub"){
  357. for(int i=1;i<words.size();i++){
  358. if(isint(words[i])==false){
  359. for(int j=1;j<=count;j++){
  360. total+=stoi(words[j]);
  361. }
  362. x=1;
  363. count =0;
  364. break;
  365. }else{
  366. total-=stoi(words[i]);
  367. }
  368. }
  369. }else if(words[0]=="times"){
  370. for(int i=1;i<words.size();i++){
  371. if(isint(words[i])==false){
  372. for(int j=1;j<=count;j++){
  373. total/=stoi(words[j]);
  374. }
  375. x=1;
  376. count =0;
  377. break;
  378. }else{
  379. total*=stoi(words[i]);
  380. }
  381. }
  382. }else if(words[0]=="exit"){
  383. cout<<"bye"<<endl;
  384. x=9;
  385. break;
  386. }else{
  387. cout<<"unknown op "<<words[0]<<endl;
  388. }
  389. count=0;
  390. if(x==1){
  391. cout<<"invalid number"<<endl;
  392. x=0;
  393. count =0;
  394. }
  395. cout<<"value: "<<total<<endl;
  396. }
  397. }
  398. void game4(){
  399. string line;
  400. while (getline(cin, line)) {
  401. int sum =0;
  402. vector<string> words =split(line);
  403. bool good =true;
  404. for(string word:words){
  405. if(isint(word)==true){
  406. sum+=stoi(word);
  407. }else{
  408. good =false;
  409. break;
  410. }
  411. }
  412. if(good){
  413. cout<<sum<<endl;
  414. }else{
  415. cout<<"invalid"<<endl;
  416. }
  417. }
  418. }
  419. void game3(){
  420. string line;
  421. while (getline(cin, line)) {
  422. int sum =0;
  423. vector<string> words =split(line);
  424. for(string word:words){
  425. sum+=stoi(word);
  426. }
  427. cout<<sum<<endl;
  428. }
  429. }
  430. void game2(){
  431. int nlines = 0;
  432. string line, word;
  433. while (getline(cin, line)) {
  434. ++nlines;
  435. vector<string> words =split(line);
  436. cout<<"L"<<nlines<<": ";
  437. for(int i=0;i<words.size();i++){
  438. cout<<i+1<<". "<<words[i]<<" ";
  439. }
  440. cout<<endl;
  441. }
  442. }
  443. void game1(){
  444. int nlines = 0;
  445. string line, word;
  446. while (getline(cin, line)) {
  447. ++nlines;
  448. cout<<"L"<< nlines<<": ";
  449. istringstream iss(line);
  450. int i=1;
  451. while (iss >> word) {
  452. cout <<i<<". " << word<<" ";
  453. i++;
  454. }
  455. cout<<endl;
  456. }
  457. }
  458.  
  459. int main() {
  460. srand(time(NULL));
  461. //game1();
  462. //game2();
  463. //game3();
  464. //game4();
  465. //game5();
  466. //game6();
  467. bingo1();
  468. }
  469.  
  470. /* (1) game1, game2
  471. input:
  472.  
  473. Stone Campus
  474. Campus Coding Stone Good
  475. Programming Campus
  476. Best Campus Best
  477.  
  478. output:
  479.  
  480. L1: 1. Stone 2. Campus
  481. L2: 1. Campus 2. Coding 3. Stone 4. Good
  482. L3: 1. Programming 2. Campus
  483. L4: 1. Best 2. Campus 3. Best
  484.  
  485.  
  486. (2) game3 - use split()
  487. input:
  488.  
  489. 100 200
  490. 10 20 30
  491. 123
  492.  
  493. output:
  494.  
  495. 300
  496. 60
  497. 123
  498.  
  499. (3) game4 - use split()
  500. input:
  501.  
  502. 100 200
  503. 10 20a 30
  504. 10 20 3a0
  505. 123
  506.  
  507. output:
  508.  
  509. 300
  510. invalid
  511. invalid
  512. 123
  513.  
  514. (4) game5 - use split(), isint()
  515. input:
  516.  
  517. add 100 200
  518. sub 10 20 30
  519. add 1 20a
  520. suB 10
  521. times 10
  522. exit
  523.  
  524. output:
  525.  
  526. value: 0
  527. value: 300
  528. value: 240
  529. invalid number
  530. value: 240
  531. unknown op suB
  532. value: 240
  533. value: 2400
  534. bye
  535.  
  536. (4) game6 - use split(), vector.push_back(), pop_back(), back()
  537. input:
  538.  
  539. push 10 20
  540. add
  541. push 100
  542. sub 1
  543. Add
  544. add
  545. add
  546. push 10
  547. times
  548. exit
  549.  
  550. output:
  551.  
  552. []
  553. [10, 20]
  554. [30]
  555. [30, 100]
  556. [30, 99]
  557. unknown Add
  558. [30, 99]
  559. [129]
  560. invalid
  561. [129]
  562. [129, 10]
  563. [1290]
  564. bye
  565.  
  566. (5) bingo1: shuffle(vector<int> &arr)
  567.  
  568. input:
  569. ?
  570. start
  571. start
  572. exit
  573.  
  574. output:
  575. +-------------------+
  576. | Bingo!! |
  577. | by jy |
  578. +-------------------+
  579. (type ? for help)
  580. > ?
  581. bingo commands:
  582.   start - start game
  583.   exit - exit game
  584. > start
  585.  1 3 5 7 9
  586.  2 4 6 8 10
  587. 11 13 15 17 19
  588. 12 14 16 18 20
  589. 21 22 23 24 25
  590. > start
  591. 11 13 15 17 19
  592. 12 14 16 18 20
  593.  1 3 5 7 9
  594.  2 4 6 8 10
  595. 21 22 23 24 25
  596. > exit
  597. bye
  598.  
  599. (6) bingo2: shuffle(vector<int> &arr), isint()
  600.  
  601. input:
  602. ?
  603. start
  604. sel 1
  605. ...
  606. exit
  607.  
  608. output:
  609. +-------------------+
  610. | Bingo!! |
  611. | by jy |
  612. +-------------------+
  613. (type ? for help)
  614. > ?
  615. bingo commands:
  616.   start - start game
  617.   sel <n> - choose n
  618.   exit - exit game
  619. > start
  620.   3 1 5 7 9
  621.   2 4 6 8 10
  622.  11 13 15 17 19
  623.  12 14 16 18 20
  624.  21 22 23 24 25
  625. --------------------
  626.  step: 0 lines: 0
  627. > sel 3
  628.   3* 1 5 7 9
  629.   2 4 6 8 10
  630.  11 13 15 17 19
  631.  12 14 16 18 20
  632.  21 22 23 24 25
  633. --------------------
  634.  step: 1 lines: 0
  635. > sel 4
  636.   3* 1 5 7 9
  637.   2 4* 6 8 10
  638.  11 13 15 17 19
  639.  12 14 16 18 20
  640.  21 22 23 24 25
  641. --------------------
  642.  step: 2 lines: 0
  643.  
  644. ....
  645.  
  646. > sel 25
  647.   3* 1 5 7 9
  648.   2 4* 6 8 10
  649.  11 13 15* 17 19
  650.  12 14 16 18* 20
  651.  21 22 23 24 25*
  652. --------------------
  653.  step: 5 lines: 1
  654. ....
  655.  
  656. > sel 24
  657.   3* 1* 5* 7* 9*
  658.   2* 4* 6* 8* 10*
  659.  11 13 15* 17* 19*
  660.  12 14 16 18* 20*
  661.  21 22 23 24* 25*
  662. --------------------
  663.  step: 17 lines: 5
  664.  YOU WON!
  665. > exit
  666. bye
  667.  
  668. (7) bingo3:
  669. 多一個 undo 指令,倒回一步
  670.  
  671. */
Success #stdin #stdout 0s 4568KB
stdin
start
sel 1
sel 7
sel 13
sel 19
sel 25
sel 5
sel 9
sel 17
sel 21
stdout
+----------------------------+
|    BINGO                   |
|          Game!             |
|              made by Edward|
+----------------------------+
type?for help
>  this is your bingo table
>  
 1*  2   3   4   5  
 6   7   8   9  10  
11  12  13  14  15  
16  17  18  19  20  
21  22  23  24  25  
--------------------
step: 1    lines: 0
>  
 1*  2   3   4   5  
 6   7*  8   9  10  
11  12  13  14  15  
16  17  18  19  20  
21  22  23  24  25  
--------------------
step: 2    lines: 0
>  
 1*  2   3   4   5  
 6   7*  8   9  10  
11  12  13* 14  15  
16  17  18  19  20  
21  22  23  24  25  
--------------------
step: 3    lines: 0
>  
 1*  2   3   4   5  
 6   7*  8   9  10  
11  12  13* 14  15  
16  17  18  19* 20  
21  22  23  24  25  
--------------------
step: 4    lines: 0
>  
 1*  2   3   4   5  
 6   7*  8   9  10  
11  12  13* 14  15  
16  17  18  19* 20  
21  22  23  24  25* 
--------------------
step: 5    lines: 1
>  
 1*  2   3   4   5* 
 6   7*  8   9  10  
11  12  13* 14  15  
16  17  18  19* 20  
21  22  23  24  25* 
--------------------
step: 6    lines: 1
>  
 1*  2   3   4   5* 
 6   7*  8   9* 10  
11  12  13* 14  15  
16  17  18  19* 20  
21  22  23  24  25* 
--------------------
step: 7    lines: 1
>  
 1*  2   3   4   5* 
 6   7*  8   9* 10  
11  12  13* 14  15  
16  17* 18  19* 20  
21  22  23  24  25* 
--------------------
step: 8    lines: 2
>  
 1*  2   3   4   5* 
 6   7*  8   9* 10  
11  12  13* 14  15  
16  17* 18  19* 20  
21* 22  23  24  25* 
--------------------
step: 9    lines: 2
>