Question

Complete a program In C#: some code is included, you edit the areas that have not...

Complete a program In C#: some code is included, you edit the areas that have not been filled.

For your C# 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.

......

using System;
using System.Collections.Generic;
using System.Linq;

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;

  
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 from Player 2's hand. So increase the size
// of the Player 1 array and decrease the size of the
// Player 2 array by one.
size1++;
size2--;

//add both cards to the end of Player 1's hand
hand_one[size1 - 2] = tmp1;
hand_one[size1 - 1] = tmp2;

}

// If the computer has the higher card
else if (tmp2 > tmp1)
{
Console.WriteLine("The computer has the higher card!");

// We are going to add one card to Player 2's hand and
// remove a card from Player 1's hand. So increase the size
// of the Player 2 array and decrease the size of the
// Player 1 array by one.
size1--;
size2++;

// Add both cards to the end of Player 2's hand
hand_two[size2 - 2] = tmp2;
hand_two[size2 - 1] = tmp1;

}

// If both players have the same value card
else
{
Console.WriteLine("Tie!");
// Don't change the size of the players' hands

// Put each player's card at the end of their hand
hand_one[size1 - 1] = tmp1;
hand_two[size2 - 1] = tmp2;

}

// Pause the game at the end of each turn
Console.Write("Press any key for the next turn...");
Console.ReadKey();

Console.Write("\n");
}

// Output the winner of the game
if (size1 == 0)
{
Console.WriteLine("The computer wins the game!");
}
else if (size2 == 0)
{
Console.WriteLine("You win the game!");
}

Console.Write("Press any key to exit...");
Console.ReadKey();
} // end Main function
// DO NOT MODIFY THIS FUNCTION
//
static void getNums(int[] arr, int range, int size)
{
int i; // A loop control variable
Random rnd = new Random();

for (i = 0; i < size; i++)
{
// Add a random number within the specified range to the array
arr[i] = rnd.Next(1, 7);
}
}


// DO NOT MODIFY THIS FUNCTION
//
static void showCard(int[] hand_one, int[] hand_two, int one, int two)
{
Console.WriteLine("*****Number of cards in each player's deck*****\n");
Console.WriteLine("You: {0}", one);
Console.WriteLine("Computer: {0}", two);
Console.WriteLine("You have the card: {0}", hand_one[0]);
Console.WriteLine("The computer has the card: {0}", hand_two[0]);
}


/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//// ////
//// YOUR CODE GOES BELOW THIS POINT ////
//// ////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////


//
// Split the elements of a source array into two destination arrays
// The given size is the size of the destination arrays (they are the same size)
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
static void splitArray(int[] dest_one, int[] dest_two, int[] source, int size)
{
int ctr = 0; //Counter that will go through the source[] array.
int i; // A counter for our loop control variable

while (true)
{
if (i == 0)
{

}
  
// Loop through the destination arrays.
// Assign each element in a destination array the next element
// in the source array. Use the ctr variable to keep track of the
// current position in the source array.


}

//
// Shifts the items in an array one place to the left
// Throws out the first element in the array and sets the last element
// in the array to zero.
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
static void shift(int[] arr, int size)
{
int i; // A counter


// shift the array

// Set the last element in the array to zero


}

} // end Class

} // end Namespace
0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE CODE IS:

using System;
using System.Collections.Generic;
using System.Linq;

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;

      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 from Player 2's hand. So increase the size
          // of the Player 1 array and decrease the size of the
          // Player 2 array by one.
          size1++;
          size2--;

          //add both cards to the end of Player 1's hand
          hand_one[size1 - 2] = tmp1;
          hand_one[size1 - 1] = tmp2;

        }

        // If the computer has the higher card
        else if (tmp2 > tmp1) {
          Console.WriteLine("The computer has the higher card!");

          // We are going to add one card to Player 2's hand and
          // remove a card from Player 1's hand. So increase the size
          // of the Player 2 array and decrease the size of the
          // Player 1 array by one.
          size1--;
          size2++;

          // Add both cards to the end of Player 2's hand
          hand_two[size2 - 2] = tmp2;
          hand_two[size2 - 1] = tmp1;

        }

        // If both players have the same value card
        else {
          Console.WriteLine("Tie!");
          // Don't change the size of the players' hands
          // Put each player's card at the end of their hand
          hand_one[size1 - 1] = tmp1;
          hand_two[size2 - 1] = tmp2;

        }

        // Pause the game at the end of each turn
        Console.Write("Press any key for the next turn...");
        Console.ReadKey();

        Console.Write("\n");
      }

      // Output the winner of the game
      if (size1 == 0) {
        Console.WriteLine("The computer wins the game!");
      }
      else if (size2 == 0) {
        Console.WriteLine("You win the game!");
      }

      Console.Write("Press any key to exit...");
      Console.ReadKey();
    } // end Main function
    // DO NOT MODIFY THIS FUNCTION
    //
    static void getNums(int[] arr, int range, int size) {
      int i; // A loop control variable
      Random rnd = new Random();

      for (i = 0; i < size; i++) {
        // Add a random number within the specified range to the array
        arr[i] = rnd.Next(1, 7);
      }
    }

    // DO NOT MODIFY THIS FUNCTION
    //
    static void showCard(int[] hand_one, int[] hand_two, int one, int two) {
      Console.WriteLine("*****Number of cards in each player's deck*****\n");
      Console.WriteLine("You: {0}", one);
      Console.WriteLine("Computer: {0}", two);
      Console.WriteLine("You have the card: {0}", hand_one[0]);
      Console.WriteLine("The computer has the card: {0}", hand_two[0]);
    }

    /////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////
    //// ////
    //// YOUR CODE GOES BELOW THIS POINT ////
    //// ////
    /////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////

    //
    // Split the elements of a source array into two destination arrays
    // The given size is the size of the destination arrays (they are the same size)
    //
    // WRITE CODE TO COMPLETE THIS FUNCTION
    //
    static void splitArray(int[] dest_one, int[] dest_two, int[] source, int size) {
      int ctr = 0; //Counter that will go through the source[] array.
      int i = 0; // A counter for our loop control variable
      // loop till size of dest array size 
      while (i < size) {
        // alternatively setting element values from source array to dest_one and dest_two
        // ex. source = [1,2,3,4] & dest_one = [] & dest_two = []
        // after splitArray(): 
        // source = [1,2,3,4] &
        // dest_one = [1,3] & dest_two = [2,4] 
        dest_one[i] = source[ctr++];
        dest_two[i] = source[ctr++];

        // after element at position i in both dest_one and dest_two is filled increment i value
        i++;

        // Loop through the destination arrays.
        // Assign each element in a destination array the next element
        // in the source array. Use the ctr variable to keep track of the
        // current position in the source array.
      }

    }

    //
    // Shifts the items in an array one place to the left
    // Throws out the first element in the array and sets the last element
    // in the array to zero.
    //
    // WRITE CODE TO COMPLETE THIS FUNCTION
    //
    static void shift(int[] arr, int size) {
      int i; // A counter

      // shift the array
      for (i = 0; i < size - 1; i++) {
        arr[i] = arr[i + 1];
      }
      // Set the last element in the array to zero
      arr[size - 1] = 0;

    }

  } // end Class
} // end Namespace
Add a comment
Know the answer?
Add Answer to:
Complete a program In C#: some code is included, you edit the areas that have not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ program This program involves writing a VERY simplified version of the card game War. You...

    C++ program This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require...

  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

  • Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods...

    Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods Program Specification: You are to develop a program to play a variation on a game of chance called single player Poker. Game Description: • There is a Player who starts with 100 chips, each worth $1.00. • There is a deck of 36 cards: – Each card has a number in the range of [1, 9] printed on it - 9 possible values /...

  • C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data...

    C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize()...

  • How to I change this code to print the highest value(s) in the array? The wanted...

    How to I change this code to print the highest value(s) in the array? The wanted output (you just need to make it print the winner, ignore what comes before) is included below: -------------------------------------------------------------------------------------------- public static void playGame(int players, int cards) { if (players * cards > 52) { System.out.println("Not enough cards for that many players."); return; } boolean[] deck = new boolean[52]; int[] playerScore = new int[players]; for (int i = 0; i < players; i++) { System.out.println("Player "...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • 2 A Game of UNO You are to develop an interactive game of UNO between a...

    2 A Game of UNO You are to develop an interactive game of UNO between a number of players. The gameplay for UNO is described at https://www.unorules.com/. Your program should operate as follows. 2.1 Setup 1. UNO cards are represented as variables of the following type: typedef struct card_s { char suit[7]; int value; char action[15]; struct card_s *pt; } card; You are allowed to add attributes to this definition, but not to remove any. You can represent colors by...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • C# Arrays pne and Look at the code below Notice that the program has one class...

    C# Arrays pne and Look at the code below Notice that the program has one class called Tournament. . It defines an integer array called scores to hold all the scores in the tournament .The constructor for the class then actually creates the array of the required size. class Tournament int[ ] scores; const int MAX = 6; // define scores as an integer array // set a constant size public static void Main() //program starts executing here Tournament myTournament...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT