casparjespersen
casparjespersen

Reputation: 3870

Defining specific Matlab class structure

I have to create the following functions (to a command-line casino in Matlab):

function [wonAmount, noGuesses] = highLow(gambledAmount)
function [wonAmount, noPulls] = slotMachine(gambledAmount, betFactor)
function wonAmount = roulette(gambledAmount, typeOfBet)

This is a matter of the task I'm given, and it has to be fulfilled. I could just create simple functions, since all the games have some similar characteristics, calculation of wonAmount, etc. and generally that OOP is more structured, I would like to try it (OOP) out in Matlab.

I could create a handle class, but I have to fulfill the requirements of the task. Which a handle class with a method play - I am of the understanding that a handle class constructor HAS to return the object itself? I am looking for a class in which the constructor doesn't necessarily return the constructor - a static class/function of a kind?

How would you design this class?

Upvotes: 1

Views: 254

Answers (1)

Pete
Pete

Reputation: 2344

It sounds like you need the interface to your program to look like function calls, but internally you want to use OO programming. Is that right?

Assuming you NEED the interface to look like:

[wonAmount, noGuesses] = highLow(gambledAmount)

You could write code inside the highLow function that does:

function [wonAmount, noGuesses] = highLow(gambledAmount)
game = highLowGame; %instantiate the game, and run it:
[wonAmount, noGuesses] = highLowGame.run(gambledAmount);

Or you can use static methods:

function [wonAmount, noGuesses] = highLow(gambledAmount)
[wonAmount, noGuesses] = highLowGame.runGame(gambledAmount);

http://www.mathworks.com/help/matlab/matlab_oop/static-methods.html

Where I'm assuming highLowGame.m looks like this:

 classdef highLowGame < casinoGame

There's no good reason to use handle classes for this, unless you really want a specific calling syntax / handle behavior...

If for some reason you need this all to be in one M-file, then I'm afraid you're out of luck... but that seems like a silly restriction.

Upvotes: 2

Related Questions