Advertisement
3nriched

tic tac toe

Aug 24th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. // TicTacToeGame.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>                            ///////////////////////////////////
  6. #include <iostream>                             //    Tic-Tac-Toe                //    
  7. #include <cmath>                                //                               //
  8. #include <iomanip>                              //                 by  3nriched  //
  9. #include <string>                               ///////////////////////////////////
  10.  
  11. using namespace std;                                                    //declaration of std
  12.  
  13. void display(char [][3]);                               /////////////////////
  14. int winCheck(char [][3], int);                          //   Prototypes    //
  15. void move(char [][3], int);                             /////////////////////
  16.  
  17. char arraytest[3][3] = {{'*','*','*'}, {'*','*','*'}, {'*','*','*'}};   //the gameboard 2D array
  18. int playa = 0,                                                          //starts at player 0 but really its 1 dont change
  19.     winner = 0,                                                         //holds the variable 1 or 2 when winner is determined
  20.     flag = 0;                                                           //variable used with tie game checker
  21.  
  22. int _tmain(int argc, _TCHAR* argv[])                                   
  23. {
  24.     system("Color 17");                                                 //sets background to blue and text to white
  25.     PlaySound("Pinkythebrain.wav", NULL, SND_ASYNC);                    //plays pinky and the brain song 1minute long
  26.  
  27.     do
  28.     {                  
  29.     if(playa == 1){playa = 2;}else playa = 1;                           //cycles through players 1 and 2
  30.     display(arraytest);                                                 //displays the current gameboard                             
  31.     winner = winCheck(arraytest, winner);                               //determines if a win has happened
  32.     if(winner != 0){cout<<"\n";cout << "Player "<<winner<<" Wins!!" << endl;cout<<"\n";system("pause");return 0;} //displays the winner (if any)                             
  33.     move(arraytest, playa);                                             //handles the inputted position for players
  34.     flag += 1;
  35.     if(flag == 9){cout<<"\n";cout<<"The Game Has Tied!!\n";system("pause");return 0;}                             //displays tied game message
  36.     }
  37.     while(winner != 1 || winner != 2);                                  //continues playing until a winner has been found
  38. }
  39.  
  40. void display(char gameBoard[][3])                                  
  41. {          
  42.         cout<<"\t\tTic-Tac-Toe\tPlayer: "<<playa<<endl;                 ///////////////
  43.     for (int sixt=0;sixt<3;sixt++) {                                    //  Creates  //
  44.         for(int e=0;e<3;e++) {                                          //    Game   //
  45.             cout << gameBoard[sixt][e];                                 //   Board   //
  46.             cout << "  ";                                               ///////////////
  47.         }
  48.         cout << endl;
  49.         if(sixt!=2) {
  50.             cout << "-------" << endl;
  51.         }
  52.     }
  53. }
  54.  
  55. int winCheck(char gameBoard[][3], int winner)                           //win function - determines if a win occured.
  56. {  
  57.         //tests all rows for both X and O for a winner.
  58.         if(gameBoard[0][0]==gameBoard[0][1] && gameBoard[0][0]==gameBoard[0][2]){if(gameBoard[0][0]=='X'){winner=1;}if(gameBoard[0][0]=='O'){winner=2;}}
  59.         if(gameBoard[1][0]==gameBoard[1][1] && gameBoard[1][0]==gameBoard[1][2]){if(gameBoard[1][0]=='X'){winner=1;}if(gameBoard[1][0]=='O'){winner=2;}}
  60.         if(gameBoard[2][0]==gameBoard[2][1] && gameBoard[2][0]==gameBoard[2][2]){if(gameBoard[2][0]=='X'){winner=1;}if(gameBoard[2][0]=='O'){winner=2;}}
  61.  
  62.         //tests all colums for both X and O for a winner.
  63.         if(gameBoard[0][0]==gameBoard[1][0] && gameBoard[0][0]==gameBoard[2][0]){if(gameBoard[0][0]=='X'){winner=1;}if(gameBoard[0][0]=='O'){winner=2;}}
  64.         if(gameBoard[0][1]==gameBoard[1][1] && gameBoard[0][1]==gameBoard[1][2]){if(gameBoard[0][1]=='X'){winner=1;}if(gameBoard[0][1]=='O'){winner=2;}}
  65.         if(gameBoard[0][2]==gameBoard[1][2] && gameBoard[0][2]==gameBoard[2][2]){if(gameBoard[0][2]=='X'){winner=1;}if(gameBoard[0][2]=='O'){winner=2;}}
  66.  
  67.         //tests left top to bottom right diangle for both X and O for a winner.
  68.         if(gameBoard[0][0]==gameBoard[1][1] && gameBoard[0][0]==gameBoard[2][2]){if(gameBoard[0][0]=='X'){winner=1;}if(gameBoard[0][0]=='O'){winner=2;}}
  69.  
  70.         //tests right top to bottom left diangle for both X and O for a winner.
  71.         if(gameBoard[0][2]==gameBoard[1][1] && gameBoard[0][2]==gameBoard[2][0]){if(gameBoard[0][2]=='X'){winner=1;}if(gameBoard[0][2]=='O'){winner=2;}}
  72.        
  73.        
  74.         return winner;                                                  //returns the winner, if any is determined.                                    
  75. }
  76.  
  77.  
  78. void move(char gameBoard[][3], int playa)                               // handles the position inputed by user
  79. {
  80.     int x,y;
  81.     cout << "PLAYER "<<playa<<" ENTER ROW POSITION(0,1,2, LEFT - RIGHT): " << endl;   //requests player row input
  82.     cin >> x;
  83.     cout << "PLAYER "<<playa<<" ENTER COLUM POSITION(0,1,2, UP - DOWN): " << endl;    //requests player colum input
  84.     cin >> y;
  85.  
  86.     if(playa == 1) {gameBoard[x][y]='X';}                               //assigns X to player1 and O to player2
  87.    
  88.     if(playa == 2) {gameBoard[x][y]='O';}
  89.     system("cls");                                                      //refreshes screen
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement