Docsity

Dice War Game C Program, Assignments of Computer Science

Toggle sidebar
bg1
CPS 196
Homework Assignment 4 Due Thursday, November 15, 2007, at 10pm.
While you may discuss this assignment with others, all work you submit must be your
own. That means, after discussing it, you must go back and write it up yourself.
Software Development Process:
Part I:
The software development steps are specified in the assignment, so for this one time, you
do not need to submit a development process page. However, your program should be
thoroughly commented.
Part II:
4. Write a C program, using the analysis below:
Write a comment block that has the program name, your name and date.
The program should be well documented and have good variable names.
Write the program following the program steps.
Use proper programming style with reasonable indentation.
Test your program thoroughly and check your results.
Follow the above steps to write a C program for the following problem.
Program Description
For this problem, you are going to write a program that plays a game for two people that
we will call “dice war”. This game is similar to one that is often played by children with
a deck of cards and called “war”, but in this version, each player will roll a die to get their
next number.
The game will be played for 8 rounds, where each player gets a number by a roll of the
dice. In any one round, if there is a tie, i.e. if the players roll the same number, then the
dice must be rolled again to get two new numbers. In fact, as long as there is a tie, the
dice will be rolled again.
After the players have rolled the dice for 8 rounds, then the rounds are scored. Each
player will receive a score that is the sum of their scores for the rounds. In each round,
the player with the highest of the two numbers from the dice will get the sum of the
numbers. The player with the highest score from the 8 rounds will win.
Program Analysis
This program has no inputs from the user. (Note: like the children’s game “war”, there
is no skill involved to play this game!)
pf3

Partial preview of the text

Download Dice War Game C Program and more Assignments Computer Science in PDF only on Docsity!

CPS 196

Homework Assignment 4 Due Thursday, November 15, 2007, at 10pm.

While you may discuss this assignment with others, all work you submit must be your own. That means, after discussing it, you must go back and write it up yourself.

Software Development Process:

Part I:

The software development steps are specified in the assignment, so for this one time, you do not need to submit a development process page. However, your program should be thoroughly commented.

Part II:

  1. Write a C program, using the analysis below: Write a comment block that has the program name, your name and date. The program should be well documented and have good variable names. Write the program following the program steps. Use proper programming style with reasonable indentation. Test your program thoroughly and check your results.

Follow the above steps to write a C program for the following problem.

Program Description

For this problem, you are going to write a program that plays a game for two people that we will call “dice war”. This game is similar to one that is often played by children with a deck of cards and called “war”, but in this version, each player will roll a die to get their next number.

The game will be played for 8 rounds, where each player gets a number by a roll of the dice. In any one round, if there is a tie, i.e. if the players roll the same number, then the dice must be rolled again to get two new numbers. In fact, as long as there is a tie, the dice will be rolled again.

After the players have rolled the dice for 8 rounds, then the rounds are scored. Each player will receive a score that is the sum of their scores for the rounds. In each round, the player with the highest of the two numbers from the dice will get the sum of the numbers. The player with the highest score from the 8 rounds will win.

Program Analysis

This program has no inputs from the user. (Note: like the children’s game “war”, there is no skill involved to play this game!)

The outputs will be printouts showing the state of the game as it progresses. For each round, the program will print the numbers of the rolls of the dice. If there is a tie in a round, it will show the numbers from rolling the dice again. (Note: I think that in the children’s game, if you have a tie, it is called a war.) After all 8 rounds of play, then the score is computed, and printed out. A message stating whether the final scores show a tie or a win for one of the players should be printed.

The output should look something like:

Welcome to the game of dice war. There will be 8 rounds of play.

Player 1 rolled 1, Player 2 rolled 4 Player 1 rolled 5, Player 2 rolled 2 Player 1 rolled 6, Player 2 rolled 3 Player 1 rolled 1, Player 2 rolled 1 War! Player 1 rolled 2, Player 2 rolled 4 Player 1 rolled 2, Player 2 rolled 4 Player 1 rolled 1, Player 2 rolled 2 Player 1 rolled 3, Player 2 rolled 3 War! Player 1 rolled 5, Player 2 rolled 5 War! Player 1 rolled 3, Player 2 rolled 1 Player 1 rolled 3, Player 2 rolled 1

Player 1 scored 24, Player 2 scored 20

Player 1 wins!

Other variables. Here is a description of the variables, and I leave it up to you to pick out names:

  • For this homework assignment, it is required that you use an integer 2- dimensional array to hold the resulting rolls of the dice for the 8 rounds. There will be 8 rows and 2 columns. Each row will hold the two player’s numbers for one round.
  • two double variables to hold the result of the rolls of the dice for each round
  • two double variables to sum up the scores for each player
  • an integer variable for the loop index

Program Steps

Functions: Use “srand( )” from stdlib.h and “time(0)” from time.h to generate a random seed for the random number generator.