Test poker hand by using edit text boxes GUI

1 次查看(过去 30 天)
For a project for my engineering class we are supposed to make a helper to any game. I chose poker and I am having trouble simplifying the conditions such that I do not have to test every possible hand manually. In my function I call get the string of the text boxes (Ace=1,King=2,Queen=3, etc.). I need help on testing the conditions (what card they are) of the edit text boxes and displaying the correct hand output. If more information is needed please let me know!
function pushbutton2_Callback(hObject, eventdata, handles)
box1=str2double(get(handles.edit1,'string'));
box2=str2double(get(handles.edit2,'string'));
box3=str2double(get(handles.edit3,'string'));
box4=str2double(get(handles.edit4,'string'));
box5=str2double(get(handles.edit5,'string'));
box6=str2double(get(handles.edit6,'string'));
box7=str2double(get(handles.edit7,'string'));
if box1==1 && box2==2 && box3==3 && box6==4 && box7==5
set(handles.Hand,'string','Royal Flush')

采纳的回答

Rik
Rik 2019-4-3
What I would do is first of all change your value convention to something more standard (Ace=1,Jack=11,Queen=12,King=13) or keep them in a char format. Having an array of handles instead of numbered ones really helps to simplifies your code. Then I would sort the numbers. Because the order doesn't matter, the user can input different orders and you can describe entire vectors at a time.
%if you're using GUIDE (which you shouldn't), you can use this in the startup:
%for n=1:7
% handles.card_input(n)=handles.(sprintf('edit%d',n));
%end
function pushbutton2_Callback(hObject, eventdata, handles)
box=zeros(1,numel(handles.card_input));
for n=1:numel(box)
box(n)=str2double(get(handles.card_input(n),'string'));
end
box=sort(box);
if isequal(box,[1 10 11 12 13])
name='Royal Flush';
elseif numel(unique(box))==2
name='full house'
elseif all(diff(box)==1)
name='straight';
else
name='high card';%default option
end
set(handles.Hand,'String',name)
end
You can add very complicated tests to find all named hands.
  5 个评论
Rik
Rik 2019-4-3
编辑:Rik 2019-4-3
You could use the numbered setup that you originally had, or use an array of handles. This is a two-step process: first getting the card values from the user and then figuring out the name of the hand.
The concept of 'all at once' is not always easy. Do you consider my loop to be all at once? They are all processed with the same code, but you could argue that only matrix operations would count as all at once.
Since there are only 7 cards you need to process I would suggest a loop. I would also suggest using the uicontrol Value property to find the card value:
%assuming this order: {empty,A,K,Q,J,10:2}
card_value=[NaN 1 13:-1:2];
%assuming the handles of the inputs for the river and
%the player's hand are in a single handle array
box=zeros(1,numel(handles.card_input));
for n=1:numel(box)
temp=get(handles.popupmenu1,'Value');
if temp==1
errordlg('invalid card selected, retry')
return
end
box(n)=card_value(temp);
end
Rik
Rik 2019-4-3
partly responing to your email:
The likely reason why you are unsure of where you should put what handles is that you are using GUIDE. You need to be quite good at Matlab to not be confused by GUIDE. That is why I created a small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
To avoid a complete redesign, you could also use something like the code below in the startup function of your GUI:
handles.card_input=handles.popupmenu1;
handles.card_input(2)=handles.popupmenu2;
handles.card_input(3)=handles.popupmenu3;
handles.card_input(4)=handles.popupmenu4;
handles.card_input(5)=handles.popupmenu5;
handles.card_input(6)=handles.popupmenu6;
handles.card_input(7)=handles.popupmenu7;
Two closing remarks: you're welcome to attach your files to your question or a comment, although I suspect it is better if you make the modifications yourself, and secondly, I made a copy-paste error in my previous comment:
temp=get(handles.popupmenu1,'Value');
%should have been:
temp=get(handles.card_input(n),'Value');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Card games 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by