MrAlex

V2 tic tac toe

Sep 6th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. //****************************************************MAIN.CPP
  2.  
  3. #include "stdafx.h"
  4. #include "declare.h"
  5.  
  6. int main()
  7. {
  8.     int board[9];
  9.     int *pBoard = &board[0];
  10.     Init(pBoard);
  11.     Print(pBoard);
  12.     int check = 0;                                                                      //checking if there is any winner
  13.  
  14.     cout<<"pls, choose between you who is the x anad who is the 0"<<endl;               //instructions
  15.     cout<<"after you have chosen,Lets start :)"<<endl;
  16.    
  17.     while(check == 0)
  18.     {
  19.         //player X enters number
  20.         int numBox;
  21.         cout<<"X pls enter number which represents the box you have chosen"<<endl;
  22.  
  23.         //checking if the location is fulled or not
  24.         int checkNum;
  25.         cin>>checkNum;
  26.         if(board[checkNum-1] != 0)
  27.         {
  28.             cout<<"Error,pls enter other location"<<endl;
  29.             continue;
  30.         }
  31.         else
  32.         {
  33.             numBox = checkNum;
  34.         }
  35.  
  36.         //checking if the number is in the range
  37.         if(numBox < 10 && numBox >0)
  38.         board[numBox-1] = 1;
  39.         else
  40.         {
  41.             cout<<"error"<<endl;
  42.             continue;
  43.         }
  44.  
  45.         Print(pBoard);                          //prints the new board
  46.         //checks if there is any winner
  47.         check = CheckWinnerX(pBoard);          
  48.         if(check != 0) continue;
  49.  
  50.         //player 0 enters number
  51.         cout<<"0 pls enter number which represents the box you have chosen"<<endl;
  52.  
  53.         //checking if the location is fulled or not
  54.         cin>>checkNum;
  55.         if(board[checkNum-1] != 0)
  56.         {
  57.             cout<<"Error,pls enter other location"<<endl;
  58.             continue;
  59.         }
  60.         else
  61.         {
  62.             numBox = checkNum;
  63.         }
  64.  
  65.         //checking if the number is in the range
  66.         if(numBox < 10 && numBox >0)
  67.         board[numBox-1] = 2;
  68.         else
  69.         {
  70.             cout<<"Error"<<endl;
  71.             continue;
  72.         }
  73.         Print(pBoard);                          //prints the board
  74.         //checks if there is any winner
  75.         check = CheckWinner0(pBoard);
  76.         if(check != 0) continue;
  77.        
  78.     }
  79.  
  80.     return 0;
  81. }
  82.  
  83. //********************************************************************************function.cpp
  84.  
  85. #include "stdafx.h"
  86. #include "declare.h"
  87. //initalizate the board
  88. void Init(int *arr)
  89. {
  90.     for(int i = 0;i < 9;i++)
  91.     {
  92.         arr[i] = 0;
  93.     }
  94. }
  95.  
  96. //prints the board
  97. void Print(int *arr)
  98. {
  99.     cout<<"this is the real board"<<endl;
  100.     for(int i = 0;i< 9;i++)
  101.     {
  102.         if(arr[i] == 1)
  103.             cout<<'x';
  104.         else if(arr[i] ==2)
  105.             cout<<'0';
  106.         else
  107.             cout<<'-';
  108.         if(i == 2 || i  == 5 || i ==8)
  109.         {
  110.             cout<<endl;
  111.         }
  112.     }
  113.     cout<<endl;
  114.     cout<<"this is your keyboard\n"<<endl;
  115.     for(int box = 1;box <= 9;box++)
  116.     {
  117.         cout<<box;
  118.         if(box == 3 || box == 6 || box ==9)
  119.             cout<<endl;
  120.     }
  121.     cout<<endl;
  122.     cout<<"The numbers represnt each box"<<endl;
  123. }
  124.  
  125. //checking if X is winner
  126. int CheckWinnerX(int *arr)
  127. {
  128.     if(arr[0] == 1 && arr[3] == 1 && arr[6] == 1)
  129.     {
  130.         cout<<"the winner is "<<'X'<<endl;
  131.         return 1;
  132.     }
  133.     else if(arr[0] ==1 && arr[1] == 1 && arr[2]== 1)
  134.     {
  135.         cout<<"the winner is "<<'X'<<endl;
  136.         return 1;
  137.     }
  138.     else if(arr[2] == 1 && arr[5] == 1 && arr[8]== 1)
  139.     {
  140.         cout<<"the winner is "<<'X'<<endl;
  141.         return 1;
  142.     }
  143.     else if(arr[6] == 1 && arr[7] == 1 && arr[8]== 1)
  144.     {
  145.         cout<<"the winner is "<<'X'<<endl;
  146.         return 1;
  147.     }
  148.     else if(arr[0] == 1 && arr[4] == 1 && arr[8]== 1)
  149.     {
  150.         cout<<"the winner is "<<'X'<<endl;
  151.         return 1;
  152.     }
  153.     else if(arr[2] == 1 && arr[4] == 1 && arr[6]== 1)
  154.     {
  155.         cout<<"the winner is "<<'X'<<endl;
  156.         return 1;
  157.     }
  158.     else if(arr[1] == 1 && arr[4] == 1 && arr[7]== 1)
  159.     {
  160.         cout<<"the winner is "<<'X'<<endl;
  161.         return 1;
  162.     }
  163.     else if(arr[3] == 1 && arr[4] == 1 && arr[5]== 1)
  164.     {
  165.         cout<<"the winner is "<<'X'<<endl;
  166.         return 1;
  167.     }
  168.     else
  169.         return 0;
  170. }
  171.  
  172.  
  173. //if 0 is winner
  174. int CheckWinner0(int *arr)
  175. {
  176.     if(arr[0] == 2 && arr[3] == 2 && arr[6] == 2)
  177.     {
  178.         cout<<"the winner is "<<'0'<<endl;
  179.         return 1;
  180.     }
  181.     else if(arr[0] == 2 && arr[1] == 2 && arr[2]== 2)
  182.     {
  183.         cout<<"the winner is "<<'0'<<endl;
  184.         return 1;
  185.     }
  186.     else if(arr[2] == 2 && arr[5] == 2 && arr[8]== 2)
  187.     {
  188.         cout<<"the winner is "<<'0'<<endl;
  189.         return 1;
  190.     }
  191.     else if(arr[6] == 2 && arr[7] == 2 && arr[8]== 2)
  192.     {
  193.         cout<<"the winner is "<<'0'<<endl;
  194.         return 1;
  195.     }
  196.     else if(arr[0] == 2 && arr[4] == 2 && arr[8]== 2)
  197.     {
  198.         cout<<"the winner is "<<'0'<<endl;
  199.         return 1;
  200.     }
  201.     else if(arr[2] == 2 && arr[4] == 2 && arr[6]== 2)
  202.     {
  203.         cout<<"the winner is "<<'0'<<endl;
  204.         return 1;
  205.     }
  206.     else if(arr[1] == 2 && arr[4] == 2 && arr[7]== 2)
  207.     {
  208.         cout<<"the winner is "<<'0'<<endl;
  209.         return 1;
  210.     }
  211.     else if(arr[3] == 2 && arr[4] == 2 && arr[5]== 2)
  212.     {
  213.         cout<<"the winner is "<<'0'<<endl;
  214.         return 1;
  215.     }
  216.     else
  217.         return 0;
  218. }
  219.  
  220. //**************************************************declare.h
  221.  
  222. #ifndef DECLARE_H
  223. #define DECLARE_H
  224.  
  225. //headers
  226. #include <iostream>
  227. using namespace std;
  228.  
  229. //functions
  230. void Init(int *arr);
  231. void Print(int *arr);
  232. int CheckWinnerX(int *arr);
  233. int CheckWinner0(int *arr);
  234.  
  235. #endif
Advertisement
Add Comment
Please, Sign In to add comment