Great Deal! Get Instant \$10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

# CITP 3310 XXXXXXXXXXSurvey of Programming Languages XXXXXXXXXXLab 13 Lab 13 – Arrays In this lab you will practice working with arrays. Arrays allow you to group data of the same type together using...

CITP 3310 XXXXXXXXXXSurvey of Programming Languages XXXXXXXXXXLab 13
Lab 13 – A
ays
In this lab you will practice working with a
ays. A
ays allow you to group data of the same type
together using one variable name and index numbers to access each of the elements.
Example Program
The example program for this lab is a rewritten version of Lab 12. In the Reference Parameters lab you
wrote a program that found the average of 5 judges' scores, minus the minimum and maximum score.
You had to repeat a lot of code in that lab in order to get input from each judge, and to find the
minimum and maximum values.
The example program uses an a
ay to store the judges' scores. This allows us to use for loops to get the
input, to find the minimum and maximum, and to take the sum. A huge benefit of using a
ays is that we
can make the functions that find the minimum and maximum, and that take the average, more general.
These functions can now use an a
ay of numbers of any size (not just 5). The number of judges is set by
a constant at the top of the source file. If you want to run the program with more (or fewer) judges, all
we have to do is change the value of that constant, and everything else will change automatically. Try it
out!
ay.cs file, examine the code, and run the program. Pay attention to
how to use a
ays as a parameter to a function. Notice that in addition to the a
ay, another variable is
always included to specify the size of the a
ay.
For your program you will complete code that plays the War card game. In this game, the deck of cards
is evenly divided among two players. The players are not allowed to look at the cards in their hand. On
each round of the game, both players lay down the top card from their hand. The player with the higher
value card wins both cards and places the cards on the bottom of his hand. For our version of the game,
in the case of a tie, each player places their card back at the bottom of their hand (the real game has
different rules for ties). Game play continues until one of the players has all of the cards.
In order to simplify our program, our "deck" is a series of random numbers ranging from 1 to 13
(because a real deck of cards has 13 values). The size of the deck is set by a constant. This game can take
a long time to play, so a deck size of 10 cards is a reasonable size for testing the game. You can change
the deck size if you wish, although larger numbers will likely take much longer to finish.
The main function is provided to you in warGame.cs. You should not make any modifications to this
function, or to the other two functions provided (to print the results of each round of the game, and to
create the "deck" of cards.) You will write two functions, described below.
Remember to write small parts of the program at a time and test each part before moving on to the next
part.
CITP 3310 XXXXXXXXXXSurvey of Programming Languages XXXXXXXXXXLab 13
Function 1: Deal the Cards. This function takes the deck of cards and splits it between the two players
y alternately dealing a card to each player. Note that the a
ay for each player's hand is the same size
of the deck. This is because we need to be able to store all cards in the winner's hand. Therefore, the
a
ay for each player's hand is a partially filled a
ay. In the main function I initialized all elements of the
players' hand a
ays to zero. In our game, when an a
ay element has the value zero it indicates that
there is no card at that index. All of the cards must be placed at the beginning of the a
ay. An example
of a deck dealt to two players is shown below.
Deck:
XXXXXXXXXX9
Player 1's Hand:
XXXXXXXXXX
Player 2's Hand:
XXXXXXXXXX
Function 2: Shift the Cards Forward. This function moves all cards in a hand one place forward,
emoving the first card and setting the value of the last card to zero. This simulates a player removing a
card from her hand. An example of a hand of cards before and after shifting is shown below.
Before shift:
XXXXXXXXXX
After shift:
XXXXXXXXXX
Challenge
I think this one will be challenging enough… If you complete this lab, I’ll throw in the 10 bonus points for
free.
Answered Same Day Jul 25, 2021

## Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WarGame
{
class warGame
{

GLOBAL CONSTANTS
const int SIZE = 10;
This constant sets the size of our "deck"

Increasing it will cause your program to

take longer to reach a conclusion.
static void Main(string[] args)
{
int[] deck = new int[SIZE];
The deck of cards
int[] hand_one = new int[SIZE];
A hand of cards, with all elements initialized to zero
int[] hand_two = new int[SIZE];
A hand of cards, with all elements initialized to zero
int tmp1;
Temporary variable to hold a card
int tmp2;
Temporary variable to hold a card
int size1 = SIZE / 2;
Number of cards in hand 1
int size2 = SIZE / 2;
Number of cards in hand 2
string tmpStr;
Used for asking the user to press enter to continue

Get a deck of numbers (to represent cards)

Because we are simulating a deck of cards, we will get random numbers

in the range of 1 to 13.
getNums(deck, 13, SIZE);

Deal the deck between the two players
splitA
ay(hand_one, hand_two, deck, SIZE);

loop until one player has no more cards.
while (size1 != 0 && size2 != 0)
{

Show the card at the front of each players hand
showCard(hand_one, hand_two, size1, size2);

Store the front cards into temporary variables
tmp1 = hand_one[0];
tmp2 = hand_two[0];

Shift the cards forward in each hand
shift(hand_one, size1);
shift(hand_two, size2);

If the first player has the higher card
if (tmp1 > tmp2)
{
Console.WriteLine("You have the higher card!");

We are going to add one card to Player 1's hand and

remove a card...
SOLUTION.PDF