### GUI guessing card game

I would like to try to make a guessing card game using GUI and c++ on visual studios.
Here is the brief description of the guessing card game on the google docs

Did tried it, still struggle to do since I have no idea should I use rand() for this program of the game but it only require 2 cards for the random guessing card game since the cards must be randomly generated(either 1 or 2 is correct or wrong). So far I can think of how to work with the options using a switch and cin input of guesscards.

Not to mention I have no idea how to make a gui for the guessing card game

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; int main() { int guesscards; int revealanswer; cout << "1. Spade of ace\n"; cout << "2. Heart ace\n"; cin >> guesscards; revealanswer = rand() % 2; switch(guesscards) { case 1: if (guesscards != revealanswer) { cout << "correct answer" << endl; break; } else { cout << "wrong answer" << endl; } break; case 2: if (guesscards != revealanswer) { cout << "correct answer" << endl; break; } else { cout << "wrong answer" << endl; } break; } }``````

Keep in mind the program should be a beginners level so it makes me easier to understand as a beginner, therefore dont make the program very complicated

Last edited on
Using rand that way will never have Ace of Hearts be a correct answer. You are generating a random number of either 1 or 0. `rand() % 2 + 1` is what you are looking for. Should include <cstdlib> for rand.

You should also seed the C random generator ONCE using srand, preferably near the top of main(). srand could be seeded with time() (in <ctime>)

Though using the C random generator in a C++ program is not a good thing when C++ has had good facilities in <random> since C++11.

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

A C++ working paper on using the C++ random number facilities:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf

Learn C++ has a "should read" page on generating random numbers:
https://www.learncpp.com/cpp-tutorial/random-number-generation/
beginner or not, try to use <random> instead of rand. There are plenty of drop-in examples that replace rand() and give you better code (rand isnt very random, in a nutshell).

making a gui .. is this windows? Windows used to have a card deck pre-made that I think anyone can use (?).
go ahead and finish your game as a console program.
the first step is to abstract the interface from the work.
so instead of cout and cin, you may have something like this:

 ``1234567`` `````` case 2: if (guesscards != revealanswer) { show_correct_answer_function(); //this hides cout, setwindowtext, whatever output break; //this break is not needed. you need 1 break in each case //if you want to avoid fall through, unless you have unusual logic (you do not). }``````

and you also want to condense and simplify logic (easier to follow and read).
perhaps
 ``1234567`` ``````string answertext[2]{"wrong answer", "correct answer"}; ... switch ... case 2: show_text(answertext[guesscards == revealanswer]); break;``````

so if the two are equal, the expression results in 'true' which is 1. answertext[1] is "correct answer". If they are not equal, it results in false, which is 0, the "wrong answer" words.
showtext would still be your interface (cout, gui text, or whatever else).

Last edited on
s021623 wrote:
make a guessing card game using GUI and c++ on visual studio

Nice goal, but the requirements to do GUI, even if using the WinAPI, is a whole lot more work than doing a console mode program.

I won't scare you with the gory details. Even a simple WinAPI app bit of code is quite a number of lines. Windows apps require a lot of basic housekeeping details to be attended to to work.

jonnin wrote:
Windows used to have a card deck pre-made

cards.dll. Win 7 and later stopped including it.
https://catonmat.net/cards-dll
@jonnin
@seeplus

I am using visual studios on windows 7 , so yeah these card decks did not include it sadly

As for <random>, can you be specific how to use in my coding with `string answertext[2]{"wrong answer", "correct answer"}`?
Last edited on
Using rand(), then possibly something like:

 ``123456789101112131415161718192021222324252627`` ``````#include #include #include int main() { srand(static_cast(std::time(nullptr))); unsigned guesscards {}; do { std::cout << "\nI have obtained a card. Do you think it is either:\n"; std::cout << "1. Spade of ace\n"; std::cout << "2. Heart ace\n"; do { std::cout << "Please enter 1, 2 or 0 to quit: "; std::cin >> guesscards; std::cin.clear(); } while ((guesscards > 2) && (std::cout << "Incorrect input\n")); if (guesscards != 0) if (guesscards == rand() % 2 + 1) std::cout << "Correct guess!\n"; else std::cout << "Wrong answer\n"; } while (guesscards != 0); }``````

or using C++ random:
 ``123456789101112131415161718192021222324252627`` ``````#include #include int main() { std::default_random_engine rng {std::random_device {}()}; const std::uniform_int_distribution card {1, 2}; unsigned guesscards {}; do { std::cout << "\nI have obtained a card. Do you think it is either:\n"; std::cout << "1. Spade of ace\n"; std::cout << "2. Heart ace\n"; do { std::cout << "Please enter 1, 2 or 0 to quit: "; std::cin >> guesscards; std::cin.clear(); } while ((guesscards > 2) && (std::cout << "Incorrect input\n")); if (guesscards != 0) if (guesscards == card(rng)) std::cout << "Correct guess!\n"; else std::cout << "Wrong answer\n"; } while (guesscards != 0); }``````

unrelated. the array of strings is used as I said -- a boolean expression chooses the first string [0] via false, or the second string [1] via true.

random is to replace rand().

eg

 ``12345678910`` `````` //two setup lines (and third optional seed line) outside of your loops or whatever, just do them once early in main() std::default_random_engine generator; generator.seed( seed); //seed can be the time(0) used by srand or whatever you like std::uniform_int_distribution distribution(1,2); //1 or 2 for your needs? or 0-1 for you? foo = distribution(generator); //this replaces 'rand()%2+1' or whatever //call the above wherever you had rand() //you can have multiple setups to give different ranges, eg 1-4, 1-6, 1-8, 1-10, 1-12, 1-20 for the common game dice, and so on... random can also generate random floating point and do many more things as you dig into its features, but a simple replace for rand() all you need is the above. ``````
Last edited on
@seeplus
OK I decide to use rand() for the guessing card game

However the I need to know how to use GUI coding for the card game since I used visual studios on Windows 7
That link was for introductory lessons for Windows that worked for Win9x and beyond.
Last edited on
@George p

Does is work with Windows 7?

>Get Borked!
Are you serious? At this moment not sure if you are trolling or not but come on
Last edited on
The following is what I tried to avoid earlier, throwing a heaping helping of complex (yet very simplistic) code out for a beginner to get totally confused.

Console mode "Hello World" source:
 ``123456`` ``````#include int main() { std::cout << "Hello World!\n"; }``````

The smallest possible WinAPI GUI app source, it displays a message box, works with Win XP and above, requires Unicode encodings:
 ``12345678`` ``````#include int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR szCmdLine, _In_ int iCmdShow) { MessageBoxW(NULL, L"Hello World!", L"Hello Message", MB_OK); return 0; }``````

WinAPI (GUI) "Hello Windows!" source that works with Win XP and above, requires Unicode encodings:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109`` ``````// INCLUDES ==================================================================== #include // FUNCTION PROTOTYPES ========================================================= LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // entry point for a Windows application ======================================= int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR szCmdLine, _In_ int nWinMode) { // define the window class name static const WCHAR szAppName[ ] = L"MinimalWindowsApp"; // create an instance of the window class structure WNDCLASSEXW wc; wc.cbSize = sizeof(WNDCLASSEXW); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = (HICON) LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR); wc.hIconSm = (HICON) LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR); wc.hCursor = (HCURSOR) LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED); wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = szAppName; if (0 == RegisterClassExW(&wc)) { MessageBoxW(NULL, L"Can't Register the Window Class!", szAppName, MB_OK | MB_ICONERROR); return E_FAIL; } // define the application title static const WCHAR szAppTitle[ ] = L"Win32 API Skeletal Application"; // create the window HWND hwnd = CreateWindowW(szAppName, szAppTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // check if the window was created, exit if fail if (NULL == hwnd) { MessageBoxW(NULL, L"Unable to Create the Main Window!", szAppName, MB_OK | MB_ICONERROR); return E_FAIL; } // show and update the window ShowWindow(hwnd, nWinMode); UpdateWindow(hwnd); static BOOL bRet; static MSG msg; // enter the main message loop while ((bRet = GetMessageW(&msg, NULL, 0, 0)) != 0) // 0 = WM_QUIT { // check for error if (-1 == bRet) { // handle the error and possibly exit // for this app simply report error and exit MessageBoxW(NULL, L"Unable to Continue!", szAppName, MB_OK | MB_ICONERROR); return E_FAIL; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } // the app is done, exit normally, return control to Windows return (int) msg.wParam; } // processes the messages that Windows sends to the application ================ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { // choose which Windows messages you want to use switch (message) { case WM_PAINT: HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); // draw some text centered in the client area RECT rect; GetClientRect(hwnd, &rect); DrawTextW(hdc, L"Hello, Windows!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hwnd, &ps); return S_OK; case WM_DESTROY: // exit the application PostQuitMessage(0); return S_OK; } // let Windows process any unhandled messages return DefWindowProcW(hwnd, message, wParam, lParam); }``````

WinAPI GUI requires a butt-load of code to successfully hook into the preemptive multitasking OS that is WindowsÂ®.

But what the hey, have at it. You asked for "how to do GUI".

I'm done here now.
Last edited on
If you don't want all the 'standard boiler code' that goes with using WIN32 API as above, then you need to use additional gui library. VS comes with MFC (basically windows as classes - which needs a god understanding of classes to use). Otherwise you're looking at 3rd party. One simple free one is FLTK which is used by Stroustrup in his book 'Programming: Principles and Practice Using C++'.
@seeplus

So what happens when someone does not have FLTK when I send the cpp file with the gui coding to others so they can open and see my cpp file of the gui coding?
Standard C++ doesn't include any GUI. If you use GUI with C++ then you're going to have to use 3rd party library - whether this is Windows WIN32, MFC, FLTK, QT etc.

With the right installation, VS can use WIN32 and MFC. If MFC, then anyone else will also need to have MFC installed via VS.

If you use something like FLTK then for others it's the same as for any C++ project requiring 3rd party library - they have to get it to use the project.

There is also C++/cli which 'fuses' C++ with .net if you want to go down that route. https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=msvc-170

Then there is C++/winrt which is gui based but produces a Windows 'app' rather than an .exe. See https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt
Last edited on
 So what happens when someone does not have FLTK when I send the cpp file with the gui coding to others so they can open and see my cpp file of the gui coding?

well, what happens?
A scenario:
they see in the source code repository or documentation that it is required to have the FLTK library (and whatever else) to build the code.
The documentation and repo would further provide the project files, along with the source files, whether that is just an old school makefile or a bunch of files for a visual studio project.
It would provide the versions you used of those tools as well, just in case newer versions radically changed something and broke it.

with all that info, the person opening your project will have no trouble building it!
As a general rule of thumb, if I can't build source code pretty much as-downloaded, I dispose of it and find something else. As-downloaded may mean installing some libraries and setting some paths up etc, but my tolerance for stuff that takes days to figure out how to compile it is next to zero.
Last edited on
Registered users can post here. Sign in or register to post.