Go Back   Tech Support Forum > The IT Pro > Programming

C++ software code program,

This is a discussion on C++ software code program, within the Programming forums, part of the Tech Support Forum category.


Reply
 
Thread Tools Search this Thread
Old 04-11-2012, 01:46 PM   #1
Registered Member
 
Join Date: Apr 2012
Posts: 2
OS: vista



as it can be seen from my age i'm to old for this, but i'm going back to school to get my masters in mechanical engineering and this C++ has nothing to do with mechanical engineering. its a class that is mandatory like public speaking

so no funny business please just help an old gal out!



You will develop a simple windows-based game of dice. The game is played once only, and here are the rules for 2 players:

1- You choose if you wish to be player 1 or player 2

2- 2 dice are rolled on behalf of player 1

3- 2 dice are rolled on behalf of player 2

4- The winner is decided as follows:

a. If neither of the outcomes are a double such as ‘1-1’ or ’2-2’ or ’3-3’ or ‘4-4’ or ‘5-5’ or ‘6-6’ one of player 1 with the larger total wins, and if the totals are equal, it is a draw

b. If one outcome is a double, and the other is not, the player with a double wins and the totals will be irrelevant

c. If both outcomes are a double, then the player with the higher double wins, and if the doubles are equal, it is a draw

__________________
amy12345 is offline   Reply With Quote
Old 04-11-2012, 06:17 PM   #2
TSF-Emeritus
 
Ninjaboi's Avatar
 
Join Date: Dec 2010
Location: U.S.A., Texas
Posts: 2,047
OS: Windows XP SP3, Ubuntu 10

My System


Hello Amy, welcome to TSF!

I'm glad to hear that you chose to get back into school for such a degree. We can't directly help you as per the TSF rules:

Quote:
You may not ask for assistance with homework assignments, projects or book reports for school college or university
We can however help you understand the topic better and push you in the right direction.




Quote:
You will develop a simple windows-based game of dice. The game is played once only, and here are the rules for 2 players:
This part means that you should only have the program run the game conditions one time. Therefore, once a round of the game has finished it should close the program. No need for multiple rounds or restarts.

Quote:
You choose if you wish to be player 1 or player 2
This just means you have to create a starting option to choose whether you're player 1 ( who gets the two dice rolled first ) or player 2 ( who gets the two dice rolled second ).

Quote:
2 dice are rolled on behalf of player 1
By dice in the instructions you should assume a standard six-side die ( two of them ). The rolls should produce random values between or equal to 1 and 6 to indicate sides of a standard die. You should have these random values saved in two separate variables for the player specifically.

Quote:
2 dice are rolled on behalf of player 2
This is the same as player 1's rolls, though it's after player 1 has rolled their dice. You should save these values in two separate variables specific to player 2.

Now, for the winning conditions I'll describe them by the first condition you should check for up to the last condition you should check for after the rolls.

Quote:
c. If both outcomes are a double, then the player with the higher double wins, and if the doubles are equal, it is a draw
This should be checked first. If both players have double numbers ( example '3-3' and '1-1' ) in their rolls, you have to add each players numbers together and see which value is the largest. In the example I give, player 1 would win since their numbers add up to 6 while player 2 adds up to 2. If they have the same values ( example '4-4' and '4-4' ), the game has a draw.

Quote:
If one outcome is a double, and the other is not, the player with a double wins and the totals will be irrelevant
If one of the players have a dice roll that has two matching numbers while the other player doesn't have matching numbers, the player with the matching numbers wins regardless of what the dice add up to. This could mean that player 1 has a '6-5', but if player 2 has a '1-1' player 2 is still the victor.

Quote:
a. If neither of the outcomes are a double such as ‘1-1’ or ’2-2’ or ’3-3’ or ‘4-4’ or ‘5-5’ or ‘6-6’ one of player 1 with the larger total wins, and if the totals are equal, it is a draw
This is the final condition to check if none of the other conditions apply to the dice rolls. If the players don't have doubles in their rolls, then their dice values will add together to see who has the higher amount. The one with the higher amount wins. If they have the same amount, it ends in a draw.



Make sure to use comments in your source code as that's what professors look for when they see work being turned in.

__________________
Ninjaboi is offline   Reply With Quote
Old 04-14-2012, 12:30 AM   #3
Registered Member
 
Join Date: Apr 2012
Posts: 2
OS: vista



Hi again,, Okay I worked really hard and this is what I got so far!!

PLEASE WOULD YOU BE SO KIND TO HELP ME WITH THE CODE ON HOW TO DECIDE THE WINNERS?!!?!


4- The winner is decided as follows:

a. If neither of the outcomes are a double such as ‘1-1’ or ’2-2’ or ’3-3’ or ‘4-4’ or ‘5-5’ or ‘6-6’ one of player 1 with the larger total wins, and if the totals are equal, it is a draw

b. If one outcome is a double, and the other is not, the player with a double wins and the totals will be irrelevant

c. If both outcomes are a double, then the player with the higher double wins, and if the doubles are equal, it is a draw

Please help me with deciding the winners code that's the most vital!!


ALSO,, my game is playing with the computer how do I change it to two players???

THE CODE SO FAR!!!




#include <iostream>
#include <time.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
void one();
void two();
void three();
void four();
void five();
void six();
//Declare Functions used
int main()
{
int score = 0;
int compScore = 0;
int num = 0;
int num2 = 0;
int compNum = 0;
int compNum2 = 0;
int sum = 0;
int compSum = 0;
char letter;
//Declare Variables
srand(time(NULL));
//Initialize random number generator
system("title Joe's Dice Game");
while (letter != 'q')
{
cout << "Your Score: " << score << endl;
cout << "computer's Score: " << compScore << endl << endl;
cout << "Press r to roll or q to quit: ";
cin >> letter;
num = 1 + rand() % (6 - 1 + 1);
num2 = 1 + rand() % (6 - 1 + 1);
compNum = 1 + rand() % (6 - 1 + 1);
compNum2 = 1 + rand() % (6 - 1 + 1);

//Random numbers

sum = num + num2;
compSum = compNum + compNum2;

//Calculate Sums

if (letter == 'q')
break;
if (letter != 'r')
{
system("cls");
continue;
}

switch (num)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
default:
cout << "Error...";
break;
} //end switch

switch (num2)
{
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
default:
cout << "Error...";
break;
} //end switch

cout << endl << "Yours: " << num << ", " << num2 << endl;
cout << "Computer's: " << compNum << ", " << compNum2 << "\n\n";

//Display dice and numbers

if (sum > compSum)
{
cout << "You won!!" << endl << endl;
score++;
}
else
{
compScore++;
cout << "you lost..." << endl << endl;
}

//Calculate score

system("pause");
system("cls");

if (score == 12)
{
MessageBox(0, "You Won!!!", "Results:", MB_ICONEXCLAMATION);
break;
}
if (compScore == 12)
{
MessageBox(0, "You lost...", "Results:", MB_ICONEXCLAMATION);
break;
}
}
return 0;
}

void one()
{
cout << " -----" << endl;
cout << "| |" << endl;
cout << "| O |" << endl;
cout << "| |" << endl;
cout << " -----" << endl;
}
void two()
{
cout << " -----" << endl;
cout << "| O|" << endl;
cout << "| |" << endl;
cout << "|O |" << endl;
cout << " -----" << endl;
}
void three()
{
cout << " -----" << endl;
cout << "| O|" << endl;
cout << "| O |" << endl;
cout << "|O |" << endl;
cout << " -----" << endl;
}
void four()
{
cout << " -----" << endl;
cout << "|O O|" << endl;
cout << "| |" << endl;
cout << "|O O|" << endl;
cout << " -----" << endl;
}
void five()
{
cout << " -----" << endl;
cout << "|O O|" << endl;
cout << "| O |" << endl;
cout << "|O O|" << endl;
cout << " -----" << endl;
}
void six()
{
cout << " -----" << endl;
cout << "|O O|" << endl;
cout << "|O O|" << endl;
cout << "|O O|" << endl;
cout << " -----" << endl;
}
__________________
amy12345 is offline   Reply With Quote
Old 04-14-2012, 01:14 AM   #4
Windows Tech Team
 
Join Date: Jul 2010
Location: SA
Posts: 1,921
OS: DOS 3.3



Hi

Here is one way to do it (in pseudocode):

Code:
//Player1
player1total = player1dice1 + player1dice2
if player1dice1 = player1dice2 then
   player1double=true
//Player2
player2total = player2dice1 + player2dice2
if player2dice1 = player2dice2 then
   player2double=true

//both double
if player1double and player2double then
   if player1total > player2total then
      player1 wins
   else if player2total > player1total then
      player2 wins
   else
      game is a draw

//only one double
else if player1double
   player1 wins
else if player2double
   player2 wins

//no double check totals
else
if player1total > player2total then
   player1 wins
else if player2total > player1total then
   player2 wins
else
   game is a draw
As for playing 2 people it does not really make a difference. The question just says decide if you are player 1 or 2, which automatically makes the other player the pc or irrelevant?
__________________
AlbertMC2 is offline   Reply With Quote
Old 04-15-2012, 09:41 AM   #5
Registered Member
 
Join Date: Jan 2007
Posts: 159
OS: xp,linux



An alternative implementation could be to let data structures do the work:

create a 36 x 3 data structure of scores where none doubles are simply totals and doubles are weighted.

Code:
<die1>   1  1  1  1  1  1  2  2  2  2  2  2  3  3  3  3  3  3  4  4  4  4  4  4  5  5  5  5  5  5  6  6  6  6  6  6
<die2>   1  2  3  4  5  6  1  2  3  4  5  6  1  2  3  4  5  6  1  2  3  4  5  6  1  2  3  4  5  6  1  2  3  4  5  6
<score> 12  3  4  5  6  7  3 13  5  6  7  8  4  5 14  7  8  9  5  6  7 15  9 10  6  7  8  9 16 11  7  8  9 10 11 17
generate a random number X between 1 and 36 for each player
print out die1[X] and die2[X] for each player - though your spec doesn't demand this.
test whether player1.score[X] is greater than, less than or equal to player2.score[X].
announce the result.

You could get really cryptic and just generate two random numbers between 1 and 14 but that might be pushing the spirit of the excercise a bit.

__________________
stirling is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2


All times are GMT -7. The time now is 11:08 AM.


Copyright 2001 - 2014, Tech Support Forum

Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts