A Blackjack GUI in Matlab – Part IV of using Guide

With this post we wrap up our introduction to using guide with a look at the usage of a push button and a drop-down menu in our creation of a blackjack strategy guide GUI. As a look back, or to catch up, you can visit our three previous posts: Part I: Creating the layout using guide; Part II: Setting up a table; and Part III: getting user input from radio buttons and text boxes. Finally, we have also attached all of the necessary code to the end of this post to do as you please.

The components that are remaining in our GUI are the drop-down menu for the selection of different strategies and the push button to complete the request and analysis for a combination of player cards and dealer’s face card.

– Drop-down Menu of Blackjack Strategies
We have created the two different tables already for 1) basic strategy and 2) a random decision, and these can be found in our blackjackTables.m file we’ve attached. In order to display the appropriate table to the screen though, we will be utilizing the drop-down menu. Within the drop-down menu properties, we have edited the “String” to have two values, with the “Value” defaulting to “1”, or “Basic Strategy”, and the table “Tag” being “Strategies”.
blackjack drop down strategies
If different/more strategies are wanted, you could simply edit the “String” property and then update the blackjackTables.m file. Additionally, the “Tag” property can be renamed as you please, though the callback will also change with it within the associated guide created m file. Speaking of the callback, in order to invoke the table upon clicking the drop-down menu, we simply need to use the function get and call our decisionTable.m GUI program (as described previously). The callback will look as follows:

function Strategies_Callback(hObject, eventdata, handles)

handles.strat = get(hObject,'Value');
handles.tableData = decisionTable(handles.strat);
guidata(hObject, handles);

As you can see, we are accessing the “Value” Property from the drop-down menu, and passing the type selected onto the decision table. Following those two lines, we run the command “guidata(hObject,handles);” as a way to update our handles structure, as we have added two values to the struct in “strat” and “tableData”. At this point, the menu should be complete! The GUI can work with or without accessing the drop-down menu, depending on whether the user wants to see the strategy table or not. Additionally, only one instance of the table will pop open.

– The OK Push Button
For this GUI, the point of the OK button is to output a result to the static “Recommended Decision” text box (Tagged as “decisionText” and set to be “inactive” under the enable property). Such results could include, “Stand”, “Hit”, “Double Down”, or “Split”. Additionally, you might end up with a blackjack (A + T/J/Q/K = 21). In other other GUI’s you might create, the OK button might indicate all form fields are complete and the GUI can close or continue on. Instead, this GUI will remain open until force closed, as strategies can continually be evaluated.

Previously, we have shown that the dealer radio button and player text box values are obtained on an OK button push event, by using the get function. i.e. as follows:

dealer = get(get(handles.dealerCard,'SelectedObject'),'String');
card1 = get(handles.FirstCard,'String');
card2 = get(handles.SecCard,'String');

We won’t get into all the nitty gritty (take a look at the attached code if you please for that), but now we have a table saved in the handles struct as handles.tableData, and all we need to do is perform a lookup for all possible outcomes. This can simply be done by assigning a value to each card type, and indexing within the tableData array. Once this index is determined and the result obtained, the result will be spit out to the screen in the inactive decisionText textbox.

% create output strings
outString = {'Hit','Stand','Split','Double', 'Blackjack!'};
    
% give a recommendation and compute result.
% where result is based on outString values, index, and handles.tableData value
set(handles.decisionText,'String',result);

One possible error that might come up, is a blank value within either of the player’s text boxes upon clicking OK. One solution to this event is to call up an errordlg window.

if isempty(card1) || isempty(card2)
    errordlg({'No value entered for player card!'; 'Enter a card value : ';'(2-10,T,J,Q,K,A)'},'Value Entered Error')
else
% Code for converting card/dealer values to indices and accessing handles.tableData.
end

That’s it! You should now have a working blackjack strategy guide. Good luck at the tables 🙂

While we have done a quick introduction to guide, and hopefully provided some entertainment through the use of a blackjack GUI, we’ve just scratched the surface of creating GUIs. We’ll keep trying to provide additional tips, but in the meantime to play around with this GUI you can download all the code here: Matlabgeeks Blackjack GUI

One thought on “A Blackjack GUI in Matlab – Part IV of using Guide

Leave a Reply

Your email address will not be published. Required fields are marked *