Announcing EZee Copyright Protector

Hello again guys!

I wish to announce that I have released a new WordPress Plugin called ‘EZee Copyright Protector’.  You can find this plugin at the URL https://wordpress.org/plugins/ezee-cpyright-protector/.

What does the plugin do? Suppose you have a wordpress website where you have created original content painstakingly and published it. Ordinarily, there is nothing to prevent other webmasters from blatantly copying your content. So unless you have taken precautions to prevent it from happening,
you are in for a nasty shock, which is your content showing up on other sites without your permission.

The EZee Copyright Protector plugin prevents any person with malafide intentions from copying your content. This includes prevention of selection, copying, cutting, pasting, dragging and saving of your content.

On trying any of these operations, a modal popup is presented to the user.
This popup can only be dismissed by clicking the cross on the top right of the popup. The admin of the wordpress site can change the title and text of the copyright notice which appears as a modal popup. This plugin does not claim to support shortcodes from other plugins in its text editor field.

EZee Copyright Protector prevents copying and plagiarism of your original or copyrighted content. It works on Mozilla Firefox, Google Chrome, Microsoft Edge and Opera browsers.

As a user of this plugin, you can change the title of the popup and add text, graphics, audio and video to the body of popup. Only HTML5 based video and audio is allowed. As for images, only formats compatible with wordpress are supported.

This plugin supports internationalization in the frontend as well as wp-admin screens. You can preview this plugin at https://myplugins.net
For more details, go to https://wordpress.org/plugins/ezee-cpyright-protector/

A Simple IP Blocker Plugin for WordPress

In my post here, I had shown how to create a basic “Hello World” plugin that greets the user with a simple “hello world” message in the admin dashboard. Now, let me show you how to create a wordpress plugin that actually does something more useful. In this post, I shall discuss a plugin that restricts the number of login attempts a user can make to login to the admin dashboard of your wordpress site. It makes sense to do this to secure your dashboard from being broken into because an unauthorized user can do just about anything to your site if he manages to access your admin dashboard. This includes defacing your site, deleting your content and God knows what else!

We will call our plugin “Simple IP Blocker”. It will restrict the number of failed login attempts to a fixed number, say three. On the fourth attempt, the user will see an error message “IP address blocked”. The user will be blocked for the remainder of the day on which he made the three unsuccessful login attempts. He will be able to try again on the following day.

Now, let’s get down to the nuts and bolts of the actual plugin. The steps are:

  1. Create a folder for the plugin
  2. Create a PHP file for the code called index.php in that folder.
  3. Create a header for the plugin.
  4. Initialize the required constants.
  5. Create a database table through the activation hook.
  6. Create a function that hooks into wp_login_failed action.
    It logs the failed attempts in the database table and blocks the IP address of the user.
  7. Create a function that displays the logged data from the table.
  8. Hook the function in 7 into the admin_menu action so that the logged data can be displayed.
  9. Create a function that hooks into the login action ‘wp_authenticate’
    that will give an error on trying to login in after ip is blocked.
  10. Activate your plugin and test it.

Let us discuss these steps in detail:

1. Navigate to the wp-content folder in your wordpress installation. Go to the plugins sub folder in it.
Create a folder called “simple-ip-blocker”.

2. Create a blank file titled index.php using your text editor and save it in the folder created in step 1

3. Now, let’s create the header in the index.php file.
Print the following code in after the opening php tag in the index.php file:

<?php 
/ * 
* Plugin Name: My Simple IP Blocker 
*/ 
?>

4. We will initialize the following constants:

<?php 
global $wpdb; 
define("LOGIN_FAILURES", 3); 
define("IPBLOCKER_TABLE", $wpdb-&gt;prefix.'ip_blocker');
?>

5. The above step will create a new line in the plugins section of your wordpress installation and define
the necessary constants.

We need to create a database table to log and count failed login attempts.
We will do this by using the “register_activation_hook” function.
We will create a PHP function named “simple_ip_blocker_create_table”.
In this function, we will write a “CREATE TABLE” MYSQL query. We will execute it by calling $wpdb->query
function on the global $wpdb variable by passing the query in it as a parameter.

We will then pass the “simple_ip_blocker_create_table” function name in the “register_activation_hook” function.

The code is as follows:

<?php 

function simple_ip_blocker_create_table() { 

     global $wpdb, $table_prefix; 
     //mysql query $create_tab_query = 
"CREATE TABLE IF NOT EXISTS `".IPBLOCKER_TABLE."`( 
`id` bigint(20) NOT NULL AUTO_INCREMENT, 
`ip_addr` varchar(20) NOT NULL, `attempts` int(4) NOT NULL, 
`try_date` date NOT NULL, `is_blocked` tinyint(1) NOT NULL, 
PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8"; 

$wpdb->query($create_tab_query); } 

register_activation_hook( __FILE__, 'simple_ip_blocker_create_table' ); ?>

 

Explanation of table structure:

id – PRIMARY KEY
ip_addr – IP address of user trying to log in.
attempts – number of failed logins per user.
try_date – date on which user s trying to log in.
is_blocked – flag too indicate whether the user is blocked or not.

6. Next, we will create a function that will hook into “wp_login_failed”. We will call
this function simple_ip_blocker_reset_bruteforce. This function will fire whenever the
user tries to login but fails. We will maintain a counter in the $_SESSION. Each failed
login will increment the counter. On reaching 3 attempts, the IP blocked flag, is_blocked
will be marked as 1 to indicate that the particular user is blocked for the remainder of
the day. However, the user can try again on the next day. Each attempt is logged in the
database table we created. For the first failed attempt, a new row will be created in the
table. For subsequent attempts, that row will be updated.For the third failed login,
the “is_blocked” field of that row will be updated to 1.

Check out the code below:

<?php 

function simple_ip_blocker_reset_bruteforce() { 

@session_start(); 
global $wpdb, $table_prefix; 
if(!isset($_SESSION["brute_count"])) { 
$_SESSION["brute_count"] = 1; 
$query = "INSERT INTO ".IPBLOCKER_TABLE."(ip_addr, attempts, try_date, is_blocked) VALUES(%s, %d, %s, %d)"; 
$try_date = gmdate("Y-m-d"); 

$wpdb-&gt;query( $wpdb-&gt;prepare( 
        $query, 
        $_SERVER["REMOTE_ADDR"], 
        $_SESSION["brute_count"], 
        $try_date,
        0
      ) );
      
     }
     else
     {
      $_SESSION["brute_count"] = $_SESSION["brute_count"] + 1;
     
      $try_date = gmdate("Y-m-d"); 
       
       //check limit
        
      $check_query = "SELECT attempts FROM ".IPBLOCKER_TABLE." WHERE ip_addr = %s AND try_date = %s";
        
      $attempt_count = $wpdb-&gt;get_var($wpdb-&gt;prepare($check_query, $_SERVER['REMOTE_ADDR'], $try_date ));
    
       if($attempt_count &gt;= LOGIN_FAILURES)
       {
         unset($_SESSION["brute_count"]);
         
         $update_query = "UPDATE ".IPBLOCKER_TABLE." SET is_blocked = 1 WHERE ip_addr = %s AND try_date = %s";
         
         $wpdb-&gt;query($wpdb-&gt;prepare($update_query, $_SERVER['REMOTE_ADDR'], $try_date ));
         
         wp_redirect(site_url('/wp-admin/'));
         
         exit;
         
       }
       else{
         
        $query = "UPDATE ".IPBLOCKER_TABLE." SET attempts = %d WHERE ip_addr = %s AND try_date = %s";
        
        $wpdb-&gt;query( $wpdb-&gt;prepare( 
          $query,  
          $_SESSION["brute_count"],
          $_SERVER['REMOTE_ADDR'],
          $try_date
         ) ); 
         
         
       }
     } 
  }  

  add_action("wp_login_failed", "simple_ip_blocker_reset_bruteforce");
?>

 

7. So far so good. Now we will create a function to display the data of
failed logins in the wordpress backend. We will call this function
“simple_ipblocker_menu_callback”. It will simply query the database table
that we created and list out the information in tabular form.

See the code below:

<?php 

function simple_ipblocker_menu_callback() { 

  global $wpdb; 
  $get_history = "SELECT * FROM ".IPBLOCKER_TABLE; //&quot; LIMIT $offset, 10; 
  
  $history = $wpdb->get_results($get_history, ARRAY_A); ?> 
  <h1>Simple IP Blocker active!</h1> 
  <table border="0" cellspacing="5" cellpadding="5" class="widefat" > 
  <tr>
    <td>S. No.</td>
    <td>IP Address</td>
    <td>Login Attempts</td> 
    <td>Try Date</td>
    <td>Blocked</td> 
  </tr> 
  <?php foreach($history as $row){ ?>
    <tr>
      <td><?php echo $row["id"]; ?></td>
      <td><?php echo $row["ip_addr"]; ?></td>
      <td><?php echo $row["attempts"]; ?></td>
      <td><?php echo $row["try_date"]; ?></td> 
      <td><?php echo ($row["is_blocked"] == 1)?"blocked":"unblocked"; ?></td>
    </tr>
  <?php } ?> 
  </table> 
<?php } ?>

 

8. In this step, we will simply write some code to call the function created above whenever the user
clicks the link for the plugin in the admin sidebar on the left. See below:

<?php 

function simple_ipblocker_menu_add() { 

     add_menu_page("Simple IP Blocker", "Simple IP Blocker", "administrator", "simple-ip-blocker", "simple_ipblocker_menu_callback" ); 

} 

add_action("admin_menu", "simple_ipblocker_menu_add"); 

?>

 

9. The last function to be created is the function that checks if the user’s IP is blocked. If so,
the user will not be able to login to the admin dashboard on that day. However, the user may try
logging in on the next day again.
We will call this function “simple_ip_blocker_check_ip”. In this function, we will check if the user’s
IP address is marked 1 in the is_blocked field. If so, the user will be prevented from logging in.
The function hooks into “wp_authenticate” and hence will fire before every login attempt.

The code is as follows:

<?php 
       function simple_ip_blocker_check_ip() { 
               global $wpdb; 
               $try_date = gmdate("Y-m-d"); 
               $check_IP = "SELECT is_blocked FROM ".IPBLOCKER_TABLE." WHERE ip_addr = %s AND try_date = %s"; 
               $blocked = $wpdb-&gt;get_var( $wpdb-&gt;prepare( 
          $check_IP,  
          $_SERVER['REMOTE_ADDR'],
          $try_date
         ) );
         
    if($blocked == 1)
    {
      wp_die("IP address blocked!");
    }
  }

  add_action("wp_authenticate", "simple_ip_blocker_check_ip");
?>

 

10. Lastly, now that the coding is complete, we need to activate the “Simple IP Blocker” plugin listed in the
wp-admin and test it according to the functionality in the steps above.

The full listing is as follows:

<?php  
/*
* Plugin Name: My Simple IP Blocker
*/  
global $wpdb;

define("LOGIN_FAILURES", 3);
define("IPBLOCKER_TABLE", $wpdb->prefix.'ip_blocker');

function simple_ip_blocker_create_table()
{
  global $wpdb, $table_prefix;
  
  $create_tab_query = "CREATE TABLE IF NOT EXISTS `".IPBLOCKER_TABLE."`(
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `ip_addr` varchar(20) NOT NULL,
    `attempts` int(4) NOT NULL,
    `try_date` date NOT NULL,
    `is_blocked` tinyint(1) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
  
  $wpdb->query($create_tab_query);
}

register_activation_hook( __FILE__, 'simple_ip_blocker_create_table' );


function simple_ip_blocker_reset_bruteforce()
{ 
   @session_start(); 
   
   global $wpdb, $table_prefix;
   
   if(!isset($_SESSION["brute_count"]))
   {
    $_SESSION["brute_count"] = 1; 
    
    $query = "INSERT INTO ".IPBLOCKER_TABLE."(ip_addr, attempts, try_date, is_blocked) VALUES(%s, %d, %s, %d)";
    
    $try_date = gmdate("Y-m-d");
    
    $wpdb->query( $wpdb->prepare( 
      $query, 
      $_SERVER["REMOTE_ADDR"], 
      $_SESSION["brute_count"], 
      $try_date,
      0
    ) );
    
   }
   else
   {
    $_SESSION["brute_count"] = $_SESSION["brute_count"] + 1;
   
    $try_date = gmdate("Y-m-d"); 
     
     //check limit
      
    $check_query = "SELECT attempts FROM ".IPBLOCKER_TABLE." WHERE ip_addr = %s AND try_date = %s";
      
    $attempt_count = $wpdb->get_var($wpdb->prepare($check_query, $_SERVER['REMOTE_ADDR'], $try_date ));
  
     if($attempt_count >= LOGIN_FAILURES)
     {
       unset($_SESSION["brute_count"]);
       
       $update_query = "UPDATE ".IPBLOCKER_TABLE." SET is_blocked = 1 WHERE ip_addr = %s AND try_date = %s";
       
       $wpdb->query($wpdb->prepare($update_query, $_SERVER['REMOTE_ADDR'], $try_date ));
       
       wp_redirect(site_url('/wp-admin/'));
       
       exit;
       
     }
     else{
       
      $query = "UPDATE ".IPBLOCKER_TABLE." SET attempts = %d WHERE ip_addr = %s AND try_date = %s";
      
      $wpdb->query( $wpdb->prepare( 
        $query,  
        $_SESSION["brute_count"],
        $_SERVER['REMOTE_ADDR'],
        $try_date
       ) ); 
       
       
     }
   } 
}  

add_action("wp_login_failed", "simple_ip_blocker_reset_bruteforce");  //plugin admin panel


//pre-authentication

function simple_ip_blocker_check_ip()
{
  //die('done');
  global $wpdb;
  
  $try_date = gmdate("Y-m-d");
  
  $check_IP = "SELECT is_blocked FROM ".IPBLOCKER_TABLE." WHERE ip_addr = %s AND try_date = %s";
  
  $blocked = $wpdb->get_var( $wpdb->prepare( 
        $check_IP,  
        $_SERVER['REMOTE_ADDR'],
        $try_date
       ) );
       
  if($blocked == 1)
  {
    wp_die("IP address blocked!");
  }
}

add_action("wp_authenticate", "simple_ip_blocker_check_ip");

function simple_ipblocker_menu_callback()
{ 

  global $wpdb;
  
  /* $page = (isset($_GET['pg']))?$_GET['pg']:1;
  
  $offset = ($page - 1) * 10; */
  
  $get_history = "SELECT * FROM ".IPBLOCKER_TABLE; //" LIMIT $offset, 10";
  
  $history = $wpdb->get_results($get_history, ARRAY_A);
  
  ?>
  <h1>Simple IP Blocker active!</h1>
    <table border="0" cellspacing="5" cellpadding="5" class="widefat">
      <tr>
        <td>S. No.</td>
        <td>IP Address</td>
        <td>Login Attempts</td>
        <td>Try Date</td>
        <td>Blocked</td>
      </tr>
      <?php 
        foreach($history as $row)
        {
          ?>
          <tr>
            <td><?php echo $row["id"]; ?></td>
            <td><?php echo $row["ip_addr"]; ?></td>
            <td><?php echo $row["attempts"]; ?></td>
            <td><?php echo $row["try_date"]; ?></td>
            <td><?php echo ($row["is_blocked"] == 1)?"blocked":"unblocked"; ?></td>
          </tr>
          <?php 
        }
      ?>
    </table>
  <?php
  
  
  
}  

function simple_ipblocker_menu_add()
{
  add_menu_page("Simple IP Blocker", "Simple IP Blocker", "administrator", "simple-ip-blocker", "simple_ipblocker_menu_callback" );
}  

add_action("admin_menu", "simple_ipblocker_menu_add");
//plugin admin panel end

?>

 

Create a Basic WordPress Plugin From Scratch

In my post here, I had shown you how to create a plugin from existing code. In this post, I will show how to create a basic ‘Hello World’ wordpress plugin from scratch.

First of all, create a ‘hello-world’ folder in your plugins subfolder under the wp-content folder of your wordpress installation.
Then, create a blank file in the ‘hello-world’ folder and name it ‘index.php’.
After that, open your index.php file and put the following code in it:

<?php
/*
Plugin Name: Hello World Plugin
Plugin URI: http://myexamplesite.com/hello-world
Description: A simple WordPress Hello World plugin
Version: 1.0
Author: Author Name 
Author URI: http://myownsite.com
License: GPL2
*/
 ?>

 

This will create a descriptive header in the listing of plugins under the plugins section in wp-admin.

Now create a simple hello_world function in the example index.php page like so:

<?php 

function hello_world()
{
     echo "<h1>Hello World!</h1>"; 
}

?>

But this function needs to be called somewhere to get its output.

To get the output, we need to add the following code to our index.php file.

<?php

function hello_world_callback()
{
      add_menu_page("Hello World", "Hello World", "administrator", "hello-world", "hello_world");
}

add_action("admin_menu", "hello_world_callback");

?>

The add_action function hooks into ‘admin_menu’ to create a link to the plugin in the admin sidebar on the left of your dashboard. On clicking this link, the ‘hello_world’ function is called and the output is displayed on that page.

We will get the output in wp-admin under the “Hello World” tab once we activate the plugin.

So the complete code that needs to be there in the index.php is the following:

<?php
/*
Plugin Name: Hello World Plugin
Plugin URI: http://myexamplesite.com/hello-world
Description: A simple WordPress Hello World plugin
Version: 1.0
Author: Author Name 
Author URI: http://myownsite.com
License: GPL2
*/

function hello_world()
{
     echo "<h1>Hello World!</h1>"; 
} 

function hello_world_callback() { 
     add_menu_page("Hello World", "Hello World", "administrator", "hello-world", "hello_world"); 
} 

add_action("admin_menu", "hello_world_callback");

?>

 

The video above shows the installation and working of the plugin.

Click the following link for a more detailed tutorial.

GUI Tic-Tac-Toe in Java Revisited

In my post found here I discussed a tic-tac-toe game coded in Java which was a version using a Graphical User Interface. The code listing for the classes and interfaces can be found in the same post. In this post I will discuss a Graphical User Interface (GUI) version of the same application with some additional features. The GUI tic-tac-toe app is based on a console application version found here. I shall only provide the changes and additions to the code listings here. Note that I will use the NetBeans IDE.

We will primarily make changes to the AWTBoard class. After making these changes, the user will be asked whether he really wants to quit when a game is in progress. If he clicks Cancel, the game will continue. If he clicks OK, the game will be terminated. If the game is over or not yet begun, the user will be able to stop the game as usual. See the video below the code listing to understand how it works.

package com.tictactoe.components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Dell
 */
public class AWTBoard extends Board implements ActionListener{
    
    private Button[][] buttons = new Button[3][3];
    private Frame f;
    private Label l;
    
    private Move currMove;
    private Game myGame;
    
    public AWTBoard()
    {
        super();
        f = new Frame("Tic-Tac-Toe");
        f.setSize(300, 300);
        
        
        Panel topPanel = new Panel();
        
        topPanel.setLayout(new BorderLayout());
        
        Panel p = new Panel();
        
        GridLayout grd = new GridLayout(3,3);
        
        p.setLayout(grd);
        
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++)
            {
                buttons[i][j] = new Button();
                
                buttons[i][j].addActionListener(this);
                
                p.add(buttons[i][j], i, j); //problem
                
            }
        }
        
        topPanel.add(p, BorderLayout.CENTER);
        
        l = new Label();
        
        topPanel.add(l, BorderLayout.SOUTH);
        
        f.add(topPanel);

        /****Begin changes****/
        f.addWindowListener(new WindowAdapter(){ 
            
            public void windowClosing(WindowEvent e){ 
                
                if(myGame.isGameOver() || Game.getMovesTillNow() == 0)
                {
                    f.dispose(); 
                    System.exit(0);
                }else{
                    int result = JOptionPane.showConfirmDialog((Component) null, "Game in progress. Are you sure you want to quit?",
        "Message", JOptionPane.OK_CANCEL_OPTION);
            
                    if(result == 0)
                    {
                        f.dispose(); 
                        System.exit(0);
                    }
            
                }
            } 
            
        });
        /****End changes****/        

        //f.pack();
        f.setBounds(200, 200, f.getWidth(), f.getHeight());
        
        f.setVisible(true);
    }
    
    public void setGame(Game game)
    {
        myGame = game;
    }
    
    public void display()
    {
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++) 
            {  

                buttons[i][j].setLabel( ""+cells[i][j] );
               
            } 
             
        }  
    }
    
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
            
            buttons[i - 1][j - 1].setLabel(""+mark);
        }
        else
        {
            //System.out.println("INVALID MOVE!\n\n");
            l.setText("INVALID MOVE!");
        }
    }
    
    public Label getStatusLabel()
    {
        return l;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        Button b = (Button)e.getSource();
        
        for(int i = 0 ; i &lt; 3; i++ )
        {
            for(int j = 0; j &lt; 3; j++)
            {
                if(b == buttons[i][j])
                {
                    currMove = new Move();
                    currMove.setRow(i + 1);
                    currMove.setColumn(j + 1);
                    currMove.setIsValid(true);
                }
            }
        }
        
        
        synchronized(this)
        {
            this.notify();
        }
        
    }
    
    public Move getCurrMove()
    {
        return currMove;
    }
}

 

An elementary security plugin protecting admin dashboard

In this post, I shall discuss an elementary plugin that can protect your wordpress admin dashboard from frequent incorrect login attempts. The plugin redirects the user to the home page after a predetermined number of incorrect logins, three for example. However, if the user enters the correct password after three wrong attempts, the user is logged in to the dashboard. The code hooks into the wp_login_failed action hook and uses a counter variable stored in the session to keep track of the number of login attempts. The plugin backend only tells the administrator that the plugin is active (See image above). The code listing is given below:

<?php 
/* 
* Plugin Name: My Simple Security 
*/ 

function simple_security_reset_bruteforce($username){ 

  @session_start(); 
  if(!isset($_SESSION["brute_count"])) { 
    $_SESSION["brute_count"] = 1; 
  } else { 
    $_SESSION["brute_count"] = $_SESSION["brute_count"] + 1; 
    if($_SESSION["brute_count"] > 3) { 
    
      unset($_SESSION["brute_count"]); 
      wp_redirect(site_url()); 
      exit; 
    } 
  } 
} 

add_action("wp_login_failed", "simple_security_reset_bruteforce"); //plugin admin panel 

function simple_security_menu_callback() { 

  echo "<h1>Simple Security active!</h1>"; 
  
} 

function simple_security_menu_add() { 

  add_menu_page("Simple Security", "Simple Security", "administrator", "simple-security", "simple_security_menu_callback" ); 
  
} 

add_action("admin_menu", "simple_security_menu_add"); //plugin admin panel end 

?>

 

A recording of the plugin is given below:

 

To turn this into a plugin and use it in your wordpress:

  1. Create an empty folder in your plugins folder.
  2. Give it an easily understandable name.
  3. Then, create a blank file in any text editor.
  4. Copy the code from the code listing here, including the “<?php” at the start.
  5. Paste the code copied above in your blank file.
  6. Save this file as index.php in your empty folder created above.
  7. Go to the wp-admin and activate the plugin.
  8. Logout from admin and try logging in to wp-admin repeatedly with the wrong password.

It should work as shown in the video above.

Deploying Tic-Tac-Toe Java GUI app

In this post, we deploy our tic-tac-toe Java GUI app so that it can be invoked either by double-clicking it or through the command line. Note: These steps below were performed on a Windows system.

  1. First, assemble all the Java source files in a folder of your choice.
  2. Open your Command Prompt.
  3. Navigate to your chosen folder in the Command Prompt using the cd command. e.g. cd Java\progs\TicTacToeGUIDemo
  4. Compile all the Java source files with javac compiler. e.g. javac -d . *.java.
    * means all Java files in the current folder.
  5. After successful compilation, the root package of your app will appear as a folder. In our case it’s ‘com’.
  6. Now, we need to create a manifest file to help us create a java archive file or JAR file.
    This file can be used to launch the app just by double-clicking on it.
    It can also be invoked using the java command from Command Prompt. e.g. java -jar TicTacToe.jar
  7. Open Notepad and create a blank new file called manifest.mf
  8. In this file, type:Main-Class: com.tictactoe.gameplay.AWTTicTacToe. This is the main class name, qualified by the full package name.
  9. After step 8, be sure to add a blank new line in the text file manifest.mf
  10. Save the manifest file and close it.
  11. Go back to your Command Prompt.
    Type the following: jar cvmf manifest.mf  –jar file name–  –root package folder name–
    e.g. jar cvmf manifest.mf TicTacToe.jar com
  12. You will get some verbose output from the jar command. To open your jar file, you can follow either of the steps in step 6 above.

View the video below to see it all in action.

Coding Tic-Tac-Toe with a Graphical User Interface

In my post found here I discussed a tic-tac-toe game coded in Java which was a console application.
The code listing for the classes and interfaces can be found in the same post. In this post I will discuss a Graphical User Interface (GUI) version of the same application. I shall only provide the changes and additions to the code listings here. Note that I will use the NetBeans IDE.

First I shall discuss the changes to the Board class. Since we shall use inheritance, we shall change the accessibility of the cells from private to protected. This is the only change required. See the listing below.

package com.tictactoe.components;  
/**
 *
 * @author pratyush
 */
public class Board { 
 
    protected static char[][] cells; //change to Board class for inheritance
    
    public Board()
    {
        cells = new char[3][3]; 
        cells[0] = new char[]{' ', ' ', ' '}; 
        cells[1] = new char[]{' ', ' ', ' '}; 
        cells[2] = new char[]{' ', ' ', ' '};
    }
    
    public void display()
    {
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++){ 
        if(cells[i][j] == ' ') 
        { 
          System.out.print((i+1)+", "+(j+1)+"\t"); 
        } 
        else{
          System.out.print(cells[i][j]+"\t"); 
        } 
      } 
      
      System.out.print("\n\n"); 
    } 
  } 
  
  public Move checkAvailable(Move move){ 
  
    int i = move.getRow(); 
    int j = move.getColumn(); 
    boolean boundValid = ((i >= 1 && i<= 3) && ( j >= 1 && j <= 3));
    
        boolean charValid = false; 
        if(boundValid)
        {
            charValid = (cells[i - 1][j - 1] == ' ');
        } 
        boolean netValid = (boundValid && charValid); 
        move.setIsValid(netValid); 
        return move;
    }
    
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
        }
        else
        {
            System.out.println("INVALID MOVE!\n\n");
        }
    } 
}

 

AWTBoard

Among the additions, we shall make a new Board class called AWTBoard which shall inherit all the properties of the Board class. However it shall add the functionalities of GUI from the Java Abstract Windowing Toolkit or AWT packages. The listing of this new class is given below. The AWT version of the Board will be a frame with a 3 X 3 grid of buttons with a status label at the bottom to display all the messages which were being outputted to the console in the console version of the application. The players will play their turns by clicking buttons in the grid.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tictactoe.components;

import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Dell
 */
public class AWTBoard extends Board implements ActionListener{
    
    private Button[][] buttons = new Button[3][3];
    private Frame f;
    private Label l;
    
    private Move currMove;
    
    public AWTBoard()
    {
        super();
        f = new Frame("Tic-Tac-Toe");
        f.setSize(300, 300);
        
        
        Panel topPanel = new Panel();
        
        topPanel.setLayout(new BorderLayout());
        
        Panel p = new Panel();
        
        GridLayout grd = new GridLayout(3,3);
        
        p.setLayout(grd);
        
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++)
            {
                buttons[i][j] = new Button();
                
                buttons[i][j].addActionListener(this);
                
                p.add(buttons[i][j], i, j);
                
            }
        }
        
        topPanel.add(p, BorderLayout.CENTER);
        
        l = new Label();
        
        topPanel.add(l, BorderLayout.SOUTH);
        
        f.add(topPanel);
        
        f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.dispose(); System.exit(0);} });
        
        
        f.setBounds(200, 200, f.getWidth(), f.getHeight());
        
        f.setVisible(true);
    }
    
    public void display()
    {
        for(int i = 0; i &lt; 3; i++)
        {
            for(int j = 0; j &lt; 3; j++) 
            {  

                buttons[i][j].setLabel( ""+cells[i][j] );
               
            } 
             
        }  
    }
    
    public void markCell(Move move, char mark)
    {
        if(move.isIsValid())
        {
            int i = move.getRow(); 
            int j = move.getColumn(); 
            cells[i - 1][j - 1] = mark;
            
            buttons[i - 1][j - 1].setLabel(""+mark);
        }
        else
        {
            
            l.setText("INVALID MOVE!");
        }
    }
    
    public Label getStatusLabel()
    {
        return l;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        Button b = (Button)e.getSource();
        
        for(int i = 0 ; i &lt; 3; i++ )
        {
            for(int j = 0; j &lt; 3; j++)
            {
                if(b == buttons[i][j])
                {
                    currMove = new Move();
                    currMove.setRow(i + 1);
                    currMove.setColumn(j + 1);
                    currMove.setIsValid(true);
                }
            }
        }
        
        
        synchronized(this)
        {
            this.notify();
        }
        
    }
    
    public Move getCurrMove()
    {
        return currMove;
    }
}

 

First, let us see the constructor of the new Board class AWTBoard. We create a Frame which is basically a window. We then create a Panel and add it to the content pane of the window created previously. We create a Border Layout for the previously added Panel with a top part and a bottom part. To the bottom part, we add a label to display any status messages needed to be displayed. To the top part of the Panel, we create a Panel with a Grid Layout to add a 3 X 3 Grid of Buttons and then add this panel to the main Panel. The Players play the game by taking turns to click the buttons in this grid.

We override the display method of the Board class to mark the buttons of the grid instead of outputting them to the console. Likewise, we override all those other methods which output to the console. The new method added is the actionPerformed method which is basically an event handler that tells Java what to do when the buttons in the 3 x 3 grid are clicked. getStatusLabel and getCurrMove are helper methods to enable passing data in and out of GUI Board.

AWTTicTacToe

Apart from the GUI Board, we need to introduce another class called AWTTicTacToe which will contain changes to invoke the GUI Board instead of the console.

package com.tictactoe.gameplay;

import com.tictactoe.components.*;

/**
 *
 * @author pratyush
 */
public class AWTTicTacToe {  /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        // TODO code application logic here 
        Game game = new Game(); 
        Player player1 = new Player(); 
        player1.setPlayerNum(1); 
        player1.setPlayerMark('X'); 
        player1.setGame(game); 
        Player player2 = new Player(); 
        player2.setPlayerNum(2); 
        player2.setPlayerMark('0'); 
        player2.setGame(game);
        
        //
        AWTBoard board = new AWTBoard();
        board.display();
        
        /*******/ 
        game.setWhoseTurn(player1); 
        game.setGameOver(false); 
        boolean status = game.isGameOver(); 
        // 
        do
        {
            int movesNow = Game.getMovesTillNow(); 
            if(movesNow == 9)
            {
                if(game.getWinner() == null)
                {
                    //
                    board.getStatusLabel().setText("TIE!\n");
                    break;
                }
            } 
            int turn = game.getWhoseTurn(); 
            //
            
            board.getStatusLabel().setText("PLAYER "+turn+" click a button\n"); 
            
            //
            try{
                synchronized(board)
                {
                    board.wait();
                }
            }
            catch(InterruptedException ie)
            {
                ie.printStackTrace();
            }
            
            Move move = board.getCurrMove();
            
            switch(turn)
            {
                case 1:
                    player1.makeMove(board, move); 
                    if(game.checkWinningMoves(player1))
                    {
                        game.setWinner(player1); 
                        //
                        board.getStatusLabel().setText("PLAYER 1 WINS!");
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow(); 
                            //
                            
                            board.getStatusLabel().setText("MOVE NUMBER "+movesNow+"\n");
                            game.setWhoseTurn(player2);
                        }
                    }
                break;
                case 2:
                    player2.makeMove(board, move); 
                    if(game.checkWinningMoves(player2))
                    {
                        game.setWinner(player2); 
                        //
                        
                        board.getStatusLabel().setText("PLAYER 2 WINS!");
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow(); 
                            // 
                            board.getStatusLabel().setText("MOVE NUMBER "+movesNow+"\n");
                            game.setWhoseTurn(player1);
                        }
                    }
                break;
                default:
                    //
                    board.getStatusLabel().setText("ERROR!\n");
                break;
            } 
            status = game.isGameOver();
        }
        while(!status); 
    } 
}

 

AWTTicTacToe is the main class containing the main method. It invokes the AWTBoard class instead of the Board class. It will not contain any statements to output to the console, although it may do so for debugging. To see the results of the invocation of this main class see the video below.

 

This GUI implementation of tic-tac-toe in Java is not perfect by any means. There is scope for improvement and new features can be added. Meanwhile, you can run this code and enjoy!

Convert Very Large Decimal Integers to Base 62 And Back

The other day I was trying to devise a way of converting arbitrarily large base 10 integers to the order of a billion billion to base 62 and did not get much help from the web. So I devised my own solution using the BC Math extension which ships with PHP by default. So, this solution works upto PHP_INT_MAX which is 9223372036854775807 for 64-bit platform. It also works upto 99 trillion on 32-bit-platform. I used an array as a stack to store successive remainders of division by the base, which is 62, and at the end popped them one by one from the stack to generate the converted base 62 number. The converse, multiplication by successive powers of 62 and addition of those powers helped to convert base 62 back to base 10.

First I used PHP OOPS to create a class called MyBase62Converter. The code is given below:

class MyBase62Converter
{
	private $base62StrVar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	private $baseInt = 62;
	
	function encode($decNum)
	{
		$base = $this->baseInt;
		
		$myStack = array();

		$remainder = 0;

		$base62Str = $this->base62StrVar;

		do 
		{
			
			$remainder = bcmod($decNum, $base);
			
			array_push($myStack, $remainder);
			
			$decNum = bcdiv(bcsub($decNum, $remainder), $base); 
			
		}
		while($decNum > 0);
		
		$binNum = '';

		while(count($myStack) > 0)
		{
			
			$binNum.= substr($base62Str, array_pop($myStack), 1);
		}

		return (strlen($binNum) >= 6)?$binNum: str_pad($binNum, 6, 0, STR_PAD_LEFT);
	}
	
	function decode($baseNum)
	{
		
		$base62Str = $this->base62StrVar;
		
		$base = $this->baseInt;

		$hashLen = strlen($baseNum);
		
		$decNum = '';
		
		for($i = 0; $i < $hashLen; $i++)
		{
			$decNum = bcadd($decNum, bcmul(bcpow($base, $i), strpos($base62Str , substr($baseNum, $hashLen - $i - 1, 1))));
		}
		
		return $decNum;
	}
	
}

Using the two simple encode and decode functions we can convert base 10 integers to base 62 representation and vice versa.

You can see the example below:

require_once("lib/class.MyBase62Converter.php"); 

$encoder = new MyBase62Converter();

echo "<br />";

echo PHP_INT_MAX." encoded is ".$encoded = $encoder->encode(PHP_INT_MAX);

echo "<br />";

echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);

echo "<br />";
 
$largeNum = bcpow(62, 6) - 1; 

echo $largeNum." encoded is ".$encoded = $encoder->encode($largeNum); 

echo "<br />"; 

echo $encoded." decoded is ".$decoded = $encoder->decode($encoded); 

echo "<br />"; 

$veryLargeNum = bcpow(62, 7) - 1; //getting 0 due to num expressed in scientfic exponential notation 2.183401055849E+14 

echo $veryLargeNum." encoded is ".$encoded = $encoder->encode($veryLargeNum); 

echo "<br />"; 

echo $encoded." decoded is ".$decoded = $encoder->decode($encoded); 

echo "<br  />"; 

$max_num = 99999999999999; 

echo $max_num." encoded is ".$encoded = $encoder->encode($max_num , 62); //99999999999999 

echo "<br />"; 

echo $encoded." decoded is ".$decoded = $encoder->decode($encoded); 

echo "<br />"; 

$new_num = 63; 

echo $new_num." encoded is ".$encoded = $encoder->encode($new_num , 62);  

echo "<br />"; 

echo $encoded." decoded is ".$decoded = $encoder->decode($encoded);

The result of code execution is given below:

Simple File Searcher App: An Exercise in Concurrency

Here is a simple app for searching files on a computer. The app uses a recursive algorithm to compare names of files and folders with the search keyword and returns all the matches.

The procedure is as follows:

1.	list the roots of the filesystem
2.	for each root listed in step1:
	a.	list folders and files
	b.	for each file in a., compare keyword with file name.  
                If matched, list this file in results 
	c.	for each folder in a, 
		i.  compare keyword with folder name.  If matched, list this folder in results 
		ii. repeat 2a. for each folder
	d.	repeat a. to c. until no subfolders are found

The Java version is given below:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.filesearch.components;

import java.io.*;
import java.io.File.*;
import java.util.concurrent.*;
import java.util.Scanner;
/**
 *
 * @author Dell
 */
public class FileSearcherComponent implements Runnable{
    
    static String fileKeyword;
  
    static long count = 0;
  
    static File logFile;
  
    static BufferedWriter bw;
  
    private void logToFile(String path)
    {
  try{
    
    bw.write(path, 0, path.length());
    bw.newLine();
    
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    }
    
    @Override
    public void run()
    {
  try{
      
    bw = new BufferedWriter(new FileWriter(logFile, true));
      
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    
  long currTime = System.currentTimeMillis();
    
        File[] roots = File.listRoots(); //get drives
        
        for(File root:roots)
        {
            
            File rootFile = new File(root.getPath());
            
            traverseRoots(rootFile);
        }
    
  long endTime = System.currentTimeMillis();
    
  double seconds = (endTime - currTime) / 1000;
    
  System.out.printf("\n %d results in %f seconds.\n", count, seconds);
    
  try{
    
    bw.flush();
    
    bw.close();
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    }
    
    private void traverseRoots(File root)
    {
        if(root.isDirectory())
        {
            
            String name = root.getName();

            if(name.contains(fileKeyword))
            {
                System.out.println(root.getAbsolutePath());
        
    logToFile(root.getAbsolutePath());
        
    count++;
            }
            
            File[] rootFolders = root.listFiles();

            if(rootFolders != null &amp;&amp; rootFolders.length &gt; 0){

                for(File rootFolder: rootFolders)
                {
                    if(rootFolder.isDirectory())
                    {
                        
                        String name1 = rootFolder.getName();

                        if(name1.contains(fileKeyword))
                        {
                            System.out.println(rootFolder.getAbsolutePath());
              
          logToFile(rootFolder.getAbsolutePath());
              
          count++;
                        }
                        
                        traverseRoots(rootFolder);
                    }
                    else if(rootFolder.isFile())
                    {
                        String name2 = rootFolder.getName();

                        if(name2.contains(fileKeyword))
                        {
                            System.out.println(rootFolder.getAbsolutePath());
              
          logToFile(rootFolder.getAbsolutePath());
              
          count++;
                        }
                    }
                    
                }
            }
        }
        else if(root.isFile())
        {
            String name3 = root.getName();

            if(name3.contains(fileKeyword))
            {
                System.out.println(root.getAbsolutePath());
        
    logToFile(root.getAbsolutePath());
        
    count++;
        
            }
        }
       
    }
    
    public static void main(String[] args)
    {
        FileSearcherComponent f = new FileSearcherComponent();
        
        ExecutorService es = Executors.newSingleThreadExecutor();
    
  logFile = new File("LOG.txt");
    
  if(logFile.exists())
  {
    logFile.delete();
  }
    
  try{
    
    logFile.createNewFile();
  }
  catch(IOException i)
  {
    i.printStackTrace();
  }
    
        System.out.print("Enter search keyword: ");
        
        fileKeyword  = new Scanner(System.in).nextLine();
    
        es.execute(f);	
    
    
    }
    
}

 

Copy, compile, run and ENJOY!

Coding a game of tic-tac-toe using 3 different languages

I used a brute force logic to develop an algorithm for tic-tac-toe.
Firstly, there are only 8 triads of winning points or moves: three vertical, three horizontal and 2 diagonals.
Players play taking turns or making moves alternately. Player 1 makes a maximum of 5 moves and player
2 makes a maximum of 4 moves because there are a maximum of 9 moves to be made and player 1 begins the game.

Furthermore, player 1 has already made 2 moves before getting his first chance to form a winning line.
Players take turns alternately which means that when player 1 gets his first chance to form a line,
player 2 has already made 2 moves of his own. So player 1 can only form a line from move 5. Player 2
can only form his first line at move 6. Player 1 gets his second chance to win at move 7 and his third
and last chance at move 9. Similarly, player 2 gets his second and last chance at move 8.

Every time there is a chance to form a winning line, all possible permutations and combinations of
the current players moves, taken three at a time, are compared with all 8 winning triads. If a match is found,
the current player wins. If player 1 forms a line first, player 2 does not get a chance to continue the game.

The game has 3 versions: PHP, Java and C#

PHP Version

It can be played via the command-line executable of your PHP installation.

In the game of tic-tac-toe there is a 3X3 grid or board.The game is for two players. The players 1 and 2 take turns to mark the cells of the grid with their mark which is a zero or a cross.

The first player to complete a vertical, horizontal or diagonal triad of zeroes or crosses, wins.

In this implementation, I have used the OOPs or Object Oriented Programming features of PHP.

I have divided the functionality and roles or responsibilities between various classes as follows.

Player makes Move on Board to play Game. So the classes are:

  1. Move
  2. Board
  3. Player
  4. Game

First we discuss the move class:

Move:

Move made by either of the players.

class Move
{
  private $row; // between 1 and 3
  private $column; //between 1 and 3 on the 3x3 board
  private $isValid; //whether the move is valid or not
  
  //getters and setters for the properties
  public function setRow($i)
  {
    $this->row = $i;
  }
  
  public function setColumn($j)
  {
    $this->column = $j;
  }
  
  public function setValid($valid)
  {
    $this->isValid = $valid;
  }
  
  public function getRow()
  {
    return $this->row;
  }
  
  public function getColumn()
  {
    return $this->column;
  }
  
  public function getValid()
  {
    return $this->isValid;
  }
}

 

Then comes the Board class where the moves are made.

Board:

The board on which the game is played.

class Board
{
  private $cells = array(); //the 3x3 grid of cells
  
  public function __construct()
  {
    $row1 = array(" ", " ", " ");
    
    $row2 = array(" ", " ", " ");
    
    $row3 = array(" ", " ", " ");
    
    $this->cells[0] = $row1;
    
    $this->cells[1] = $row2;
    
    $this->cells[2] = $row3;
  }
  
  //display the board with moves made so far
  public function display()
  {
    for($i = 0; $i < 3; $i++)
    {
      for($j = 0; $j < 3; $j++) { if($this->cells[$i][$j] == " ")
        {
          echo ($i + 1).", ".($j + 1)."\t";
        }
        else
        {
          echo $this->cells[$i][$j]."\t";
        }
      }
      
      echo "\n\n";
    }
  }
  
  //check if a cell is already occupied or not
  public function checkAvailable($move)
  {
    $i = $move->getRow();
    $j = $move->getColumn();
    
    $valid1 = (($i >= 1 && $i <= 3) && ($j >= 1 && $j <= 3));
    
    if($valid1)
    {
      $valid2 = ($this->cells[$i - 1][$j - 1] == " ");
    }
    else
    {
      $valid2 = false;
    } 

                $valid3 = ($valid1 && $valid2); 

                $move->setValid($valid3);
    
    return $move;
  }

  //put a cross or zero in a cell if the move is valid
  public function markCell($move, $mark)
  {
    if($move->getValid())
    {
      $i = $move->getRow();
      $j = $move->getColumn();
      
      $this->cells[$i - 1][$j - 1] = $mark;
    }
    else
    {
      echo "INVALID MOVE!\n\n";
    }
  }
}

 

The Player class denotes the players who make moves on the board.

Player:

Either of the two players.

class Player
{
  private $playerNum;  //player 1 or 2
  private $playerMark; // '0' or 'X'
  private $winnerOrLoser;
  private $moves = array(); //list of moves made by player
  private $g; //the game being played
  
  //getters and setters
  public function setPlayerNum($num)
  {
    $this->playerNum = $num;
  }

  public function setPlayerMark($mark)
  {
    $this->playerMark = $mark;
  }
  
  public function setPlayerWinnerLoser($winnerOrLoser)
  {
    $this->winnerOrLoser = $winnerOrLoser;
  }
  
  public function getPlayerNum()
  {
    return $this->playerNum;
  }
  
  public function getPlayerMark()
  {
    return $this->playerMark;
  }
  
  public function getPlayerWinLose()
  {
    return $this->winnerOrLoser;
  }
  
  public function getPlayerMoves()
  {
    return $this->moves;
  }
  
  //the player can put x or 0 by choosing the cell (row and column)
  public function makeMove($board, $move)
  {
    $move = $board->checkAvailable($move);
    
    $mark = $this->getPlayerMark();
    $board->markCell($move, $mark);
      
    if($move->getValid())
    {	
      $this->addMove($move);
      
      $this->getGame()->setMovesTillNow();
    }
    
    $board->display();
  }
  
  //if move is valid, add the move to the player's list of moves
  public function addMove($move)
  {
    $this->moves[] = $move;
  }
  
  public function setGame($game)
  {
    $this->g = $game;
    
  }
  
  public function getGame()
  {
    return $this->g;
  }
}

 

The Game class is the functionality of the game with all its rules.

Game:

The game being played.

class Game
{
  private $whoseTurn; //player 1 or 2
  private static $movesTillNow = 0; //how many moves till now
  private $gameOver; //is game over yet?
  private $winningMoves; //list of combination of moves which can win the game
  private $winner = null;
  
  private $winning_moves;
  
  public function __construct()
  {
    //winning moves

    $this->winning_moves = array();
    //row wise
    
    $move01 = new Move();
    $move01->setRow(1);
    $move01->setColumn(1);
    
    $move02 = new Move();
    $move02->setRow(1);
    $move02->setColumn(2);
    
    $move03 = new Move();
    $move03->setRow(1);
    $move03->setColumn(3);
    
    $move0 = array($move01, $move02, $move03);

    $this->winning_moves[0] = $move0;
    
    $move11 = new Move();
    $move11->setRow(2);
    $move11->setColumn(1);
    
    $move12 = new Move();
    $move12->setRow(2);
    $move12->setColumn(2);
    
    $move13 = new Move();
    $move13->setRow(2);
    $move13->setColumn(3);
    
    $move1 = array($move11, $move12, $move13);

    $this->winning_moves[1] = $move1;
    
    $move21 = new Move();
    $move21->setRow(3);
    $move21->setColumn(1);
    
    $move22 = new Move();
    $move22->setRow(3);
    $move22->setColumn(2);
    
    $move23 = new Move();
    $move23->setRow(3);
    $move23->setColumn(3);
    

    $move2 = array($move21, $move22, $move23);

    $this->winning_moves[2] = $move2;

    //column wise
    
    $move31 = new Move();
    $move31->setRow(1);
    $move31->setColumn(1);
    
    $move32 = new Move();
    $move32->setRow(2);
    $move32->setColumn(1);
    
    $move33 = new Move();
    $move33->setRow(3);
    $move33->setColumn(1);
    
    $move3 = array($move31, $move32, $move33);

    $this->winning_moves[3] = $move3;
    
    $move41 = new Move();
    $move41->setRow(1);
    $move41->setColumn(2);
    
    $move42 = new Move();
    $move42->setRow(2);
    $move42->setColumn(2);
    
    $move43 = new Move();
    $move43->setRow(3);
    $move43->setColumn(2);
    
    $move4 = array($move41, $move42, $move43);

    $this->winning_moves[4] = $move4;
    
    $move51 = new Move();
    $move51->setRow(1);
    $move51->setColumn(3);
    
    $move52 = new Move();
    $move52->setRow(2);
    $move52->setColumn(3);
    
    $move53 = new Move();
    $move53->setRow(3);
    $move53->setColumn(3);
    
    $move5 = array($move51, $move52, $move53);

    $this->winning_moves[5] = $move5;

    //diagonally
    
    $move61 = new Move();
    $move61->setRow(1);
    $move61->setColumn(1);
    
    $move62 = new Move();
    $move62->setRow(2);
    $move62->setColumn(2);
    
    $move63 = new Move();
    $move63->setRow(3);
    $move63->setColumn(3);
    
    $move6 = array($move61, $move62, $move63);

    $this->winning_moves[6] = $move6;
    
    $move71 = new Move();
    $move71->setRow(1);
    $move71->setColumn(3);
    
    $move72 = new Move();
    $move72->setRow(2);
    $move72->setColumn(2);
    
    $move73 = new Move();
    $move73->setRow(3);
    $move73->setColumn(1);
    
    $move7 = array($move71, $move72, $move73);

    $this->winning_moves[7] = $move7;
  }
  
  //getters and setters
  public function getWhoseTurn()
  {
    return $this->whoseTurn;
  }	
  
  public function setWhoseTurn($player)
  {
    $this->whoseTurn = $player->getPlayerNum();
  }
  
  public function getGameOver()
  {
    return $this->gameOver;
  }	
  
  public function setGameOver($gameOver)
  {
    $this->gameOver = $gameOver;
  }
  
  public function setMovesTillNow()
  {
    self::$movesTillNow++;
  }
  
  public function getMovesTillNow()
  {
    return self::$movesTillNow;
  }
  
  public function setWinner($winner)
  {
    $this->winner = $winner;
  }
  
  public function getWinner()
  {
    return $this->winner;
  }
  
  //check if winning moves have been made by either player
  public function checkWinningMoves($player)
  {
    
    $movesTillNow = $this->getMovesTillNow();
    
    $whoseTurn = $this->getWhoseTurn();
    
    $playerMoves = $player->getPlayerMoves();
    
    $moves = $movesTillNow;
    
    $winning_moves1 = $this->winning_moves;
    
    switch($whoseTurn)
    {
      case 1:
        
        if($moves < 5)
        {
          return false;
        }
        else
        {
          
          if($moves == 5)
          {
          
            $player1_set1 = array($playerMoves[0], $playerMoves[1], $playerMoves[2]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player1_set1, $winning_moves1[$i]))
              {
                return true;
              }
            }
            
          }
          
          if($moves == 7)
          {
            $player1_set2 = array($playerMoves[0], $playerMoves[1], $playerMoves[3]);
            
            $player1_set4 = array($playerMoves[0], $playerMoves[2], $playerMoves[3]);
            
            $player1_set7 = array($playerMoves[1], $playerMoves[2], $playerMoves[3]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player1_set2, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set4, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set7, $winning_moves1[$i]) )
              {
                return true;
              }
            }
          }
          
          if($moves == 9)
          {
          
            $player1_set3 = array($playerMoves[0], $playerMoves[1], $playerMoves[4]);
            
            $player1_set5 = array($playerMoves[0], $playerMoves[2], $playerMoves[4]);
            
            $player1_set6 = array($playerMoves[0], $playerMoves[3], $playerMoves[4]);
            
            $player1_set8 = array($playerMoves[1], $playerMoves[2], $playerMoves[4]);
            
            $player1_set9 = array($playerMoves[1], $playerMoves[3], $playerMoves[4]);
            
            $player1_set10 = array($playerMoves[2], $playerMoves[3], $playerMoves[4]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player1_set3, $winning_moves1[$i]) ||
              $this->checkSubset($player1_set5, $winning_moves1[$i]) || $this->checkSubset($player1_set6, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set8, $winning_moves1[$i]) || $this->checkSubset($player1_set9, $winning_moves1[$i]) || 
              $this->checkSubset($player1_set10, $winning_moves1[$i]))
              {
                return true;
              }
            }
            
          }
          
          return false;
           
          
        
        }
      break;
      case 2:
        if($moves < 6)
        {
          return false;
        }
        else
        {
          if($moves == 6)
          {
            $player2_set1 = array($playerMoves[0], $playerMoves[1], $playerMoves[2]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player2_set1, $winning_moves1[$i]))
              {
                return true;
              }
            }
          }
          
          if($moves == 8)
          {
    
            $player2_set2 = array($playerMoves[0], $playerMoves[1], $playerMoves[3]);
            
            $player2_set3 = array($playerMoves[0], $playerMoves[2], $playerMoves[3]);
            
            $player2_set4 = array($playerMoves[1], $playerMoves[2], $playerMoves[3]);
            
            for($i = 0; $i < 8; $i++) { if($this->checkSubset($player2_set2, $winning_moves1[$i]) || $this->checkSubset($player2_set3, $winning_moves1[$i]) ||
              $this->checkSubset($player2_set4, $winning_moves1[$i]))
              {
                return true;
              }
            }
          }
          
          
          
          return false;
        
        }
      break;
      default:
        return false;
    }
    
    
  }
  
  //check if player moves match the winning sequence
  private function checkSubset($subset, $winning_row)
  {
    
    $count = 0;
    for($i = 0; $i < 3; $i++)
    {
      
      for($j = 0; $j < 3; $j++) { if($subset[$i]->getRow() == $winning_row[$j]->getRow() && $subset[$i]->getColumn() == $winning_row[$j]->getColumn())
        {
          $count++;
        }
      }
      
    }
    return ($count == 3);
    
  }
}

 

Putting it all together.

<?php

require("includes/move.php");  //the Move class

require("includes/player.php"); //the Player class

require("includes/board.php"); //the Board class

require("includes/game.php"); //the Game class

function main() //where all the action is
{
  $game = new Game();
  
  $player1 = new Player();
  
  $player1->setPlayerNum(1);
  
  $player1->setPlayerMark('X');
  
  $player1->setGame($game);
  
  $player2 = new Player();
  
  $player2->setPlayerNum(2);
  
  $player2->setPlayerMark('0');
  
  $player2->setGame($game);
  
  $b = new Board();

  $b->display();
  
  $game->setWhoseTurn($player1);
  
  $game->setGameOver(false);
  
  $status = $game->getGameOver();
  
  do
  {
    $movesNow = $game->getMovesTillNow();
    
    if($movesNow == 9)
    {
      
      if($game->getWinner() == null)
      {
        echo "TIE!";
        break;
      }
      
    }
    
    $turn = $game->getWhoseTurn();
    
    echo "PLAYER $turn:\n\n";
    
    $row = 0; $column = 0;
    
    echo "Enter row: ";
    
    trim(fscanf(STDIN, "%d\n", $row));
    
    echo "Enter column: ";
    
    trim(fscanf(STDIN, "%d\n", $column));
    
    echo "\n";
    
    $move = new Move();
    
    $move->setRow($row);
    
    $move->setColumn($column);
    
    switch($turn)
    {
      case 1:
        $player1->makeMove($b, $move);
        
        if($game->checkWinningMoves($player1))
        {
          
          $game->setWinner($player1);
          
          echo "PLAYER 1 WINS!\n\n";
          
          $game->setGameOver(true);
        }
        else
        {
          if($move->getValid())
          {
            $movesNow = $game->getMovesTillNow();
    
            echo "MOVE NUMBER: $movesNow\n\n";
            
            $game->setWhoseTurn($player2);
            
          }
        }
      break;
      case 2:
        $player2->makeMove($b, $move);
        
        if($game->checkWinningMoves($player2))
        {
          
          $game->setWinner($player2);
          
          echo "PLAYER 2 WINS!\n\n";
          
          $game->setGameOver(true);
        }
        else
        {
          if($move->getValid())
          {
            $movesNow = $game->getMovesTillNow();
    
            echo "MOVE NUMBER: $movesNow\n\n";
            
            $game->setWhoseTurn($player1);
          }
        }
      break;
      default:
        echo "ERROR!\n\n";
      break;
    }
    
    $status = $game->getGameOver();
    
  }while(!$status);
  
}

main(); //invoking the game play

?>

 

 

This implementation is not perfect by any means. Any suggestions for improvement are welcome. Screenshots given below:

Java Version

Here is an elementary tic-tac-toe or noughts and crosses game made in Java.
It can be played via the java interpreter from the command line. In the game of tic-tac-toe there is a 3X3 grid or board.The game is for two players. The players take turns to mark the cells of the grid with their mark which is a zero or a cross.

The first player to complete a vertical, horizontal or diagonal triad of zeroes or crosses, wins.

The PHP version can be found here

Player makes Move on Board to play Game. So the classes are:

  1. Move
  2. Board
  3. Player
  4. Game
  5. TicTacToe

There is one new class “TicTacToe” because Java needs a main class having the “main” method implemented in it.

The first four classes are in the package named “com.tictactoe.components” , while the main class is in the package named “com.tictactoe”.

First we discuss the move class:

Move:

Move made by either of the players.

package com.tictactoe.components;

/**
 *
 * @author pratyush
 */
public class Move {
    
    private int row, column;
    private boolean isValid;

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public boolean isIsValid() {
        return isValid;
    }

    public void setIsValid(boolean isValid) {
        this.isValid = isValid;
    }
    
}

 

Then comes the Board class where the moves are made.

Board:

The board on which the game is played.

package com.tictactoe.components;

/**
 *
 * @author pratyush
 */
public class Board{
    
    private static char[][] cells;
    
    public Board()
    {
        cells = new char[3][3];
        
        cells[0] = new char[]{' ', ' ', ' '};
        
        cells[1] = new char[]{' ', ' ', ' '};
        
        cells[2] = new char[]{' ', ' ', ' '};
    }
    
    
   public void display()
   {
       for(int i = 0; i < 3; i++)
       {
           for(int j = 0; j < 3; j++) { 

                 if(cells[i][j] == ' ') { 

                       System.out.print((i+1)+", "+(j+1)+"\t"); 

                 } else { 

                       System.out.print(cells[i][j]+"\t");
  
                 } 

            } System.out.print("\n\n"); 

       } 

    } 

    public Move checkAvailable(Move move) { 

       int i = move.getRow(); 

       int j = move.getColumn(); 

       boolean boundValid = ((i >= 1 && i<= 3) && ( j >= 1 && j <= 3));
       
       boolean charValid = false;
       
       if(boundValid)
       {
           charValid = (cells[i - 1][j - 1] == ' ');
       }
       
       boolean netValid = (boundValid && charValid);
       
       move.setIsValid(netValid);
       
       return move;
   }
   
   public void markCell(Move move, char mark)
   {
       if(move.isIsValid())
       {
           int i = move.getRow();
           
           int j = move.getColumn();
           
           cells[i - 1][j - 1] = mark;
       }
       else
       {
           System.out.println("INVALID MOVE!\n\n");
       }
   }
   
}

 

The Player class denotes the players who make moves on the board.

Player:

Either of the two players.

package com.tictactoe.components;

import java.util.ArrayList;
/**
 *
 * @author pratyush
 */
public class Player {
    
    private int playerNum;
    private char playerMark;
    private int winnerOrLoser;
    private Game g;
    
    private ArrayList<Move> moves = new ArrayList<Move>();

    public int getPlayerNum() {
        return playerNum;
    }

    public void setPlayerNum(int playerNum) {
        this.playerNum = playerNum;
    }

    public char getPlayerMark() {
        return playerMark;
    }

    public void setPlayerMark(char playerMark) {
        this.playerMark = playerMark;
    }

    public int getWinnerOrLoser() {
        return winnerOrLoser;
    }

    public void setWinnerOrLoser(int winnerOrLoser) {
        this.winnerOrLoser = winnerOrLoser;
    }

    public Game getGame() {
        return g;
    }

    public void setGame(Game g) {
        this.g = g;
    }

    public ArrayList getMoves() {
        return moves;
    }

    public void addMove(Move move) {
        
        this.moves.add(move);
    }
    
    public void makeMove(Board board, Move move)
    {
        move = board.checkAvailable(move);
        
        char mark = this.getPlayerMark();
        
        board.markCell(move, mark);
        
        if(move.isIsValid())
        {
            this.addMove(move);
            Game.setMovesTillNow();
        }
        
        board.display();
    }
    
}

 

The Game class is the functionality of the game with all its rules.

Game:

The game being played.

package com.tictactoe.components;

import java.util.ArrayList;
/**
 *
 * @author pratyush
 */
public class Game{
 
    private int whoseTurn;
    private static int movesTillNow = 0;
    private boolean gameOver;
    
    private Player winner = null;
    
    Move[][] winning_moves;
    
    public Game()
    {
        
        winning_moves = new Move[8][3];
        
        //top row
        Move move01 = new Move();
        move01.setRow(1);
        move01.setColumn(1);
        
        Move move02 = new Move();
        move02.setRow(1);
        move02.setColumn(2);
        
        Move move03 = new Move();
        move03.setRow(1);
        move03.setColumn(3);
        
        Move[] move0;
        
        move0 = new Move[]{move01, move02, move03};
        
        
        winning_moves[0] = move0;
        
        //middle row
        
        Move move11 = new Move();
        move11.setRow(2);
        move11.setColumn(1);
        
        Move move12 = new Move();
        move12.setRow(2);
        move12.setColumn(2);
        
        Move move13 = new Move();
        move13.setRow(2);
        move13.setColumn(3);
        
        Move[] move1;
        
        move1 = new Move[]{move11, move12, move13};
        
        winning_moves[1] = move1;
        
        //bottom row
        
        Move move21 = new Move();
        move21.setRow(3);
        move21.setColumn(1);
        
        Move move22 = new Move();
        move22.setRow(3);
        move22.setColumn(2);
        
        Move move23 = new Move();
        move23.setRow(3);
        move23.setColumn(3);
        
        Move[] move2;
        
        move2 = new Move[]{move21, move22, move23};
        
        winning_moves[2] = move2;
        
        //column wise
        
        //column 1
        
        Move move31 = new Move();
        move31.setRow(1);
        move31.setColumn(1);
        
        Move move32 = new Move();
        move32.setRow(2);
        move32.setColumn(1);
        
        Move move33 = new Move();
        move33.setRow(3);
        move33.setColumn(1);
        
        Move[] move3;
        
        move3 = new Move[]{move31, move32, move33};
        
        winning_moves[3] = move3;
        
        //column 2
        
        Move move41 = new Move();
        move41.setRow(1);
        move41.setColumn(2);
        
        Move move42 = new Move();
        move42.setRow(2);
        move42.setColumn(2);
        
        Move move43 = new Move();
        move43.setRow(3);
        move43.setColumn(2);
        
        Move[] move4;
        
        move4 = new Move[]{move41, move42, move43};
        
        winning_moves[4] = move4;
        
        //column 3
        
        Move move51 = new Move();
        move51.setRow(1);
        move51.setColumn(3);
        
        Move move52 = new Move();
        move52.setRow(2);
        move52.setColumn(3);
        
        Move move53 = new Move();
        move53.setRow(3);
        move53.setColumn(3);
        
        Move[] move5;
        
        move5 = new Move[]{move51, move52, move53};
        
        winning_moves[5] = move5;
        
        //diagonals
        
        //diagonal 1
        
        Move move61 = new Move();
        move61.setRow(1);
        move61.setColumn(1);
        
        Move move62 = new Move();
        move62.setRow(2);
        move62.setColumn(2);
        
        Move move63 = new Move();
        move63.setRow(3);
        move63.setColumn(3);
        
        Move[] move6;
        
        move6 = new Move[]{move61, move62, move63};
        
        winning_moves[6] = move6;
        
        //diagonal 2
        
        Move move71 = new Move();
        move71.setRow(1);
        move71.setColumn(3);
        
        Move move72 = new Move();
        move72.setRow(2);
        move72.setColumn(2);
        
        Move move73 = new Move();
        move73.setRow(3);
        move73.setColumn(1);
        
        Move[] move7;
        
        move7 = new Move[]{move71, move72, move73};
        
        winning_moves[7] = move7;
        
    }

    public int getWhoseTurn() {
        return whoseTurn;
    }

    public void setWhoseTurn(Player player) {
        this.whoseTurn = player.getPlayerNum();
    }

    public static int getMovesTillNow() {
        return movesTillNow;
    }

    public static void setMovesTillNow() {
        Game.movesTillNow++;
    }

    public boolean isGameOver() {
        return gameOver;
    }

    public void setGameOver(boolean gameOver) {
        this.gameOver = gameOver;
    }

    public Player getWinner() {
        return winner;
    }

    public void setWinner(Player winner) {
        this.winner = winner;
    }
    
    private boolean checkSubset(Move[] subset, Move[] winning_row)
    {
        int count = 0;
        
        for(int i = 0;i < 3; i++)
        {
            for(int j = 0; j< 3; j++)
            {
                if(subset[i].getRow() == winning_row[j].getRow() && subset[i].getColumn() == winning_row[j].getColumn())
                {
                   count++; 
                }
            }
        }
        
        return (count == 3);
    }
    
    
    //check the winning moves
    
    public boolean checkWinningMoves(Player player)
    {
        
        /*****check winning moves*****/
        
        int movesTillNow = Game.getMovesTillNow();
        
        int whoseTurn = this.getWhoseTurn();
        
        ArrayList<Move> playerMoves = player.getMoves();
        
        int moves = movesTillNow;
        
        Move[][] winning_moves1 = this.winning_moves;
        
        switch(whoseTurn)
        {
            case 1:
                
                if(moves < 5)
                {
                    return false;
                }
                else
                {
                    if(moves == 5)
                    {
                        Move[] player1_set1 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(2)};
                        
                        for(int i = 0; i< 8; i++)
                        {
                            if(checkSubset(player1_set1, winning_moves1[i]))
                            {
                                return true;
                            }
                        }
                    }
                    
                    /******/
                    
                    if(moves == 7)
                    {
                        Move[] player1_set2 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(3)};
            
                        Move[] player1_set4 = new Move[]{playerMoves.get(0), playerMoves.get(2), playerMoves.get(3)};

                        Move[] player1_set7 = new Move[]{playerMoves.get(1), playerMoves.get(2), playerMoves.get(3)};

                        for(int i = 0; i < 8; i++)
                        {
                                if(checkSubset(player1_set2, winning_moves1[i]) || 
                                checkSubset(player1_set4, winning_moves1[i]) || 
                                checkSubset(player1_set7, winning_moves1[i]) )
                                {
                                        return true;
                                }
                        }
                    }
                    
                    if(moves == 9)
                    {
                        Move[] player1_set3 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(4)};
            
                        Move[] player1_set5 = new Move[]{playerMoves.get(0), playerMoves.get(2), playerMoves.get(4)};

                        Move[] player1_set6 = new Move[]{playerMoves.get(0), playerMoves.get(3), playerMoves.get(4)};

                        Move[] player1_set8 = new Move[]{playerMoves.get(1), playerMoves.get(2), playerMoves.get(4)};

                        Move[] player1_set9 = new Move[]{playerMoves.get(1), playerMoves.get(3), playerMoves.get(4)};

                        Move[] player1_set10 = new Move[]{playerMoves.get(2), playerMoves.get(3), playerMoves.get(4)};

                        for(int i = 0; i < 8; i++)
                        {
                                if(checkSubset(player1_set3, winning_moves1[i]) ||
                                checkSubset(player1_set5, winning_moves1[i]) || checkSubset(player1_set6, winning_moves1[i]) || 
                                checkSubset(player1_set8, winning_moves1[i]) || checkSubset(player1_set9, winning_moves1[i]) || 
                                checkSubset(player1_set10, winning_moves1[i]))
                                {
                                        return true;
                                }
                        }
                    }
                    
                    return false;
                }
                
            case 2:
                /******/
                if(moves < 6)
                {
                    return false;
                }
                else
                {
                    /*******/
                    if(moves == 6)
                    {
                        Move[] player2_set1 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(2)};
            
                        for(int i = 0; i < 8; i++)
                        {
                                if(checkSubset(player2_set1, winning_moves1[i]))
                                {
                                        return true;
                                }
                        }
                    }
                    
                    if(moves == 8)
                    {
                        Move[] player2_set2 = new Move[]{playerMoves.get(0), playerMoves.get(1), playerMoves.get(3)};
            
                        Move[] player2_set3 = new Move[]{playerMoves.get(0), playerMoves.get(2), playerMoves.get(3)};

                        Move[] player2_set4 = new Move[]{playerMoves.get(1), playerMoves.get(2), playerMoves.get(3)};

                        for(int i = 0; i < 8; i++)
                        {
                            if(checkSubset(player2_set2, winning_moves1[i]) || checkSubset(player2_set3, winning_moves1[i]) ||
                            checkSubset(player2_set4, winning_moves1[i]))
                            {
                                    return true;
                            }
                        }
                    }
                    
                    return false;
                }
            default:
                return false;
        }
    }
    
}

 

Finally, the main class:

TicTacToe

The class containing the entry point of the program also called the main class.

package com.tictactoe;

import com.tictactoe.components.*;
import java.util.Scanner;
/**
 *
 * @author pratyush
 */
public class TicTacToe{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         // TODO code application logic here
        
        Game game = new Game();
        
        Player player1 = new Player();
        
        player1.setPlayerNum(1);
        
        player1.setPlayerMark('X');
        
        player1.setGame(game);
        
        Player player2 = new Player();
        
        player2.setPlayerNum(2);
        
        player2.setPlayerMark('0');
        
        player2.setGame(game);
        
        Board board = new Board();
        
        board.display();
        
        /*******/
        
        game.setWhoseTurn(player1);
        
        game.setGameOver(false);
        
        boolean status = game.isGameOver();
        
        Scanner sc = new Scanner(System.in);
        
        do
        {
            int movesNow = Game.getMovesTillNow();
            
            if(movesNow == 9)
            {
                if(game.getWinner() == null)
                {
                    System.out.println("TIE!\n");
                    break;
                }
            }
            
            int turn = game.getWhoseTurn();
            
            System.out.println("PLAYER "+turn+"\n");
            
            int row = 0; 
            int column = 0;
      
      boolean invalid = true;
      
      while(invalid)
      {
             //code to handle invalid moves. players cannot proceed without valid input
        try{
          System.out.println("Enter row: ");
              
          String str = sc.next();
              
          if(!str.equals(""))
          {
              
            row = Integer.parseInt(str);
            invalid = false;
              
          }
          else
          {
            continue;
          }
        }
        catch(NumberFormatException n)
        {
          System.out.println("INVALID ROW!");
          invalid = true;
        }
        catch(InputMismatchException i)
        {
          System.out.println("INVALID ROW!");
          invalid = true;
        }
      }
      
      invalid = true;
      
      while(invalid)
      {
        /*code to handle invalid moves. players cannot proceed without valid input*/
          
        try{
          System.out.println("Enter column: ");
            
          String str = sc.next();
            
          if(!str.equals(""))
          {
            
            column = Integer.parseInt(str);
            invalid = false;
            
          }
          else
          {
            continue;
          }
        }
        catch(NumberFormatException n)
        {
          System.out.println("INVALID COLUMN!");
          invalid = true;
        }
        catch(InputMismatchException i)
        {
          System.out.println("INVALID COLUMN!");
          invalid = true;
        }
      }
      
      /**/
            
            Move move = new Move();
            
            move.setRow(row);
            
            move.setColumn(column);
            
            switch(turn)
            {
                case 1:
                    player1.makeMove(board, move);
                    
                    if(game.checkWinningMoves(player1))
                    {
                        game.setWinner(player1);
                        
                        System.out.println("PLAYER 1 WINS!");
                        
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow();
                            
                            System.out.println("MOVE NUMBER "+movesNow+"\n");
                            
                            game.setWhoseTurn(player2);
                        }
                    }
                    break;
                case 2:
                    player2.makeMove(board, move);
                    
                    if(game.checkWinningMoves(player2))
                    {
                        game.setWinner(player2);
                        
                        System.out.println("PLAYER 2 WINS!");
                        
                        game.setGameOver(true);
                    }
                    else
                    {
                        if(move.isIsValid())
                        {
                            movesNow = Game.getMovesTillNow();
                            
                            System.out.println("MOVE NUMBER "+movesNow+"\n");
                            
                            game.setWhoseTurn(player1);
                        }
                    }
                    break;
                default:
                    System.out.println("ERROR!\n");
                    break;
            }
            
            status = game.isGameOver();
        }
        while(!status);
        
    }
    
}

 

Copy, Compile, Run and ENJOY!

 

C# version

Here is an elementary tic-tac-toe or noughts and crosses game I made in C#, arguably the most popular language of the DotNET platform. It can be played from the command line after compiling it as an executable. In the game of tic-tac-toe there is a 3X3 grid or board.The game is for two players. The players 1 and 2 take turns to mark the cells of the grid with their mark which is a zero or a cross.

The first player to complete a vertical, horizontal or diagonal triad of zeroes or crosses, wins.

The PHP version can be found here

Player makes Move on Board to play Game. So the classes are:

  1. Move
  2. Board
  3. Player
  4. Game
  5. TicTacToe

There is one new class “TicTacToe” because C# needs a main class having the “Main” method implemented in it.

The classes are in the namespace called “TicTacToeDotNet”.

 

First we discuss the move class:

Move:

Move made by either of the players.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Move
    {

        private int row, column;
        private bool isValid;

        public int getRow()
        {
            return row;
        }

        public void setRow(int row)
        {
            this.row = row;
        }

        public int getColumn()
        {
            return column;
        }

        public void setColumn(int column)
        {
            this.column = column;
        }

        public bool isIsValid()
        {
            return isValid;
        }

        public void setIsValid(bool isValid)
        {
            this.isValid = isValid;
        }

    }
}

 

Then comes the Board class where the moves are made.

Board:

The board on which the game is played.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Board
    {

        private static char[][] cells;

        public Board()
        {
            cells = new char[3][];

            cells[0] = new char[] { ' ', ' ', ' ' };

            cells[1] = new char[] { ' ', ' ', ' ' };

            cells[2] = new char[] { ' ', ' ', ' ' };
        }


        public void display()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++) { 

                     if (cells[i][j] == ' ') { 

                          Console.Write((i + 1) + ", " + (j + 1) + "\t"); 

                      } else { 

                           Console.Write(cells[i][j] + "\t"); 

                      } 

                  } Console.Write("\n\n"); 

            } 

      } 

      public Move checkAvailable(Move move) { 

            int i = move.getRow(); 

            int j = move.getColumn(); 

            bool boundValid = ((i >= 1 && i <= 3) && (j >= 1 && j <= 3));

            bool charValid = false;

            if (boundValid)
            {
                charValid = (cells[i - 1][j - 1] == ' ');
            }

            bool netValid = (boundValid && charValid);

            move.setIsValid(netValid);

            return move;
        }

        public void markCell(Move move, char mark)
        {
            if (move.isIsValid())
            {
                int i = move.getRow();

                int j = move.getColumn();

                cells[i - 1][j - 1] = mark;
            }
            else
            {
                Console.Write("INVALID MOVE!\n\n");
            }
        }

    }

}

 

The Player class denotes the players who make moves on the board.

Player:

Either of the two players.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Player
    {

        private int playerNum;
        private char playerMark;
        private int winnerOrLoser;
        private Game g;

        private List moves = new List();

        public int getPlayerNum()
        {
            return playerNum;
        }

        public void setPlayerNum(int playerNum)
        {
            this.playerNum = playerNum;
        }

        public char getPlayerMark()
        {
            return playerMark;
        }

        public void setPlayerMark(char playerMark)
        {
            this.playerMark = playerMark;
        }

        public int getWinnerOrLoser()
        {
            return winnerOrLoser;
        }

        public void setWinnerOrLoser(int winnerOrLoser)
        {
            this.winnerOrLoser = winnerOrLoser;
        }

        public Game getGame()
        {
            return g;
        }

        public void setGame(Game g)
        {
            this.g = g;
        }

        public List getMoves()
        {
            return moves;
        }

        public void addMove(Move move)
        {

            this.moves.Add(move);
        }

        public void makeMove(Board board, Move move)
        {
            move = board.checkAvailable(move);

            char mark = this.getPlayerMark();

            board.markCell(move, mark);

            if (move.isIsValid())
            {
                this.addMove(move);
                Game.setMovesTillNow();
            }

            board.display();
        }

    }
}

 

The Game class is the functionality of the game with all its rules.

Game:

The game being played.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class Game
    {

        private int whoseTurn;
        private static int movesTillNow = 0;
        private bool gameOver;

        private Player winner = null;

        Move[][] winning_moves;

        public Game()
        {

            winning_moves = new Move[8][];

            //top row
            Move move01 = new Move();
            move01.setRow(1);
            move01.setColumn(1);

            Move move02 = new Move();
            move02.setRow(1);
            move02.setColumn(2);

            Move move03 = new Move();
            move03.setRow(1);
            move03.setColumn(3);

            Move[] move0;

            move0 = new Move[] { move01, move02, move03 };


            winning_moves[0] = move0;

            //middle row

            Move move11 = new Move();
            move11.setRow(2);
            move11.setColumn(1);

            Move move12 = new Move();
            move12.setRow(2);
            move12.setColumn(2);

            Move move13 = new Move();
            move13.setRow(2);
            move13.setColumn(3);

            Move[] move1;

            move1 = new Move[] { move11, move12, move13 };

            winning_moves[1] = move1;

            //bottom row

            Move move21 = new Move();
            move21.setRow(3);
            move21.setColumn(1);

            Move move22 = new Move();
            move22.setRow(3);
            move22.setColumn(2);

            Move move23 = new Move();
            move23.setRow(3);
            move23.setColumn(3);

            Move[] move2;

            move2 = new Move[] { move21, move22, move23 };

            winning_moves[2] = move2;

            //column wise

            //column 1

            Move move31 = new Move();
            move31.setRow(1);
            move31.setColumn(1);

            Move move32 = new Move();
            move32.setRow(2);
            move32.setColumn(1);

            Move move33 = new Move();
            move33.setRow(3);
            move33.setColumn(1);

            Move[] move3;

            move3 = new Move[] { move31, move32, move33 };

            winning_moves[3] = move3;

            //column 2

            Move move41 = new Move();
            move41.setRow(1);
            move41.setColumn(2);

            Move move42 = new Move();
            move42.setRow(2);
            move42.setColumn(2);

            Move move43 = new Move();
            move43.setRow(3);
            move43.setColumn(2);

            Move[] move4;

            move4 = new Move[] { move41, move42, move43 };

            winning_moves[4] = move4;

            //column 3

            Move move51 = new Move();
            move51.setRow(1);
            move51.setColumn(3);

            Move move52 = new Move();
            move52.setRow(2);
            move52.setColumn(3);

            Move move53 = new Move();
            move53.setRow(3);
            move53.setColumn(3);

            Move[] move5;

            move5 = new Move[] { move51, move52, move53 };

            winning_moves[5] = move5;

            //diagonals

            //diagonal 1

            Move move61 = new Move();
            move61.setRow(1);
            move61.setColumn(1);

            Move move62 = new Move();
            move62.setRow(2);
            move62.setColumn(2);

            Move move63 = new Move();
            move63.setRow(3);
            move63.setColumn(3);

            Move[] move6;

            move6 = new Move[] { move61, move62, move63 };

            winning_moves[6] = move6;

            //diagonal 2

            Move move71 = new Move();
            move71.setRow(1);
            move71.setColumn(3);

            Move move72 = new Move();
            move72.setRow(2);
            move72.setColumn(2);

            Move move73 = new Move();
            move73.setRow(3);
            move73.setColumn(1);

            Move[] move7;

            move7 = new Move[] { move71, move72, move73 };

            winning_moves[7] = move7;

        }

        public int getWhoseTurn()
        {
            return whoseTurn;
        }

        public void setWhoseTurn(Player player)
        {
            this.whoseTurn = player.getPlayerNum();
        }

        public static int getMovesTillNow()
        {
            return movesTillNow;
        }

        public static void setMovesTillNow()
        {
            Game.movesTillNow++;
        }

        public bool isGameOver()
        {
            return gameOver;
        }

        public void setGameOver(bool gameOver)
        {
            this.gameOver = gameOver;
        }

        public Player getWinner()
        {
            return winner;
        }

        public void setWinner(Player winner)
        {
            this.winner = winner;
        }

        private bool checkSubset(Move[] subset, Move[] winning_row)
        {
            int count = 0;

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (subset[i].getRow() == winning_row[j].getRow() && subset[i].getColumn() == winning_row[j].getColumn())
                    {
                        count++;
                    }
                }
            }

            return (count == 3);
        }


        //check the winning moves

        public bool checkWinningMoves(Player player)
        {

            /*****check winning moves*****/

            int movesTillNow = Game.getMovesTillNow();

            int whoseTurn = this.getWhoseTurn();

            Move[] playerMoves = player.getMoves().ToArray();

            int moves = movesTillNow;

            Move[][] winning_moves1 = this.winning_moves;

            switch (whoseTurn)
            {
                case 1:

                    if (moves < 5)
                    {
                        return false;
                    }
                    else
                    {
                        if (moves == 5)
                        {
                            Move[] player1_set1 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[2] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player1_set1, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        /******/

                        if (moves == 7)
                        {
                            Move[] player1_set2 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[3] };

                            Move[] player1_set4 = new Move[] { playerMoves[0], playerMoves[2], playerMoves[3] };

                            Move[] player1_set7 = new Move[] { playerMoves[1], playerMoves[2], playerMoves[3] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player1_set2, winning_moves1[i]) ||
                                checkSubset(player1_set4, winning_moves1[i]) ||
                                checkSubset(player1_set7, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        if (moves == 9)
                        {
                            Move[] player1_set3 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[4] };

                            Move[] player1_set5 = new Move[] { playerMoves[0], playerMoves[2], playerMoves[4] };

                            Move[] player1_set6 = new Move[] { playerMoves[0], playerMoves[3], playerMoves[4] };

                            Move[] player1_set8 = new Move[] { playerMoves[1], playerMoves[2], playerMoves[4] };

                            Move[] player1_set9 = new Move[] { playerMoves[1], playerMoves[3], playerMoves[4] };

                            Move[] player1_set10 = new Move[] { playerMoves[2], playerMoves[3], playerMoves[4] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player1_set3, winning_moves1[i]) ||
                                checkSubset(player1_set5, winning_moves1[i]) || checkSubset(player1_set6, winning_moves1[i]) ||
                                checkSubset(player1_set8, winning_moves1[i]) || checkSubset(player1_set9, winning_moves1[i]) ||
                                checkSubset(player1_set10, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        return false;
                    }

                case 2:
                    /******/
                    if (moves < 6)
                    {
                        return false;
                    }
                    else
                    {
                        /*******/
                        if (moves == 6)
                        {
                            Move[] player2_set1 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[2] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player2_set1, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        if (moves == 8)
                        {
                            Move[] player2_set2 = new Move[] { playerMoves[0], playerMoves[1], playerMoves[3] };

                            Move[] player2_set3 = new Move[] { playerMoves[0], playerMoves[2], playerMoves[3] };

                            Move[] player2_set4 = new Move[] { playerMoves[1], playerMoves[2], playerMoves[3] };

                            for (int i = 0; i < 8; i++)
                            {
                                if (checkSubset(player2_set2, winning_moves1[i]) || checkSubset(player2_set3, winning_moves1[i]) ||
                                checkSubset(player2_set4, winning_moves1[i]))
                                {
                                    return true;
                                }
                            }
                        }

                        return false;
                    }
                default:
                    return false;
            }
        }

    }
}

 

Finally, the main class:

TicTacToe

The class containing the entry point of the program also called the main class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TicTacToeDotNet
{
    public class TicTacToe
    {

        /**
         * @param args the command line arguments
         */
        public static void Main(string[] args)
        {
            // TODO code application logic here

            Game game = new Game();

            Player player1 = new Player();

            player1.setPlayerNum(1);

            player1.setPlayerMark('X');

            player1.setGame(game);

            Player player2 = new Player();

            player2.setPlayerNum(2);

            player2.setPlayerMark('0');

            player2.setGame(game);

            Board board = new Board();

            board.display();

            /*******/

            game.setWhoseTurn(player1);

            game.setGameOver(false);

            bool status = game.isGameOver();

            //

            do
            {
                int movesNow = Game.getMovesTillNow();

                if (movesNow == 9)
                {
                    if (game.getWinner() == null)
                    {
                        Console.WriteLine("TIE!\n");
                        break;
                    }
                }

                int turn = game.getWhoseTurn();

                Console.WriteLine("PLAYER " + turn + "\n");

                int row = 0;
                int column = 0;

                bool invalid = true;

                while (invalid)
                {
                    //code to handle invalid moves. players cannot proceed without valid input
                    try
                    {
                        Console.WriteLine("Enter row: ");

                        string str = Convert.ToString(Console.ReadLine());

                        if (!(str == ""))
                        {

                            row = Convert.ToInt32(str);
                            invalid = false;

                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (FormatException n)
                    {
                        Console.WriteLine("INVALID ROW!");
                        invalid = true;
                    }
                    /**/
                }

                invalid = true;

                while (invalid)
                {
                    /*code to handle invalid moves. players cannot proceed without valid input*/

                    try
                    {
                        Console.WriteLine("Enter column: ");

                        String str = Convert.ToString(Console.ReadLine());

                        if (!(str == ""))
                        {

                            column = Convert.ToInt32(str);
                            invalid = false;

                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (FormatException n)
                    {
                        Console.WriteLine("INVALID COLUMN!");
                        invalid = true;
                    }
                    /**/
                }

                /**/

                Move move = new Move();

                move.setRow(row);

                move.setColumn(column);

                switch (turn)
                {
                    case 1:
                        player1.makeMove(board, move);

                        if (game.checkWinningMoves(player1))
                        {
                            game.setWinner(player1);

                            Console.WriteLine("PLAYER 1 WINS!");

                            game.setGameOver(true);
                        }
                        else
                        {
                            if (move.isIsValid())
                            {
                                movesNow = Game.getMovesTillNow();

                                Console.WriteLine("MOVE NUMBER " + movesNow + "\n");

                                game.setWhoseTurn(player2);
                            }
                        }
                        break;
                    case 2:
                        player2.makeMove(board, move);

                        if (game.checkWinningMoves(player2))
                        {
                            game.setWinner(player2);

                            Console.WriteLine("PLAYER 2 WINS!");

                            game.setGameOver(true);
                        }
                        else
                        {
                            if (move.isIsValid())
                            {
                                movesNow = Game.getMovesTillNow();

                                Console.WriteLine("MOVE NUMBER " + movesNow + "\n");

                                game.setWhoseTurn(player1);
                            }
                        }
                        break;
                    default:
                        Console.WriteLine("ERROR!\n");
                        break;
                }

                status = game.isGameOver();
            }
            while (!status);

            Console.Read();

        }

    }
}

 

Copy, Compile, Run and ENJOY!