Monday, December 21, 2015

C++ Tic Tac Toe


//Araf Shihab
//Knaughts and Crosses Lab.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;

string table[9] = {"1","2","3","4","5","6","7","8","9"};
string player = "X"; //First Player
int fullTable = 0;
int playagainStateMachine = 0;


void showBoard();
void playerInput();
string checkWinner();
string tableNotFull();
void changePlayer();
void checkState();
void clearTable();

int main(){
    showBoard();
    while (1)
    {
        fullTable++;
        playerInput();
        showBoard();
        if (checkWinner() == "X")
        {
            cout << "\nPlayer 1: X wins!" << endl;
            playagainStateMachine = 1;
            checkState();
        }
        else if (checkWinner() == "O")
        {
            cout << "\nPlayer 2: O wins!" << endl;
            playagainStateMachine = 1;
            checkState();
        }
        else if (checkWinner() == "DRAW" && fullTable == 9)
        {
            cout << "\nIt's a draw! No winner!" << endl;
            playagainStateMachine = 1;
            checkState();
        }
        changePlayer();
    }
    system("pause");
    return 0;
}


void checkState(){
    char a;
    if (playagainStateMachine != 0) {
            cout << "\nWould you like to play again 'Y' for yes 'N' for no" << endl;
            cin >> a;
            a = toupper(a);
            if (a == 'N'){
                cout << "\nExiting tic tac toe" << endl;
                exit(EXIT_FAILURE);
            }else if(a == 'Y'){
                clearTable();
                showBoard();
            }else{
                cout << "\nEntered incorrect data, game exiting" << endl; exit(EXIT_FAILURE);
            }
        }
    if (playagainStateMachine == 1){
        cout << "\nWho will go first 'X' or 'O'?: " << endl;
        cin >> a;
        a = toupper(a);
        if (a == 'X'){ player = "X"; }
        if (a == 'Y') { player = "O"; }
    }
}

void clearTable(){
    for(int i = 0; i < 9; i++){
        if(i == 8){table[i] = "9";}
        if(i == 7){table[i] = "8";}
        if(i == 6){table[i] = "7";}
        if(i == 5){table[i] = "6";}
        if(i == 4){table[i] = "5";}
        if(i == 3){table[i] = "4";}
        if(i == 2){table[i] = "3";}
        if(i == 1){table[i] = "2";}
        if(i == 0){table[i] = "1";}
    }
}

void showBoard(){
    system("cls");
    cout << "Tic Tac Toe Board" << endl;
    for(int i = 0; i < 9; i++){
        if(i == 3 || i == 6){
            cout << endl;
        }
        cout << " " << table[i] << " ";
       }
}

void playerInput(){
    int a;
    cout << endl << "It's " << player << " turn. " <<"Press the number of the field: " << endl;
    cin >> a;

    switch(a){
        case 1:
            if (table[0] == "1"){
                table[0] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 2:
            if (table[1] == "2"){
                table[1] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 3:
            if (table[2] == "3"){
                table[2] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 4:
            if (table[3] == "4"){
                table[3] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 5:
            if (table[4] == "5"){
                table[4] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 6:
            if (table[5] == "6"){
                table[5] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 7:
            if (table[6] == "7"){
                table[6] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 8:
            if (table[7] == "8"){
                table[7] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
        case 9:
            if (table[8] == "9"){
                table[8] = player;
            }
            else
            {
                cout << "I'm sorry field is in use please try another position" << endl;
                playerInput();
            }
            break;
    }
}
void changePlayer()
{
    if (player == "X")
        player = "O";
    else
        player = "X";
}
string checkWinner()
{
    //first player
    if (table[0] == "X" && table[1] == "X" && table[2] == "X")
        return "X";
    if (table[0] == "X" && table[3] == "X" && table[6] == "X")
        return "X";
    if (table[1] == "X" && table[4] == "X" && table[7] == "X")
        return "X";

    if (table[3] == "X" && table[6] == "X" && table[9] == "X")
        return "X";
    if (table[4] == "X" && table[5] == "X" && table[6] == "X")
        return "X";
    if (table[6] == "X" && table[7] == "X" && table[8] == "X")
        return "X";

    if (table[3] == "X" && table[5] == "X" && table[7] == "X")
        return "X";
    if (table[0] == "X" && table[4] == "X" && table[8] == "X")
        return "X";

    //second player
    if (table[0] == "O" && table[1] == "O" && table[2] == "O")
        return "O";
    if (table[0] == "O" && table[3] == "O" && table[6] == "O")
        return "O";
    if (table[1] == "O" && table[4] == "O" && table[7] == "O")
        return "O";

    if (table[2] == "O" && table[5] == "O" && table[8] == "O")
        return "O";
    if (table[4] == "O" && table[5] == "O" && table[6] == "O")
        return "O";
    if (table[7] == "O" && table[8] == "O" && table[9] == "O")
        return "O";

    if (table[3] == "O" && table[5] == "O" && table[7] == "O")
        return "O";
    if (table[0] == "O" && table[4] == "O" && table[8] == "O")
        return "O";

    return "DRAW";
}

C++ Virtual Machine



#include <windows.h>
#include <iomanip>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

//Virtual Machine Global Variables
int pc=0;
int operand;
int acc;
int opcode;
int Memory_data_register = 0;
int command;
int Programcounter=0;
//Virtual Machine Functions
int menu();
void VMstart(int);
void about();
void load();
void display();
void documentation();
void execute(int);
void run();
//Virtual Machine Constants; Instruction set
const int CLR  = 0;   // acc = 0
const int LOAD = 1;   // not implemented
const int ADD = 2;   // acc = acc + operand
const int SUB = 3;   // acc = acc - operand
const int DIV = 4;
const int MUL = 5;
const int OUTPUT = 6;
const int HALT = 0xFF;  // HEX opcode to hlt cpu 244
const int JMP = 7;      // pc = jmp's operand
const int BNZ = 8;      // Branch Not Zero  operand . if (acc != 0) pc = operand
const int STORES = 9;
const int RUN = 10;
const int QUIT = 11;
//Our Ram with certain amounts of data;
int RAM[10000];
string RAMdisplay[10000];

int main()
{
    while(true)
    {
        command = menu();
        if (command == 6){cout << "We are exiting virutal machine..."; return 0;} //Digit 6 represents quit.
        VMstart(command);
    }
    return 0;
}

void VMstart(int command){
    switch(command)
        {
            case 1: //About
                about();
                break;

            case 2: //Load data from text file
                load();
                break;

            case 3: //Display data in our RAM
                display();
                break;

            case 4: //Run
                run(); // Run will lead to execution of our opcode
                break;
            case 5:
                documentation();
                break;
            default:
                cout << "Virtual Machine Exiting, Fatal error" << endl;
                exit(EXIT_FAILURE);
                system("pause");
                break;
        }
}

void run () {
    if(Programcounter == 0){cout << "Error Cannot Run without loading data first\n"; system("pause"); return;}
        while(true)
        {
            opcode = RAM[pc]; //Grab the current opcode, starting from the beginning of our RAM.
            execute(opcode);
            if (opcode==HALT || opcode==QUIT)
            {
                cout << "Value of Accumulator is: " << acc << endl;
                if (Memory_data_register != 0){cout << "Value of memory is: " << Memory_data_register << endl;}
                cout << "We are halting or quitting the virtual machine....." << endl;
                system("pause");
                return;
            }
        }
}
//Fetch Execute Cycle ALU
void execute(int opcode)
{
    switch (opcode) //Our opcode to determine what to do with each command inside of our RAM
    {
        case ADD:
                operand = RAM[++pc];
                acc += operand;
                pc++;
                break;
        case SUB:
                operand = RAM[++pc];
                acc -= operand;
                pc++;
                break;
        case MUL:
                operand = RAM[++pc];
                acc *= operand;
                pc++;
                break;
        case BNZ:
                operand = RAM[++pc];
                if ( acc != 0 ) pc = operand;
                else pc++;
                break;
        case CLR:
                acc = 0;
                pc++;
                break;
        case DIV:
                operand = RAM[++pc];
                if (operand != 0) { acc /= operand; }else{ acc = 0; }
                pc++;
                break;
        case JMP:
                operand = RAM[++pc];
                pc = operand;
                break;
        case OUTPUT:
                cout << "The content of the accumulator is: " << acc << endl;
                pc++;
                break;
        case STORES:
                Memory_data_register = acc; //
                pc++;
                break;
        case HALT:
                break;
        default:
            cout << "Unidentified word " << opcode << endl;
    }
}
//Open file and fetch each word seperated by a space and store it into our ram
void load()
{
    string filename,word;
    int i = 0;

    cout << "Enter file name in current directory: ";
    cin >> filename;
    ifstream input(filename.c_str());

    if(!input.is_open())
    {
        cout << "Your file was not found: " << endl;
        system("pause");
        return;
    }
    cout << "File found:" << endl;
    system("pause");
    //As long as we don't reach the end of the file continue to load data into our RAM
    while(!input.eof())
    {
        input >> word;
        RAMdisplay[i]=word;
        if(word=="CLR")             //Virtual machine commands that are allowed in your text file
        {
            RAM[i]=CLR;
        }
        else if(word=="ADD")
        {
            RAM[i]=ADD;
        }
        else if(word=="SUB")
        {
            RAM[i]=SUB;
        }
        else if(word=="DIV")
        {
            RAM[i]=DIV;
        }
        else if(word=="MUL")
        {
            RAM[i]=MUL;
        }
        else if(word=="OUT")
        {
            RAM[i]=OUTPUT;
        }
        else if(word=="HALT")
        {
            RAM[i]=HALT;
        }
        else if(word=="JMP")
        {
            RAM[i]=JMP;
        }
        else if(word=="BNZ")
        {
            RAM[i]=BNZ;
        }
        else if(word=="STORE")
        {
            RAM[i]=STORES;
        }
        else if(word=="RUN")
        {
            RAM[i]=RUN;
        }
        else if(word=="QUIT")
        {
            RAM[i]=QUIT;
        }
        else
        {
            stringstream number(word);
            number >> RAM[i];
            if (word!="QUIT" && word!="RUN" && word!="BNZ" && word!="JMP" && word!="HALT" && word!="OUT" && word!="MUL" && word!="DIV" && word!="SUB" && word!="ADD" && word!="CLR"){
                //A check in order to see if our virtual machine instructions are loadable.
                if(!number >> RAM[i]){
                    cout << "Error Loading data unidentified: " << word << endl;
                    system("pause");
                    return;
                }
            }
        }
        i++;
    }
    Programcounter = i;
    cout << "Data Processed"<< endl;
    input.close();
    system("pause");
}

void display()
{
    cout << "Displaying Data" << endl;
    if(Programcounter != 0){
        for(int i=0; i<Programcounter; i++)
        {
            cout << RAMdisplay[i] << endl;
        }
        system("pause");
    }else{cout << "Error cannot display data there is no data in our RAM" << endl; system("pause"); return;}
}

int menu()
{
    int choice;
    system("CLS");

    cout << "Simple CPU" << endl;
    cout << "1....About" << endl;
    cout << "2...Load Program" << endl;
    cout << "3...Display Program" << endl;
    cout << "4...Run Program" << endl;
    cout << "5...Documentation" << endl;
    cout << "6...QUIT" << endl;
    cout << "Choice:";

    cin >> choice;
    cout << endl;
    return choice;
}

void about()
{
    cout <<"Menu driven Calculator + Virtual Machine Version 1.0\n";
    cout <<"(c) copyright 2015 Araf Shihab. All rights reserved.\n" << endl;
    system("pause");
}


void documentation()
{
    cout << "HELP" << endl << endl;
    cout << "1) IF The result of the vm is different from the result I did manually." << endl;
    cout << "   --------" << "Make sure the commands that you type in your text file are proper commands" << endl << endl;
    cout << "Commands Available: CLR QUIT RUN BNZ JMP HALT OUT MUL DIV SUB ADD STORE" << endl;
    system("pause");
}

Sunday, December 9, 2012

Google Chrome For Secure Browsing?




The reason I prefer browsers to run similarly to Firefox is due to  a few of my favorite add-ons to promote security and speed.
But Google's browser Chrome has a devoted audience and is very fast pace and is constantly growing. Of course, it also generates a unique user ID and submits entries to Google to generate suggestions.

SRWare Iron is a really great alternative. The browser is based on the Chromium-source and offers the same features as Chrome but without the critical points of privacy concern. Lets just say SRWare is a great alternative when it comes to privacy.

And Comodo Dragon is a Chromium-source browser that offers you all of Chrome's features with the level of security and privacy you only get from Comodo. 











U.S Cyber Security Alert




The U.S. government does not have the best ongoing record with cyber-security. With at least 75 cyber-security failures over the past eight years and 13 in the past six months, U.S. government cyber-security efforts could use some revamping.
But the U.S. is about to go in the opposite direction. Sequestration will cut 9.4 percent from every part of the defense budget. These cuts will hit cyber operations, including cybersecurity programs in the Department of Homeland Security. 


Other Outside Links:

The Alarming Trend of Cybersecurity Breaches and Failures in the U.S. Government Continues

The Alarming Trend of Cyber-security Breaches and Failures in the U.S. Government

White House Legislative Reports relating to Cyber Security.


Thursday, November 8, 2012

How Rich Are You?





Every year Forbes releases the top richest people in the world. Usually we are able to realize who is in the top 10, however did you ever wonder where your position is? Well wonder no more, since you can easily find out where you are standing in the entire population. 

Simply Visit Here and input your annual income. It should give you a nice rough idea as to where you are. 

Many more posts to come!

Cryptography Lessons (Course)




Here is an amazing set of online resources to help you understand and learn cryptography. How do 'Crackers' actually get into systems? Well its something that isn't so easily explained. However I found a nice simple source of video tutorials in which you can learn cryptography.

You will study a detailed, step by step lessons created by a real practicing cracker. You don’t need to be a PC guru and don’t even have to master any software programming languages.
 

These learning courses will show you how to crack software with both the most simple and the most sophisticated protection systems.



Note: In fact, this is an extended version of the Lena tutorials.
The file is compressed with UHA for the maximum compression ratio.
Some of the cracking tools might show up as virus for some AVs