Thursday, December 5, 2013

Fibonacci Series

Fibonacci Series

This program will help you to print Fibonacci series on your console screen.. If you don't know about Fibonacci series it is something like this:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,......................

A new number in the series comes after the sum of last two numbers in the series like 0 + 1 = 1, so the third number is 1 and then 1 + 2 = 3, so the next number is three and so on...

Here is the output of the program I made:

Output

Code:


// Ahmad Mukhtar (www.cppfuzz.com)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
using namespace std;
void main()

{
 int num,c,i; // declaration
 int a=0,b=1,sum=0; // initiallization
 cout << "Enter a number: ";
 cin >> num; // This input will give us info that how many numbers of the series user wants to see
 for (i=0;i<num;i++)
 {
  if (i<=1)
  {
   c=i; // This condition is for printing 1 two times for series (0,1,1,2....)
  }
  else
  {
   c=a+b; // adds the last two numbers
   a=b; // assigns new number to the old variable
   b=c; // same as above
  }
  cout << c << "\t";
  sum = sum + c; // this is to print the sum of all the numbers of the series printed.
 }
 cout << endl;
 cout << endl;
 cout << "The sum of all the numbers is: " << sum;
 cout << endl;
}

Wednesday, December 4, 2013

Simple Tic Tac Toe Game

Simple Tic Tac Toe

Given below is a code for simple tic tac toe game made using do while loops....
Its really very simple... Just pay close attention to the code and comments .. If you still do not understand you can ask in comments about it :)

Output:

Output

Code:


// www.cppfuzz.com (Ahmad Mukhtar)
/*
  ______ .______   .______    _______  __    __   ________   ________  
 /      ||   _  \  |   _  \  |   ____||  |  |  | |       /  |       /  
|  ,----'|  |_)  | |  |_)  | |  |__   |  |  |  | `---/  /   `---/  /   
|  |     |   ___/  |   ___/  |   __|  |  |  |  |    /  /       /  /    
|  `----.|  |      |  |      |  |     |  `--'  |   /  /----.  /  /----.
 \______|| _|      | _|      |__|      \______/   /________| /________|
                                                                       
*/
#include <iostream>
#include <conio.h>
using namespace std;

void main() {
 char square1='1';
 char square2='2';
 char square3='3';
 char square4='4';
 char square5='5';
 char square6='6';
 char square7='7';
 char square8='8';
 char square9='9';
 int playerturn=1;
 bool gameover=true;

 // Loop for game starts here...
 do {
  // This loop prints the board.
  cout << square1 << "|" << square2 << "|" << square3 << endl;
  cout << "-+-+-"<< endl;
  cout << square4 << "|" << square5 << "|" << square6 << endl;
  cout << "-+-+-"<< endl;
  cout << square7 << "|" << square8 << "|" << square9 << endl;

  // It sets X for Player 1 and O for Player 2
  char playermark;
  if (playerturn == 1) {
   playermark = 'X';
  } else {
   playermark = 'O';
  }
  
  // It asks the user for move
  cout << "Player" << playerturn << "'s move:" << endl;
  bool validmove;
  // This loop continues until a valid move is given
  do {
   char nextmove;
   cin >> nextmove;
   validmove = true;
   system("cls");
   // Check for a valid move
   if (nextmove == '1' && square1 == '1') {
    square1 = playermark;
   } else if (nextmove == '2' && square2 == '2') {
    square2 = playermark;
   } else if (nextmove == '3' && square3 == '3') {
    square3 = playermark;
   } else if (nextmove == '4' && square4 == '4') {
    square4 = playermark;
   } else if (nextmove == '5' && square5 == '5') {
    square5 = playermark;
   } else if (nextmove == '6' && square6 == '6') {
    square6 = playermark;
   } else if (nextmove == '7' && square7 == '7') {
    square7 = playermark;
   } else if (nextmove == '8' && square8 == '8') {
    square8 = playermark;
   } else if (nextmove == '9' && square9 == '9') {
    square9 = playermark;
   } else {
    cout << "Invalid Move. Try again." << endl;
    validmove = false;
   }
  } while (!validmove);

  gameover  = false;
  bool wingame = true;
  // It checks the winning conditions
  if (square1 != '1') {
   if (square2 == square1 && square3 == square1) {
    gameover = true;
   }
   if (square4 == square1 && square7 == square1) {
    gameover = true;
   }
  }
  if (square5 != '5') {
   if (square1 == square5 && square9 == square5) {
    gameover = true;
   }
   if (square2 == square5 && square8 == square5) {
    gameover = true;
   }
   if (square4 == square5 && square6 == square5) {
    gameover = true;
   }
   if (square3 == square5 && square7 == square5) {
    gameover = true;
   }
  }
  if (square9 != '9') {
   if (square3 == square9 && square6 == square9) {
    gameover = true;
   }
   if (square7 == square9 && square8 == square9) {
    gameover = true;
   }
  }
  // It will check for draw conditions
  if (square1 != '1' && square2 != '2' && square3 != '3' &&
   square4 != '4' && square5 != '5' && square6 != '6' &&
   square7 != '7' && square8 != '8' && square9 != '9' && !gameover)
  {
   gameover = true;
   wingame = false;
  }
   if (gameover) {
   if (wingame==true) {
    cout << "Player" << playerturn << " wins..." << endl;
   }
   if (wingame==false) {
    cout << "Draw Game." << endl;
   } 
   // Final shape of board after getting all the moves till end of game
   cout << square1 << "|" << square2 << "|" << square3 << endl;
   cout << "-+-+-"<< endl;
   cout << square4 << "|" << square5 << "|" << square6 << endl;
   cout << "-+-+-"<< endl;
   cout << square7 << "|" << square8 << "|" << square9 << endl;
   
   cout << "Game Over" << endl;
   cout << "Do you want to play again (y/n)?" << endl;
   char playagain;
   cin >> playagain;

   if (playagain == 'y') {
    system("cls");
    gameover = false;
    // resets the board
    square1 = '1';
    square2 = '2';
    square3 = '3';
    square4 = '4';
    square5 = '5';
    square6 = '6';
    square7 = '7';
    square8 = '8';
    square9 = '9';
   }
   playerturn = 1;
  } else {
   // Alternate player turns
   if (playerturn == 1) {
    playerturn = 2;
   } else {
    playerturn = 1;
   }
  }
 } while (!gameover);
}

 

Copyright @ 2014 CPP Fuzz.