Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. #ifndef MANCALA_H
  2. #define MANCALA_H
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. namespace mancala{
  9.  
  10. const std::vector<std::string> rules = {
  11. "nOverview:ntMancala is an ancient family of board games,ntand there are numerous variants.ntThis is a version of the basic game,ntknown as two-rank Mancala and also known as Kalah.nt",
  12. "nHoles & Stores:ntYour holes on the board are on the bottom right side.ntYour store is the last hole on your right.nt",
  13. "nSow Stones:ntChoose a hole to pick up all pieces.ntMoving counter - clockwise, the game now deposits one ofntthe stones in each hole until the stones run out.nt",
  14. "nStore:ntIf you run into your own store deposit one piece in it.ntIf you run into your opponents store, skip it.nt",
  15. "nFree Turn:ntIf the last piece you drop is in your own store,ntyou get a free turn.nt",
  16. "nCapture:ntIf the last piece you drop is on your side,ntyou capture that piece and any pieces in thenthole directly opposite.nt",
  17. "nEnd:ntThe game ends when all six spaces on one side of the mancalantboard are empty. The player who still has pieces on his side ofntthe board when the game ends captures all of those pieces.nt",
  18. "nWin:ntCount all the piecies in each store.ntThe winner is the player with the most pieces.nt"
  19. };
  20.  
  21. enum option{
  22. play = 1,
  23. info,
  24. quit
  25. };
  26.  
  27. class match{
  28. public://constructor
  29. match() : board{4,4,4,4,4,4,0,4,4,4,4,4,4,0}{
  30. begin();
  31. while(ingame)
  32. {match::update();}
  33. results();
  34. };
  35. private://functions
  36. void results();
  37. void update();
  38. void begin();
  39. void end();
  40. //make player class
  41. void p1_display(const std::string& mod = "");
  42. void p2_display(const std::string& mod = "");
  43. int p1_pit_select();
  44. int p2_pit_select();
  45. bool p1_pit_distribute(const int& pit);
  46. bool p2_pit_distribute(const int& pit);
  47. bool p1_is_greater();
  48. bool p2_is_greater();
  49. bool p1_clear_pits();
  50. bool p2_clear_pits();
  51.  
  52. private://variables
  53. static const int pits = 14;
  54. int board[pits];
  55. bool ingame;
  56. };
  57. };
  58.  
  59. #endif//MANCALA_H
  60.  
  61. #include "mancala.h"
  62.  
  63. #include <unistd.h>
  64. #include <iomanip>
  65. #include <cstdlib>
  66.  
  67. using namespace mancala;
  68.  
  69. void match::update(){
  70.  
  71. match::p1_display();
  72.  
  73. switch(match::p1_clear_pits() || match::p2_is_greater() || match::p1_pit_distribute(match::p1_pit_select()) || match::p1_clear_pits() || match::p2_is_greater()){
  74. case true:
  75. end();
  76. return;
  77. }
  78. switch(match::p2_clear_pits() || match::p1_is_greater() || match::p2_pit_distribute(match::p2_pit_select()) || match::p2_clear_pits() || match::p1_is_greater()){
  79. case true:
  80. end();
  81. return;
  82. }
  83. };
  84.  
  85. void match::begin(){
  86. ingame = true;
  87. };
  88.  
  89. void match::end(){
  90. ingame = false;
  91. };
  92.  
  93. void match::results(){
  94. if(board[13] < board[6]){
  95. std::cout << " *** P1 Wins ***n";
  96. }else if(board[13] > board[6]){
  97. std::cout << " *** P2 Wins ***n";
  98. }else if(board[13] == board[6]){
  99. std::cout << " *** Tie ***n";
  100. }else{
  101. std::cout << " *** WHOOPS! ***n";
  102. }sleep(2);
  103. };
  104.  
  105. int match::p1_pit_select(){
  106. std::cout << "Player 1: Enter(0-5): ";
  107. std::string s_input;
  108. int n_input;
  109. std::cin >> s_input;
  110. switch(s_input.at(0)){
  111. case '0': case '1': case '2':
  112. case '3': case '4': case '5':
  113. std::cin.putback(s_input.at(0));
  114. std::cin >> n_input;
  115. if(board[n_input] == 0){
  116. match::p1_display();
  117. return p1_pit_select();
  118. }return n_input;
  119. default:
  120. if(s_input == "back"){
  121. match::p1_display();
  122. return -1;
  123. }else{
  124. match::p1_display();
  125. return p1_pit_select();
  126. }
  127. }
  128. };
  129.  
  130. int trans(const unsigned int& index){
  131. return (7 + index) % 14;
  132. };
  133.  
  134. int match::p2_pit_select(){
  135. std::cout << "Player 2: Enter(0-5): ";
  136. std::string s_input;
  137. int n_input;
  138. std::cin >> s_input;
  139. switch(s_input.at(0)){
  140. case '0': case '1': case '2':
  141. case '3': case '4': case '5':
  142. std::cin.putback(s_input.at(0));
  143. std::cin >> n_input;
  144. if(board[trans(n_input)] == 0){
  145. match::p2_display();
  146. return p2_pit_select();
  147. }return n_input;
  148. default:
  149. if(s_input == "back"){
  150. match::p2_display();
  151. return -1;
  152. }else{
  153. match::p2_display();
  154. return p2_pit_select();
  155. }
  156. }
  157. };
  158.  
  159. bool match::p2_clear_pits(){
  160. for(int index = 0; index < 6; index++){
  161. if(board[index] != 0) return false;
  162. }
  163. for(int index = 7; index < 13; index++){
  164. board[13] += board[index];
  165. board[index] = 0;
  166. match::p1_display();
  167. }return true;
  168. };
  169.  
  170. bool match::p1_pit_distribute(const int& pit){//0-5
  171. if(pit == -1) return true;
  172. int markers = board[pit];
  173. board[pit] = 0;
  174. for(int index = 1; index <= markers; index++){
  175. if(index == markers){
  176. //*** last marker ***
  177. if(board[(pit + index) % 13] == 0 && (pit + index) != 6 && (pit + index) >= 0 && (pit + index) <= 5 && board[(12 - (pit + index))] != 0){
  178. //*** capture ***
  179. board[6] += board[12 - (index + pit)] + 1;
  180. board[12 - (index + pit)] = 0;
  181. match::p2_display();
  182. }else if((pit + index == 6) && !match::p2_clear_pits()){
  183. //*** free turn ***
  184. board[6]+=1;
  185. match::p1_display("P1: ⟲");
  186. p1_pit_distribute(match::p1_pit_select());
  187. }else{
  188. //*** deposit & end turn ***
  189. board[(pit + index) % 13]+=1;
  190. match::p2_display();
  191. }
  192. }else{
  193. //*** deposit ***
  194. board[(pit + index) % 13]+=1;
  195. }
  196. }return false;
  197. };
  198.  
  199. bool match::p1_clear_pits(){
  200. for(int index = 7; index < 13; index++){
  201. if(board[index] != 0) return false;
  202. }
  203. for(int index = 0; index < 6; index++){
  204. board[6] += board[index];
  205. board[index] = 0;
  206. match::p2_display();
  207. }return true;
  208. };
  209.  
  210. bool match::p2_pit_distribute(const int& pit){//0-5
  211. if(pit == -1) return true;
  212. int markers = board[trans(pit)];
  213. board[trans(pit)] = 0;
  214. for(int index = 1; index <= markers; index++){
  215. if(index == markers){
  216. if(board[trans((pit + index))] == 0 && trans(pit + index) != 13 && (pit + index) >= 0 && (pit + index) <= 5 && board[trans(12 - (pit + index))] != 0){
  217. board[13] += board[trans(12 - (index + pit))] + 1;
  218. board[trans(12 - (index + pit))] = 0;
  219. match::p1_display();
  220. }else if((trans(pit + index) == 13) && !p1_clear_pits()){
  221. board[13]+=1;
  222. match::p2_display("P2: ⟲");
  223. p2_pit_distribute(match::p2_pit_select());
  224. }else{
  225. board[trans(pit + index)]+=1;
  226. match::p1_display();
  227. }
  228. }else{
  229. board[trans(pit + index)]+=1;
  230. }
  231. }return false;
  232. };
  233.  
  234. static bool ask = true;
  235.  
  236. bool match::p2_is_greater(){
  237. if((board[13] > 24) && ask){
  238. match::p1_display("bbP2: > 24");
  239. std::cout << "Player 1: Continue?: ";
  240. std::string option;
  241. std::cin >> option;
  242. if(option == "yes"){
  243. match::p1_display();
  244. return ask = false;
  245. }else if(option == "no"){
  246. match::p1_display();
  247. return true;
  248. }else{
  249. match::p1_display();
  250. return p2_is_greater();
  251. }
  252. }else return false;
  253. };
  254.  
  255. static bool _ask = true;
  256.  
  257. bool match::p1_is_greater(){
  258. if((board[6] > 24) && _ask){
  259. match::p2_display("bbP1: > 24");
  260. std::cout << "Player 2: Continue?: ";
  261. std::string option;
  262. std::cin >> option;
  263. if(option == "yes"){
  264. match::p2_display();
  265. return _ask = false;
  266. }else if(option == "no"){
  267. match::p2_display();
  268. return true;
  269. }else{
  270. match::p2_display();
  271. return p1_is_greater();
  272. }
  273. }else return false;
  274. };
  275.  
  276. void match::p1_display(const std::string& mod){
  277. system("clear");
  278. for(int index = pits - 1; index >= pits / 2; index--)
  279. std::cout << std::setw(2) << std::setfill(' ') << board[index] << ' ';
  280. std::cout << "P1nP2 ";
  281. for(int index = 0; index < pits / 2; index++)
  282. std::cout << std::setw(2) << std::setfill(' ') << board[index] << ' ';
  283. std::cout << "n " << mod << "n ";
  284. for(int index = 0; index < 6; index ++)
  285. std::cout << std::setw(2) << std::setfill('*') << index << ' ';
  286. std::cout << 'n';
  287. };
  288.  
  289. void match::p2_display(const std::string& mod){
  290. system("clear");
  291. for(int index = pits - 1; index >= pits / 2; index--)
  292. std::cout << std::setw(2) << std::setfill(' ') << board[trans(index)] << ' ';
  293. std::cout << "P2nP1 ";
  294. for(int index = 0; index < pits / 2; index++)
  295. std::cout << std::setw(2) << std::setfill(' ') << board[trans(index)] << ' ';
  296. std::cout << "n " << mod << "n ";
  297. for(int index = 0; index < 6; index ++)
  298. std::cout << std::setw(2) << std::setfill('*') << index << ' ';
  299. std::cout << 'n';
  300. };
  301.  
  302. #ifndef MENU_H
  303. #define MENU_H
  304.  
  305. #include <iostream>
  306. #include <string>
  307. #include <vector>
  308.  
  309. class menu{
  310. public:
  311. menu(const std::string& title, const std::vector<std::string>& options)
  312. : title(title), options(options){;};
  313. void display();
  314. int get_input();
  315. private:
  316. std::string title;
  317. std::vector<std::string> options;
  318. };
  319.  
  320. #endif//MENU_H
  321.  
  322. #include "menu.h"
  323.  
  324. #include <cstdlib>
  325.  
  326. int menu::get_input(){
  327. display();
  328. int index;
  329. std::cin >> index;
  330. if(!std::cin){
  331. std::cin.clear();
  332. std::cin.ignore(256, 'n');
  333. return get_input();
  334. }return index;
  335. };
  336.  
  337. void menu::display(){
  338. system("clear");
  339. std::cout << title << std::endl;
  340. for(int index = 0; index < options.size(); index++){
  341. std::cout << "[" << index + 1 << "]" << options[index] << std::endl;
  342. }std::cout << "Enter index: ";
  343. };
  344.  
  345. #include "mancala.h"
  346. #include "menu.h"
  347.  
  348. menu home("Mancala - Type "back" from anywhere to return to the main menu. ", {"Play","Info","Quit"});
  349.  
  350. int main(){bool end = false;
  351. while(!end){
  352. switch(home.get_input()){
  353. case mancala::option::play:
  354. mancala::match();
  355. break;
  356. case mancala::option::info:
  357. system("clear");
  358. for(int index = 0; index < mancala::rules.size(); index++){
  359. std::cout << "Mancala: Rules n"
  360. << mancala::rules[index]
  361. << " Enter any key: ";
  362. std::string ws;
  363. std::cin >> ws;
  364. if(ws == "back"){
  365. break;
  366. }else{
  367. system("clear");
  368. }
  369. }
  370. break;
  371. case mancala::option::quit:
  372. end = true;
  373. }
  374. }return 0;
  375. }
  376.  
  377. int main(){bool end = false;
  378.  
  379. int main()
  380. {
  381. bool end = false;
  382. // etc.
  383.  
  384. void cls()
  385. {
  386. std::cout << "x1b[2J";
  387. }
  388.  
  389. int trans(const unsigned int& index){
  390. return (7 + index) % 14;
  391. }; // <-- this semicolon is neither needed nor welcome!
  392.  
  393. void match::results() const { // etc.
  394.  
  395. switch(match::p2_clear_pits() || match::p1_is_greater() || match::p2_pit_distribute(match::p2_pit_select()) || match::p2_clear_pits() || match::p1_is_greater()){
  396. case true:
  397. end();
  398. return;
  399. }
  400.  
  401. if (match::p1_clear_pits() || match::p2_is_greater() ||
  402. match::p1_pit_distribute(match::p1_pit_select()) ||
  403. match::p1_clear_pits() || match::p2_is_greater()) { //etc.
  404.  
  405. match() : board{4,4,4,4,4,4,0,4,4,4,4,4,4,0}{
  406. begin();
  407. while(ingame)
  408. {match::update();}
  409. results();
  410. };
  411.  
  412. match() : board{4,4,4,4,4,4,0,4,4,4,4,4,4,0}
  413. {}
  414. void play() {
  415. while (update())
  416. { }
  417. results();
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement