Showing posts with label Source Codes. Show all posts
Showing posts with label Source Codes. Show all posts

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. 











Friday, November 2, 2012

[C#] Image Screen Capture [C#]

Here is a nice source code which can enable you to capture your desktop screen. This would be a nice source to take a look at and analyze especially if you are trying to make your own application that will be using this feature. Simply just create a class and add this source code. However I would never recomend copying and pasting source codes that you do see, that necessarily isn't 'learning the code'.

I will be also releasing the source of an application in which you can upload your screen captured image to an image hosting website, so stay tuned.


Tuesday, October 30, 2012

Check If A Program Is Running [C++]

This is a self explanatory source code on how to properly utilize and use async sockets.



Visit Textinguy.com For More.

Friday, October 26, 2012

VB.Net 4Chan Image Downloader





This source code is pretty much self explanatory. Click the button below to take a look.



Tuesday, October 2, 2012

Money and Banking Online Course Review





Hey guys, coming right back at you with very easy and simple online money and banking course notes. This is an online course ware to help you understand finance. This course is from Queens borough Community College. Note that these course notes works quite well especially if you are alittle bit more experienced with marketing. 

Money was developed as a way of exchange to facilitate trade. Here you can learn about five stages in the evolution of money. Beginning with barter, trace the rise of commodities as money, the invention of coins, paper money backed by precious metals, which has value by agreement alone.

Wednesday, September 12, 2012

Online College Courses Software Testing





There is no doubt that the IT field is constantly growing. There are just so many topics to cover that necessarily one degree cannot cover the whole area. Which is why sometimes you may end up having to take some extra courses after your bachelors degree. What better way to take these classes other than taking them right at home. Online Classrooms are growing without a doubt. It is defiantly for out convenience especially if we are bombarded with work and other duties.

Software testing is something that is constantly growing in the IT field. However lets not completely negate all the other areas in the IT field. Software testing is something that will always be needed. Well, why is this the case? There's so many vital and important pieces of software out there which have bugs that need to be known or something fatal can go wrong and happen, resulting in damage to the business.


One great service that replicates Online Colleges would be the ILX group. The course ware they provide is very high quality and allows you to learn easily, as if you were in an online college classroom. They even offer a simple little preview/demonstration on how it works.

Click Here To Visit


Another great course for learning software testing is wiz-IQ. This service is really great because it completely simplifies everything. Online College learning at its simplest and finest.


Click Here To Visit

Monday, September 10, 2012

Economics QCC College Course Notes (PDF)




Macroeconomics Free College Course Notes:

From Queens borough Community College:

Macroeconomics is simply the study of economics as a whole. Not pertaining to any specific nation or country but using the basics of economics how it ties into the whole world.

These slides/notes are pretty much a brief overview and simplified. This is extremely great for anyone that doesn't not want to read a whole book on macroeconomics and whats to cover the course in a simple and quick fashion.

CLICK HERE TO DOWNLOAD COURSE NOTES/SLIDES 



Microeconomics Free College Course Notes:

From Queens borough Community College:

Microeconomics is simply the study of economics in specific. Covering the primitive form and basics. Here are the notes for this course: