Author Topic: My first C++ Program  (Read 3854 times)

Offline Ta-183

  • Bot Destroyer
  • ***
  • Posts: 105
    • View Profile
My first C++ Program
« on: July 23, 2009, 01:53:02 PM »
As a few of you know, I'm taking courses on programming in C++ at the community college while I'm still in high school (with a crazy mother of a discount, $40 a class!) and I've always had a bit of an interest in a certain programming 'sport', if you will, that was played back around a decade ago. Competitive rock paper scissors. Programmers build bots that go against one another for god knows how many rounds and use a sophisticated ranking system to see who wins. Some of the systems that were developed were out of this freaking world, including some incredible behavioral analysis routines that were just about spot on. About six months ago, I had implemented my own version of such a sport in TI-BASIC on my TI-84. I lost said systems to a random RAM reset (and was VERY pissed off). Now, I've finished my first program in C++ that I've built myself from the bottom up, and replicated an early version of my main bot. If any of you guys know your C++, please take a look over my source code. Comments and criticism are welcomed. It doesn't use any funky libraries, so just about any compiler will launch it pretty quickly. If you have no compiler, I've included the executable for those who just wanna dink around with it.

[code]//Rock-Paper-Scissors v1.1.1
//By Ta-183
//Written in C++ in Bloodshed Dev-C++ 4.9.9.2 IDE

#include <iostream>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int computerRESP(int, int, int[3][99]);
int computerRARAY(int, int, int[3][99]);
int computerRAND();
int humanPlayer();
int main()
{
    int player1 = 0;
    int player2 = 0;
    int p1mode = 0;
    int p2mode = 0;
    int win = 0;
    int fail = 0;
    int draw = 0;
    int count = 0;
    int exit = 0;
    int mode = 0;
    char displog = ' ';
    int logArray[3][99] = {0};
    int rounds = 0;
    //NOTE; Row 0 is win/loss logging. 1=player 1 win, 2=player 2 win, 3=tie
    //Row 1 Is player 1 logging. Same selection scheme as rest of program.
    //Row 2 is player 2 logging.
    // 1 = Rock, 2 = Paper, 3 = Scissors
    //Vaguely considering switching to a string array. Interger based logging is a holdover from my experience using TI-BASIC.

    const int mintypes = 1;
    const int maxtypes = 4;
    srand(int(time(0)));

    do
    {
        cout << "\nSelect Player 1 control.";
        cout << "\n1. Human Player";
        cout << "\n2. Computer (Random Generator)";
        cout << "\n3. Computer (Counter Bot)";
        cout << "\n4. Computer (ResponderC++)\nPlayer 1: ";
        cin >> p1mode;
        if (p1mode < mintypes || p1mode > maxtypes)
        {
                   cout << "\n\nThe valid choices are " << mintypes << " through " << maxtypes << ".\n\n";
        }
    } while (p1mode < mintypes || p1mode > maxtypes);
      do
    {
        cout << "\nSelect Player 2 control.";
        cout << "\n1. Human Player";
        cout << "\n2. Computer (Random Generator)";
        cout << "\n3. Computer (Counter Bot)";
        cout << "\n4. Computer (ResponderC++)\nPlayer 2: ";
        cin >> p2mode;
        if (p2mode < mintypes || p2mode > maxtypes)
        {
                   cout << "\n\nThe valid choices are " << mintypes << " through " << maxtypes << ".\n\n";
        }
    } while (p2mode < mintypes || p2mode > maxtypes);
    do
    {
        cout << "\n\nEnter number of rounds to be played. (Must be between 1 and 99)\nNumber of rounds: ";
        cin >> rounds;
        if (rounds < 1 || rounds > 99)
        {
                   cout << "\nThe number of rounds must be between 1 and 99.";
        }
    } while (rounds < 1 || rounds > 99);

    for (int count = 0; count <= (rounds - 1); count = count + 1)
    {
        mode = 1;
        switch (p1mode)
        {
               case 1: player1 = humanPlayer();
               break;
               case 2: player1 = computerRAND();
               break;
               case 3: player1 = computerRARAY(mode, count, logArray);
               break;
               case 4: player1 = computerRESP(mode, count, logArray);
               break;
        }

        mode = 2;
        switch (p2mode)
        {
               case 1: player2 = humanPlayer();
               break;
               case 2: player2 = computerRAND();
               break;
               case 3: player2 = computerRARAY(mode, count, logArray);
               break;
               case 4: player2 = computerRESP(mode, count, logArray);
               break;
        }      
            if ((player1 == 1 && player2 ==2) || (player1 == 2 && player2 == 3) || (player1 == 3 && player2 == 1))
            {
                 fail = fail + 1;
                 logArray[0][count] = 2;
                 logArray[1][count] = player1;
                 logArray[2][count] = player2;
            }
            else if ((player1 == 2 && player2 ==1) || (player1 == 3 && player2 == 2) || (player1 == 1 && player2 == 3))
            {
                 win = win + 1;
                 logArray[0][count] = 1;
                 logArray[1][count] = player1;
                 logArray[2][count] = player2;
            }
            else if (player1 == player2)
            {
                 draw = draw + 1;
                 logArray[0][count] = 3;
                 logArray[1][count] = player1;
                 logArray[2][count] = player2;
            }//End ifs
            //Text output for human players
            if (p1mode == 1 || p2mode == 1)
            {
                switch (player1)
                {
                       case 1: cout << "\nPlayer 1 chose Rock.";
                       break;
                       case 2: cout << "\nPlayer 1 chose Paper.";
                       break;
                       case 3: cout << "\nPlayer 1 chose Scissors.";
                       break;
                }//End Switch
                switch (player2)
                {
                       case 1: cout << "\nPlayer 2 chose Rock.";
                       break;
                       case 2: cout << "\nPlayer 2 chose Paper.";
                       break;
                       case 3: cout << "\nPlayer 2 chose Scissors.";
                       break;
                } //End Switch
                switch (logArray[0][count])
                {
                       case 1: cout << "\nPlayer 1 Wins!\n";
                       break;
                       case 2: cout << "\nPlayer 2 Wins!\n";
                       break;
                       case 3: cout << "\nDraw!\n";
                       break;
                }//End Switch
            }//End if
        player1 = 0;
     } //endfor
     cout <<"\n\n------ENDGAME------";
     cout << "\nPlayer 1: " << win << endl << "Player 2: " << fail << endl << "Draw: " << draw;
     cout << "\n\nDisplay game log? Y/N: ";
     cin >> displog;
     //Begin Endgame Log Display
     while (displog == 'y' || displog == 'Y')
     {
           cout << "\nRound   Player 1   Player 2   Result\n";
           for (int count = 0; count <= (rounds - 1); count = count + 1)
           {
               cout << endl;
               //Slight aesthetic fix, makes output ine up better.
               if (count <= 8)
               {
                         cout << "0";
               }
               cout << (count + 1) << ":     ";
               switch (logArray[1][count])
               {
                      case 1: cout << "rock       ";
                      break;
                      case 2: cout << "paper      ";
                      break;
                      case 3: cout << "scissors   ";
                      break;
                      default: cout << "ERROR      ";
                      break;
               }
               switch (logArray[2][count])
               {
                      case 1: cout << "rock       ";
                      break;
                      case 2: cout << "paper      ";
                      break;
                      case 3: cout << "scissors   ";
                      break;
                      default: cout << "ERROR      ";
               }
               switch (logArray[0][count])
               {
                      case 1: cout << "P1 Wins";
                      break;
                      case 2: cout << "P2 Wins";
                      break;
                      case 3: cout << "Draw";
                      break;
               }
           } //End for
           displog = ' ';
     } //End While
     cout << "\n\nEnter 1 to end program.";
     cin >> exit;
     return 0;
}//end of main

//------------------------------------------------------------------------------------------
//------------------------------------------------------------ResponderC++ v1.1
//------------------------------------------------------------------------------------------
//Complete rebuild of ResponderTI in C++.
//Does not require or utilize the complicated addons and equipment routines that ResponderTI used.
int computerRESP(int posit, int turn, int movelog[][99])
{
    //Initialize internal variables
    int internCounterRES = 0;
    double didntwinRES = 0.0;
    int lpincRES = 0;
    int listCounterRES = 0;
    int nmeLastMoveRES = 0;
    int myLastMoveRES = 0;
    int myMoveRES = 0;
    int rockResponseRES = 0;
    int paperResponseRES = 0;
    int scissorsResponseRES = 0;
    int highAvgRES = 0;
    //Begin Responder code ------(Position 1)------
    if (posit == 1 && turn >= 2)
    {
              myLastMoveRES = movelog[1][turn - 1];
    //Begin Average Response Computation
    //Responses to 'Rock'
              if (movelog[1][turn - 1] == 1)
              {
                      //Loop for average responses
                      for (internCounterRES = 0; internCounterRES <= (turn - 1); internCounterRES = internCounterRES + 1)
                      {
                            if (movelog[1][internCounterRES] == 1)
                            {
                                    switch (movelog[2][(internCounterRES + 1)])
                                    {
                                           case 1: rockResponseRES = rockResponseRES + 1;
                                           break;
                                           case 2: paperResponseRES = paperResponseRES + 1;
                                           break;
                                           case 3: scissorsResponseRES = scissorsResponseRES + 1;
                                           break;
                                    }//End switch
                            }//End if
                      }//End for
                      //Find most common response
                      if (rockResponseRES >= paperResponseRES && rockResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 1;
                      }
                      else if (paperResponseRES >= rockResponseRES && paperResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 2;
                      }
                      else if (scissorsResponseRES >= rockResponseRES && scissorsResponseRES >= paperResponseRES)
                      {
                            highAvgRES = 3;
                      }//End ifs
              }//End if
              //End Rock responses
    //Responses to 'Paper'
              if (movelog[1][turn - 1] == 2)
              {
                      //Loop for average responses
                      for (internCounterRES = 0; internCounterRES <= (turn - 1); internCounterRES = internCounterRES + 1)
                      {
                            if (movelog[1][internCounterRES] == 2)
                            {
                                    switch (movelog[2][(internCounterRES + 1)])
                                    {
                                           case 1: rockResponseRES = rockResponseRES + 1;
                                           break;
                                           case 2: paperResponseRES = paperResponseRES + 1;
                                           break;
                                           case 3: scissorsResponseRES = scissorsResponseRES + 1;
                                           break;
                                    }//End switch
                            }//End if
                      }//End for
                      //Find most common response
                      if (rockResponseRES >= paperResponseRES && rockResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 1;
                      }
                      else if (paperResponseRES >= rockResponseRES && paperResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 2;
                      }
                      else if (scissorsResponseRES >= rockResponseRES && scissorsResponseRES >= paperResponseRES)
                      {
                            highAvgRES = 3;
                      }//End ifs
              }//End if
              //End Paper responses
    //Responses to 'Scissors'
              if (movelog[1][turn - 1] == 3)
              {
                      //Loop for average responses
                      for (internCounterRES = 0; internCounterRES <= (turn - 1); internCounterRES = internCounterRES + 1)
                      {
                            if (movelog[1][internCounterRES] == 3)
                            {
                                    switch (movelog[2][(internCounterRES + 1)])
                                    {
                                           case 1: rockResponseRES = rockResponseRES + 1;
                                           break;
                                           case 2: paperResponseRES = paperResponseRES + 1;
                                           break;
                                           case 3: scissorsResponseRES = scissorsResponseRES + 1;
                                           break;
                                    }//End switch
                            }//End if
                      }//End for
                      //Find most common response
                      if (rockResponseRES >= paperResponseRES && rockResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 1;
                      }
                      else if (paperResponseRES >= rockResponseRES && paperResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 2;
                      }
                      else if (scissorsResponseRES >= rockResponseRES && scissorsResponseRES >= paperResponseRES)
                      {
                            highAvgRES = 3;
                      }//End ifs
              }//End if
              //End Scissors responses
    }//End if
    //End Position 1 Responder code
//Begin Responder code ------(Position 2)------
    if (posit == 2 && turn >= 2)
    {
              myLastMoveRES = movelog[2][turn - 1];
    //Begin Average Response Computation
    //Responses to 'Rock'
              if (movelog[2][turn - 1] == 1)
              {
                      //Loop for average responses
                      for (internCounterRES = 0; internCounterRES <= (turn - 1); internCounterRES = internCounterRES + 1)
                      {
                            if (movelog[2][internCounterRES] == 1)
                            {
                                    switch (movelog[1][(internCounterRES + 1)])
                                    {
                                           case 1: rockResponseRES = rockResponseRES + 1;
                                           break;
                                           case 2: paperResponseRES = paperResponseRES + 1;
                                           break;
                                           case 3: scissorsResponseRES = scissorsResponseRES + 1;
                                           break;
                                    }//End switch
                            }//End if
                      }//End for
                      //Find most common response
                      if (rockResponseRES >= paperResponseRES && rockResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 1;
                      }
                      else if (paperResponseRES >= rockResponseRES && paperResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 2;
                      }
                      else if (scissorsResponseRES >= rockResponseRES && scissorsResponseRES >= paperResponseRES)
                      {
                            highAvgRES = 3;
                      }//End ifs
              }//End if
              //End Rock responses
    //Responses to 'Paper'
              if (movelog[2][turn - 1] == 2)
              {
                      //Loop for average responses
                      for (internCounterRES = 0; internCounterRES <= (turn - 1); internCounterRES = internCounterRES + 1)
                      {
                            if (movelog[2][internCounterRES] == 2)
                            {
                                    switch (movelog[1][(internCounterRES + 1)])
                                    {
                                           case 1: rockResponseRES = rockResponseRES + 1;
                                           break;
                                           case 2: paperResponseRES = paperResponseRES + 1;
                                           break;
                                           case 3: scissorsResponseRES = scissorsResponseRES + 1;
                                           break;
                                    }//End switch
                            }//End if
                      }//End for
                      //Find most common response
                      if (rockResponseRES >= paperResponseRES && rockResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 1;
                      }
                      else if (paperResponseRES >= rockResponseRES && paperResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 2;
                      }
                      else if (scissorsResponseRES >= rockResponseRES && scissorsResponseRES >= paperResponseRES)
                      {
                            highAvgRES = 3;
                      }//End ifs
              }//End if
              //End Paper responses
    //Responses to 'Scissors'
              if (movelog[2][turn - 1] == 3)
              {
                      //Loop for average responses
                      for (internCounterRES = 0; internCounterRES <= (turn - 1); internCounterRES = internCounterRES + 1)
                      {
                            if (movelog[2][internCounterRES] == 3)
                            {
                                    switch (movelog[1][(internCounterRES + 1)])
                                    {
                                           case 1: rockResponseRES = rockResponseRES + 1;
                                           break;
                                           case 2: paperResponseRES = paperResponseRES + 1;
                                           break;
                                           case 3: scissorsResponseRES = scissorsResponseRES + 1;
                                           break;
                                    }//End switch
                            }//End if
                      }//End for
                      //Find most common response
                      if (rockResponseRES >= paperResponseRES && rockResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 1;
                      }
                      else if (paperResponseRES >= rockResponseRES && paperResponseRES >= scissorsResponseRES)
                      {
                            highAvgRES = 2;
                      }
                      else if (scissorsResponseRES >= rockResponseRES && scissorsResponseRES >= paperResponseRES)
                      {
                            highAvgRES = 3;
                      }//End ifs
              }//End if
              //End Scissors responses