MrAlex

first version of tic tac toe

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