Guest User

Capture The Sarrum

a guest
Aug 26th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.55 KB | None | 0 0
  1. // ConsoleApplication7.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. #include <string>
  8. #include <stdlib.h>
  9.  
  10. using namespace std;
  11.  
  12. const int BoardDimension = 8;
  13.  
  14. void DisplayWhoseTurnItIs(char WhoseTurn)
  15. {
  16.     if (WhoseTurn == 'W') {
  17.         cout << "It is White's Turn" << endl;
  18.     }
  19.     else
  20.     {
  21.         cout << "It is Black's Turn" << endl;
  22.     }
  23. }
  24.  
  25. void DisplayWinner(char WhoseTurn)
  26. {
  27.     if (WhoseTurn == 'W') {
  28.         cout << "Black's Sarrum has been captured. White win!" << endl;
  29.     }
  30.     else
  31.     {
  32.         cout << "White's Sarrum has been captured. Black wins!" << endl;
  33.     }
  34.     cout << endl;
  35. }
  36.  
  37. bool CheckIfGameWillBeWon(string Board[8][8], int FinishRank, int FinishFile)
  38. {
  39.     if (Board[FinishRank][FinishFile].at(1) == 'S') {
  40.         return true;
  41.     }
  42.     else
  43.     {
  44.         return false;
  45.     }
  46.  
  47. }
  48.  
  49. void DisplayBoard(string Board[8][8])
  50. {
  51.     int RankNo, FileNo;
  52.     cout << endl;
  53.     for (RankNo = 1;RankNo <= BoardDimension;RankNo++)
  54.     {
  55.         cout << " _________________________" << endl;
  56.         cout << RankNo << " ";
  57.  
  58.         for (FileNo = 1;RankNo <= BoardDimension;FileNo++)
  59.         {
  60.             cout << "|" << Board[RankNo][FileNo];
  61.         }
  62.         cout << "|" << endl;
  63.     }
  64.     cout << " _________________________" << endl;
  65.     cout << endl;
  66.     cout << " 1 2 3 4 5 6 7 8";
  67.     cout << endl;
  68.     cout << endl;
  69. }
  70. bool CheckRedumMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile, char ColourOfPiece)
  71. {
  72.     if (ColourOfPiece == 'W')
  73.     {
  74.         if (FinishRank == (StartRank - 1))
  75.         {
  76.             if ((FinishFile == StartFile) && (Board[FinishRank][FinishFile] == " "))
  77.             {
  78.                 return true;
  79.             }
  80.             else if (abs(FinishFile - StartFile) == 1 && (Board[FinishRank][FinishFile].at(0) == 'B'))
  81.             {
  82.                 return true;
  83.             }
  84.         }
  85.     }
  86.     else
  87.     {
  88.         if (FinishRank == StartRank + 1)
  89.         {
  90.             if ((FinishFile == StartFile) && (Board[FinishRank][FinishFile] == " "))
  91.             {
  92.                 return true;
  93.             }
  94.             else if (abs(FinishFile - StartFile) == 1 && (Board[FinishRank][FinishFile].at(0) == 'W'))
  95.             {
  96.                 return true;
  97.             }
  98.         }
  99.     }
  100.     return false;
  101.  
  102. }
  103.  
  104. bool CheckSarrumMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile)
  105. {
  106.     if ((abs(FinishFile - StartFile) <= 1) && (abs(FinishRank - StartRank) <= 1))
  107.     {
  108.         return true;
  109.     }
  110.     return false;
  111. }
  112.  
  113. bool CheckGisgigirMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile)
  114.  
  115. {
  116.     bool GisgigirMoveIsLegal = false;
  117.     int count, RankDifference, FileDifference;
  118.  
  119.     RankDifference = FinishRank - StartRank;
  120.     FileDifference = FinishFile - StartFile;
  121.  
  122.     if (RankDifference == 0)
  123.     {
  124.         if (FileDifference >= 1)
  125.         {
  126.             GisgigirMoveIsLegal = true;
  127.             for (count = 1;count<FileDifference;count++)
  128.             {
  129.                 if (Board[StartRank][StartFile + count] != " ")
  130.                 {
  131.                     GisgigirMoveIsLegal = false;
  132.                 }
  133.             }
  134.         }
  135.         else if (FileDifference <= -1)
  136.         {
  137.             GisgigirMoveIsLegal = true;
  138.             for (count = -1;count>FileDifference;count--)
  139.             {
  140.                 if (Board[StartRank][StartFile + count] != " ")
  141.                 {
  142.                     GisgigirMoveIsLegal = false;
  143.                 }
  144.             }
  145.         }
  146.     }
  147.     else if (FileDifference == 0)
  148.     {
  149.         if (RankDifference >= 1)
  150.         {
  151.             GisgigirMoveIsLegal = true;
  152.             for (count = 1;count<RankDifference;count++)
  153.             {
  154.                 if (Board[StartRank + count][StartFile] != " ")
  155.                 {
  156.                     GisgigirMoveIsLegal = false;
  157.                 }
  158.             }
  159.         }
  160.         else if (RankDifference <= 1)
  161.         {
  162.             GisgigirMoveIsLegal = true;
  163.             for (count = -1;count>RankDifference;count--)
  164.             {
  165.                 if (Board[StartRank + count][StartFile] != " ")
  166.                 {
  167.                     GisgigirMoveIsLegal = false;
  168.                 }
  169.             }
  170.  
  171.         }
  172.     }
  173.     return GisgigirMoveIsLegal;
  174. }
  175. bool CheckNabuMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile)
  176. {
  177.     if ((abs(FinishFile - StartFile) == 1) && (abs(FinishRank - StartRank) == 1))
  178.     {
  179.         return true;
  180.  
  181.     }
  182.     return false;
  183. }
  184.  
  185. bool CheckMarzazPaniMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile)
  186. {
  187.     if (((abs(FinishFile - StartFile) == 1) && (abs(FinishRank - StartRank) == 0)) || ((abs(FinishFile - StartFile) == 0) && (abs(FinishRank - StartRank) == 1)))
  188.     {
  189.         return true;
  190.     }
  191.     return false;
  192. }
  193.  
  194. bool CheckEtluMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile)
  195. {
  196.     if (((abs(FinishFile - StartFile) == 2) && (abs(FinishRank - StartRank) == 0)) || ((abs(FinishFile - StartFile) == 0) && (abs(FinishRank - StartRank) == 2)))
  197.     {
  198.         return true;
  199.     }
  200.     return false;
  201. }
  202.  
  203. bool CheckMoveIsLegal(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile, char WhoseTurn)
  204. {
  205.     char PieceType, PieceColour;
  206.     if ((FinishFile == StartFile) && (FinishRank == StartRank))
  207.     {
  208.         return false;
  209.     }
  210.     PieceType = Board[StartRank][StartFile].at(1);
  211.     PieceColour = Board[StartRank][StartFile].at(0);
  212.     if (WhoseTurn == 'W')
  213.     {
  214.         if (PieceColour != 'W')
  215.         {
  216.             return false;
  217.         }
  218.         if (Board[FinishRank][FinishFile].at(0) == 'W')
  219.         {
  220.             return false;
  221.         }
  222.     }
  223.     else
  224.     {
  225.         if (PieceColour != 'B')
  226.         {
  227.             return false;
  228.         }
  229.         if (Board[FinishRank][FinishFile].at(0) == 'B')
  230.         {
  231.             return false;
  232.         }
  233.     }
  234.     switch (PieceType) {
  235.     case 'R':
  236.         return CheckRedumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, PieceColour);
  237.         break;
  238.     case 'S':
  239.         return CheckSarrumMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile);
  240.         break;
  241.     case 'M':
  242.         return CheckMarzazPaniMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile);
  243.         break;
  244.     case 'G':
  245.         return CheckGisgigirMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile);
  246.         break;
  247.     case 'N':
  248.         return CheckNabuMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile);
  249.         break;
  250.     case 'E':
  251.         return CheckEtluMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile);
  252.         break;
  253.  
  254.     default:
  255.         return false;
  256.         break;
  257.     }
  258. }
  259.  
  260. void InitialiseBoard(string Board[8][8], char SampleGame)
  261. {
  262.     int RankNo, FileNo;
  263.  
  264.     if (SampleGame == 'Y')
  265.     {
  266.         for (RankNo = 1;RankNo <= BoardDimension;RankNo++)
  267.         {
  268.             for (FileNo = 1;FileNo <= BoardDimension;FileNo++)
  269.             {
  270.                 Board[RankNo][FileNo] = " ";
  271.             }
  272.         }
  273.         Board[1][2] = "BG";
  274.         Board[1][4] = "BS";
  275.         Board[1][8] = "WG";
  276.         Board[2][1] = "WR";
  277.         Board[3][1] = "WS";
  278.         Board[3][2] = "BE";
  279.         Board[3][8] = "BE";
  280.         Board[6][8] = "BR";
  281.     }
  282.     else
  283.     {
  284.         for (RankNo = 1;RankNo <= BoardDimension;RankNo++)
  285.         {
  286.             for (FileNo = 1;FileNo <= BoardDimension;FileNo++)
  287.             {
  288.                 if (RankNo == 2)
  289.                 {
  290.                     Board[RankNo][FileNo] = "BR";
  291.                 }
  292.                 else if (RankNo == 7)
  293.                 {
  294.  
  295.                     Board[RankNo][FileNo] = "WR";
  296.                 }
  297.                 else if (RankNo == 1 || RankNo == 8)
  298.                 {
  299.                     if (RankNo == 1)
  300.                     {
  301.                         Board[RankNo][FileNo] = "B";
  302.                     }
  303.                     if (RankNo == 8)
  304.                     {
  305.                         Board[RankNo][FileNo] = "W";
  306.                     }
  307.                     switch (FileNo) {
  308.                     case 1:
  309.                     case 8:
  310.                         Board[RankNo][FileNo] = Board[RankNo][FileNo] + "G";
  311.                         break;
  312.                     case 2:
  313.                     case 7:
  314.                         Board[RankNo][FileNo] = Board[RankNo][FileNo] + "E";
  315.                         break;
  316.                     case 3:
  317.                     case 6:
  318.                         Board[RankNo][FileNo] = Board[RankNo][FileNo] + "N";
  319.                         break;
  320.                     case 4:
  321.                         Board[RankNo][FileNo] = Board[RankNo][FileNo] + "M";
  322.                         break;
  323.                     case 5:
  324.                         Board[RankNo][FileNo] = Board[RankNo][FileNo] + "S";
  325.                         break;
  326.                     }
  327.                 }
  328.                 else {
  329.                     Board[RankNo][FileNo] = " ";
  330.                 }
  331.  
  332.             }
  333.         }
  334.  
  335.     }
  336.  
  337. }
  338.  
  339. void GetMove(int StartSquare, int FinishSquare)
  340. {
  341.     cout << "Enter coordinates of sqauare containing piece to move to(file first): ";
  342.     cin >> StartSquare;
  343.     cout << "Enter coordinates of square to move piece to: ";
  344.     cin >> FinishSquare;
  345. }
  346.  
  347. void MakeMove(string Board[8][8], int StartRank, int StartFile, int FinishRank, int FinishFile, char WhoseTurn)
  348. {
  349.     if ((WhoseTurn == 'W') && (FinishRank = 1) && (Board[StartRank][StartFile].at(1) = 'R'))
  350.     {
  351.         Board[FinishRank][FinishFile] = "WM";
  352.         Board[StartRank][StartFile] = " ";
  353.     }
  354.     else if ((WhoseTurn == 'B') && (FinishRank = 8) && (Board[StartRank][StartFile].at(1) = 'R'))
  355.     {
  356.         Board[FinishRank][FinishFile] = "BM";
  357.         Board[StartRank][StartFile] = " ";
  358.     }
  359.     else
  360.     {
  361.         Board[FinishRank][FinishFile] = Board[StartRank][StartFile];
  362.         Board[StartRank][StartFile] = " ";
  363.     }
  364. }
  365.  
  366. int main()
  367. {
  368.     string Board[BoardDimension][BoardDimension];
  369.     bool GameOver, MoveIsLegal;
  370.     int StartSquare=0, FinishSquare=0, StartRank, StartFile, FinishRank, FinishFile;
  371.     char PlayAgain, SampleGame, WhoseTurn;
  372.     PlayAgain = 'Y';
  373.  
  374.     do
  375.  
  376.     {
  377.         WhoseTurn = 'W';
  378.         GameOver = false;
  379.         cout << "Do you want to play the sample game(enter Y for Yes)?";
  380.         cin >> SampleGame;
  381.         InitialiseBoard(Board, SampleGame);
  382.  
  383.         do
  384.         {
  385.             DisplayBoard(Board);
  386.             DisplayWhoseTurnItIs(WhoseTurn);
  387.             MoveIsLegal = false;
  388.  
  389.             do
  390.             {
  391.                 GetMove(StartSquare, FinishSquare);
  392.                 StartRank = StartSquare % 10;
  393.                 StartFile = StartSquare / 10;
  394.                 FinishRank = FinishSquare % 10;
  395.                 FinishFile = FinishSquare / 10;
  396.                 MoveIsLegal = CheckMoveIsLegal(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn);
  397.                 if (MoveIsLegal == false)
  398.                 {
  399.                     cout << "That is an illegal move-please try again" << endl;
  400.  
  401.                 }
  402.  
  403.             } while (!MoveIsLegal);
  404.  
  405.             GameOver = CheckIfGameWillBeWon(Board, FinishRank, FinishFile);
  406.             MakeMove(Board, StartRank, StartFile, FinishRank, FinishFile, WhoseTurn);
  407.             if (GameOver == true)
  408.             {
  409.                 DisplayWinner(WhoseTurn);
  410.  
  411.             }
  412.             if (WhoseTurn == 'W')
  413.             {
  414.                 WhoseTurn = 'B';
  415.             }
  416.             else
  417.             {
  418.                 WhoseTurn = 'W';
  419.             }
  420.  
  421.         } while (!GameOver);
  422.  
  423.         cout << "Do you want to play again(enter Y for Yes)?";
  424.         cin >> PlayAgain;
  425.  
  426.     } while (PlayAgain == 'Y');
  427.     return 0;
  428. }
Advertisement
Add Comment
Please, Sign In to add comment