Code game

Hello, i am new to C++ and doing an assignment, "Binary Code Breaker" I've looked through the forums already before signing up and found a similar thread, wasn't really what i was looking for though but helped give me a few ideas on how to go about my code.

what i have to do is make a game where the player has to guess a four digit code made up of 1's and 0's. The game can either be made so that the play guesses all 4 digits at once, or one digit at a time.

Right now, i'm just trying to get it functional, and would gladly take any help or tips on how to go about doing this.



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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>


using namespace std;


int main()
{
	srand(static_cast<unsigned int>(time(0)));

	int userSelection;
	
	const short PLAY_GAME = 1;
	const short QUIT_GAME = 2;

	do
	{
		
		cout << "[1] Start Game." << endl;
		cout << "[2] Quit Game." << endl;
		cout << "> ";
		cin >> userSelection;

		switch (userSelection)
		{
		case PLAY_GAME:
			break;
		case QUIT_GAME:
			cout << endl;
			cout << "Goodbye!" << endl;
			cout << endl;
			system("PAUSE");
			break;
		default:
			cout << endl;
			cout << "That is not a valid option!" << endl << endl;
			cout << endl;
		}
		while (userSelection == 1)
		{
			const int CODES = 10;

			string BIN[CODES] =
			{
				"0100",
				"1010",
				"1110",
				"1011",
				"0001",
				"0010",
				"0000",
				"1111",
				"1000",
				"0100"
			};

			const int TRIES = 5;
			string guess;

			do
			{
				cout << "play Game? (y/n) " << endl << endl;
				char decision = _getch();

				do{
					if (decision == 'y')
					{
						int outCode = (rand() % CODES);
						string secretCode = BIN[outCode];

						for (int i = 0; i < TRIES; i++)
						{
							int counter = 0;

							cout << "Enter digit #1" " You have (" << TRIES - i << ") Attempts left. ->";
							cout << "Enter your guess -> ";
							getline(cin, guess);

							for (size_t i = 0; i < guess.size(); i++)
							{
								if (guess[i] == secretCode[i])
								{
									cout << endl;
									cout << guess[i];
									cout << endl;
									++counter;
									cout << "Is correct" << endl;
								}
								else
								{
									cout << endl << endl << " ** WRONG ** " << endl << endl;
								}
							} cout << endl;
							if (counter == 4)
							{
								cout << "You win" << endl;
							}
							else
							{
								cout << "Sorry, you lose." << endl;
							}
						}
					}
					else if (decision == 'n')
					{
						cout << "Goodbye";
					}
				} while (decision == 'n');
			} while (true);
		}
	} while (userSelection != 2);

	

	system("PAUSE");
	return 0;
}


**EDIT**

When i..
[1]start game,
Press - y - to play
it produces the first two lines as if i have already made a guess.

"Enter digit #1" " You have (5) Attempts left. ->"
"Enter your guess -> ";
sorry, you lose.
"Enter digit #1" " You have (4) Attempts left. ->"
"Enter your guess -> "
Last edited on
This is untested, but the logic should work if I understand your assignment correctly.
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
int main () {
	srand(time());
	char keyPressed;
	int code, entry;
	do {
		cout<<"Play (p) or Quit (q)?";
		keyPressed = getch();
		system("cls");
		if (keyPressed == 'p' || keyPressed == 'P') {
			for (int i = 0; i < 5; i++) {
				code = 1000 * (rand() % 2);
				code += 100 * (rand() % 2);
				code += 10 * (rand() % 2);
				code += rand() % 2);
				
				cout<<"Enter guess: ";
				cin>>entry;
				if (entry == code) {cout<<"You win!"; break;}
				cout<<"Wrong"<<endl;
			}
		} else if (keyPressed == 'q' || keyPressed == 'Q') cout<<"Goodbye!"<<endl;
	} while (keyPressed != 'q' && keyPressed != 'Q');
	system("pause");
	return 0;
}
I have also made a binary code encoder and decoder mine gives it the key the the program uses the key to encode or decode

Header.h
1
2
3
4
5
6
#include <iostream>
#include <string>
#include <cctype>
#include <stdio.h>
#include <windows.h>
#include <unordered_map> 


source.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
#include "Header.h"

using namespace std;

unordered_map<char, string> substitute_for =
{
	{ 'A', "100 0001" }, { 'B', "100 0010" }, { 'C', "100 0011" }, { 'D', "100 0100" },
	{ 'E', "100 0101" }, { 'F', "100 0110" }, { 'G', "100 0111" }, { 'H', "100 1000" },
	{ 'I', "100 1001" }, { 'J', "100 1010" }, { 'K', "100 1011" }, { 'L', "100 1100" },
	{ 'M', "100 1101" }, { 'N', "100 1110" }, { 'O', "100 1111" }, { 'P', "101 0000" },
	{ 'Q', "101 0001" }, { 'R', "101 0010" }, { 'S', "101 0011" }, { 'T', "101 0100" },
	{ 'U', "101 0101" }, { 'V', "101 0110" }, { 'W', "101 0111" }, { 'X', "101 1000" },
	{ 'Y', "101 1001" }, { 'Z', "101 1010" }
};

string convert(const string& text)
{
	string result;

	for (auto ch : text)
	{
		if (isalpha(ch)) result += substitute_for[toupper(ch)];
		else result += ch;
	}

	return result;
}

int main()
{
	string text;

	cout << "Enter statement you would like to convert to binary: ";
	getline(cin, text);

	cout << convert(text) << '\n';
}
Topic archived. No new replies allowed.