Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //****************************************************MAIN.CPP
- #include "stdafx.h"
- #include "declare.h"
- int main()
- {
- int board[9];
- int *pBoard = &board[0];
- Init(pBoard);
- Print(pBoard);
- int check = 0; //checking if there is any winner
- cout<<"pls, choose between you who is the x anad who is the 0"<<endl; //instructions
- cout<<"after you have chosen,Lets start :)"<<endl;
- while(check == 0)
- {
- //player X enters number
- int numBox;
- cout<<"X pls enter number which represents the box you have chosen"<<endl;
- //checking if the location is fulled or not
- int checkNum;
- cin>>checkNum;
- if(board[checkNum-1] != 0)
- {
- cout<<"Error,pls enter other location"<<endl;
- continue;
- }
- else
- {
- numBox = checkNum;
- }
- //checking if the number is in the range
- if(numBox < 10 && numBox >0)
- board[numBox-1] = 1;
- else
- {
- cout<<"error"<<endl;
- continue;
- }
- Print(pBoard); //prints the new board
- //checks if there is any winner
- check = CheckWinnerX(pBoard);
- if(check != 0) continue;
- //player 0 enters number
- cout<<"0 pls enter number which represents the box you have chosen"<<endl;
- //checking if the location is fulled or not
- cin>>checkNum;
- if(board[checkNum-1] != 0)
- {
- cout<<"Error,pls enter other location"<<endl;
- continue;
- }
- else
- {
- numBox = checkNum;
- }
- //checking if the number is in the range
- if(numBox < 10 && numBox >0)
- board[numBox-1] = 2;
- else
- {
- cout<<"Error"<<endl;
- continue;
- }
- Print(pBoard); //prints the board
- //checks if there is any winner
- check = CheckWinner0(pBoard);
- if(check != 0) continue;
- }
- return 0;
- }
- //********************************************************************************function.cpp
- #include "stdafx.h"
- #include "declare.h"
- //initalizate the board
- void Init(int *arr)
- {
- for(int i = 0;i < 9;i++)
- {
- arr[i] = 0;
- }
- }
- //prints the board
- void Print(int *arr)
- {
- cout<<"this is the real board"<<endl;
- for(int i = 0;i< 9;i++)
- {
- if(arr[i] == 1)
- cout<<'x';
- else if(arr[i] ==2)
- cout<<'0';
- else
- cout<<'-';
- if(i == 2 || i == 5 || i ==8)
- {
- cout<<endl;
- }
- }
- cout<<endl;
- cout<<"this is your keyboard\n"<<endl;
- for(int box = 1;box <= 9;box++)
- {
- cout<<box;
- if(box == 3 || box == 6 || box ==9)
- cout<<endl;
- }
- cout<<endl;
- cout<<"The numbers represnt each box"<<endl;
- }
- //checking if X is winner
- int CheckWinnerX(int *arr)
- {
- if(arr[0] == 1 && arr[3] == 1 && arr[6] == 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[0] ==1 && arr[1] == 1 && arr[2]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[2] == 1 && arr[5] == 1 && arr[8]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[6] == 1 && arr[7] == 1 && arr[8]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[0] == 1 && arr[4] == 1 && arr[8]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[2] == 1 && arr[4] == 1 && arr[6]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[1] == 1 && arr[4] == 1 && arr[7]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else if(arr[3] == 1 && arr[4] == 1 && arr[5]== 1)
- {
- cout<<"the winner is "<<'X'<<endl;
- return 1;
- }
- else
- return 0;
- }
- //if 0 is winner
- int CheckWinner0(int *arr)
- {
- if(arr[0] == 2 && arr[3] == 2 && arr[6] == 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[0] == 2 && arr[1] == 2 && arr[2]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[2] == 2 && arr[5] == 2 && arr[8]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[6] == 2 && arr[7] == 2 && arr[8]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[0] == 2 && arr[4] == 2 && arr[8]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[2] == 2 && arr[4] == 2 && arr[6]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[1] == 2 && arr[4] == 2 && arr[7]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else if(arr[3] == 2 && arr[4] == 2 && arr[5]== 2)
- {
- cout<<"the winner is "<<'0'<<endl;
- return 1;
- }
- else
- return 0;
- }
- //**************************************************declare.h
- #ifndef DECLARE_H
- #define DECLARE_H
- //headers
- #include <iostream>
- using namespace std;
- //functions
- void Init(int *arr);
- void Print(int *arr);
- int CheckWinnerX(int *arr);
- int CheckWinner0(int *arr);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment