(1) create a function called DEAL which will [randomly] \'deal out\' four piles
ID: 3543809 • Letter: #
Question
(1) create a function called DEAL which will [randomly] 'deal out' four
piles of 13 cards each, which should be displayed in the format shown:
Q982AQT93T934 AKJ657428J987 K865AKJ54KQT3 T743JQ762A652
SSSSHHHHHDDDC SSSSSHHHDCCCC HHHHDDDDDCCCC SSSSHDDDDCCCC
DEAL will return a different result each time; the above is just a sample.
You must return a 2x56 character array (56 = 52 cards + 4 blanks [four,
because you will later find it convenient to also have blanks on the end
of the rows]), representing the cards in the deck, separated [by blanks]
into four 'hands'. Each hand should be sorted by suit (Spades on the left,
then Hearts, then Diamonds, and Clubs on the right). Within each suit,
the cards should be arranged in descending order (AKQJT98765432).
(2) create a SCORE function, which will accept input in the form that DEAL
returns (that is, a 2x56 character array) and 'decorate' this with labels
(one for each player, traditionally labeled NORTH, EAST, SOUTH, and WEST).
Your SCORE function will additionally tally the 'points' each of the four hands
are worth [details about how to do this will be given later, at the end of this
file] and display these totals below each hand. The format should look like:
Your SCORE function should accept a left and a right argument; you can ignore
the right argument unless you are attempting the extra credit. The left
argument is a 2x56 array; a typical invocation would be:
HAND1 SCORE 0
or
DEAL SCORE 0
your SCORE function will have monadic behavior as
well as dyadic behavior. In particular, if you have no left argument, as in:
SCORE 0
...then SCORE should behave as though you had said:
DEAL SCORE 0
(that is, if score is not given a hand to score, it should generate a hand
and score that [random] hand)
It's alright if your don't know how to do this part, but help if you can:
create a 3-dimensional array when given a
righthand argument of 1. Here is the proper format for
HAND1 SCORE 1
Explanation / Answer
To create a programming language, you must specify all the rules that control how it works. How will variables be handled? How will functions be called? (etc.). Once you have all that figured out, you can write your compiler. Most CC++ compilers do not translate directly into machine code (GCC is an example here), instead they translate into assembly, which is then compiled again into machine code. This can be a bit slower, but also allows for multi-level optimization, since both the C-compiler and the assembly-compiler get a crack at optimizing the code.
At the lowest level, a compiler works lexically, that is, parsing the input. For a C compiler, the first part of an input file might look like this:
#include <stdlib.h>
#include <stdio.h>
void main(void)
{
int a, b;
a = b = 1;
a += b;
printf("Result: %i",a);
}
The compiler's lexer would most likely break it down into something like this: (note that whitespace; the un-needed space between commands; is skipped) :
Directive: "#include <stdlib.h>"
Directive: "#include <stdio.h>"
Function declaration: "void main(void)"
Function begin: "{"
Variable declaration: "int a,b";
Variable assignment: "a = b = 1"
Mathematical assignment operation: "a += b"
Function call: "printf('Result: %i",a)"
Function end: "}"
Once the lexer has sorted everything in tokens (like we saw above), the actual compiler can kick in. (Note that the lexer would also catch any bad syntax errors in the source). The compiler would go "grab" the contents of the include files, inserting their contents dynamically into the source file, and then generating the assembly code equivalent to the C code shown.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.