196k views
5 votes
Create a console-based computerized game of War named WarCardGameConsole in the Final Project Part 1 folder. This is C#

1. Use an array of 52 integers to store unique values for each card.
2. Write a method named FillDeck() that places 52 unique values into this array.
3. Write another method named SelectCard() that you call twice on each deal to randomly
select a unique card for each player, with no repetition of cards in 26 deals.
4. To pause the play between each dealt hand, use a call to ReadLine().
5. At the end of a play of the game of the 26 deals:
a. display the Computer’s and Player’s final scores
b. display who won the game
c. record the results in a text file (see step 6)
d. give the player the choice of stopping or playing again.
6. Record the results of each game in a text file:
a. The text file should be saved as FirstNameLastName Results.txt in your project
folder. The file will automatically save in WarCardGameConsole\bin\Debug folder.
b. At the beginning of your program you should check to see if your file exists;
 If not, your program will create it
 If it does exist, you program will open it.
c. At the of a play of the game you will record the results
d. When the player indicates they want to stop playing, the program should close the
file.
7. Your program must have meaningful comments, including your name.

1 Answer

3 votes

Answer:

Here is a console-based C# program for the card game War:

Step-by-step explanation:

using System;

using System.IO;

namespace WarCardGameConsole

{

class WarCardGame

{

// Array to hold 52 cards

int[] deck = new int[52];

// Player and computer scores

int playerScore;

int computerScore;

// File to record results

StreamWriter results;

// Fill deck with cards

void FillDeck()

{

// Add cards to deck

for (int i = 0; i < deck.Length; i++)

{

deck[i] = i;

}

}

// Select random card

int SelectCard()

{

// Generate random index

Random rand = new Random();

int index = rand.Next(deck.Length);

// Remove selected card from deck

int card = deck[index];

deck[index] = deck[deck.Length - 1];

Array.Resize(ref deck, deck.Length - 1);

// Return selected card

return card;

}

// Play one round of war

void PlayRound()

{

// Select cards for player and computer

int playerCard = SelectCard();

int computerCard = SelectCard();

// Display cards

Console.WriteLine($"Player card: {playerCard} Computer card: {computerCard}");

// Check who has higher card

if (playerCard > computerCard)

{

playerScore++;

Console.WriteLine("Player wins this round!");

}

else if (computerCard > playerCard)

{

computerScore++;

Console.WriteLine("Computer wins this round!");

}

else

{

Console.WriteLine("Tie! No points awarded.");

}

// Pause before next round

Console.WriteLine("Press any key to continue...");

Console.ReadKey();

}

void PlayGame()

{

// Fill deck with cards

FillDeck();

// Play 26 rounds

for (int i = 0; i < 26; i++)

{

PlayRound();

}

// Display final scores

Console.WriteLine($"Player final score: {playerScore}");

Console.WriteLine($"Computer final score: {computerScore}");

// Determine winner

if (playerScore > computerScore)

{

Console.WriteLine("Player wins the game!");

}

else if (computerScore > playerScore)

{

Console.WriteLine("Computer wins the game!");

}

else

{

Console.WriteLine("Tie game!");

}

// Record results in file

results.WriteLine($"{DateTime.Now} - {playerScore} to {computerScore}");

// Play again?

Console.Write("Play again? (Y/N) ");

string input = Console.ReadLine();

if (input.ToUpper() == "Y")

{

// Restart game

playerScore = 0;

computerScore = 0;

PlayGame();

}

else

{

// Close file and exit

results.Close();

Environment.Exit(0);

}

}

static void Main(string[] args)

{

// Create game object

WarCardGame game = new WarCardGame();

// Check if results file exists

if (!File.Exists("JohnDoe Results.txt"))

{

// Create file

game.results = File.CreateText("JohnDoe Results.txt");

}

else

{

// Open existing file

game.results = File.AppendText("JohnDoe Results.txt");

}

// Play game

game.PlayGame();

}

}

}

User Ilia Rebane
by
7.9k points