Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. string setBoard();
  10.  
  11. int main()
  12. {
  13. string guess; //the player's guess will be stored
  14. string spot; //where colors will be randomly assigned
  15. string chkSpot; //checks how accurate the player's guess is
  16. char again='y'; //Variable that user input will determine if he plays again
  17.  
  18. spot=setBoard();
  19.  
  20. cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
  21. cout<<"== [ "<<"?"<<" "<<"?"<<" "<<"?"<<" "<<"?"<<" ]";
  22. cout<<" -- CHOICES: R B G Y -- "<<endl;
  23. do{
  24. cout<<"Enter your guess: ";
  25. getline(cin,guess);
  26.  
  27. //Checks for valid input
  28. while(guess.size()!=4)
  29. { cout<<"Invalid input. Please type in four letters"<<endl;
  30. cout<<"Enter your guess: ";
  31. getline(cin,guess);
  32. }
  33.  
  34. //for loop checks each spot to see how it corresponds with the player's
  35. //guess, and then will assign O or X to a variable that will output
  36. //during the game so that the player can know how accurate his guess was
  37. for (int j=0; j<4; j++)
  38. {
  39. (guess[j]==spot[j]) ? (chkSpot[j]='O') : (chkSpot[j]='X');
  40. }
  41.  
  42. //if statement checks if user guessed the colors assigned to each
  43. //spot on the board correctly. If the player is successful, tell em.
  44. //if not, tell em.
  45. if (chkSpot[0]=='O'&&chkSpot[1]=='O'&&chkSpot[2]=='O'&&chkSpot[3]=='O')
  46. {
  47. cout<<"YOU WIN"<<endl;
  48.  
  49. cout<<"|| ["<<spot[0]<<"]["<<spot[1]<<"]["<<spot[2]<<"]["<<spot[3]<<"] || ";
  50. cout<<"== [ "<<chkSpot[0]<<" "<<chkSpot[1]<<" "<<chkSpot[2]<<" "<<chkSpot[3]<<" ]"<<endl;
  51. cout<<endl;
  52. cout<<"Would you like to play again on a new board? Enter 'y' for yes, or anything else to quit."<<endl;
  53. spot=setBoard();
  54. cin>>again;
  55. }
  56. else
  57. {
  58.  
  59. cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
  60. cout<<"== [ "<<chkSpot[0]<<" "<<chkSpot[1]<<" "<<chkSpot[2]<<" "<<chkSpot[3]<<" ]";
  61. cout<<" -- CHOICES: R B G Y -- "<<endl;
  62. }
  63.  
  64. }while(again=='y'||again=='Y');
  65.  
  66. return 0;
  67. }//end of main
  68.  
  69. string setBoard(){
  70. string spot; //where colors will be randomly assigned
  71. string chkSpot; //checks how accurate the player's guess is
  72. int boardSet; //where random number assignment (1-4) will be stored
  73. int repeat[4]={}; //tests for number repetition
  74. srand(time(NULL));
  75.  
  76. //for loop for randomizing the board
  77. for (int i=0; i<4; i++)
  78. {
  79. boardSet=(rand() % 4) + 1;
  80.  
  81. //first if test will test if the random number is identical to any previous random number.
  82. //if the random number IS equal to any previous ones, it will decrement 'i'
  83. if ((boardSet==repeat[0])||(boardSet==repeat[1])||(boardSet==repeat[2])||(boardSet==repeat[3]))
  84. {
  85. //decrements so that the for loop will run an extra loop so that it can
  86. //assign a number that hasn't already been taken
  87. i--;
  88. }
  89. //if the random number is unique, it assigns it to a spot on the board
  90. else{
  91. //each number 1-4 corresponds to a color that will be assigned to a spot
  92. //on the board
  93. switch (boardSet)
  94. {
  95. case 1: {
  96. spot[i]='R'; //Assigns red to a spot
  97. chkSpot[i]=spot[i]; //Assigns red to check for if the user guessed correctly.
  98. repeat[i]=boardSet; //assigns number 1 in repeat variable so it can test for future repeats
  99. break;
  100. }
  101. case 2: {
  102. spot[i]='B'; //Assigns blue to a spot
  103. chkSpot[i]=spot[i]; //Assigns blue to check for if the user guessed correctly.
  104. repeat[i]=boardSet; //assigns number 2 in repeat variable so it can test for future repeats
  105. break;
  106. }
  107. case 3: {
  108. spot[i]='Y'; //Assigns yellow to a spot
  109. chkSpot[i]=spot[i]; //Assigns yellow to check for if the user guessed correctly.
  110. repeat[i]=boardSet; //assigns number 3 in repeat variable so it can test for future repeats
  111. break;
  112. }
  113. case 4: {
  114. spot[i]='G'; //Assigns green to a spot
  115. chkSpot[i]=spot[i]; //Assigns green to check for if the user guessed correctly.
  116. repeat[i]=boardSet; //assigns number 4 in repeat variable so it can test for future repeats
  117. break;
  118. }
  119. }//ends switch
  120. }//ends else statement
  121. }//ends for loop
  122. return spot;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement