Home | | Syllabus | | Assignments | | Lectures | | Resources
//******************************************************** // proj2.cc // Author: A good solution writer // Date: February 24, 2014 // Class: CSCI 131 project2, Professor Royden // Purpose: Plays the children's game: Rock, Paper, Scissors // Each player independently chooses a "weapon" // Rock beats Scissors. // Scissors beat Paper. // Paper beats Rock. // The same option ties and no one wins. // Input: (from proj2.dat file) Inputs decision of each of two playerss // Output: (to proj2.out file) Outputs the winner of the game //******************************************************* #include <iostream> //iostream libraries #include <fstream> using namespace std; int main(void) { char player1; //Players weapons. Characters "R"/"P"/"S" char player2; ofstream outFile; //File streams for opening files ifstream Labin; Labin.open("proj2.dat"); //open the input file if (!Labin ) { //check for errors cout << "** Can't open proj2.dat **" << endl; return 1; } //if no input file outFile.open ("proj2.out"); //open the output file Labin >> player1 >> player2; cout << "player1 = " << player1 << "; player2 = " << player2 << endl; Labin.close(); //done with input file, so close it // convert the players choices to upper case. player1 = toupper(player1); player2 = toupper(player2); if ((player1 != 'R') && (player1 != 'S') && (player1 != 'P')){ cout << "\"" << player1 << "\"" << ", an illegal option! Exiting Program." << endl; return 1; } if ((player2 != 'R') && (player2 != 'S') && (player2 != 'P')){ cout << "\"" << player2 << "\"" << ", an illegal option! Exiting Program." << endl; return 1; } outFile << endl << "---------------------------------------------------------" << endl; // Check player one and then check all the possible choices // for player two and determine the winner. if (player1 == 'R') { //PLAYER ONE CHOOSES ROCK outFile << "Player 1 plays Rock "; if (player2 == 'R') { outFile << "and Player 2 plays Rock." << endl; outFile << "It's a tie! Neither wins; neither loses." << endl; } //if player two has a rock and ties else if (player2 == 'S') { outFile << "and Player 2 plays Scissors." << endl; outFile << "Player 1 wins! Rock smashes Scissors." << endl; } //elsif player two has scissors and loses else if (player2 == 'P') { /*Note: can use just else here */ outFile << "and Player 2 plays Paper." << endl; outFile << "Player 2 wins! Paper covers Rock." << endl; } //elsif player two has paper and wins } //player one has a ROCK else if (player1 == 'S') { //PLAYER ONE CHOOSES SCISSORS outFile << "Player 1 plays Scissors "; if (player2 == 'S') { outFile << "and Player 2 plays Scissors." << endl; outFile << "It's a tie! Neither wins; neither loses." << endl; } //if player two has scissors and ties else if (player2 == 'R') { outFile << "and Player 2 plays Rock." << endl; outFile << "Player 2 wins! Rock smashes Scissors." << endl; } //elsif player two has rock and wins else if (player2 == 'P') { /*Note: can use just else here */ outFile << "and Player 2 plays Paper." << endl; outFile << "Player 1 wins! Scissors cuts Paper." << endl; } //elsif player two has paper and loses } //player one has SCISSORS else if (player1 == 'P') { //PLAYER ONE CHOOSES PAPER /*Note: can use just else here */ outFile << "Player 1 plays Paper "; if (player2 == 'P') { outFile << "and Player 2 plays Paper." << endl; outFile << "It's a tie! Neither wins; neither loses." << endl; } //if player two has paper and ties else if (player2 == 'S') { outFile << "and Player 2 plays Scissors." << endl; outFile << "Player 2 wins! Scissors cuts paper." << endl; } //elsif player two has scissors and wins else if (player2 == 'R') { /*Note: can use just else here */ outFile << "and Player 2 plays Rock." << endl; outFile << "Player 1 wins! Paper covers Rock." << endl; } //elsif player two has a rock and loses } //player one has PAPER outFile << "---------------------------------------------------------" << endl; outFile << endl; outFile.close(); cout << "All Done!\n"; return 0; } //end of ROCK, PAPER, SCISSORS game
Home | | Syllabus | | Assignments | | Lectures | | Resources
Constance Royden--croyden@cs.holycross.edu
CSCI 131: Techniques of Programming
Last Modified: February 23, 2014
Page Expires: October 12, 2014