Monday, 8 October 2018

Tic Tac Toe Game Coding

 
 Activity_main.xml File
  
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:ads="http://schemas.android.com/apk/res-auto"     
android:layout_width="match_parent"     
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity">


   <RelativeLayout         
android:layout_width="match_parent"         
android:layout_height="wrap_content"
 
<TextView             
android:id ="@+id/text_view_p1" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:text = "Player 1:  0" 
 android:textSize="30sp" 
 android:textColor="#FF00FF"            />

        <TextView             
android:id ="@+id/text_view_p2"             
android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:layout_below="@+id/text_view_p1"             
android:text = "Player 2:  0" 
 android:textSize="30sp" 
 android:textColor="#FF00FF"            />

        <Button 
 android:id="@+id/button_reset" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:layout_alignParentEnd="true" 
 android:layout_centerVertical="true" 
 android:layout_marginEnd="33dp" 
 android:text="reset" 
 android:textColor="#DC143C"            />

    </RelativeLayout>

<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="0dp"     
android:layout_weight="1"    >

    <Button        android:id="@+id/button_00" 
 android:layout_width="0dp"         
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"        />
    <Button         
android:id="@+id/button_01"         
android:layout_width="0dp"         
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"        />
    <Button 
 android:id="@+id/button_02" 
 android:layout_width="0dp" 
 android:layout_height="match_parent"         
android:layout_weight="1" 
 android:textSize="60sp"        />
</LinearLayout>

    <LinearLayout         
android:layout_width="match_parent" 
 android:layout_height="0dp"         
android:layout_weight="1"        >

        <Button 
 android:id="@+id/button_10" 
 android:layout_width="0dp"             
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_11" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_12" 
 android:layout_width="0dp"             
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
    </LinearLayout>
    <LinearLayout 
 android:layout_width="match_parent"         
android:layout_height="0dp" 
 android:layout_weight="1"        >

        <Button             
android:id="@+id/button_20" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_21" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button             
android:id="@+id/button_22" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
    </LinearLayout>
 
 <com.google.android.gms.ads.AdView         
android:id="@+id/adView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true" 
 android:layout_alignParentBottom="true" 
 ads:adSize="BANNER"         
ads:adUnitId="ca-app-pub-5864813702378205/9734846806">
    </com.google.android.gms.ads.AdView>

</LinearLayout




MainActivity.Java File

package com.example.irawen.tictactoe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.widget.Button;
import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AdView mAdView;

    private Button[][] buttons = new Button[3][3];
    private boolean player1turn = true;
    private int roundCount;
    private int player1Points;
    private int player2Points;



    private TextView textViewPlayer1;
    private TextView textViewPlayer2;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewPlayer1 = findViewById(R.id.text_view_p1);
        textViewPlayer2 = findViewById(R.id.text_view_p2);

 for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
       String buttonID = "button_" + i + j;
        int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
         buttons[i][j] = findViewById(resID);
          buttons[i][j].setOnClickListener(this);

            }
        }
        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                resetGame();

            }
        });

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
    }

    @Override    public void onClick(View v) {
        if (!((Button) v).getText().toString().equals("")) {
            return;
        }
        if (player1turn) {
            ((Button) v).setText("X");
        } else {
            ((Button) v).setText("O");
        }
        roundCount++;
         if (checkForWin()){
             if (player1turn){
                 player1Wins();
             }else{
                 player2Wins();
             }
         }else if(roundCount == 9){
             draw();

         }else{
             player1turn = !player1turn;
         }
    }

    private boolean checkForWin() {
        String[][] field = new String[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                field[i][j] = buttons[i][j].getText().toString();
            }
        }
        for (int i = 0; i < 3; i++) {
            if ((field[i][0].equals(field[i][1])
                    && field[i][0].equals(field[i][2])
                    && ! field[i][0].equals(""))){
                return true;
            }
        }
        for (int i = 0; i < 3; i++) {
            if ((field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && ! field[0][i].equals(""))){
                return true;
            }
        }
        if ((field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && ! field[0][0].equals(""))){
            return true;
        }
        if ((field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && ! field[0][2].equals(""))){
            return true;
        }
        return false;
    }
    private void player1Wins() {
        player1Points++;
        Toast.makeText(this,  "Player 1 wins!",Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }


    private void player2Wins() {
        player2Points++;
        Toast.makeText(this,  "Player 2 wins!",Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void draw() {
        Toast.makeText(this, "Draw!",Toast.LENGTH_SHORT).show();
        resetBoard();
    }
    private void updatePointsText(){
        textViewPlayer1.setText("Player 1: " +player1Points);
        textViewPlayer2.setText("Player 2: " +player2Points);
    }
    private void resetBoard() {
        for ( int i = 0; i < 3; i++){
            for ( int j = 0; j < 3; j++){
                buttons[i][j].setText("");
            }
        }
        roundCount = 0;
        player1turn = true;

    }
    private void resetGame(){
        player1Points = 0;
        player2Points = 0;
        updatePointsText();
        resetBoard();
    }
}
 
Preview
 


Sunday, 7 October 2018

Basics of Calculations_Calculator_Built in Function Assignments

Integer Division %/%

Integer Division :  Division in which the fractional part (remainder) is discarded

> c (2, 3, 5, 7)  % / %  c(2,3)

[1]  1  1  2  2



Modulo Division (x mod y)  %%:

x mod y : modulo operation finds the remainder after division of one number by another

> c (2,3,5,7)  %% 2
  [1]  0  1  1  1



Maximum: max



Maximum: min




Overview Over Further Functions



Example :

> abs ( -4)
  [1] 4

> abs (c(-1, -2, -3, 4, 5) )
 [1]  1  2  3  4  5
> sqrt (4)
  [1]  2

> sqrt ( c(4,9,16,25) )
  [1]  2  3  4  5

 
 > sum (c(2,3,5,7) )
  [1]  17

> prod ( c( 2,3,5,7) )
  [1]   20

> round  (1.23)
  [1]  1

> round (1.83)
  [1]  2



Assignments

Assignments can be made in two ways:

>  x<-6
>  x
    [1]   6

> mode(x)
  [1]  "numeric"

> x=8
> x
   [1]   8

>  mode (x)
    [1]  "numeric"


An assignments can also be used to save values in variables:

>  x1  <-  c(1,2,3,4)

>  x2  <-  x1^2

>  x2
  [1]  1  4  9  16

ATTENTION: R is case sensitive (X is not the same as x

Friday, 5 October 2018

Top 10 Reasons to Learn Python

1. Simple & Easy To Learn
  • Open Source
  • High-level
  • Interpreted
  • Large community
2. Portable & Extensible



3. Web Development
  • Develop web applications
  • Scrape websites
Frameworks
 - django
 - Flask
 - Pylons
 - WEB2PY

4. Artificial Intelligence

  Libraries
      - Scikit-learn
      - Keras
      - Tensorflow
      - Opencv


5. Computer Graphics
  • Graphical User Interface
  • Desktop applications
  • Game development
Libraries
   - Tkinter
   - Jython
   - wxPython
   - PYGAME

6. Testing Frameworks
  • Python supports testing with cross-platform & cross-browser.
  • Built in testing framework which covers debugging time and fastest workflows.
Tools :- Splinter
Framework :- Pytest

7. Big Data
  • Python handles BIG DATA!
  • Python supports parallel computing
  • You can write MapReduce codes in Python
Libraries
    - PYDOOP
    - DASK
    - PySpark

8. Scripting: Automation
  • It is the most popular scripting language in the industry
  • Automate certain tasks in a program
  • They are interpreted rather being compiled
 Scripts → Machine reads & interprets → Runtime error check


 9. Data Science
  • Well-suited for data manipulation & analysis
  • Deals with tabular data with heterogeneously-typed columns
  • Arbitrary matrix data
  • Observational/statistical datasets
Libraries
    - NumPy
    - Pandas
    - matpltlib
    - Seaborn

10.  Popularity & High Salary


    USD  $ 116,028

Java vs Python Comparison

Java :-

It is a fast, secure and reliable general purpose computer programming language.

Python :-

A Readable, efficient and powerful high level programming language.




1. Speed

Java 
    → Statically Typed and Faster than Python
Python
    → Dynamically typed and Slower than Java.

2. Legacy :-

Java 
    → Java legacy systems are typically larger and numerous
Python
    → Python has less legacy problem

3. Code

Java
    → Longer lines of code when compared to Python
Python
    → Shorter Lines of code when compared to Java

4. Databases

Java
     → Most popular and widely used database
Python
      → Access layers are weaker than Java's JDBC

5. Practical Agility

Java
       → Popular for mobile and web applications
Python
        →  Popular for Data Science, ML, AI and IoT.

6. Trends



7. Salary 




8. Syntax 

Basic Differences

Java
     → Java is a compiled language.
     → Java is an object oriented programming language.
     → Java is statically typed.
Python
     → Python is an interpreted language.
     → Python is a scripting language.
     → Python is dynamically typed.

Number of  Lines

Java
        public class HelloWorld {
             public static void (string[] args)  {
                   // print "Hello World" to the terminal window.
                   System.out.println("Hello, World");
              }
       }
Python
             The program prints Hello, World!
          print ("Hello, World!");

 Semicolon

Java
              class Programming  {
                    // constructor method
                Programming ( )  {
                  System.out.println ("constructor method called");
                       }
                public static void main(string[ ] args)  {
                  Programming object = new programming ( );
                  }
             }
Python
                class Student:
                   # Constructor - Parametrized
                   def _int_(self, name):
                    print ("This is parametrized constructor")
                       self.name = name
                    def show (self)
                      print ("Hello", self.name)
                student = Student ("Irawen")
                   student.show( )

Indentation

Tuesday, 2 October 2018

Top 10 Python Libraries

1. Pandas

Pandas is a software library written for the python programming language for data manipulation and analysis.

Pandas is well suited for many different kinds of data:
  • Tabular data with heterogeneously-types columns.
  • Ordered and unordered time series data.
  • Arbitrary matrix data with row and column labels.
  • Any other form of observational / statistical data sets.
 The data actually need not be labeled at all to be placed into a pandas data structure.

2. NumPy

Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.



3. Matplotlib

Matplotlib is a Python package used for 2D graphics.
  • Bar graph
  • Histograms
  • Scatter Plot
  • Pie Plot
  • Hexagonal Bin Plot
  • Area Plot


4. Selenium

The selenium package is used to automate web browser interaction from Python.

5. OpenCV

OpenCV- Python is a library of Python designed to solve computer vision problems.

6. SciPy

Scipy is a free and open-source Python library used for scientific computing and technical computing.



7. Scikit-Learn

Scikit-learn (formerly scikits.learn) is a free software machine learning library for the Python programming language. It features various classification, regression and clustering algorithms.

8.  PySpark

The Spark Python API (PySpark) exposes the Spark programming model to Python.




9. Django

Diango is a Python web framework. A framework provides a structure and common methods to make the life of a web application developer much easier for building flexible, scalable and maintainable web applications
  • Django is a high-level and has a MVC-MVT styled architecture.
  • Django web framework is written on quick and powerful Python language.
  • Django has a open-source collection of libraries for building a fully functioning web application.

10. Tensor Flow

TensorFlow is a Python library used to implement deep networks. In TensorFlow, computation is approached as a dataflow graph.

Monday, 1 October 2018

Introduction to Computating & Problem Solving with Python



This book will help every student, teacher and researcher to understand the computing basics and advanced Python Programming language. The Python programming topics include the reserved keywords, identifiers, variables, operators, data types and their operations, flowcontrol techniques which include decision making and looping, modules, files and exception handling techniques. More than 300 solved lab exercises available in this book is tested in Python 3.4.3 version for Windows. The book covers syllabus for more than 35 International Universities and 45 Indian universities. 

Table of Contents 

1: Introduction to computers
2: Program Logic and flow Charts
3: Introduction to Python
4: Data Types and Operations
5: Flow Control
6: Functions
7: Modules and Packages
8: File Handling
9: Object Oriented Programming
10: Exception Handling
11: Regular Expressions
12: Database Programming





About the Author

Jeeva Jose completed Ph. D. in Computer Science from Mahatma Gandhi University, Kerala, India and is a faculty member at BPC College, Kerala. Her passion is teaching and areas of interests include world wide web, Data Mining and Cyber laws. She has been in higher education for the last 15 years and has completed three research projects funded by UGC and KSCSTE. She has published more than twenty research papers in various refereed journals and conference proceedings. She has edited three books and has given many invited talks in various conferences. She is a recipient of ACM-W Scholarship provided by Association for Computing Machinery, New York. P. Sojan Lal was awarded PH. D. from Cochin University of Science and technology, kerala, India. He is Professor, department of computer science and engineering, MBITS, kerala, India and Research supervisor for PH. D. programs of University of petroleum & Energy Studies, Dehardun, India as well as school of computer science, Mahatma Gandhi University, kerala, India. He has 29 Years of academic and industrial experience with 60 publications inclusive of two technical books. His joint publications are recorded with world records India (2014) for most number of papers in several international technical journals within short duration. He has also obtained MBA from starthclyde Business School, Scotland, UK and is a fellow of the Institution of engineers (FIE-India). He was the district Operationg Board member for ASME, Middle east and Africa region. He is a member of ISTE, ASME, IEEE, CSI and engineering Council (UK). He is listed in marquis who's who in the world since 2009 as the biographical reference representing the world's most accomplished individuals.

Core Python Programming by R. Nageswara Rao (Author)



At present, Java occupies number 1 rank as the most used programming language since almost all the projects are developed in Java. Python is already occupying 2nd to 4th position and will be the most demanded language after Java in near future. Python is used with other programming languages on Internet as well as for developing standalone applications. Python programmers are paid high salaries in the software development industry. Hence, it is time for beginners as well as existing programmers to focus their attention on Python.

.








Popular Posts

Categories

100 Python Programs for Beginner (56) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (938) Python Coding Challenge (372) Python Quiz (29) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses