PROJECT OF ITCS
By
Fatima batool & nimra mazhar
qudsiya yousaf & omama snober
to
sir tahir
TICK TAC TOE GAME
•INTRODUCTION:
Tic-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for
two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who
succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the
game.
The following example game is won by the first player, X:
Players soon discover that best play from both parties leads to a draw. Hence, tic-tac-toe is
most often played by young children.
TICK TAC TOE IN C++
• #include <iostream>
• using namespace std;
• char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
• char turn = 'x';
• int row,column;
• bool draw = false;
• void display_board()
• {
• system("cls");
• cout<<"nn Tick Cross Game"<<endl;
• cout<<"tPlayer1 [x] n tPlayer2 [O]nn";
• cout<<"tt | | n";
TICK TAC TOE IN C++
• cout<<"tt "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" n";
• cout<<"tt_____|_____|_____n";
• cout<<"tt | | n";
• cout<<"tt "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" n";
• cout<<"tt_____|_____|_____n";
• cout<<"tt | | n";
• cout<<"tt "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" n";
• cout<<"tt | | n";
• }
• void player_turn()
• {
• int choice;
• int row,column;
TICK TAC TOE IN C++
• if(turn =='x')
• cout<<"ntplayer1 [x] turn:";
•
• if(turn == 'k')
• cout<<"ntplayer2 [O] turn;";
•
• cin>>choice;
• switch(choice)
• {
• case 1 : row=0; column=0; break;
• case 2 : row=0; column=1; break;
• case 3 : row=0; column=2; break;
• case 4 : row=1; column=0; break;
• case 5 : row=1; column=1; break;
• case 6 : row=1; column=2; break;
• case 7 : row=2; column=0; break;
• case 8 : row=2; column=1; break;
• case 9 : row=2; column=2; break;
•
• default:
• cout<<"invalid choicen";
• break;
•
• }
• if(turn == 'x' && board[row][column]!='x' && board[row][column]!='x')
• {
• board[row][column]= 'x';
• turn = 'O';
• }
• else if(turn == 'O' && board[row][column]!='O' && board[row][column]!='O')
• {
• board[row][column]= 'O';
• turn= 'x';
• }
• else
• {
• cout<<"box already fielled!n please try again!!nn";
• player_turn();
• }
• cout<<" Player1 [x] turn:";
• }
• bool gameover()
• {
• //check win
• for(int i=0;i<3;i++)
• if(board[i][0] == board [i][1] && board[i][0] == board[i][2] || board[0][i] ==
board[1][i] && board[0][i] == board[2][i])
• return false;
• if(board[0][0] == board [1][1] && board[0][0] == board[2][2] || board[0][2] ==
board[1][1] && board[0][0] == board[0][2])
• return false;
•
• for(int i=0; i<3; i++)
• for(int j=0; j<3; j++)
• if(board[i][j] !='x' && board[i][j]!='O')
• return true;
• draw = true;
• return false;
• }
• main()
• {
• while(gameover())
• {
• display_board();
• player_turn();
• display_board();
• gameover();
• }
• if(turn=='O')
• cout<<"player1[x] wins!! congratulationn";
• else if(turn=='x')
• cout<<"player2[O] wins!! congratulationn”;
• }
• program end
INTRO OF FUNCTIONS IN C++
• In this article, you'll learn everything about functions in C++; what type of functions
are there, how to use them with examples. In programming, function refers to a segment
that groups code to perform a specific task.
• Depending on whether a function is predefined or created by programmer; there are two
types of function:
• Library Function
• User-defined Function
•Library Function
• Library functions are the built-in function in C++ programming.
• Programmer can use library function by invoking function directly; they don't need to write it
themselves.
INTRO OF FUNCTIONS IN C++
User-defined Function
• C++ allows programmer to define their own function.
• A user-defined function groups code to perform a specific task and that group of code is
given a name(identifier).
• When the function is invoked from any part of program, it all executes the codes defined in
the body of function
FOR LOOP
syntax
For(initializing statement;test expression;update statement;)
• The initialization statement is executed only once at the beginning
• Then, the test expression is evaluated.
• If the test expression is false, for loop is terminated. But if the test expression is
true, codes inside body of for loop is executed and update expression is updated
• Again the test expression is evaluated and this process repeats until the test
expression is false
WHILE LOOP
syntax
While(condition)
• {
statements
• }
• The while loop evaluates the test expression.
• If the test expression is true, codes inside the body of while loop is evaluated.
• Then, the test expression is evaluated again. This process goes on until the test
expression is false.
When the test expression is false, while loop is terminated.
SWITCH STATEMENT
•Syntax of switch statement:
• switch(expression)
• {
• Case constant1:
• Break;
• Case constant2:
• Break;
• Default:
• }
• When a case constant is found that matches the switch expression, control of the program passes to the
block of code associated with that case.
• In the above psedocode suppose the value of n is equal to constant2. The compiler will execute the block of
code associated with the case statement until the end of switch block, or until the break statement is
encountered.
• The break statement is used to prevent the code running into the next case.
ARRAY
• In programming, one of the frequently arising problem is to handle numerous data
of same type.
• Consider this situation, you are taking a survey of 100 people and you have to store
their age. To solve this problem in C++, you can create an integer array having 100
elements.
• An array is a collection of data that holds fixed number of values of same type. For
example: int age[100]
• Here, the age array can hold maximum of 100 elements of integer type.
• The size and type of arrays cannot be changed after its declaration.

tick cross game

  • 1.
    PROJECT OF ITCS By Fatimabatool & nimra mazhar qudsiya yousaf & omama snober to sir tahir
  • 2.
    TICK TAC TOEGAME •INTRODUCTION: Tic-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. The following example game is won by the first player, X: Players soon discover that best play from both parties leads to a draw. Hence, tic-tac-toe is most often played by young children.
  • 3.
    TICK TAC TOEIN C++ • #include <iostream> • using namespace std; • char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}}; • char turn = 'x'; • int row,column; • bool draw = false; • void display_board() • { • system("cls"); • cout<<"nn Tick Cross Game"<<endl; • cout<<"tPlayer1 [x] n tPlayer2 [O]nn"; • cout<<"tt | | n";
  • 4.
    TICK TAC TOEIN C++ • cout<<"tt "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" n"; • cout<<"tt_____|_____|_____n"; • cout<<"tt | | n"; • cout<<"tt "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" n"; • cout<<"tt_____|_____|_____n"; • cout<<"tt | | n"; • cout<<"tt "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" n"; • cout<<"tt | | n"; • } • void player_turn() • { • int choice; • int row,column;
  • 5.
    TICK TAC TOEIN C++ • if(turn =='x') • cout<<"ntplayer1 [x] turn:"; • • if(turn == 'k') • cout<<"ntplayer2 [O] turn;"; • • cin>>choice;
  • 6.
    • switch(choice) • { •case 1 : row=0; column=0; break; • case 2 : row=0; column=1; break; • case 3 : row=0; column=2; break; • case 4 : row=1; column=0; break;
  • 7.
    • case 5: row=1; column=1; break; • case 6 : row=1; column=2; break; • case 7 : row=2; column=0; break; • case 8 : row=2; column=1; break; • case 9 : row=2; column=2; break; • • default:
  • 8.
    • cout<<"invalid choicen"; •break; • • } • if(turn == 'x' && board[row][column]!='x' && board[row][column]!='x') • { • board[row][column]= 'x';
  • 9.
    • turn ='O'; • } • else if(turn == 'O' && board[row][column]!='O' && board[row][column]!='O') • { • board[row][column]= 'O'; • turn= 'x';
  • 10.
    • } • else •{ • cout<<"box already fielled!n please try again!!nn"; • player_turn(); • } • cout<<" Player1 [x] turn:"; • }
  • 11.
    • bool gameover() •{ • //check win • for(int i=0;i<3;i++) • if(board[i][0] == board [i][1] && board[i][0] == board[i][2] || board[0][i] == board[1][i] && board[0][i] == board[2][i]) • return false;
  • 12.
    • if(board[0][0] ==board [1][1] && board[0][0] == board[2][2] || board[0][2] == board[1][1] && board[0][0] == board[0][2]) • return false; • • for(int i=0; i<3; i++) • for(int j=0; j<3; j++) • if(board[i][j] !='x' && board[i][j]!='O') • return true;
  • 13.
    • draw =true; • return false; • } • main() • { • while(gameover()) • { • display_board();
  • 14.
    • player_turn(); • display_board(); •gameover(); • } • if(turn=='O') • cout<<"player1[x] wins!! congratulationn"; • else if(turn=='x') • cout<<"player2[O] wins!! congratulationn”; • } • program end
  • 15.
    INTRO OF FUNCTIONSIN C++ • In this article, you'll learn everything about functions in C++; what type of functions are there, how to use them with examples. In programming, function refers to a segment that groups code to perform a specific task. • Depending on whether a function is predefined or created by programmer; there are two types of function: • Library Function • User-defined Function •Library Function • Library functions are the built-in function in C++ programming. • Programmer can use library function by invoking function directly; they don't need to write it themselves.
  • 16.
    INTRO OF FUNCTIONSIN C++ User-defined Function • C++ allows programmer to define their own function. • A user-defined function groups code to perform a specific task and that group of code is given a name(identifier). • When the function is invoked from any part of program, it all executes the codes defined in the body of function
  • 17.
    FOR LOOP syntax For(initializing statement;testexpression;update statement;) • The initialization statement is executed only once at the beginning • Then, the test expression is evaluated. • If the test expression is false, for loop is terminated. But if the test expression is true, codes inside body of for loop is executed and update expression is updated • Again the test expression is evaluated and this process repeats until the test expression is false
  • 18.
    WHILE LOOP syntax While(condition) • { statements •} • The while loop evaluates the test expression. • If the test expression is true, codes inside the body of while loop is evaluated. • Then, the test expression is evaluated again. This process goes on until the test expression is false. When the test expression is false, while loop is terminated.
  • 19.
    SWITCH STATEMENT •Syntax ofswitch statement: • switch(expression) • { • Case constant1: • Break; • Case constant2: • Break; • Default: • } • When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case. • In the above psedocode suppose the value of n is equal to constant2. The compiler will execute the block of code associated with the case statement until the end of switch block, or until the break statement is encountered. • The break statement is used to prevent the code running into the next case.
  • 20.
    ARRAY • In programming,one of the frequently arising problem is to handle numerous data of same type. • Consider this situation, you are taking a survey of 100 people and you have to store their age. To solve this problem in C++, you can create an integer array having 100 elements. • An array is a collection of data that holds fixed number of values of same type. For example: int age[100] • Here, the age array can hold maximum of 100 elements of integer type. • The size and type of arrays cannot be changed after its declaration.