All pastes #824197 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-12-19 21:08 UTC
rendered paste body
#include <iostream.h>
#include <iomanip.h>

int main()
{
    //Initializes winner to false
    int w = 0;
    //declares r for use as row counter
    int r;
    //declares c for use as column counter
    int c;
    //declares move1 for use as player one's position - X
    int move1;
    //declares move2 for use as player two's position - O
    int move2;
    //declares ones for use to strip one's digit
    int ones;
    //dclares tens for use to strip ten's digit
    int tens;
    //declares turncount for use as turn counter
    int turncount;
    //declares the char variable for player two's symbol - O
    char o = 'O';
    //declares the char variable for board's blank spaces
    char p = '-';    
    //declares the char variable for player one's symbol - X
    char x = 'X';
    //declares M[3][3] as a multidimensional array for the board
    char M[3][3] = {p,p,p,p,p,p,p,p,p};
    
    //title
    cout << "                         WELCOME TO CHASE'S TIC-TAC-TOE GAME!" << endl;
    cout << endl;
    
    //increments playing board's rows                    
    for(r=0;r<=2;r++)
    {
                     //increments playing board's columns
                     for(c=0;c<=2;c++)
                     {
                                      //prints rows and columns - playing board
                                      cout << setw(2) << M[r][c];
                     }
    cout << endl;
    }
    
    //initializes turncount to 1                    
    turncount = 1;
 
                        //main loop that checks for winner "w" and full board "turncount"
                        while(w==0 && turncount<=9)
                        {
                                 //checks for player one's turn                        
                                 if(turncount%2==1)
                                 {
                                 cout << endl;
                                 cout << endl;
                                 //prompts player one to make a move
                                 cout << "Player 1 where do you want to move?\n";
                                 //scans in player one's move
                                 cin >> move1;
                                 cin.get();
                                 //strips and decrements digit in ones place by one to be used as a column position
                                 ones = (move1%10)-1;
                                 //strips and decrements digit in tens place by one to be used as a row position
                                 tens = (move1%100-move1%10)/10-1;
                                 
                                      //nested loop to check for an invalid move
                                      while(M[tens][ones]!=p)
                                      {
                                                    cout << endl;
                                                    cout << endl;
                                                    //reprompts player one to make another move
                                                    cout << "Invalid move.  Try again." << endl << endl;
                                                    cout << "Player 1 where do you want to move?\n";
                                                    //scans in player one's new move
                                                    cin >> move1;
                                                    cin.get();
                                                    //strips and decrements digit in ones place by one to be used as a column position
                                                    ones = (move1%10)-1;
                                                    //strips and decrements digit in tens place by one to be used as a row position
                                                    tens = (move1%100-move1%10)/10-1;
                                      }
                                 //updates player one's move on board
                                 M[tens][ones]=x;
                                 }        
                                 
                                 //checks for player two's turn
                                 if(turncount%2==0)
                                 {
                                 cout << endl;
                                 cout << endl;
                                 //prompts player two to make a move
                                 cout << "Player 2 where do you want to move?\n";
                                 //scans in player two's move
                                 cin >> move2;
                                 cin.get();
                                 //strips and decrements digit in ones place by one to be used as a column position
                                 ones = (move2%10)-1;
                                 //strips and decrements digit in tens place by one to be used as a row position
                                 tens = (move2%100-move2%10)/10-1;
                                          //nested loop to check for an invalid move
                                          while(M[tens][ones]!=p)
                                          {
                                                    cout << endl;
                                                    cout << endl;
                                                    //reprompts player to make another move
                                                    cout << "Invalid move.  Try again." << endl << endl;
                                                    cout << "Player 2 where do you want to move?\n";
                                                    //scans in player two's new move
                                                    cin >> move2;
                                                    cin.get();
                                                    //strips and decrements digit in ones place by one to be used as a column position
                                                    ones = (move2%10)-1;
                                                    //strips and decrements digit in tens place by one tobe used as a row position
                                                    tens = (move2%100-move2%10)/10-1;
                                          }
                                 //updates player two's move on board
                                 M[tens][ones]=o;
                                 }
                                 //increments turncount by 1
                                 turncount++;
                                 
                                 cout << endl;
                                 cout << endl;
                                 
                                 //prints updated board                 
                                 for(r=0;r<=2;r++)
                                 {
                                         for(c=0;c<=2;c++)
                                         {
                                         cout << setw(2) << M[r][c];
                                         }
                                cout << endl;
                                }
                                
                                //loop to check if player has three in a row
                                for(r=0;r<=2;r++)
                                {                
                                                 //check for matching row entries for player two
                                                 if(M[r][0] == o && M[r][1] == o && M[r][2] == o)
                                                 {
                                                            cout << endl;
                                                            //prints player two as winner
                                                            cout << "Player 2 is the WINNER!" << endl;
                                                            //sets w to be true
                                                            w=1;
                                                            //breaks from loop
                                                            break;
                                                 }
                                                 //check for matching row entries for player one            
                                                 if(M[r][0] == x && M[r][1] == x && M[r][2] == x)
                                                 {
                                                            cout << endl;
                                                            //prints player one as winner
                                                            cout << "Player 1 is the WINNER!" << endl;
                                                            //sets w to be true
                                                            w=1;
                                                            //breaks from loop
                                                            break;
                                                 }
                                                 
                                } 
                                
                                //loop to check if player has three in a column
                                for(c=0; c<=2; c++)
                                {
                                         //check for matching column entries for player two
                                         if(M[0][c] == o && M[1][c] == o && M[2][c] == o)
                                         {
                                                    //prints player two as winner
                                                    cout << "Player 2 is the WINNER!" << endl;
                                                    //sets w to be true
                                                    w=1;
                                                    //breaks from loop
                                                    break;
                                         }
                                         //check for matching column entries for player one
                                         if(M[0][c] == x && M[1][c] == x && M[2][c] == x)
                                         {
                                                    //prints player one as winner
                                                    cout << "Player 1 is the WINNER!" << endl;
                                                    //sets w to be true
                                                    w=1;
                                                    //breaks from loop
                                                    break;
                                         }
                                } 
                                //sets c to be 0
                                c=0;
                                //sets r to be 0
                                r=0;
                                         //check for matching diagonal entries for player one
                                         if(M[r][c] == x && M[r+1][c+1] == x && M[r+2][c+2] == x)
                                         {
                                                    //prints player one as winner
                                                    cout << "Player 1 is the WINNER!" << endl;
                                                    //sets w to be true
                                                    w=1;
                                                    //breaks from loop
                                                    break;
                                         }
                                         //checks for matching diagonal entries for player two
                                         if(M[r][c] == o && M[r+1][c+1] == o && M[r+2][c+2] == o)
                                         {
                                                    //prints player two as winner
                                                    cout << "Player 2 is the WINNER!" << endl;
                                                    //sets w to be true
                                                    w=1;
                                                    //breaks from loop
                                                    break;
                                         }
                                //sets c to be 0
                                c=0;
                                //sets r to be 2
                                r=2;
                                         //checks for other diagonal for player one
                                         if(M[r][c] == x && M[r-1][c+1] == x && M[r-2][c+2] == x)
                                         {
                                                    //prints player one as winner
                                                    cout << "Player 1 is the WINNER!" << endl;
                                                    //sets w to be true
                                                    w=1;
                                                    //break from loop
                                                    break;
                                         }
                                         //checks for diagonal for player two
                                         if(M[r][c] == o && M[r-1][c+1] == o && M[r-2][c+2] == o)
                                         {
                                                    //prints player two as winner
                                                    cout << "Player 2 is the WINNER!" << endl;
                                                    //sets w to be true
                                                    w=1;
                                                    //breaks from loop
                                                    break;
                                         }
                                         //checks for a tie
                                         if(turncount==10)
                                         {
                                                         cout << endl;
                                                         cout << endl;
                                                         //prints out it's a tie
                                                         cout << "It's a tie! Play again?\n";
                                         }
                        //ends main loop                                 
                        }
    cin.get();
    return 0;
}